Skip to content

Commit 86000ad

Browse files
crisbetoangular-robot[bot]
authored andcommitted
fix(cdk/layout): resolve CSP errors
The `MediaMatcher` needs to insert a `style` tag in some cases. These changes use the new `CSP_NONCE` token to avoid these issues.
1 parent 26a4651 commit 86000ad

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/cdk/layout/media-matcher.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Injectable} from '@angular/core';
8+
import {Injectable, CSP_NONCE, Optional, Inject} from '@angular/core';
99
import {Platform} from '@angular/cdk/platform';
1010

1111
/** Global registry for all dynamically-created, injected media queries. */
@@ -20,7 +20,10 @@ export class MediaMatcher {
2020
/** The internal matchMedia method to return back a MediaQueryList like object. */
2121
private _matchMedia: (query: string) => MediaQueryList;
2222

23-
constructor(private _platform: Platform) {
23+
constructor(
24+
private _platform: Platform,
25+
@Optional() @Inject(CSP_NONCE) private _nonce?: string | null,
26+
) {
2427
this._matchMedia =
2528
this._platform.isBrowser && window.matchMedia
2629
? // matchMedia is bound to the window scope intentionally as it is an illegal invocation to
@@ -37,7 +40,7 @@ export class MediaMatcher {
3740
*/
3841
matchMedia(query: string): MediaQueryList {
3942
if (this._platform.WEBKIT || this._platform.BLINK) {
40-
createEmptyStyleRule(query);
43+
createEmptyStyleRule(query, this._nonce);
4144
}
4245
return this._matchMedia(query);
4346
}
@@ -52,14 +55,19 @@ export class MediaMatcher {
5255
* inside the `@media` match existing elements on the page. We work around it by having one rule
5356
* targeting the `body`. See https://github.com/angular/components/issues/23546.
5457
*/
55-
function createEmptyStyleRule(query: string) {
58+
function createEmptyStyleRule(query: string, nonce: string | undefined | null) {
5659
if (mediaQueriesForWebkitCompatibility.has(query)) {
5760
return;
5861
}
5962

6063
try {
6164
if (!mediaQueryStyleNode) {
6265
mediaQueryStyleNode = document.createElement('style');
66+
67+
if (nonce) {
68+
mediaQueryStyleNode.nonce = nonce;
69+
}
70+
6371
mediaQueryStyleNode.setAttribute('type', 'text/css');
6472
document.head!.appendChild(mediaQueryStyleNode);
6573
}

tools/public_api_guard/cdk/layout.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export class LayoutModule {
6060

6161
// @public
6262
export class MediaMatcher {
63-
constructor(_platform: Platform);
63+
constructor(_platform: Platform, _nonce?: string | null | undefined);
6464
matchMedia(query: string): MediaQueryList;
6565
// (undocumented)
66-
static ɵfac: i0.ɵɵFactoryDeclaration<MediaMatcher, never>;
66+
static ɵfac: i0.ɵɵFactoryDeclaration<MediaMatcher, [null, { optional: true; }]>;
6767
// (undocumented)
6868
static ɵprov: i0.ɵɵInjectableDeclaration<MediaMatcher>;
6969
}

0 commit comments

Comments
 (0)