Skip to content

Commit a28615d

Browse files
committed
fix(@angular/build): add CSP nonce attribute to script tags when inline critical CSS is disabled
Prior to this change when inline critical CSS is disabled CSP `nonce` was not added to script tags. Closes #28102 (cherry picked from commit 636cb69)
1 parent c0933f2 commit a28615d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
13+
describe('Behavior: "CSP Nonce"', () => {
14+
it('should add CSP nonce to scripts when optimization is disabled', async () => {
15+
await harness.modifyFile('src/index.html', (content) =>
16+
content.replace(/<app-root/g, '<app-root ngCspNonce="{% nonce %}" '),
17+
);
18+
19+
harness.useTarget('build', {
20+
...BASE_OPTIONS,
21+
polyfills: [],
22+
optimization: false,
23+
});
24+
25+
const { result } = await harness.executeOnce();
26+
expect(result?.success).toBeTrue();
27+
28+
const indexFileContent = harness.expectFile('dist/browser/index.html').content;
29+
indexFileContent.toContain(
30+
'<script src="main.js" type="module" nonce="{% nonce %}"></script>',
31+
);
32+
indexFileContent.toContain('<app-root ngcspnonce="{% nonce %}"');
33+
});
34+
});
35+
});

packages/angular/build/src/utils/index-file/index-html-generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export class IndexHtmlGenerator {
7878
this.csrPlugins.push(inlineCriticalCssPlugin(this));
7979
}
8080

81+
this.csrPlugins.push(addNoncePlugin());
82+
8183
// SSR plugins
8284
if (options.generateDedicatedSSRContent) {
8385
this.ssrPlugins.push(addEventDispatchContractPlugin(), addNoncePlugin());

0 commit comments

Comments
 (0)