Skip to content

Commit 7f34580

Browse files
vanceingallsclaude
andcommitted
fix(core): escape hfId in selector + warn on duplicate match (R1, T7 review)
Addresses review on #1272 (Miguel P3 + Rames): findTargetElement interpolated target.hfId raw into a [data-hf-id="..."] selector. Escape it (CSS attr-value injection guard) and warn when a hfId matches more than one element instead of silently patching an arbitrary one. Adds an injection-guard test. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 533c488 commit 7f34580

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎packages/core/src/studio-api/helpers/sourceMutation.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,22 @@ describe("T7 — data-hf-id targeting (spec for R1)", () => {
415415
expect(html).toMatch(/color:\s*blue/);
416416
});
417417

418+
it("does not break out of the selector on a crafted hfId (CSS injection guard)", () => {
419+
// A value with a quote/bracket must be escaped, not injected — it should
420+
// simply match nothing and leave the source untouched, never throw.
421+
const source = `<h1 class="safe">A</h1><h1 class="victim">B</h1>`;
422+
const evil = `x"] , [class="victim`;
423+
const run = () =>
424+
patchElementInHtml(source, { hfId: evil }, [
425+
{ type: "text-content", property: "textContent", value: "HACKED" },
426+
]);
427+
expect(run).not.toThrow();
428+
const { html, matched } = run();
429+
expect(matched).toBe(false);
430+
expect(html).toBe(source);
431+
expect(html).not.toContain("HACKED");
432+
});
433+
418434
// The Studio edit path targets by id/selector (it never sends hfId). Once a
419435
// persisted data-hf-id exists in source, those edits must NOT strip it — else
420436
// the stable handle is destroyed by the next edit. This is the preservation

‎packages/core/src/studio-api/helpers/sourceMutation.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,39 @@ function querySelectorAllWithTemplates(root: Document | Element, selector: strin
3232
return [];
3333
}
3434

35+
// Prevent CSS attribute-selector injection via a crafted hfId: escape
36+
// backslashes first, then double-quotes. Keeps a malformed/hostile value from
37+
// breaking out of the `[data-hf-id="…"]` selector once callers beyond the
38+
// internal mint contract (R2+ user flows) pass values here.
39+
function escapeCssAttrValue(value: string): string {
40+
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
41+
}
42+
43+
function findByHfId(document: Document, hfId: string): Element | null {
44+
try {
45+
const matches = querySelectorAllWithTemplates(
46+
document,
47+
`[data-hf-id="${escapeCssAttrValue(hfId)}"]`,
48+
);
49+
if (matches.length > 1) {
50+
// The mint contract guarantees uniqueness; a duplicate means upstream
51+
// id drift. Don't silently patch an arbitrary one — surface it.
52+
// eslint-disable-next-line no-console
53+
console.warn(
54+
`sourceMutation: data-hf-id "${hfId}" matched ${matches.length} elements; using the first. ids must be unique per document.`,
55+
);
56+
}
57+
return matches[0] ?? null;
58+
} catch {
59+
// Malformed selector despite escaping — let the caller fall back.
60+
return null;
61+
}
62+
}
63+
3564
function findTargetElement(document: Document, target: SourceMutationTarget): Element | null {
3665
if (target.hfId) {
37-
const matches = querySelectorAllWithTemplates(document, `[data-hf-id="${target.hfId}"]`);
38-
if (matches[0]) return matches[0];
66+
const el = findByHfId(document, target.hfId);
67+
if (el) return el;
3968
}
4069

4170
if (target.id) {

0 commit comments

Comments
 (0)