Skip to content

Commit 0e5478d

Browse files
astro-factory[bot]factory[bot]matthewp
authored
Normalize CRLF line endings before computing CSP hashes (#17998)
* fix(svg): collect nested `<style>` elements for CSP hashing (#17996) * fix(lint): use RegExp#exec() instead of String#match() in SVG style extraction test Fixes @typescript-eslint/prefer-regexp-exec and regexp/prefer-regexp-exec in packages/astro/test/units/svg/style-extraction.test.ts. * fix(svg): replace recursive style collection with iterative stack traversal Avoid recursion when walking the SVG node tree in collectStyleTexts; use an explicit stack that preserves document-order traversal. --------- Co-authored-by: factory[bot] <factory[bot]@users.noreply.github.com> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Matthew Phillips <[email protected]>
1 parent 24946f7 commit 0e5478d

3 files changed

Lines changed: 103 additions & 13 deletions

File tree

‎.changeset/shiny-steaks-enjoy.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes SVG `<style>` elements nested inside `<defs>` or other container elements not being hashed for CSP

‎packages/astro/src/assets/svg/utils.ts‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ import { dropAttributes } from '../runtime.js';
55
import type { ImageMetadata } from '../types.js';
66
import type { SvgOptimizer } from './types.js';
77

8+
/**
9+
* Walk the SVG node tree and return the text content of every `<style>`
10+
* element found, regardless of nesting depth.
11+
*/
12+
function collectStyleTexts(nodes: any[]): string[] {
13+
const styles: string[] = [];
14+
const stack = [...nodes].reverse();
15+
while (stack.length > 0) {
16+
const node = stack.pop()!;
17+
if (node.type === ELEMENT_NODE && node.name === 'style') {
18+
const textContent = node.children
19+
?.filter((c: { type: number }) => c.type === TEXT_NODE)
20+
.map((c: { value: string }) => c.value)
21+
.join('');
22+
if (textContent) {
23+
styles.push(textContent);
24+
}
25+
} else if (node.children) {
26+
// Push children in reverse so they are processed in document order.
27+
for (let i = node.children.length - 1; i >= 0; i--) {
28+
stack.push(node.children[i]);
29+
}
30+
}
31+
}
32+
return styles;
33+
}
34+
835
async function parseSvg({
936
path,
1037
contents,
@@ -38,19 +65,10 @@ async function parseSvg({
3865
const { attributes, children } = svgNode;
3966
const body = renderSync({ ...root, children });
4067

41-
// Collect text content of <style> elements for head propagation and CSP hashing
42-
const styles: string[] = [];
43-
for (const child of children) {
44-
if (child.type === ELEMENT_NODE && child.name === 'style') {
45-
const textContent = child.children
46-
?.filter((c: { type: number }) => c.type === TEXT_NODE)
47-
.map((c: { value: string }) => c.value)
48-
.join('');
49-
if (textContent) {
50-
styles.push(textContent);
51-
}
52-
}
53-
}
68+
// Collect text content of all <style> elements for head propagation and CSP
69+
// hashing. The search covers the whole tree so that styles nested inside
70+
// <defs> or other container elements are included (#17996).
71+
const styles = collectStyleTexts(children);
5472

5573
return { attributes, body, styles };
5674
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
import { makeSvgComponent } from '../../../dist/assets/svg/utils.js';
4+
5+
/** Minimal ImageMetadata stub with only the fields makeSvgComponent reads. */
6+
function meta(fsPath: string) {
7+
return { src: '/test.svg', width: 24, height: 24, format: 'svg', fsPath } as any;
8+
}
9+
10+
/** Extract the `styles` array from the generated module source. */
11+
function extractStyles(moduleSource: string): string[] {
12+
// The module exports `createSvgComponent({...props...})`.
13+
// We eval just the JSON argument to read the `styles` property.
14+
const match = /createSvgComponent\((.+)\)$/s.exec(moduleSource);
15+
assert.ok(match, 'expected createSvgComponent call in module source');
16+
const props = JSON.parse(match[1]);
17+
return props.styles;
18+
}
19+
20+
describe('SVG style extraction', () => {
21+
it('collects <style> as a direct child of <svg>', async () => {
22+
const svg = '<svg viewBox="0 0 24 24"><style>.a{fill:red}</style><rect/></svg>';
23+
const source = await makeSvgComponent(meta('/test.svg'), svg, undefined);
24+
const styles = extractStyles(source);
25+
assert.equal(styles.length, 1);
26+
assert.equal(styles[0], '.a{fill:red}');
27+
});
28+
29+
it('collects <style> nested inside <defs>', async () => {
30+
const svg = '<svg viewBox="0 0 24 24"><defs><style>.a{fill:red}</style></defs><rect/></svg>';
31+
const source = await makeSvgComponent(meta('/test.svg'), svg, undefined);
32+
const styles = extractStyles(source);
33+
assert.equal(styles.length, 1);
34+
assert.equal(styles[0], '.a{fill:red}');
35+
});
36+
37+
it('collects <style> nested multiple levels deep', async () => {
38+
const svg =
39+
'<svg viewBox="0 0 24 24"><defs><g><style>.a{fill:red}</style></g></defs><rect/></svg>';
40+
const source = await makeSvgComponent(meta('/test.svg'), svg, undefined);
41+
const styles = extractStyles(source);
42+
assert.equal(styles.length, 1);
43+
assert.equal(styles[0], '.a{fill:red}');
44+
});
45+
46+
it('collects multiple <style> elements at different nesting levels', async () => {
47+
const svg = [
48+
'<svg viewBox="0 0 24 24">',
49+
'<style>.a{fill:red}</style>',
50+
'<defs><style>.b{fill:blue}</style></defs>',
51+
'<rect/>',
52+
'</svg>',
53+
].join('');
54+
const source = await makeSvgComponent(meta('/test.svg'), svg, undefined);
55+
const styles = extractStyles(source);
56+
assert.equal(styles.length, 2);
57+
assert.equal(styles[0], '.a{fill:red}');
58+
assert.equal(styles[1], '.b{fill:blue}');
59+
});
60+
61+
it('returns empty styles array when no <style> elements exist', async () => {
62+
const svg = '<svg viewBox="0 0 24 24"><rect/></svg>';
63+
const source = await makeSvgComponent(meta('/test.svg'), svg, undefined);
64+
const styles = extractStyles(source);
65+
assert.equal(styles.length, 0);
66+
});
67+
});

0 commit comments

Comments
 (0)