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

Commit 7673810

Browse files
josephperrottpetebacondarwin
authored andcommitted
refactor(SanitizeUriProvider): remove usages of whitelist
Changes aHrefSanitizationWhitelist to aHrefSanitizationTrustedUri and imgSrcSanitizationWhitelist to imgSrcSanitizationTrustedUri updating references to use the new symbols. For the purposes of backward compatibility, the previous symbols are aliased to the new symbols.
1 parent c953af6 commit 7673810

File tree

6 files changed

+69
-66
lines changed

6 files changed

+69
-66
lines changed

docs/content/guide/migration.ngdoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ break.
283283

284284
To fix this you need to ensure that the values used for binding to the affected
285285
`xlink:href` contexts are considered safe URLs, e.g. by whitelisting them in
286-
`$compileProvider`'s `aHrefSanitizationWhitelist` (for `<a>` elements) or
287-
`imgSrcSanitizationWhitelist` (for `<image>` elements).
286+
`$compileProvider`'s `aHrefSanitizationTrustedUri` (for `<a>` elements) or
287+
`imgSrcSanitizationTrustedUri` (for `<image>` elements).
288288

289289
<hr />
290290

src/ng/compile.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@
11161116
* By default, `$sce` will throw an error if it detects untrusted HTML content, and will not bind the
11171117
* content.
11181118
* However, if you include the {@link ngSanitize ngSanitize module}, it will try to sanitize the
1119-
* potentially dangerous HTML, e.g. strip non-whitelisted tags and attributes when binding to
1119+
* potentially dangerous HTML, e.g. strip non-trusted tags and attributes when binding to
11201120
* `innerHTML`.
11211121
*
11221122
* @example
@@ -1698,62 +1698,64 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16981698

