Skip to content

Commit c069cab

Browse files
authored
fix(cdk/private): guard createPolicy against DOM clobbering (#33410)
Wraps trustedTypes.createPolicy in a try/catch to handle two failure cases: the policy name already being registered (e.g. in a micro-frontend setup), and window.trustedTypes being DOM-clobbered by an HTML element before Angular bootstraps. In both cases the policy falls back to null, and trustedHTMLFromString continues to work via plain strings while sanitization in _setInnerHtml still runs.
1 parent 67a5031 commit c069cab

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/cdk/private/trusted-types.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ function getPolicy(): TrustedTypePolicy | null {
4545
if (typeof window !== 'undefined') {
4646
const ttWindow = window as unknown as {trustedTypes?: TrustedTypePolicyFactory};
4747
if (ttWindow.trustedTypes !== undefined) {
48-
policy = ttWindow.trustedTypes.createPolicy('angular#components', {
49-
createHTML: (s: string) => s,
50-
});
48+
try {
49+
policy = ttWindow.trustedTypes.createPolicy('angular#components', {
50+
createHTML: (s: string) => s,
51+
});
52+
} catch (error) {
53+
// createPolicy can throw if the name is already registered, or if
54+
// window.trustedTypes was DOM-clobbered with an HTML element before
55+
// Angular bootstrapped. trustedHTMLFromString falls back to plain
56+
// strings — sanitization in _setInnerHtml still runs.
57+
console.error(error);
58+
}
5159
}
5260
}
5361
}

0 commit comments

Comments
 (0)