Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2c2b4a

Browse files
authored
fix(core): sanitize sensitive attributes on SVG script elements
This commit updates the DOM security schema and sanitization logic to properly recognize and sanitize `href` and `xlink:href` attributes on SVG `<script>` elements.
1 parent 4755bbd commit c2c2b4a

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

packages/compiler-cli/test/ngtsc/ngtsc_spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8530,6 +8530,34 @@ runInEachFileSystem((os: string) => {
85308530
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
85318531
});
85328532

8533+
it('should generate sanitizers for URL properties in SVG script fn in Component', () => {
8534+
env.write(
8535+
'test.ts',
8536+
`
8537+
import {Component} from '@angular/core';
8538+
8539+
@Component({
8540+
selector: 'test-cmp',
8541+
template: \`
8542+
<svg>
8543+
<script [attr.xlink:href]="attr" [attr.href]="attr"></script>
8544+
</svg>
8545+
\`,
8546+
})
8547+
export class TestCmp {
8548+
attr = './script.js';
8549+
}
8550+
`,
8551+
);
8552+
8553+
env.driveMain();
8554+
8555+
const jsContents = env.getContents('test.js');
8556+
expect(jsContents).toContain(
8557+
'i0.ɵɵattribute("href", ctx.attr, i0.ɵɵsanitizeResourceUrl, "xlink")("href", ctx.attr, i0.ɵɵsanitizeResourceUrl);',
8558+
);
8559+
});
8560+
85338561
it('should not generate sanitizers for URL properties in hostBindings fn in Component', () => {
85348562
env.write(
85358563
`test.ts`,

packages/compiler/src/schema/dom_security_schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export function SECURITY_SCHEMA(): {[k: string]: SecurityContext} {
134134
'object|codebase',
135135
'object|data',
136136
'script|src',
137+
// The below two are for Script SVG
138+
// See: https://developer.mozilla.org/en-US/docs/Web/API/SVGScriptElement/href
139+
'script|href',
140+
'script|xlink:href',
137141
]);
138142

139143
// Keep this in sync with SECURITY_SENSITIVE_ELEMENTS in packages/core/src/sanitization/sanitization.ts

packages/core/src/sanitization/sanitization.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ export function ɵɵtrustConstantResourceUrl(url: TemplateStringsArray): Trusted
213213
return trustedScriptURLFromString(url[0]);
214214
}
215215

216+
// Define sets outside the function for O(1) lookups and memory efficiency
217+
const SRC_RESOURCE_TAGS = new Set(['embed', 'frame', 'iframe', 'media', 'script']);
218+
const HREF_RESOURCE_TAGS = new Set(['base', 'link', 'script']);
219+
216220
/**
217221
* Detects which sanitizer to use for URL property, based on tag name and prop name.
218222
*
@@ -221,18 +225,12 @@ export function ɵɵtrustConstantResourceUrl(url: TemplateStringsArray): Trusted
221225
* If tag and prop names don't match Resource URL schema, use URL sanitizer.
222226
*/
223227
export function getUrlSanitizer(tag: string, prop: string) {
224-
if (
225-
(prop === 'src' &&
226-
(tag === 'embed' ||
227-
tag === 'frame' ||
228-
tag === 'iframe' ||
229-
tag === 'media' ||
230-
tag === 'script')) ||
231-
(prop === 'href' && (tag === 'base' || tag === 'link'))
232-
) {
233-
return ɵɵsanitizeResourceUrl;
234-
}
235-
return ɵɵsanitizeUrl;
228+
const isResource =
229+
(prop === 'src' && SRC_RESOURCE_TAGS.has(tag)) ||
230+
(prop === 'href' && HREF_RESOURCE_TAGS.has(tag)) ||
231+
(prop === 'xlink:href' && tag === 'script');
232+
233+
return isResource ? ɵɵsanitizeResourceUrl : ɵɵsanitizeUrl;
236234
}
237235

238236
/**

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"HOST",
116116
"HOST_ATTR",
117117
"HOST_TAG_NAME",
118+
"HREF_RESOURCE_TAGS",
118119
"HYDRATION",
119120
"HistoryStateManager",
120121
"HostAttributeToken",
@@ -274,6 +275,7 @@
274275
"SIGNAL",
275276
"SIGNAL_NODE",
276277
"SIMPLE_CHANGES_STORE",
278+
"SRC_RESOURCE_TAGS",
277279
"SVG_NAMESPACE",
278280
"SafeSubscriber",
279281
"SafeValueImpl",

0 commit comments

Comments
 (0)