16991699
/**
17001700
* @ngdoc method
1701-
* @name $compileProvider#aHrefSanitizationWhitelist
1701+
* @name $compileProvider#aHrefSanitizationTrustedUri
17021702
* @kind function
17031703
*
17041704
* @description
1705-
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
1705+
* Retrieves or overrides the default regular expression that is used for determining trusted safe
17061706
* urls during a[href] sanitization.
17071707
*
17081708
* The sanitization is a security measure aimed at preventing XSS attacks via html links.
17091709
*
17101710
* Any url about to be assigned to a[href] via data-binding is first normalized and turned into
1711-
* an absolute url. Afterwards, the url is matched against the `aHrefSanitizationWhitelist`
1711+
* an absolute url. Afterwards, the url is matched against the `aHrefSanitizationTrustedUri`
17121712
* regular expression. If a match is found, the original url is written into the dom. Otherwise,
17131713
* the absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM.
17141714
*
1715-
* @param {RegExp=} regexp New regexp to whitelist urls with.
1715+
* @param {RegExp=} regexp New regexp to trust urls with.
17161716
* @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for
17171717
* chaining otherwise.
17181718
*/
1719-
this.aHrefSanitizationWhitelist = function(regexp) {
1719+
this.aHrefSanitizationTrustedUri = function(regexp) {
17201720
if (isDefined(regexp)) {
1721-
$$sanitizeUriProvider.aHrefSanitizationWhitelist(regexp);
1721+
$$sanitizeUriProvider.aHrefSanitizationTrustedUri(regexp);
17221722
return this;
17231723
} else {
1724-
return $$sanitizeUriProvider.aHrefSanitizationWhitelist();
1724+
return $$sanitizeUriProvider.aHrefSanitizationTrustedUri();
17251725
}
17261726
};
1727+
this.aHrefSanitizationWhitelist = this.aHrefSanitizationTrustedUri;
17271728

17281729

17291730
/**
17301731
* @ngdoc method
1731-
* @name $compileProvider#imgSrcSanitizationWhitelist
1732+
* @name $compileProvider#imgSrcSanitizationTrustedUri
17321733
* @kind function
17331734
*
17341735
* @description
1735-
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
1736+
* Retrieves or overrides the default regular expression that is used for determining trusted safe
17361737
* urls during img[src] sanitization.
17371738
*
17381739
* The sanitization is a security measure aimed at prevent XSS attacks via html links.
17391740
*
17401741
* Any url about to be assigned to img[src] via data-binding is first normalized and turned into
1741-
* an absolute url. Afterwards, the url is matched against the `imgSrcSanitizationWhitelist`
1742+
* an absolute url. Afterwards, the url is matched against the `imgSrcSanitizationTrustedUri`
17421743
* regular expression. If a match is found, the original url is written into the dom. Otherwise,
17431744
* the absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM.
17441745
*
1745-
* @param {RegExp=} regexp New regexp to whitelist urls with.
1746+
* @param {RegExp=} regexp New regexp to trust urls with.
17461747
* @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for
17471748
* chaining otherwise.
17481749
*/
1749-
this.imgSrcSanitizationWhitelist = function(regexp) {
1750+
this.imgSrcSanitizationTrustedUri = function(regexp) {
17501751
if (isDefined(regexp)) {
1751-
$$sanitizeUriProvider.imgSrcSanitizationWhitelist(regexp);
1752+
$$sanitizeUriProvider.imgSrcSanitizationTrustedUri(regexp);
17521753
return this;
17531754
} else {
1754-
return $$sanitizeUriProvider.imgSrcSanitizationWhitelist();
1755+
return $$sanitizeUriProvider.imgSrcSanitizationTrustedUri();
17551756
}
17561757
};
1758+
this.imgSrcSanitizationWhitelist = this.imgSrcSanitizationTrustedUri;
17571759

17581760
/**
17591761
* @ngdoc method

src/ng/sanitizeUri.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88
function $$SanitizeUriProvider() {
99

10-
var aHrefSanitizationWhitelist = /^\s*(https?|s?ftp|mailto|tel|file):/,
11-
imgSrcSanitizationWhitelist = /^\s*((https?|ftp|file|blob):|data:image\/)/;
10+
var aHrefSanitizationTrustedUri = /^\s*(https?|s?ftp|mailto|tel|file):/,
11+
imgSrcSanitizationTrustedUri = /^\s*((https?|ftp|file|blob):|data:image\/)/;
1212

1313
/**
1414
* @description
15-
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
15+
* Retrieves or overrides the default regular expression that is used for determining trusted safe
1616
* urls during a[href] sanitization.
1717
*
1818
* The sanitization is a security measure aimed at prevent XSS attacks via HTML anchor links.
@@ -21,27 +21,27 @@ function $$SanitizeUriProvider() {
2121
* the $sce.URL security context. When interpolation occurs a call is made to `$sce.trustAsUrl(url)`
2222
* which in turn may call `$$sanitizeUri(url, isMedia)` to sanitize the potentially malicious URL.
2323
*
24-
* If the URL matches the `aHrefSanitizationWhitelist` regular expression, it is returned unchanged.
24+
* If the URL matches the `aHrefSanitizationTrustedUri` regular expression, it is returned unchanged.
2525
*
2626
* If there is no match the URL is returned prefixed with `'unsafe:'` to ensure that when it is written
2727
* to the DOM it is inactive and potentially malicious code will not be executed.
2828
*
29-
* @param {RegExp=} regexp New regexp to whitelist urls with.
29+
* @param {RegExp=} regexp New regexp to trust urls with.
3030
* @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for
3131
* chaining otherwise.
3232
*/
33-
this.aHrefSanitizationWhitelist = function(regexp) {
33+
this.aHrefSanitizationTrustedUri = function(regexp) {
3434
if (isDefined(regexp)) {
35-
aHrefSanitizationWhitelist = regexp;
35+
aHrefSanitizationTrustedUri = regexp;
3636
return this;
3737
}
38-
return aHrefSanitizationWhitelist;
38+
return aHrefSanitizationTrustedUri;
3939
};
4040

4141

4242
/**
4343
* @description
44-
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
44+
* Retrieves or overrides the default regular expression that is used for determining trusted safe
4545
* urls during img[src] sanitization.
4646
*
4747
* The sanitization is a security measure aimed at prevent XSS attacks via HTML image src links.
@@ -51,27 +51,28 @@ function $$SanitizeUriProvider() {
5151
* `$sce.trustAsMediaUrl(url)` which in turn may call `$$sanitizeUri(url, isMedia)` to sanitize
5252
* the potentially malicious URL.
5353
*
54-
* If the URL matches the `aImgSanitizationWhitelist` regular expression, it is returned unchanged.
54+
* If the URL matches the `imgSrcSanitizationTrustedUrlList` regular expression, it is returned
55+
* unchanged.
5556
*
5657
* If there is no match the URL is returned prefixed with `'unsafe:'` to ensure that when it is written
5758
* to the DOM it is inactive and potentially malicious code will not be executed.
5859
*
59-
* @param {RegExp=} regexp New regexp to whitelist urls with.
60+
* @param {RegExp=} regexp New regexp to trust urls with.
6061
* @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for
6162
* chaining otherwise.
6263
*/
63-
this.imgSrcSanitizationWhitelist = function(regexp) {
64+
this.imgSrcSanitizationTrustedUri = function(regexp) {
6465
if (isDefined(regexp)) {
65-
imgSrcSanitizationWhitelist = regexp;
66+
imgSrcSanitizationTrustedUri = regexp;
6667
return this;
6768
}
68-
return imgSrcSanitizationWhitelist;
69+
return imgSrcSanitizationTrustedUri;
6970
};
7071

7172
this.$get = function() {
7273
return function sanitizeUri(uri, isMediaUrl) {
7374
// if (!uri) return uri;
74-
var regex = isMediaUrl ? imgSrcSanitizationWhitelist : aHrefSanitizationWhitelist;
75+
var regex = isMediaUrl ? imgSrcSanitizationTrustedUri : aHrefSanitizationTrustedUri;
7576
var normalizedVal = urlResolve(uri && uri.trim()).href;
7677
if (normalizedVal !== '' && !normalizedVal.match(regex)) {
7778
return 'unsafe:' + normalizedVal;

src/ngSanitize/sanitize.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var htmlSanitizeWriter;
4646
* it into the returned string.
4747
*
4848
* The whitelist for URL sanitization of attribute values is configured using the functions
49-
* `aHrefSanitizationWhitelist` and `imgSrcSanitizationWhitelist` of {@link $compileProvider}.
49+
* `aHrefSanitizationTrustedUri` and `imgSrcSanitizationTrustedUri` of {@link $compileProvider}.
5050
*
5151
* The input may also contain SVG markup if this is enabled via {@link $sanitizeProvider}.
5252
*
@@ -277,8 +277,8 @@ function $SanitizeProvider() {
277277
* **Note**:
278278
* The new attributes will not be treated as URI attributes, which means their values will not be
279279
* sanitized as URIs using `$compileProvider`'s
280-
* {@link ng.$compileProvider#aHrefSanitizationWhitelist aHrefSanitizationWhitelist} and
281-
* {@link ng.$compileProvider#imgSrcSanitizationWhitelist imgSrcSanitizationWhitelist}.
280+
* {@link ng.$compileProvider#aHrefSanitizationTrustedUri aHrefSanitizationTrustedUri} and
281+
* {@link ng.$compileProvider#imgSrcSanitizationTrustedUri imgSrcSanitizationTrustedUri}.
282282
*
283283
* <div class="alert alert-info">
284284
* This method must be called during the {@link angular.Module#config config} phase. Once the

test/ng/compileSpec.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,30 @@ describe('$compile', function() {
151151

152152
describe('configuration', function() {
153153

154-
it('should use $$sanitizeUriProvider for reconfiguration of the `aHrefSanitizationWhitelist`', function() {
154+
it('should use $$sanitizeUriProvider for reconfiguration of the `aHrefSanitizationTrustedUri`', function() {
155155
module(function($compileProvider, $$sanitizeUriProvider) {
156156
var newRe = /safe:/, returnVal;
157157

158-
expect($compileProvider.aHrefSanitizationWhitelist()).toBe($$sanitizeUriProvider.aHrefSanitizationWhitelist());
159-
returnVal = $compileProvider.aHrefSanitizationWhitelist(newRe);
158+
expect($compileProvider.aHrefSanitizationTrustedUri()).toBe($$sanitizeUriProvider.aHrefSanitizationTrustedUri());
159+
returnVal = $compileProvider.aHrefSanitizationTrustedUri(newRe);
160160
expect(returnVal).toBe($compileProvider);
161-
expect($$sanitizeUriProvider.aHrefSanitizationWhitelist()).toBe(newRe);
162-
expect($compileProvider.aHrefSanitizationWhitelist()).toBe(newRe);
161+
expect($$sanitizeUriProvider.aHrefSanitizationTrustedUri()).toBe(newRe);
162+
expect($compileProvider.aHrefSanitizationTrustedUri()).toBe(newRe);
163163
});
164164
inject(function() {
165165
// needed to the module definition above is run...
166166
});
167167
});
168168

169-
it('should use $$sanitizeUriProvider for reconfiguration of the `imgSrcSanitizationWhitelist`', function() {
169+
it('should use $$sanitizeUriProvider for reconfiguration of the `imgSrcSanitizationTrustedUri`', function() {
170170
module(function($compileProvider, $$sanitizeUriProvider) {
171171
var newRe = /safe:/, returnVal;
172172

173-
expect($compileProvider.imgSrcSanitizationWhitelist()).toBe($$sanitizeUriProvider.imgSrcSanitizationWhitelist());
174-
returnVal = $compileProvider.imgSrcSanitizationWhitelist(newRe);
173+
expect($compileProvider.imgSrcSanitizationTrustedUri()).toBe($$sanitizeUriProvider.imgSrcSanitizationTrustedUri());
174+
returnVal = $compileProvider.imgSrcSanitizationTrustedUri(newRe);
175175
expect(returnVal).toBe($compileProvider);
176-
expect($$sanitizeUriProvider.imgSrcSanitizationWhitelist()).toBe(newRe);
177-
expect($compileProvider.imgSrcSanitizationWhitelist()).toBe(newRe);
176+
expect($$sanitizeUriProvider.imgSrcSanitizationTrustedUri()).toBe(newRe);
177+
expect($compileProvider.imgSrcSanitizationTrustedUri()).toBe(newRe);
178178
});
179179
inject(function() {
180180
// needed to the module definition above is run...
@@ -11334,9 +11334,9 @@ describe('$compile', function() {
1133411334
// IE9 rejects the `video` / `audio` tags with "Error: Not implemented"
1133511335
if (msie !== 9 || tag === 'img') {
1133611336
describe(tag + '[src] context requirement', function() {
11337-
it('should NOT require trusted values for whitelisted URIs', inject(function($rootScope, $compile) {
11337+
it('should NOT require trusted values for trusted URIs', inject(function($rootScope, $compile) {
1133811338
element = $compile('<' + tag + ' src="{{testUrl}}"></' + tag + '>')($rootScope);
11339-
$rootScope.testUrl = 'http://example.com/image.mp4'; // `http` is whitelisted
11339+
$rootScope.testUrl = 'http://example.com/image.mp4'; // `http` is trusted
1134011340
$rootScope.$digest();
1134111341
expect(element.attr('src')).toEqual('http://example.com/image.mp4');
1134211342
}));
@@ -11372,9 +11372,9 @@ describe('$compile', function() {
1137211372
if (msie !== 9) {
1137311373
['source', 'track'].forEach(function(tag) {
1137411374
describe(tag + '[src]', function() {
11375-
it('should NOT require trusted values for whitelisted URIs', inject(function($rootScope, $compile) {
11375+
it('should NOT require trusted values for trusted URIs', inject(function($rootScope, $compile) {
1137611376
element = $compile('<video><' + tag + ' src="{{testUrl}}"></' + tag + '></video>')($rootScope);
11377-
$rootScope.testUrl = 'http://example.com/image.mp4'; // `http` is whitelisted
11377+
$rootScope.testUrl = 'http://example.com/image.mp4'; // `http` is trusted
1137811378
$rootScope.$digest();
1137911379
expect(element.find(tag).attr('src')).toEqual('http://example.com/image.mp4');
1138011380
}));
@@ -11509,14 +11509,14 @@ describe('$compile', function() {
1150911509
});
1151011510
});
1151111511

11512-
it('should NOT require trusted values for whitelisted values', inject(function($rootScope, $compile, $sce) {
11512+
it('should NOT require trusted values for trusted URI values', inject(function($rootScope, $compile, $sce) {
1151311513
element = $compile('<img srcset="{{testUrl}}"></img>')($rootScope);
11514-
$rootScope.testUrl = 'http://example.com/image.png'; // `http` is whitelisted
11514+
$rootScope.testUrl = 'http://example.com/image.png'; // `http` is trusted
1151511515
$rootScope.$digest();
1151611516
expect(element.attr('srcset')).toEqual('http://example.com/image.png');
1151711517
}));
1151811518

11519-
it('should accept trusted values, if they are also whitelisted', inject(function($rootScope, $compile, $sce) {
11519+
it('should accept trusted values, if they are also trusted URIs', inject(function($rootScope, $compile, $sce) {
1152011520
element = $compile('<img srcset="{{testUrl}}"></img>')($rootScope);
1152111521
$rootScope.testUrl = $sce.trustAsUrl('http://example.com');
1152211522
$rootScope.$digest();
@@ -11602,8 +11602,8 @@ describe('$compile', function() {
1160211602
});
1160311603

1160411604
describe('a[href] sanitization', function() {
11605-
it('should NOT require trusted values for whitelisted values', inject(function($rootScope, $compile) {
11606-
$rootScope.testUrl = 'http://example.com/image.png'; // `http` is whitelisted
11605+
it('should NOT require trusted values for trusted URI values', inject(function($rootScope, $compile) {
11606+
$rootScope.testUrl = 'http://example.com/image.png'; // `http` is trusted
1160711607
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
1160811608
$rootScope.$digest();
1160911609
expect(element.attr('href')).toEqual('http://example.com/image.png');
@@ -11613,8 +11613,8 @@ describe('$compile', function() {
1161311613
expect(element.attr('ng-href')).toEqual('http://example.com/image.png');
1161411614
}));
1161511615

11616-
it('should accept trusted values for non-whitelisted values', inject(function($rootScope, $compile, $sce) {
11617-
$rootScope.testUrl = $sce.trustAsUrl('javascript:foo()'); // `javascript` is not whitelisted
11616+
it('should accept trusted values for non-trusted URI values', inject(function($rootScope, $compile, $sce) {
11617+
$rootScope.testUrl = $sce.trustAsUrl('javascript:foo()'); // `javascript` is not trusted
1161811618
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
1161911619
$rootScope.$digest();
1162011620
expect(element.attr('href')).toEqual('javascript:foo()');
@@ -11624,8 +11624,8 @@ describe('$compile', function() {
1162411624
expect(element.attr('ng-href')).toEqual('javascript:foo()');
1162511625
}));
1162611626

11627-
it('should sanitize non-whitelisted values', inject(function($rootScope, $compile) {
11628-
$rootScope.testUrl = 'javascript:foo()'; // `javascript` is not whitelisted
11627+
it('should sanitize non-trusted values', inject(function($rootScope, $compile) {
11628+
$rootScope.testUrl = 'javascript:foo()'; // `javascript` is not trusted
1162911629
element = $compile('<a href="{{testUrl}}"></a>')($rootScope);
1163011630
$rootScope.$digest();
1163111631
expect(element.attr('href')).toEqual('unsafe:javascript:foo()');
@@ -11678,7 +11678,7 @@ describe('$compile', function() {
1167811678
$provide.value('$$sanitizeUri', $$sanitizeUri);
1167911679
});
1168011680
inject(function($compile, $rootScope) {
11681-
// This URL would fail the RESOURCE_URL whitelist, but that test shouldn't be run
11681+
// This URL would fail the RESOURCE_URL trusted list, but that test shouldn't be run
1168211682
// because these interpolations will be resolved against the URL context instead
1168311683
$rootScope.testUrl = 'https://bad.example.org';
1168411684

@@ -11700,7 +11700,7 @@ describe('$compile', function() {
1170011700
$provide.value('$$sanitizeUri', $$sanitizeUri);
1170111701
});
1170211702
inject(function($compile, $rootScope) {
11703-
// This URL would fail the RESOURCE_URL whitelist, but that test shouldn't be run
11703+
// This URL would fail the RESOURCE_URL trusted list, but that test shouldn't be run
1170411704
// because these interpolations will be resolved against the URL context instead
1170511705
$rootScope.testUrl = 'https://bad.example.org';
1170611706

test/ng/sanitizeUriSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ describe('sanitizeUri', function() {
125125
expect(sanitizeImg(testUrl)).toBe('data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
126126
});
127127

128-
it('should allow reconfiguration of the src whitelist', function() {
128+
it('should allow reconfiguration of the src trusted URIs', function() {
129129
var returnVal;
130-
expect(sanitizeUriProvider.imgSrcSanitizationWhitelist() instanceof RegExp).toBe(true);
131-
returnVal = sanitizeUriProvider.imgSrcSanitizationWhitelist(/javascript:/);
130+
expect(sanitizeUriProvider.imgSrcSanitizationTrustedUri() instanceof RegExp).toBe(true);
131+
returnVal = sanitizeUriProvider.imgSrcSanitizationTrustedUri(/javascript:/);
132132
expect(returnVal).toBe(sanitizeUriProvider);
133133

134134
testUrl = 'javascript:doEvilStuff()';
@@ -226,10 +226,10 @@ describe('sanitizeUri', function() {
226226
expect(sanitizeHref(testUrl)).toBe('file:///foo/bar.html');
227227
}));
228228

229-
it('should allow reconfiguration of the href whitelist', function() {
229+
it('should allow reconfiguration of the href trusted URIs', function() {
230230
var returnVal;
231-
expect(sanitizeUriProvider.aHrefSanitizationWhitelist() instanceof RegExp).toBe(true);
232-
returnVal = sanitizeUriProvider.aHrefSanitizationWhitelist(/javascript:/);
231+
expect(sanitizeUriProvider.aHrefSanitizationTrustedUri() instanceof RegExp).toBe(true);
232+
returnVal = sanitizeUriProvider.aHrefSanitizationTrustedUri(/javascript:/);
233233
expect(returnVal).toBe(sanitizeUriProvider);
234234

235235
testUrl = 'javascript:doEvilStuff()';

0 commit comments

Comments
 (0)