Skip to content

Commit a8cc449

Browse files
committed
fix($compile): sanitize values bound to a[href]
1 parent 2aa212b commit a8cc449

File tree

3 files changed

+196
-8
lines changed

3 files changed

+196
-8
lines changed

src/ng/compile.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ function $CompileProvider($provide) {
155155
Suffix = 'Directive',
156156
COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,
157157
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/,
158-
MULTI_ROOT_TEMPLATE_ERROR = 'Template must have exactly one root element. was: ';
158+
MULTI_ROOT_TEMPLATE_ERROR = 'Template must have exactly one root element. was: ',
159+
urlSanitizationWhitelist = /^\s*(https?|ftp|mailto):/;
159160

160161

161162
/**
@@ -209,11 +210,41 @@ function $CompileProvider($provide) {
209210
};
210211

211212

213+
/**
214+
* @ngdoc function
215+
* @name ng.$compileProvider#urlSanitizationWhitelist
216+
* @methodOf ng.$compileProvider
217+
* @function
218+
*
219+
* @description
220+
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
221+
* urls during a[href] sanitization.
222+
*
223+
* The sanitization is a security measure aimed at prevent XSS attacks via html links.
224+
*
225+
* Any url about to be assigned to a[href] via data-binding is first normalized and turned into an
226+
* absolute url. Afterwards the url is matched against the `urlSanitizationWhitelist` regular
227+
* expression. If a match is found the original url is written into the dom. Otherwise the
228+
* absolute url is prefixed with `'unsafe:'` string and only then it is written into the DOM.
229+
*
230+
* @param {RegExp=} regexp New regexp to whitelist urls with.
231+
* @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for
232+
* chaining otherwise.
233+
*/
234+
this.urlSanitizationWhitelist = function(regexp) {
235+
if (isDefined(regexp)) {
236+
urlSanitizationWhitelist = regexp;
237+
return this;
238+
}
239+
return urlSanitizationWhitelist;
240+
};
241+
242+
212243
this.$get = [
213244
'$injector', '$interpolate', '$exceptionHandler', '$http', '$templateCache', '$parse',
214-
'$controller', '$rootScope',
245+
'$controller', '$rootScope', '$document',
215246
function($injector, $interpolate, $exceptionHandler, $http, $templateCache, $parse,
216-
$controller, $rootScope) {
247+
$controller, $rootScope, $document) {
217248

218249
var Attributes = function(element, attr) {
219250
this.$$element = element;
@@ -235,7 +266,8 @@ function $CompileProvider($provide) {
235266
*/
236267
$set: function(key, value, writeAttr, attrName) {
237268
var booleanKey = getBooleanAttrName(this.$$element[0], key),
238-
$$observers = this.$$observers;
269+
$$observers = this.$$observers,
270+
normalizedVal;
239271

240272
if (booleanKey) {
241273
this.$$element.prop(key, value);
@@ -254,6 +286,19 @@ function $CompileProvider($provide) {
254286
}
255287
}
256288

289+
290+
// sanitize a[href] values
291+
if (nodeName_(this.$$element[0]) === 'A' && key === 'href') {
292+
urlSanitizationNode.setAttribute('href', value);
293+
294+
// href property always returns normalized absolute url, so we can match against that
295+
normalizedVal = urlSanitizationNode.href;
296+
if (!normalizedVal.match(urlSanitizationWhitelist)) {
297+
this[key] = value = 'unsafe:' + normalizedVal;
298+
}
299+
}
300+
301+
257302
if (writeAttr !== false) {
258303
if (value === null || value === undefined) {
259304
this.$$element.removeAttr(attrName);
@@ -297,7 +342,8 @@ function $CompileProvider($provide) {
297342
}
298343
};
299344

300-
var startSymbol = $interpolate.startSymbol(),
345+
var urlSanitizationNode = $document[0].createElement('a'),
346+
startSymbol = $interpolate.startSymbol(),
301347
endSymbol = $interpolate.endSymbol(),
302348
denormalizeTemplate = (startSymbol == '{{' || endSymbol == '}}')
303349
? identity
@@ -1025,10 +1071,10 @@ function $CompileProvider($provide) {
10251071
function addAttrInterpolateDirective(node, directives, value, name) {
10261072
var interpolateFn = $interpolate(value, true);
10271073

1028-
10291074
// no interpolation found -> ignore
10301075
if (!interpolateFn) return;
10311076

1077+
10321078
directives.push({
10331079
priority: 100,
10341080
compile: valueFn(function attrInterpolateLinkFn(scope, element, attr) {

src/ng/directive/booleanAttrs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ forEach(['src', 'href'], function(attrName) {
309309

310310
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
311311
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
312-
// to set the property as well to achieve the desired effect
313-
if (msie) element.prop(attrName, value);
312+
// to set the property as well to achieve the desired effect.
313+
// we use attr[attrName] value since $set can sanitize the url.
314+
if (msie) element.prop(attrName, attr[attrName]);
314315
});
315316
}
316317
};

test/ng/compileSpec.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,148 @@ describe('$compile', function() {
23592359
expect(jqLite(element.find('span')[1]).text()).toEqual('T:true');
23602360
});
23612361
});
2362+
});
2363+
2364+
2365+
describe('href sanitization', function() {
2366+
2367+
it('should sanitize javascript: urls', inject(function($compile, $rootScope) {
2368+
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
2369+
$rootScope.testUrl = "javascript:doEvilStuff()";
2370+
$rootScope.$apply();
2371+
2372+
expect(element.attr('href')).toBe('unsafe:javascript:doEvilStuff()');
2373+
}));
2374+
2375+
2376+
it('should sanitize data: urls', inject(function($compile, $rootScope) {
2377+
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
2378+
$rootScope.testUrl = "data:evilPayload";
2379+
$rootScope.$apply();
2380+
2381+
expect(element.attr('href')).toBe('unsafe:data:evilPayload');
2382+
}));
2383+
2384+
2385+
it('should sanitize obfuscated javascript: urls', inject(function($compile, $rootScope) {
2386+
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
2387+
2388+
// case-sensitive
2389+
$rootScope.testUrl = "JaVaScRiPt:doEvilStuff()";
2390+
$rootScope.$apply();
2391+
expect(element[0].href).toBe('unsafe:javascript:doEvilStuff()');
2392+
2393+
// tab in protocol
2394+
$rootScope.testUrl = "java\u0009script:doEvilStuff()";
2395+
$rootScope.$apply();
2396+
expect(element[0].href).toMatch(/(http:\/\/|unsafe:javascript:doEvilStuff\(\))/);
2397+
2398+
// space before
2399+
$rootScope.testUrl = " javascript:doEvilStuff()";
2400+
$rootScope.$apply();
2401+
expect(element[0].href).toBe('unsafe:javascript:doEvilStuff()');
2402+
2403+
// ws chars before
2404+
$rootScope.testUrl = " \u000e javascript:doEvilStuff()";
2405+
$rootScope.$apply();
2406+
expect(element[0].href).toMatch(/(http:\/\/|unsafe:javascript:doEvilStuff\(\))/);
2407+
2408+
// post-fixed with proper url
2409+
$rootScope.testUrl = "javascript:doEvilStuff(); http://make.me/look/good";
2410+
$rootScope.$apply();
2411+
expect(element[0].href).toBeOneOf(
2412+
'unsafe:javascript:doEvilStuff(); http://make.me/look/good',
2413+
'unsafe:javascript:doEvilStuff();%20http://make.me/look/good'
2414+
);
2415+
}));
2416+
2417+
2418+
it('should sanitize ngHref bindings as well', inject(function($compile, $rootScope) {
2419+
element = $compile('<a ng-href="{{testUrl}}"></a>')($rootScope);
2420+
$rootScope.testUrl = "javascript:doEvilStuff()";
2421+
$rootScope.$apply();
2422+
2423+
expect(element[0].href).toBe('unsafe:javascript:doEvilStuff()');
2424+
}));
2425+
2426+
2427+
it('should not sanitize valid urls', inject(function($compile, $rootScope) {
2428+
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
2429+
2430+
$rootScope.testUrl = "foo/bar";
2431+
$rootScope.$apply();
2432+
expect(element.attr('href')).toBe('foo/bar');
2433+
2434+
$rootScope.testUrl = "/foo/bar";
2435+
$rootScope.$apply();
2436+
expect(element.attr('href')).toBe('/foo/bar');
2437+
2438+
$rootScope.testUrl = "../foo/bar";
2439+
$rootScope.$apply();
2440+
expect(element.attr('href')).toBe('../foo/bar');
2441+
2442+
$rootScope.testUrl = "#foo";
2443+
$rootScope.$apply();
2444+
expect(element.attr('href')).toBe('#foo');
23622445

2446+
$rootScope.testUrl = "http://foo/bar";
2447+
$rootScope.$apply();
2448+
expect(element.attr('href')).toBe('http://foo/bar');
23632449

2450+
$rootScope.testUrl = " http://foo/bar";
2451+
$rootScope.$apply();
2452+
expect(element.attr('href')).toBe(' http://foo/bar');
2453+
2454+
$rootScope.testUrl = "https://foo/bar";
2455+
$rootScope.$apply();
2456+
expect(element.attr('href')).toBe('https://foo/bar');
2457+
2458+
$rootScope.testUrl = "ftp://foo/bar";
2459+
$rootScope.$apply();
2460+
expect(element.attr('href')).toBe('ftp://foo/bar');
2461+
2462+
$rootScope.testUrl = "mailto:[email protected]";
2463+
$rootScope.$apply();
2464+
expect(element.attr('href')).toBe('mailto:[email protected]');
2465+
}));
2466+
2467+
2468+
it('should not sanitize href on elements other than anchor', inject(function($compile, $rootScope) {
2469+
element = $compile('<div href="{{testUrl}}"></div>')($rootScope);
2470+
$rootScope.testUrl = "javascript:doEvilStuff()";
2471+
$rootScope.$apply();
2472+
2473+
expect(element.attr('href')).toBe('javascript:doEvilStuff()');
2474+
}));
2475+
2476+
2477+
it('should not sanitize attributes other than href', inject(function($compile, $rootScope) {
2478+
element = $compile('<a title="{{testUrl}}"></a>')($rootScope);
2479+
$rootScope.testUrl = "javascript:doEvilStuff()";
2480+
$rootScope.$apply();
2481+
2482+
expect(element.attr('title')).toBe('javascript:doEvilStuff()');
2483+
}));
2484+
2485+
2486+
it('should allow reconfiguration of the href whitelist', function() {
2487+
module(function($compileProvider) {
2488+
expect($compileProvider.urlSanitizationWhitelist() instanceof RegExp).toBe(true);
2489+
var returnVal = $compileProvider.urlSanitizationWhitelist(/javascript:/);
2490+
expect(returnVal).toBe($compileProvider);
2491+
});
2492+
2493+
inject(function($compile, $rootScope) {
2494+
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
2495+
2496+
$rootScope.testUrl = "javascript:doEvilStuff()";
2497+
$rootScope.$apply();
2498+
expect(element.attr('href')).toBe('javascript:doEvilStuff()');
2499+
2500+
$rootScope.testUrl = "http://recon/figured";
2501+
$rootScope.$apply();
2502+
expect(element.attr('href')).toBe('unsafe:http://recon/figured');
2503+
});
2504+
});
23642505
});
23652506
});

0 commit comments

Comments
 (0)