fix(brand): the importer strips to a fixed point and refuses non-path data

CodeQL (3 high, incomplete multi-character sanitization): one pass of the
comment/<style> stripping over '<!<!--x-->-- … -->' leaves a fresh '<!--'
behind, and what it hid was then read as markup. The path is narrow — a
crafted SVG given to brand:import — but a mark's path data is written
verbatim into public files (favicon.svg), so it is closed twice:

- stripping repeats until nothing changes;
- every shape's path data must be pure path syntax (commands, numbers,
  separators), or the import is refused with a message. A quote or angle
  bracket can never reach a file, whatever the input looked like.

Tested both: a hidden shape behind a nested comment is not imported (a
single-pass strip imports it), and markup smuggled through a single-quoted
d is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M6u11m24oVkG98wAVbN7nf
This commit is contained in:
Claude committed 2026-10-11 23:32:21 +00:00
1 parent 87fb8fe561
commit a3e2b25b59
2 files changed
+32 -6

No files matched your search

@@ -107,10 +107,16 @@ describe('svgToMark', () => {
['text', '<text>Hi</text>', /outlines/],
['a <use>', '<use href="#a"/>', /expand it/],
['nothing to draw', '<g></g>', /nothing to draw/],
['markup smuggled into path data', `<path d='M0 0"/><script>x</script>'/>`, /not path data/],
])('refuses %s with a way forward', (_label, body, message) => {
expect(() => svgToMark(svg(body))).toThrow(MarkImportError);
expect(() => svgToMark(svg(body))).toThrow(message);
});
it('strips nested comments completely rather than leaving a fresh one behind', () => {
const { shapes } = svgToMark(svg('<!<!--x-->-- <path d="M9 9h1"/> --><path d="M0 0h1"/>'));
expect(shapes).toEqual([{ paint: 'fill', d: 'M0 0h1' }]);
});
});
describe('the sources the importer writes', () => {
+26 -6
View File
@@ -188,6 +188,13 @@ function shapePath(tag: string, a: Style): string | null {
}
}
/**
* What SVG path data may contain: commands, numbers, separators. A mark's
* path is written verbatim into public files (favicon.svg, the share cards),
* so anything else — a quote, an angle bracket — is refused, never escaped.
*/
const PATH_DATA = /^[MmLlHhVvCcSsQqTtAaZz0-9eE.,+\-\s]+$/;
const isPaint = (value: string | undefined) =>
value !== undefined && value !== 'none' && value !== 'transparent';
@@ -217,12 +224,20 @@ export function svgToMark(svg: string): ImportResult {
.join('\n');
const classes = parseClassRules(css, warnings);
const cleaned = svg
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<\?[\s\S]*?\?>/g, '')
.replace(/<!DOCTYPE[^>]*>/gi, '')
.replace(/<(defs|title|desc|metadata|style)\b[\s\S]*?<\/\1\s*>/gi, '')
.replace(/<(defs|title|desc|metadata|style)\b[^>]*\/>/gi, '');
// Strip until nothing changes: one pass over `<!<!---->--` leaves a fresh
// `<!--` behind. Safety does not rest on this — every path is checked
// against PATH_DATA below before it can reach a file — but a half-stripped
// comment would otherwise be read as markup.
let cleaned = svg;
for (let previous = ''; previous !== cleaned;) {
previous = cleaned;
cleaned = cleaned
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<\?[\s\S]*?\?>/g, '')
.replace(/<!DOCTYPE[^>]*>/gi, '')
.replace(/<(defs|title|desc|metadata|style)\b[\s\S]*?<\/\1\s*>/gi, '')
.replace(/<(defs|title|desc|metadata|style)\b[^>]*\/>/gi, '');
}
const tagRe =
/<(\/?)([a-zA-Z][\w:.-]*)((?:\s+[^\s=>/]+(?:\s*=\s*(?:"[^"]*"|'[^']*'))?)*)\s*(\/?)>/g;
@@ -285,6 +300,11 @@ export function svgToMark(svg: string): ImportResult {
throw new MarkImportError(`found <${tag}> outside an <svg> element`);
}
const d = shapePath(tag, attrs);
if (d && !PATH_DATA.test(d)) {
throw new MarkImportError(
`<${tag}> has path data that is not path data (only commands, numbers and separators are allowed)`
);
}
if (d) {
const fill = style.fill ?? 'black';
const stroke = style.stroke;