SkillSpector icon indicating copy to clipboard operation
SkillSpector copied to clipboard

Detect whitespace padding used to hide prompt-injection instructions (P9)

Open korjavin opened this issue 5 months ago • 3 comments

Adds rule P9 "Whitespace Padding" under Prompt Injection, for issue #20. It detects padding that pushes injected instructions out of a reviewer's view while the agent still reads them.

P6 through P8 were taken by System Prompt Leakage, so this uses P9. One id covers all three signals; confidence carries the weighting.

Signals (all reported as P9):

  1. Vertical: 20+ consecutive blank or whitespace-only lines. MEDIUM, raised to HIGH when content follows a gap of 40+ lines. Confidence 0.8 with trailing content, 0.6 without.
  2. Horizontal: 80+ consecutive whitespace characters in a line, including leading indentation. MEDIUM, confidence 0.7.
  3. Ratio: a contiguous whitespace block over 2 KB, or whitespace over 90% of a file larger than 4 KB. LOW, confidence 0.4.

Whitespace is classified by Unicode category rather than ASCII space/tab: controls (\t \n \r \v \f), categories Zs/Zl/Zp (U+00A0, U+2028, U+2029, U+3000, and so on), and the zero-width family (U+200B/C/D, U+2060, U+FEFF). That zero-width set is now one shared constant (ZERO_WIDTH_CHARS) used by P2's regex and the mcp_tool_poisoning zero-width check, so the two cannot drift; the MCP check also picks up U+2060/U+FEFF.

Each finding points at the line where padding starts and includes a visible snippet of what was hidden (for example U+00A0 x82 or \n x80).

False-positive guards: markdown fenced code is skipped for the horizontal signal; vendored files are skipped (*.min.js, *.min.css, *.lock, package-lock.json, yarn.lock, *.svg, *.map); binary-ish content (containing U+FFFD) bails out; the ratio signal stays at LOW. Eval-dataset prose and files over 1 MB are already skipped upstream.

MCP manifest description fields are covered by the same detector (horizontal and block signals; the per-file ratio signal is skipped since fields are short).

Tests cover all three signals at their thresholds, the full Unicode evasion set, the false-positive guards, and the MCP path. Thresholds are named constants, easy to tune against a real corpus. Happy to adjust the signals or thresholds before merge.

korjavin avatar Jun 11 '26 14:06 korjavin

@korjavin - Please resolve the conflicts, minor issues and merge the PR.

rng1995 avatar Jun 22 '26 19:06 rng1995

Rebased/merged main and addressed the review feedback. Pushed as a merge commit + a follow-up fix commit.

Conflicts resolved

  • README.md — pattern/category counts. Base was 64/16; this branch adds P9 (+1 pattern) and main adds anti-refusal (+1 category, +4 patterns), so the merged totals are 69 patterns / 17 categories (verified against the per-category counts in the detail table).
  • static_patterns_prompt_injection.py (P2) — kept this branch's zero-width class built from the shared ZERO_WIDTH_CHARS constant and main's new bidi-control pattern [U+202A–U+202E, U+2066–U+2069]. The shared constant expands to exactly main's [U+200B U+200C U+200D U+2060 U+FEFF], so no code points are lost.

Review feedback addressed

  • U+FFFD bailout (the important one) is no longer an evasion vector. P9 now bails only when U+FFFD density exceeds a threshold (0.30) rather than when a single U+FFFD appears anywhere — a lone replacement char can no longer disable P9 for the whole file. Genuine binary blobs (mostly U+FFFD) still bail. Added the regression test you suggested ("embedded U+FFFD must not suppress an otherwise-detected pad") plus a high-density bail test.
  • Completeness: U+0085 (NEL) and U+180E are now treated as padding chars, so horizontal/block runs built from them are caught. Added unit + end-to-end coverage.
  • Micro-perf: added an ASCII fast-path in is_padding_char so the common case skips the per-char unicodedata.category() call.

Left as-is: the "a region can emit both a vertical and per-line horizontal finding" note — it's minor noise and not incorrect, and reworking the dedup risks regressions for no correctness gain. Happy to follow up if you'd prefer it suppressed.

All P9/static-pattern/MCP tests pass locally (149 passed). The CI run shows action_required — it needs a maintainer to approve the workflow run for this fork PR.

korjavin avatar Jun 24 '26 19:06 korjavin

Rebased onto current main — merge conflicts resolved (P2 Unicode tag block from main + P9 whitespace padding from this branch now coexist). CI linter fix applied.

korjavin avatar Jun 30 '26 10:06 korjavin

Hi! I've resolved the merge conflict by merging the latest main into this branch. Since the previous review has passed, could we go ahead and merge this? Thanks!

korjavin avatar Jul 15 '26 19:07 korjavin