Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
27 changes: 27 additions & 0 deletions extension/src/rules/__tests__/prompt-injection-redact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ describe("prompt-injection-redact", () => {
expect(document.querySelector(`.${PLACEHOLDER_CLASS}`)).not.toBeNull();
});

it("does not redact the surrounding article when injection text lives inside an SVG", () => {
// svg-text-strip is the rule that scrubs SVG title/desc/text content.
// prompt-injection-redact must not fire on those text nodes — the SVG
// has no paragraph-like ancestor, so it would otherwise escalate the
// placeholder all the way up to the wrapping <article> and wipe out
// the whole section (#133).
document.body.innerHTML = `
<article>
<h1>Product title that stays visible.</h1>
<div>
<svg>
<title>${FIXTURES.IGNORE_HACKED}</title>
<desc>${FIXTURES.OVERRIDE_GUARDRAILS}</desc>
<text>${FIXTURES.DISREGARD}</text>
</svg>
</div>
</article>
`;
promptInjectionRedactRule.apply(document.body);

expect(document.querySelector("article")).not.toBeNull();
expect(document.querySelector("h1")?.textContent).toBe(
"Product title that stays visible.",
);
expect(document.querySelector(`.${PLACEHOLDER_CLASS}`)).toBeNull();
});

it("does not process text inside SCRIPT or STYLE", () => {
document.body.innerHTML = `
<script>${FIXTURES.SCRIPT_STRING}</script>
Expand Down
11 changes: 10 additions & 1 deletion extension/src/rules/prompt-injection-redact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ function findContainer(textNode: Text): HTMLElement | null {

function apply(root: ParentNode): void {
const containers = new Set<HTMLElement>();
for (const node of walkTextNodes(root, { minLength: MIN_TEXT_LENGTH })) {
for (const node of walkTextNodes(root, {
minLength: MIN_TEXT_LENGTH,
// SVG <title>/<desc>/<text> are the injection carriers inside SVG, and
// `svg-text-strip` handles them by blanking the text in place. Letting
// prompt-injection-redact also fire on those text nodes is destructive:
// the SVG has no p/li/td ancestor, so `findContainer` escalates all the
// way up to the surrounding <article>/<section> and the entire product
// header gets replaced with a single placeholder (#133).
shouldSkipParent: (parent) => parent.closest("svg") !== null,
})) {
if (!containsInjection(node.nodeValue ?? "")) {
continue;
}
Expand Down
Loading