diff --git a/extension/jest.config.cjs b/extension/jest.config.cjs index 6b16a30..8d044db 100644 --- a/extension/jest.config.cjs +++ b/extension/jest.config.cjs @@ -105,10 +105,10 @@ module.exports = { lines: 68, }, "./src/rules/": { - statements: 90, - branches: 77, - functions: 95, - lines: 90, + statements: 91, + branches: 78, + functions: 96, + lines: 91, }, }, }; diff --git a/extension/src/rules/__tests__/prompt-injection-redact.test.ts b/extension/src/rules/__tests__/prompt-injection-redact.test.ts index 0b12ac6..70382fe 100644 --- a/extension/src/rules/__tests__/prompt-injection-redact.test.ts +++ b/extension/src/rules/__tests__/prompt-injection-redact.test.ts @@ -176,4 +176,40 @@ describe("prompt-injection-redact", () => { expect(document.querySelector(`.${PLACEHOLDER_CLASS}`)).toBeNull(); expect(document.body.textContent).toContain(FIXTURES.IGNORE_ALL); }); + + describe("findContainer escalation", () => { + // When injection text lives directly in with no block-level + // ancestor, the rule must not redact — wrapping in a placeholder + // would black out the whole page. findContainer returns null on the + // BODY/HTML guard. + it("does not redact text that is a direct child of ", () => { + document.body.innerHTML = ""; + document.body.append(document.createTextNode(FIXTURES.IGNORE_ALL)); + + promptInjectionRedactRule.apply(document.body); + + expect(document.querySelector(`.${PLACEHOLDER_CLASS}`)).toBeNull(); + expect(document.body.textContent).toContain(FIXTURES.IGNORE_ALL); + }); + + // When the only ancestor is a non-block element (a bare
//etc. + // not in BLOCK_CONTAINER_SELECTOR), the rule falls back to that direct + // parent rather than escalating. Confirms the "return parent" branch. + it("wraps the direct parent when no block-level ancestor matches", () => { + document.body.innerHTML = `
${FIXTURES.IGNORE_ALL}
`; + + promptInjectionRedactRule.apply(document.body); + + // The is the direct parent of the text node and not in the + // block selector — it gets replaced rather than escalating to . + expect(document.querySelector("#leaf")).toBeNull(); + expect(document.querySelector("#wrap")).not.toBeNull(); + const placeholder = document.querySelector(`.${PLACEHOLDER_CLASS}`); + expect(placeholder).not.toBeNull(); + // Placeholder sits inside the wrapper, not in place of the wrapper. + expect( + document.querySelector("#wrap")?.contains(placeholder ?? null), + ).toBe(true); + }); + }); });