File tree Expand file tree Collapse file tree
packages/angular/build/src/utils/index-file Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,7 +17,8 @@ const NONCE_ATTR_PATTERN = /ngCspNonce/i;
1717const TARGET_LINK_RELS : ReadonlySet < string > = new Set ( [ 'stylesheet' , 'modulepreload' ] ) ;
1818
1919/**
20- * Finds the `ngCspNonce` value and copies it to all inline `<style>` and `<script> `tags.
20+ * Finds the `ngCspNonce` value and copies it to all `<style>` and `<script>` tags,
21+ * as well as stylesheet and modulepreload `<link>` tags.
2122 * @param html Markup that should be processed.
2223 */
2324export async function addNonce ( html : string ) : Promise < string > {
Original file line number Diff line number Diff line change @@ -93,11 +93,12 @@ describe('addNonce', () => {
9393 expect ( result ) . toContain ( `<script nonce="{% nonce %}">console.log('bar');</script>` ) ;
9494 } ) ;
9595
96- it ( 'should add the nonce expression to stylesheet link tags' , async ( ) => {
96+ it ( 'should add the nonce expression to stylesheet and modulepreload link tags' , async ( ) => {
9797 const result = await addNonce ( `
9898 <html>
9999 <head>
100100 <link rel="stylesheet" href="styles.css">
101+ <link rel="modulepreload" href="main.js">
101102 <link rel="preload" href="font.woff2" as="font">
102103 </head>
103104 <body>
@@ -107,6 +108,24 @@ describe('addNonce', () => {
107108 ` ) ;
108109
109110 expect ( result ) . toContain ( '<link rel="stylesheet" href="styles.css" nonce="{% nonce %}">' ) ;
111+ expect ( result ) . toContain ( '<link rel="modulepreload" href="main.js" nonce="{% nonce %}">' ) ;
110112 expect ( result ) . toContain ( '<link rel="preload" href="font.woff2" as="font">' ) ;
111113 } ) ;
114+
115+ it ( 'should add the nonce expression when link rel has mixed case or multiple tokens' , async ( ) => {
116+ const result = await addNonce ( `
117+ <html>
118+ <head>
119+ <link rel="Stylesheet" href="styles.css">
120+ <link rel="MODULEPRELOAD" href="main.js">
121+ </head>
122+ <body>
123+ <app ngCspNonce="{% nonce %}"></app>
124+ </body>
125+ </html>
126+ ` ) ;
127+
128+ expect ( result ) . toContain ( '<link rel="Stylesheet" href="styles.css" nonce="{% nonce %}">' ) ;
129+ expect ( result ) . toContain ( '<link rel="MODULEPRELOAD" href="main.js" nonce="{% nonce %}">' ) ;
130+ } ) ;
112131} ) ;
You can’t perform that action at this time.
0 commit comments