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

Commit ff090d7

Browse files
rjametNarretz
authored andcommitted
fix($compile): lower the $sce context for src on video, audio, and track.
Previously, video, audio, and track sources were $sce.RESOURCE_URL. This is not justified as no attacks are possible through these attributes as far as I can tell. This change is not breaking, and uses of $sce.trustAsResourceUrl before assigning to src or ng-src attributes will just be silently ignored.
1 parent e50e91c commit ff090d7

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/ng/compile.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,13 +3151,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
31513151
return $sce.HTML;
31523152
}
31533153
var tag = nodeName_(node);
3154+
// All tags with src attributes require a RESOURCE_URL value, except for
3155+
// img and various html5 media tags.
3156+
if (attrNormalizedName === 'src' || attrNormalizedName === 'ngSrc') {
3157+
if (['img', 'video', 'audio', 'track'].indexOf(tag) === -1) {
3158+
return $sce.RESOURCE_URL;
3159+
}
31543160
// maction[xlink:href] can source SVG. It's not limited to <maction>.
3155-
if (attrNormalizedName === 'xlinkHref' ||
3156-
(tag === 'form' && attrNormalizedName === 'action') ||
3157-
// links can be stylesheets or imports, which can run script in the current origin
3158-
(tag === 'link' && attrNormalizedName === 'href') ||
3159-
(tag !== 'img' && (attrNormalizedName === 'src' ||
3160-
attrNormalizedName === 'ngSrc'))) {
3161+
} else if (attrNormalizedName === 'xlinkHref' ||
3162+
(tag === 'form' && attrNormalizedName === 'action')
3163+
) {
31613164
return $sce.RESOURCE_URL;
31623165
}
31633166
}

src/ng/sce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ function $SceDelegateProvider() {
537537
* | `$sce.HTML` | For HTML that's safe to source into the application. The {@link ng.directive:ngBindHtml ngBindHtml} directive uses this context for bindings. If an unsafe value is encountered and the {@link ngSanitize $sanitize} module is present this will sanitize the value instead of throwing an error. |
538538
* | `$sce.CSS` | For CSS that's safe to source into the application. Currently unused. Feel free to use it in your own directives. |
539539
* | `$sce.URL` | For URLs that are safe to follow as links. Currently unused (`<a href=` and `<img src=` sanitize their urls and don't constitute an SCE context. |
540-
* | `$sce.RESOURCE_URL` | For URLs that are not only safe to follow as links, but whose contents are also safe to include in your application. Examples include `ng-include`, `src` / `ngSrc` bindings for tags other than `IMG` (e.g. `IFRAME`, `OBJECT`, etc.) <br><br>Note that `$sce.RESOURCE_URL` makes a stronger statement about the URL than `$sce.URL` does and therefore contexts requiring values trusted for `$sce.RESOURCE_URL` can be used anywhere that values trusted for `$sce.URL` are required. |
540+
* | `$sce.RESOURCE_URL` | For URLs that are not only safe to follow as links, but whose contents are also safe to include in your application. Examples include `ng-include`, `src` / `ngSrc` bindings for tags other than `IMG`, `VIDEO`, `AUDIO` and `TRACK` (e.g. `IFRAME`, `OBJECT`, etc.) <br><br>Note that `$sce.RESOURCE_URL` makes a stronger statement about the URL than `$sce.URL` does and therefore contexts requiring values trusted for `$sce.RESOURCE_URL` can be used anywhere that values trusted for `$sce.URL` are required. |
541541
* | `$sce.JS` | For JavaScript that is safe to execute in your application's context. Currently unused. Feel free to use it in your own directives. |
542542
*
543543
* ## Format of items in {@link ng.$sceDelegateProvider#resourceUrlWhitelist resourceUrlWhitelist}/{@link ng.$sceDelegateProvider#resourceUrlBlacklist Blacklist} <a name="resourceUrlPatternItem"></a>

test/ng/compileSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10152,6 +10152,18 @@ describe('$compile', function() {
1015210152
expect(element.attr('src')).toEqual('http://example.com/image2.png');
1015310153
}));
1015410154

10155+
it('should NOT require trusted values for video / audio / track src', inject(function($rootScope, $compile, $sce) {
10156+
element = $compile('<video src="{{testUrl}}"></video>')($rootScope);
10157+
$rootScope.testUrl = 'http://example.com/image.mp4';
10158+
$rootScope.$digest();
10159+
expect(element.attr('src')).toEqual('http://example.com/image.mp4');
10160+
10161+
// But it should accept trusted values anyway.
10162+
$rootScope.testUrl = $sce.trustAsUrl('http://example.com/image2.mp4');
10163+
$rootScope.$digest();
10164+
expect(element.attr('src')).toEqual('http://example.com/image2.mp4');
10165+
}));
10166+
1015510167
it('should not sanitize attributes other than src', inject(function($compile, $rootScope) {
1015610168
element = $compile('<img title="{{testUrl}}"></img>')($rootScope);
1015710169
$rootScope.testUrl = 'javascript:doEvilStuff()';

0 commit comments

Comments
 (0)