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

fix($compile): turn link[href] into a RESOURCE_URL context. #14687

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
// maction[xlink:href] can source SVG. It's not limited to <maction>.
if (attrNormalizedName === "xlinkHref" ||
(tag === "form" && attrNormalizedName === "action") ||
// links can be stylesheets or imports, which can run script in the current origin
(tag === "link" && attrNormalizedName === "href") ||
(tag !== "img" && (attrNormalizedName === "src" ||
attrNormalizedName === "ngSrc"))) {
return $sce.RESOURCE_URL;
Expand Down
31 changes: 31 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10002,6 +10002,7 @@ describe('$compile', function() {
"loading resource from url not allowed by $sceDelegate policy. URL: javascript:doTrustedStuff()");
}));


it('should pass through $sce.trustAs() values in action attribute', inject(function($compile, $rootScope, $sce) {
/* jshint scripturl:true */
element = $compile('<form action="{{testUrl}}"></form>')($rootScope);
Expand All @@ -10012,6 +10013,36 @@ describe('$compile', function() {
}));
});

describe('link[href]', function() {
it('should reject invalid RESOURCE_URLs', inject(function($compile, $rootScope) {
element = $compile('<link href="{{testUrl}}" rel="stylesheet" />')($rootScope);
$rootScope.testUrl = "https://evil.example.org/css.css";
expect(function() { $rootScope.$apply(); }).toThrowMinErr(
"$interpolate", "interr", "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
"loading resource from url not allowed by $sceDelegate policy. URL: " +
"https://evil.example.org/css.css");
}));

it('should accept valid RESOURCE_URLs', inject(function($compile, $rootScope, $sce) {
element = $compile('<link href="{{testUrl}}" rel="stylesheet" />')($rootScope);

$rootScope.testUrl = "./css1.css";
$rootScope.$apply();
expect(element.attr('href')).toContain('css1.css');

$rootScope.testUrl = $sce.trustAsResourceUrl('https://elsewhere.example.org/css2.css');
$rootScope.$apply();
expect(element.attr('href')).toContain('https://elsewhere.example.org/css2.css');
}));

it('should accept valid constants', inject(function($compile, $rootScope) {
element = $compile('<link href="https://elsewhere.example.org/css2.css" rel="stylesheet" />')($rootScope);

$rootScope.$apply();
expect(element.attr('href')).toContain('https://elsewhere.example.org/css2.css');
}));
});

if (!msie || msie >= 11) {
describe('iframe[srcdoc]', function() {
it('should NOT set iframe contents for untrusted values', inject(function($compile, $rootScope, $sce) {
Expand Down