Detect whitespace padding used to hide prompt-injection instructions (P9)
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):
- 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.
- Horizontal: 80+ consecutive whitespace characters in a line, including leading indentation. MEDIUM, confidence 0.7.
- 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 - Please resolve the conflicts, minor issues and merge the PR.
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) andmainadds 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 sharedZERO_WIDTH_CHARSconstant andmain's new bidi-control pattern[U+202A–U+202E, U+2066–U+2069]. The shared constant expands to exactlymain'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_charso the common case skips the per-charunicodedata.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.
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.
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!