Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f33ce17

Browse files
IgorMinarpetebacondarwin
authored andcommitted
fix($compile): properly sanitize xlink:href attribute interoplation
Closes #12524
1 parent 181fc56 commit f33ce17

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
11241124

11251125
nodeName = nodeName_(this.$$element);
11261126

1127-
if ((nodeName === 'a' && key === 'href') ||
1127+
if ((nodeName === 'a' && (key === 'href' || key === 'xlinkHref')) ||
11281128
(nodeName === 'img' && key === 'src')) {
11291129
// sanitize a[href] and img[src] values
11301130
this[key] = value = $$sanitizeUri(value, key === 'src');

test/ng/compileSpec.js

+48
Original file line numberDiff line numberDiff line change
@@ -7215,6 +7215,54 @@ describe('$compile', function() {
72157215
});
72167216
});
72177217

7218+
it('should use $$sanitizeUri when declared via ng-href', function() {
7219+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
7220+
module(function($provide) {
7221+
$provide.value('$$sanitizeUri', $$sanitizeUri);
7222+
});
7223+
inject(function($compile, $rootScope) {
7224+
element = $compile('<a ng-href="{{testUrl}}"></a>')($rootScope);
7225+
$rootScope.testUrl = "someUrl";
7226+
7227+
$$sanitizeUri.andReturn('someSanitizedUrl');
7228+
$rootScope.$apply();
7229+
expect(element.attr('href')).toBe('someSanitizedUrl');
7230+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
7231+
});
7232+
});
7233+
7234+
it('should use $$sanitizeUri when working with svg and xlink:href', function() {
7235+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
7236+
module(function($provide) {
7237+
$provide.value('$$sanitizeUri', $$sanitizeUri);
7238+
});
7239+
inject(function($compile, $rootScope) {
7240+
element = $compile('<svg><a xlink:href="" ng-href="{{ testUrl }}"></a></svg>')($rootScope);
7241+
$rootScope.testUrl = "evilUrl";
7242+
7243+
$$sanitizeUri.andReturn('someSanitizedUrl');
7244+
$rootScope.$apply();
7245+
expect(element.find('a').prop('href').baseVal).toBe('someSanitizedUrl');
7246+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
7247+
});
7248+
});
7249+
7250+
7251+
it('should use $$sanitizeUri when working with svg and xlink:href', function() {
7252+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
7253+
module(function($provide) {
7254+
$provide.value('$$sanitizeUri', $$sanitizeUri);
7255+
});
7256+
inject(function($compile, $rootScope) {
7257+
element = $compile('<svg><a xlink:href="" ng-href="{{ testUrl }}"></a></svg>')($rootScope);
7258+
$rootScope.testUrl = "evilUrl";
7259+
7260+
$$sanitizeUri.andReturn('someSanitizedUrl');
7261+
$rootScope.$apply();
7262+
expect(element.find('a').prop('href').baseVal).toBe('someSanitizedUrl');
7263+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
7264+
});
7265+
});
72187266
});
72197267

72207268
describe('interpolation on HTML DOM event handler attributes onclick, onXYZ, formaction', function() {

0 commit comments

Comments
 (0)