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
5 changes: 3 additions & 2 deletions packages/angular/build/src/utils/index-file/inline-fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ export class InlineFontsProcessor {
attrs.find(({ name, value }) => name === 'href' && hrefsContent.has(value));
if (hrefAttr) {
const href = hrefAttr.value;
const cssContent = hrefsContent.get(href);
rewriter.emitRaw(`<style>${cssContent}</style>`);
const cssContent = hrefsContent.get(href) ?? '';
// Prevent the CSS from terminating the generated raw-text style element.
rewriter.emitRaw(`<style>${cssContent.replace(/<\/(?=style)/gi, '<\\/')}</style>`);
} else {
rewriter.emitStartTag(tag);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/angular/build/src/utils/index-file/inline-fonts_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.dev/license
*/

import { htmlRewritingStream } from './html-rewriting-stream';
import { InlineFontsProcessor } from './inline-fonts';
import { addNonce } from './nonce';

describe('InlineFontsProcessor', () => {
describe('Google fonts', () => {
Expand Down Expand Up @@ -100,6 +102,32 @@ describe('InlineFontsProcessor', () => {
`<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>`,
);
});

it('should escape closing style tags in inlined CSS', async () => {
const inlineFontsProcessor = new InlineFontsProcessor({
minify: true,
});
spyOn(inlineFontsProcessor, 'processURL').and.resolveTo(
'body { color: red; }</StYlE><script>globalThis.attack = true;</script><style>',
);

const html = await inlineFontsProcessor.process(`
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body><app-root ngCspNonce="trusted-nonce"></app-root></body>
</html>`);
const htmlWithNonce = await addNonce(html);
const { rewriter, transformedContent } = await htmlRewritingStream(htmlWithNonce);
const elementNames: string[] = [];
rewriter.on('startTag', ({ tagName }) => elementNames.push(tagName));
await transformedContent();

expect(htmlWithNonce).toContain('body { color: red; }<\\/StYlE>');
expect(elementNames.filter((name) => name === 'style')).toHaveSize(1);
expect(elementNames).not.toContain('script');
});
});

describe('Adobe Typekit fonts', () => {
Expand Down
Loading