Skip to content

Commit f6ba1ef

Browse files
IgorMinarbenjaminParisel
authored andcommitted
fix($compile): properly sanitize xlink:href attribute interoplation
Closes angular#12524 (cherry picked from commit f33ce17)
1 parent 2475f57 commit f6ba1ef

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
@@ -1044,7 +1044,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10441044

10451045
nodeName = nodeName_(this.$$element);
10461046

1047-
if ((nodeName === 'a' && key === 'href') ||
1047+
if ((nodeName === 'a' && (key === 'href' || key === 'xlinkHref')) ||
10481048
(nodeName === 'img' && key === 'src')) {
10491049
// sanitize a[href] and img[src] values
10501050
this[key] = value = $$sanitizeUri(value, key === 'src');

test/ng/compileSpec.js

+48
Original file line numberDiff line numberDiff line change
@@ -6322,6 +6322,54 @@ describe('$compile', function() {
63226322
});
63236323
});
63246324

6325+
it('should use $$sanitizeUri when declared via ng-href', function() {
6326+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
6327+
module(function($provide) {
6328+
$provide.value('$$sanitizeUri', $$sanitizeUri);
6329+
});
6330+
inject(function($compile, $rootScope) {
6331+
element = $compile('<a ng-href="{{testUrl}}"></a>')($rootScope);
6332+
$rootScope.testUrl = "someUrl";
6333+
6334+
$$sanitizeUri.andReturn('someSanitizedUrl');
6335+
$rootScope.$apply();
6336+
expect(element.attr('href')).toBe('someSanitizedUrl');
6337+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
6338+
});
6339+
});
6340+
6341+
it('should use $$sanitizeUri when working with svg and xlink:href', function() {
6342+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
6343+
module(function($provide) {
6344+
$provide.value('$$sanitizeUri', $$sanitizeUri);
6345+
});
6346+
inject(function($compile, $rootScope) {
6347+
element = $compile('<svg><a xlink:href="" ng-href="{{ testUrl }}"></a></svg>')($rootScope);
6348+
$rootScope.testUrl = "evilUrl";
6349+
6350+
$$sanitizeUri.andReturn('someSanitizedUrl');
6351+
$rootScope.$apply();
6352+
expect(element.find('a').prop('href').baseVal).toBe('someSanitizedUrl');
6353+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
6354+
});
6355+
});
6356+
6357+
6358+
it('should use $$sanitizeUri when working with svg and xlink:href', function() {
6359+
var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri');
6360+
module(function($provide) {
6361+
$provide.value('$$sanitizeUri', $$sanitizeUri);
6362+
});
6363+
inject(function($compile, $rootScope) {
6364+
element = $compile('<svg><a xlink:href="" ng-href="{{ testUrl }}"></a></svg>')($rootScope);
6365+
$rootScope.testUrl = "evilUrl";
6366+
6367+
$$sanitizeUri.andReturn('someSanitizedUrl');
6368+
$rootScope.$apply();
6369+
expect(element.find('a').prop('href').baseVal).toBe('someSanitizedUrl');
6370+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
6371+
});
6372+
});
63256373
});
63266374

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

0 commit comments

Comments
 (0)