Skip to content

Commit 7a51937

Browse files
committed
1 parent 0498ced commit 7a51937

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/core/compile/compile.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14853,6 +14853,85 @@ describe("$compile", () => {
1485314853
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, false);
1485414854
});
1485514855

14856+
it("should use $$sanitizeUri when working with svg image href bindings", async () => {
14857+
const $$sanitizeUri = jasmine
14858+
.createSpy("$$sanitizeUri")
14859+
.and.returnValue("https://clean.example.org");
14860+
module.config(($provide) =>
14861+
$provide.value("$$sanitizeUri", $$sanitizeUri),
14862+
);
14863+
initInjector("test1");
14864+
$rootScope.testUrl = "https://bad.example.org";
14865+
14866+
const interpolatedHref = $compile(
14867+
'<svg><image href="{{ testUrl }}"></image></svg>',
14868+
)($rootScope);
14869+
await wait();
14870+
expect(interpolatedHref.querySelector("image").getAttribute("href")).toBe(
14871+
"https://clean.example.org",
14872+
);
14873+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, true);
14874+
14875+
$$sanitizeUri.calls.reset();
14876+
14877+
const ngHref = $compile(
14878+
'<svg><image ng-href="{{ testUrl }}" xlink:href=""></image></svg>',
14879+
)($rootScope);
14880+
await wait();
14881+
expect(ngHref.querySelector("image").getAttribute("href")).toBe(
14882+
"https://clean.example.org",
14883+
);
14884+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, true);
14885+
14886+
$$sanitizeUri.calls.reset();
14887+
14888+
const ngAttrHref = $compile(
14889+
'<svg><image ng-attr-href="{{ testUrl }}"></image></svg>',
14890+
)($rootScope);
14891+
await wait();
14892+
expect(ngAttrHref.querySelector("image").getAttribute("href")).toBe(
14893+
"https://clean.example.org",
14894+
);
14895+
expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, true);
14896+
});
14897+
14898+
it("should apply imgSrcSanitizationTrustedUrlList to svg image href bindings", async () => {
14899+
module.config(($compileProvider) =>
14900+
$compileProvider.imgSrcSanitizationTrustedUrlList(
14901+
/^https:\/\/angularjs\.org\//,
14902+
),
14903+
);
14904+
initInjector("test1");
14905+
14906+
const disallowedDataUrl = "data:image/svg+xml;base64,PHN2Zy8+";
14907+
14908+
const hrefInterpolated = $compile(
14909+
'<svg><image href="{{ testUrl }}"></image></svg>',
14910+
)($rootScope);
14911+
$rootScope.testUrl = disallowedDataUrl;
14912+
await wait();
14913+
expect(hrefInterpolated.querySelector("image").getAttribute("href")).toBe(
14914+
`unsafe:${disallowedDataUrl}`,
14915+
);
14916+
14917+
const ngHrefInterpolated = $compile(
14918+
'<svg><image ng-href="{{ testUrl }}" xlink:href=""></image></svg>',
14919+
)($rootScope);
14920+
$rootScope.testUrl = disallowedDataUrl;
14921+
await wait();
14922+
expect(
14923+
ngHrefInterpolated.querySelector("image").getAttribute("href"),
14924+
).toBe(`unsafe:${disallowedDataUrl}`);
14925+
14926+
const ngAttrHref = $compile(
14927+
'<svg><image ng-attr-href="data:image/svg+xml;base64,PHN2Zy8+"></image></svg>',
14928+
)($rootScope);
14929+
await wait();
14930+
expect(ngAttrHref.querySelector("image").getAttribute("href")).toBe(
14931+
`unsafe:${disallowedDataUrl}`,
14932+
);
14933+
});
14934+
1485614935
it("should require a RESOURCE_URL context for href by if not on an anchor or image", async () => {
1485714936
let error = [];
1485814937
module.decorator("$exceptionHandler", () => {

src/core/compile/compile.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ export class CompileProvider {
18001800
applyInterpolatedAttrValue(linkState, attr, interpolateFn(scope));
18011801
});
18021802
} else {
1803-
applyInterpolatedAttrValue(linkState, attr, newValue);
1803+
applyInterpolatedAttrValue(linkState, attr, interpolateFn(scope));
18041804
}
18051805
}
18061806

@@ -3523,6 +3523,13 @@ export class CompileProvider {
35233523
return $sce.RESOURCE_URL;
35243524
}
35253525

3526+
if (
3527+
nodeName === "image" &&
3528+
(attrNormalizedName === "href" || attrNormalizedName === "ngHref")
3529+
) {
3530+
return $sce.MEDIA_URL;
3531+
}
3532+
35263533
if (
35273534
// Formaction
35283535
(nodeName === "form" && attrNormalizedName === "action") ||

0 commit comments

Comments
 (0)