Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
indexFileContent.toContain('app-shell works!');
indexFileContent.toContain('p{color:#000}');
indexFileContent.toContain(
`<link rel="stylesheet" href="styles.css" media="print" data-beasties-media="all">`,
`<link rel="stylesheet" href="styles.css" nonce="{% nonce %}" media="print" data-beasties-media="all">`,
);
indexFileContent.toContain('<style nonce="{% nonce %}">p{color:#000}');
indexFileContent.toContain('<style nonce="{% nonce %}" ng-app-id="ng">');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,31 @@ describe('inlineCriticalCss', () => {
expect(warnings.length).toBeGreaterThan(0);
expect(warnings[0]).toContain('missing.css');
});

it('should retain nonce on stylesheet links and noscript fallback', async () => {
const initialContent = `
<html>
<head>
<link href="styles.css" rel="stylesheet" nonce="test-nonce">
</head>
<body></body>
</html>`;

const { content, errors, warnings } = await inlineCriticalCss(
initialContent,
'/dist/',
undefined,
false,
readAsset,
);

expect(errors).toEqual([]);
expect(warnings).toEqual([]);
expect(content).toContain(
'<link href="styles.css" rel="stylesheet" nonce="test-nonce" media="print" data-beasties-media="all">',
);
expect(content).toContain(
'<noscript><link href="styles.css" rel="stylesheet" nonce="test-nonce"></noscript>',
);
});
});
30 changes: 25 additions & 5 deletions packages/angular/build/src/utils/index-file/nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { htmlRewritingStream } from './html-rewriting-stream';
*/
const NONCE_ATTR_PATTERN = /ngCspNonce/i;

const TARGET_LINK_RELS: ReadonlySet<string> = new Set(['stylesheet', 'modulepreload']);

/**
* Finds the `ngCspNonce` value and copies it to all inline `<style>` and `<script> `tags.
* @param html Markup that should be processed.
Expand All @@ -28,11 +30,29 @@ export async function addNonce(html: string): Promise<string> {
const { rewriter, transformedContent } = await htmlRewritingStream(html);

rewriter.on('startTag', (tag) => {
if (
(tag.tagName === 'style' || tag.tagName === 'script') &&
!tag.attrs.some((attr) => attr.name === 'nonce')
) {
tag.attrs.push({ name: 'nonce', value: nonce });
const { tagName, attrs } = tag;

if (tagName === 'style' || tagName === 'script' || tagName === 'link') {
let isTarget = tagName !== 'link';
let hasNonce = false;

for (const attr of attrs) {
if (attr.name === 'nonce') {
hasNonce = true;
break;
}

if (tagName === 'link' && attr.name === 'rel' && attr.value) {
const tokens = attr.value.trim().toLowerCase().split(/\s+/);
if (tokens.some((token) => TARGET_LINK_RELS.has(token))) {
isTarget = true;
Comment thread
alan-agius4 marked this conversation as resolved.
}
}
}

if (isTarget && !hasNonce) {
attrs.push({ name: 'nonce', value: nonce });
}
}

rewriter.emitStartTag(tag);
Expand Down
17 changes: 17 additions & 0 deletions packages/angular/build/src/utils/index-file/nonce_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ describe('addNonce', () => {
expect(result).toContain('<script src="./main.js" nonce="{% nonce %}"></script>');
expect(result).toContain(`<script nonce="{% nonce %}">console.log('bar');</script>`);
});

it('should add the nonce expression to stylesheet link tags', async () => {
const result = await addNonce(`
<html>
<head>
<link rel="stylesheet" href="styles.css">
<link rel="preload" href="font.woff2" as="font">
</head>
<body>
<app ngCspNonce="{% nonce %}"></app>
</body>
</html>
`);

expect(result).toContain('<link rel="stylesheet" href="styles.css" nonce="{% nonce %}">');
expect(result).toContain('<link rel="preload" href="font.woff2" as="font">');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('AppShell Builder', () => {
expect(content).toContain('<app-root ngcspnonce="{% nonce %}"');
expect(content).toContain('<script nonce="{% nonce %}">');
expect(content).toMatch(
/<link rel="stylesheet" href="styles\.[a-z0-9]+\.css" media="print" data-beasties-media="all">/,
/<link rel="stylesheet" href="styles\.[a-z0-9]+\.css" nonce="{% nonce %}" media="print" data-beasties-media="all">/,
);
});
});
Loading