Skip to content

Commit 26cdc53

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 5b4fd22 commit 26cdc53

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

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

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

8281+
it('should generate sanitizers for URL properties in SVG script fn in Component', () => {
8282+
env.write(
8283+
'test.ts',
8284+
`
8285+
import {Component} from '@angular/core';
8286+
8287+
@Component({
8288+
selector: 'test-cmp',
8289+
template: \`
8290+
<svg>
8291+
<script [attr.xlink:href]="attr" [attr.href]="attr"></script>
8292+
</svg>
8293+
\`,
8294+
})
8295+
export class TestCmp {
8296+
attr = './script.js';
8297+
}
8298+
`,
8299+
);
8300+
8301+
env.driveMain();
8302+
8303+
const jsContents = env.getContents('test.js');
8304+
expect(jsContents).toContain(
8305+
'i0.ɵɵattribute("href", ctx.attr, i0.ɵɵsanitizeResourceUrl, "xlink")("href", ctx.attr, i0.ɵɵsanitizeResourceUrl);',
8306+
);
8307+
});
8308+
82818309
it('should not generate sanitizers for URL properties in hostBindings fn in Component', () => {
82828310
env.write(
82838311
`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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"EventType2",
8383
"GuardsCheckEnd",
8484
"GuardsCheckStart",
85+
"HREF_RESOURCE_TAGS",
8586
"HistoryStateManager",
8687
"INITIAL_NAVIGATION",
8788
"INITIAL_VALUE",
@@ -211,6 +212,7 @@
211212
"SIGNAL",
212213
"SIGNAL_NODE",
213214
"SIMPLE_CHANGES_STORE",
215+
"SRC_RESOURCE_TAGS",
214216
"SafeSubscriber",
215217
"SafeValueImpl",
216218
"Sanitizer",
@@ -748,4 +750,4 @@
748750
"ɵɵsanitizeUrl",
749751
"ɵɵtext",
750752
"ɵɵtextInterpolate1"
751-
]
753+
]

0 commit comments

Comments
 (0)