Skip to content

Commit e8f3419

Browse files
committed
fix(cdk/layout): avoid CSS injection attacks in media matcher
The media matcher needs to create a dummy stylesheet to work around some browser quirks. These changes ensure we don't accidentally inject malicious CSS into the page. (cherry picked from commit 231f94f)
1 parent d55b745 commit e8f3419

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/cdk/layout/media-matcher.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ function createEmptyStyleRule(query: string, nonce: string | undefined | null) {
7373
}
7474

7575
if (mediaQueryStyleNode.sheet) {
76-
mediaQueryStyleNode.sheet.insertRule(`@media ${query} {body{ }}`, 0);
76+
mediaQueryStyleNode.sheet.insertRule(
77+
// Drop the curly braces to avoid injection attacks. Curly braces aren't
78+
// valid media query syntax so this should be a no-op in valid cases.
79+
`@media ${query.replace(/[{}]/g, '')} {body{ }}`,
80+
0,
81+
);
7782
mediaQueriesForWebkitCompatibility.add(query);
7883
}
7984
} catch (e) {

0 commit comments

Comments
 (0)