Skip to content

Commit 73a34ed

Browse files
committed
fixup! fix(@angular/build): add CSP nonce to stylesheet and modulepreload link elements
1 parent bb10239 commit 73a34ed

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎packages/angular/build/src/utils/index-file/nonce.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const NONCE_ATTR_PATTERN = /ngCspNonce/i;
1717
const 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
*/
2324
export async function addNonce(html: string): Promise<string> {

‎packages/angular/build/src/utils/index-file/nonce_spec.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)