prompt-guard-scanner 0.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. prompt_guard_scanner-0.4.2/.python-version +1 -0
  2. prompt_guard_scanner-0.4.2/CHANGELOG.md +34 -0
  3. prompt_guard_scanner-0.4.2/LICENSE +21 -0
  4. prompt_guard_scanner-0.4.2/PKG-INFO +465 -0
  5. prompt_guard_scanner-0.4.2/README.md +437 -0
  6. prompt_guard_scanner-0.4.2/SECURITY.md +23 -0
  7. prompt_guard_scanner-0.4.2/SKILL.md +122 -0
  8. prompt_guard_scanner-0.4.2/pyproject.toml +75 -0
  9. prompt_guard_scanner-0.4.2/pyproject.toml.orig +73 -0
  10. prompt_guard_scanner-0.4.2/scripts/evaluate.py +1033 -0
  11. prompt_guard_scanner-0.4.2/scripts/hook_pre_tool_use.py +11 -0
  12. prompt_guard_scanner-0.4.2/scripts/scan.py +11 -0
  13. prompt_guard_scanner-0.4.2/src/prompt_guard/__init__.py +1 -0
  14. prompt_guard_scanner-0.4.2/src/prompt_guard/__main__.py +5 -0
  15. prompt_guard_scanner-0.4.2/src/prompt_guard/cli.py +22 -0
  16. prompt_guard_scanner-0.4.2/src/prompt_guard/hook_pre_tool_use.py +592 -0
  17. prompt_guard_scanner-0.4.2/src/prompt_guard/limits.py +59 -0
  18. prompt_guard_scanner-0.4.2/src/prompt_guard/pdf_extract.py +773 -0
  19. prompt_guard_scanner-0.4.2/src/prompt_guard/sanitize.py +396 -0
  20. prompt_guard_scanner-0.4.2/src/prompt_guard/sarif.py +218 -0
  21. prompt_guard_scanner-0.4.2/src/prompt_guard/scan.py +2378 -0
  22. prompt_guard_scanner-0.4.2/tests/conftest.py +32 -0
  23. prompt_guard_scanner-0.4.2/tests/expected_other.py +67 -0
  24. prompt_guard_scanner-0.4.2/tests/expected_pdf.py +171 -0
  25. prompt_guard_scanner-0.4.2/tests/other_fixtures.py +218 -0
  26. prompt_guard_scanner-0.4.2/tests/pdf_fixtures.py +1024 -0
  27. prompt_guard_scanner-0.4.2/tests/pdfgen.py +137 -0
  28. prompt_guard_scanner-0.4.2/tests/schemas/README.md +7 -0
  29. prompt_guard_scanner-0.4.2/tests/schemas/sarif-schema-2.1.0.json +3389 -0
  30. prompt_guard_scanner-0.4.2/tests/test_action.py +166 -0
  31. prompt_guard_scanner-0.4.2/tests/test_cli_strict.py +97 -0
  32. prompt_guard_scanner-0.4.2/tests/test_entry_points.py +98 -0
  33. prompt_guard_scanner-0.4.2/tests/test_evaluation.py +118 -0
  34. prompt_guard_scanner-0.4.2/tests/test_false_positive_tightening.py +295 -0
  35. prompt_guard_scanner-0.4.2/tests/test_hook.py +188 -0
  36. prompt_guard_scanner-0.4.2/tests/test_html_toggle_panels.py +233 -0
  37. prompt_guard_scanner-0.4.2/tests/test_limits.py +322 -0
  38. prompt_guard_scanner-0.4.2/tests/test_naming.py +36 -0
  39. prompt_guard_scanner-0.4.2/tests/test_other_formats.py +48 -0
  40. prompt_guard_scanner-0.4.2/tests/test_pdf_scan.py +448 -0
  41. prompt_guard_scanner-0.4.2/tests/test_sarif.py +288 -0
  42. prompt_guard_scanner-0.4.2/uv.lock +697 -0
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.4.2 - 2026-12-06
6
+
7
+ - Added PyPI package metadata for distribution `prompt-guard-scanner` (installing the `prompt-guard` command) and a Trusted Publishing job; no behaviour change.
8
+
9
+ ## 0.4.1 - 2026-12-06
10
+
11
+ - Changed: repository moved to the account `lzffelix`; no behaviour change besides URLs in SARIF output.
12
+
13
+ ## 0.4.0 - 2026-12-06
14
+
15
+ - Add SARIF 2.1.0 output (`prompt-guard scan --format sarif`) mapping `HIGH` to `error`, `MEDIUM` to `warning`, and `SUSPECT`/`LOW` to `note`, with `security-severity` scores and offline OASIS schema validation in tests.
16
+ - Add `--exclude-dir` for recursive directory scans and a SHA-pinned composite GitHub Action (`action.yml`) with lockfile-based installation (`uv sync --frozen --no-dev`), default exclusions (`.git`, `.venv`, `node_modules`), optional SARIF upload, and a dogfooding workflow.
17
+ - Add a reproducible synthetic evaluation benchmark (`scripts/evaluate.py`, `docs/EVALUATION.md`) covering original and paraphrased payload sets plus quoted-injection and multilingual benign cases, guarded by two-sided baseline and doc-sync tests.
18
+ - Add project hygiene files (`CONTRIBUTING.md`, `CODE_OF_CONDUCT.md`, issue and PR templates, `CODEOWNERS`, `dependabot.yml`), `docs/` documentation index, a tag-triggered GitHub Release workflow (`.github/workflows/release.yml`), and README badges.
19
+
20
+ ## 0.3.0 - 2026-12-06
21
+
22
+ - Package the scanner and four-host hook as `prompt-guard scan` and `prompt-guard hook` in an installable wheel and source distribution.
23
+ - Keep the checkout scripts for existing invocations and move the agent skill to the standalone project.
24
+ - Exercise the console command, run the test suite on Linux and macOS in CI, and document standalone installation.
25
+
26
+ ## 0.2.0
27
+
28
+ From the [upstream v0.2.0 release notes](https://github.com/lzffelix/skills/releases/tag/v0.2.0).
29
+
30
+ - Reduce false positives for script-toggled UI panels, short hidden labels, `@media print` rules, and white text on dark backgrounds in HTML, and for shading, single-character runs, and long non-prose metadata in DOCX.
31
+ - Rate a boundary `U+FEFF` LOW. Rate up to 10 `U+200B` zero-width spaces MEDIUM when the text has no injection or instruction wording.
32
+ - Report real-document measurements in the [README](README.md#false-positives-on-real-documents). HIGH false positives dropped from 12 to 3 in the first HTML sample, from 3 to 1 in the out-of-sample HTML set of 60, and from 3 to 2 in the out-of-sample DOCX set.
33
+ - Harden resource limits and output and add [SECURITY.md](SECURITY.md).
34
+ - The scanner cannot read legacy binary `.doc` files.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Li Zhuofei Felix
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,465 @@
1
+ Metadata-Version: 2.4
2
+ Name: prompt-guard-scanner
3
+ Version: 0.4.2
4
+ Summary: Deterministic pre-ingestion scanner and tool-call hook for hidden text and prompt injection in PDF, Office, HTML, SVG, image, Markdown and text files.
5
+ Keywords: prompt-injection,security,llm,ai-agents,scanner,pdf,sarif,claude-code,mcp
6
+ Author: Felix Li
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Intended Audience :: Information Technology
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Topic :: Security
17
+ Classifier: Topic :: Software Development :: Quality Assurance
18
+ Requires-Dist: beautifulsoup4>=4.12.0
19
+ Requires-Dist: pdfminer-six>=20260107
20
+ Requires-Dist: pypdf>=5.0.0
21
+ Requires-Python: >=3.12
22
+ Project-URL: Homepage, https://github.com/lzffelix/prompt-guard
23
+ Project-URL: Documentation, https://github.com/lzffelix/prompt-guard/blob/main/docs/README.md
24
+ Project-URL: Source, https://github.com/lzffelix/prompt-guard
25
+ Project-URL: Issues, https://github.com/lzffelix/prompt-guard/issues
26
+ Project-URL: Changelog, https://github.com/lzffelix/prompt-guard/blob/main/CHANGELOG.md
27
+ Description-Content-Type: text/markdown
28
+
29
+ # prompt-guard
30
+
31
+ [![CI](https://github.com/lzffelix/prompt-guard/actions/workflows/ci.yml/badge.svg)](https://github.com/lzffelix/prompt-guard/actions/workflows/ci.yml)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/lzffelix/prompt-guard/blob/main/LICENSE)
33
+ [![Python >=3.12](https://img.shields.io/badge/Python-%3E%3D3.12-blue.svg)](https://github.com/lzffelix/prompt-guard/blob/main/pyproject.toml)
34
+
35
+ Prompt Guard scans files before an AI agent reads them. It reports hidden text, obfuscated text
36
+ and prompt-injection phrases in PDF, DOCX, PPTX, XLSX, HTML, SVG, image, Markdown and plain-text
37
+ files. It also provides a visible-text extraction mode and a tool-call hook for Claude Code,
38
+ Codex, Cursor and Antigravity.
39
+
40
+ Detection is deterministic and pattern based. It covers recognized text channels and patterns.
41
+ Image pixels, unknown phrasing and paths assembled dynamically by code are outside its coverage.
42
+
43
+ ## Install
44
+
45
+ Python 3.12 or newer is required. The PyPI distribution name is `prompt-guard-scanner`, and it installs the `prompt-guard` command (available on PyPI starting with `0.4.2`).
46
+
47
+ Run on demand from PyPI with `uvx`:
48
+
49
+ ```bash
50
+ uvx --from prompt-guard-scanner prompt-guard --help
51
+ uvx --from prompt-guard-scanner prompt-guard scan "document.pdf"
52
+ ```
53
+
54
+ Install from PyPI for repeated use or for a hook:
55
+
56
+ ```bash
57
+ uv tool install prompt-guard-scanner
58
+ # Or use pipx:
59
+ pipx install prompt-guard-scanner
60
+ ```
61
+
62
+ To install from GitHub or pin a specific Git release tag (for example `v0.4.2` or `v0.4.1`):
63
+
64
+ ```bash
65
+ uvx --from 'git+https://github.com/lzffelix/prompt-guard.git@v0.4.2' prompt-guard scan "document.pdf"
66
+ uv tool install 'git+https://github.com/lzffelix/prompt-guard.git@v0.4.2'
67
+ # Or use pipx:
68
+ pipx install 'git+https://github.com/lzffelix/prompt-guard.git@v0.4.2'
69
+ ```
70
+
71
+ Install the agent skill from the root `SKILL.md`:
72
+
73
+ ```bash
74
+ npx skills add lzffelix/prompt-guard --skill prompt-guard
75
+ ```
76
+
77
+ Run from a Git checkout with locked dependencies:
78
+
79
+ ```bash
80
+ git clone https://github.com/lzffelix/prompt-guard.git
81
+ cd prompt-guard
82
+ uv sync --managed-python --frozen
83
+ uv run --managed-python --frozen prompt-guard --help
84
+ ```
85
+
86
+ If the checkout already exists, run `uv run --managed-python --project "<checkout>" --frozen prompt-guard scan "document.pdf"`
87
+ from any directory. The legacy commands `scripts/scan.py` and `scripts/hook_pre_tool_use.py` still
88
+ work with `uv run --managed-python --project "<checkout>" --frozen python "<checkout>/scripts/<script>.py"`.
89
+
90
+ ## Scan files
91
+
92
+ ```bash
93
+ prompt-guard scan "document.pdf"
94
+ prompt-guard scan --recursive --exclude-dir .git --exclude-dir .venv "documents/"
95
+ prompt-guard scan --recursive --format sarif "documents/" > results.sarif
96
+ prompt-guard scan --sanitize "document.pdf"
97
+ ```
98
+
99
+ Use `-r` or `--recursive` to descend into directories (`--exclude-dir NAME` skips named
100
+ directories during recursive scans, repeatable), `--json` (or `--format json`) for structured
101
+ findings, and `--format sarif` for SARIF 2.1.0 output (mapping `HIGH` to `error`, `MEDIUM` to
102
+ `warning`, and `SUSPECT` and `LOW` to `note`, with unscannable files emitted as `scan_error` at
103
+ `error` level).
104
+ `--strict` exits with code 2 when a scan finds HIGH, MEDIUM or SUSPECT signals or when a selected
105
+ file is unscannable. Ordinary scans return 0 even when they report findings or file errors.
106
+ Missing paths emit a warning and are skipped. With `--sanitize`, the output is a visible-text view
107
+ with hidden content and injection-like lines removed. In this mode, `--strict` exits 2 on
108
+ extraction errors. Images have no sanitized text view.
109
+
110
+ ### Severities
111
+
112
+ | Level | Meaning |
113
+ | --- | --- |
114
+ | HIGH | Concealed text that carries an injection phrase or reads like prose: white, tiny, transparent, off-page or hidden-layer text, hidden elements, and comments, metadata, alt text, invisible characters or encoded payloads that contain an injection phrase. **A file that cannot be scanned also counts as HIGH.** |
115
+ | MEDIUM | A weaker concealment signal: a short hidden token, an embedded file, a hidden sheet or layer, or metadata (over 250 characters), image alt text (over 160) or HTML meta content (over 300) that is unusually long but has no injection phrase. |
116
+ | LOW | A comment of 20 or more characters (HTML, XML, SVG, Markdown, Word, PowerPoint) with no injection phrase, a script-toggled HTML UI panel or control, a single-character hidden Word run, a boundary byte-order mark (`U+FEFF`), or long non-prose OOXML metadata. Reported, but it does not trigger `--strict` or the hook. |
117
+ | SUSPECT | An injection-like sentence in text the reader can see. |
118
+
119
+ **Script-toggled UI panels and benign styling (HTML and DOCX).** A `hidden` HTML element with a UI panel role (`tabpanel`, `dialog`, `alertdialog`, `menu`, `tooltip`)—or a container role (`region`, `group`, `status`, `alert`, `navigation`, `complementary`) explicitly named by `aria-controls` or `data-target`, its `hidden`-only nested children, or a short `hidden` button/menuitem/status control—is reported as LOW instead of HIGH when only `hidden` hides it and the page has both visible controls and an inline `hidden`-toggling script. An element with `hidden`, `id`, and a UI role referenced in that inline script drops to MEDIUM, and short (`< 8` words) hidden HTML labels without injection or AI-instruction wording drop to MEDIUM. White text on a non-white background, `color: transparent` with `background-clip: text` on a gradient, and benign `@media print { display: none }` rules are treated as visible UI (the same dark-background exemption applies to DOCX `w:shd`, `w:highlight`, table headers, and shapes). Any injection phrase or instruction-like wording aimed at an AI stays HIGH in every container and hiding method. Markdown with raw HTML is not covered by the script-toggled rule.
120
+
121
+ ### Unscannable files count as HIGH
122
+
123
+ The scanner never reports a file it could not read as clean. A damaged or truncated PDF, a file
124
+ that needs a password, a corrupt archive or any other parse error raises an exception. On the
125
+ command line that prints `[ERROR] Failed to scan ...` (the exit code stays 0 unless you pass
126
+ `--strict`, which exits with `2`); with `--json` the file is listed under `unscannable`; in the hook the file becomes a HIGH
127
+ finding in the channel `scan_error`, and the hook asks or denies like for any other HIGH finding.
128
+
129
+ ## GitHub Action
130
+
131
+ The repository includes a composite GitHub Action at [`action.yml`](https://github.com/lzffelix/prompt-guard/blob/main/action.yml) that installs
132
+ `prompt-guard` from `uv.lock` into an isolated environment with `uv`, scans the selected paths,
133
+ and optionally writes and uploads a SARIF 2.1.0 report to GitHub Code Scanning. It is available on
134
+ `main` and ships with releases from `v0.4.0` on; `v0.3.0` does not contain `action.yml`.
135
+
136
+ Pin the action to a full commit SHA (and pin `actions/checkout` by SHA). Private repositories may
137
+ require GitHub Advanced Security to upload SARIF to Code Scanning:
138
+
139
+ ```yaml
140
+ permissions:
141
+ contents: read
142
+ security-events: write # required only when upload-sarif is "true"
143
+
144
+ jobs:
145
+ prompt-guard:
146
+ runs-on: ubuntu-latest
147
+ steps:
148
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
149
+ with:
150
+ persist-credentials: false
151
+
152
+ - uses: lzffelix/prompt-guard@v0.4.2 # pinning a full commit SHA is recommended
153
+ with:
154
+ paths: "docs/ fixtures/"
155
+ recursive: "true"
156
+ strict: "true"
157
+ format: sarif
158
+ sarif-file: prompt-guard.sarif
159
+ upload-sarif: "true"
160
+ ```
161
+
162
+ | Input | Default | Description |
163
+ | --- | --- | --- |
164
+ | `paths` | `.` | Space-separated file or directory paths to scan (passed after `--`). |
165
+ | `recursive` | `true` | Pass `--recursive` to descend into directories (`"true"` or `"false"`). |
166
+ | `exclude-dirs` | `.git,.venv,node_modules` | Comma- or space-separated directory names skipped during recursive scans (empty to scan all). |
167
+ | `strict` | `true` | Pass `--strict` to exit `2` on HIGH, MEDIUM, SUSPECT, or unscannable files (`"true"` or `"false"`). |
168
+ | `format` | `text` | Output format (`text`, `json`, or `sarif`). Forced to `sarif` when `upload-sarif` is `"true"`. |
169
+ | `sarif-file` | `prompt-guard.sarif` | Output path for the generated SARIF file. |
170
+ | `upload-sarif` | `false` | Upload the SARIF file via `github/codeql-action/upload-sarif` (pinned by SHA). Use lowercase `"true"` or `"false"`. |
171
+ | `sarif-category` | `prompt-guard` | Category passed to `upload-sarif`. |
172
+ | `python-version` | `3.12` | Managed Python version installed by `uv`. |
173
+
174
+ Outputs: `exit-code` (scan process exit code) and `sarif-file` (path written when SARIF is generated).
175
+ When `upload-sarif` is `"true"`, the action uploads the SARIF report before enforcing a non-zero
176
+ `--strict` exit code. See [`.github/workflows/dogfood.yml`](https://github.com/lzffelix/prompt-guard/blob/main/.github/workflows/dogfood.yml) for a
177
+ workflow that exercises the action against generated clean and intentional sample fixtures.
178
+
179
+ ## The hook
180
+
181
+ `prompt-guard hook` reads a JSON event from standard input, scans the files the event refers to
182
+ and prints a JSON decision on standard output. Install the command once before configuring hooks.
183
+ If the host does not inherit your shell's `PATH`, substitute the shell-quoted absolute path to
184
+ the installed `prompt-guard` executable in the examples.
185
+
186
+ | Host (`--client`) | Pre-tool event | Optional prompt event | Payload and decision |
187
+ | --- | --- | --- | --- |
188
+ | Claude Code (`claude`) | `PreToolUse` | `UserPromptSubmit` | Reads `tool_name`, `tool_input`, `cwd`, `permission_mode` and `prompt`. Returns `hookSpecificOutput` with a permission decision or additional context. |
189
+ | Codex (`codex`) | `PreToolUse` | `UserPromptSubmit` | Uses the same JSON payload and decision format as Claude Code. |
190
+ | Cursor (`cursor`) | `preToolUse` | `beforeSubmitPrompt` | Also reads `workspace_roots` and `attachments`. Returns `permission` and optional `user_message`, `agent_message` or `additional_context`. |
191
+ | Antigravity (`antigravity`) | `PreToolUse` | `PreInvocation` | Reads `toolCall.name`, `toolCall.args`, `cwd`, `conversationId` and `lastUserInput`. Returns `decision` or prompt `injectSteps`. |
192
+
193
+ The tests exercise the hook's JSON interface. The location and wiring of hook configuration
194
+ depend on the host version.
195
+
196
+ ### Claude Code
197
+
198
+ Merge the following entry into `~/.claude/settings.json` or the project's Claude Code settings.
199
+ For the optional prompt hook, use the same command shape under `UserPromptSubmit` and append
200
+ `--event UserPromptSubmit`.
201
+
202
+ ```json
203
+ {
204
+ "hooks": {
205
+ "PreToolUse": [
206
+ {
207
+ "hooks": [
208
+ { "type": "command", "command": "prompt-guard hook --client claude" }
209
+ ]
210
+ }
211
+ ]
212
+ }
213
+ }
214
+ ```
215
+
216
+ ### Codex
217
+
218
+ Merge the following into the Codex `hooks.json` file (for example `~/.codex/hooks.json`).
219
+ For the optional prompt hook, use the same command shape under `UserPromptSubmit` and append
220
+ `--event UserPromptSubmit`.
221
+
222
+ ```json
223
+ {
224
+ "hooks": {
225
+ "PreToolUse": [
226
+ {
227
+ "hooks": [
228
+ { "type": "command", "command": "prompt-guard hook --client codex" }
229
+ ]
230
+ }
231
+ ]
232
+ }
233
+ }
234
+ ```
235
+
236
+ ### Cursor
237
+
238
+ Merge the following into `~/.cursor/hooks.json` or the project's `.cursor/hooks.json`.
239
+ For the optional prompt hook, add the command under `beforeSubmitPrompt` and append
240
+ `--event beforeSubmitPrompt`.
241
+
242
+ ```json
243
+ {
244
+ "version": 1,
245
+ "hooks": {
246
+ "preToolUse": [
247
+ { "command": "prompt-guard hook --client cursor --event preToolUse" }
248
+ ]
249
+ }
250
+ }
251
+ ```
252
+
253
+ ### Antigravity
254
+
255
+ Register a command hook for `PreToolUse` through the hook configuration supported by your
256
+ Antigravity version. For the optional prompt event, register the second command for
257
+ `PreInvocation`.
258
+
259
+ ```bash
260
+ prompt-guard hook --client antigravity
261
+ prompt-guard hook --client antigravity --event PreInvocation
262
+ ```
263
+
264
+ ### What it blocks, asks about or lets through
265
+
266
+ Files it looks at: `.pdf .docx .pptx .xlsx .html .htm .xhtml .svg .png .jpg .jpeg .webp .gif .tiff .tif`
267
+ always, and `.md .markdown .txt .csv .xml .rtf .tex .eml` when the file exists and is not under a
268
+ trusted directory. `.json`, `.yaml` and `.yml` files are **not** scanned by the hook (the command-line
269
+ scanner does scan them). A checkout-based hook skips its own checkout. A wheel-installed hook
270
+ skips only its installed package directory. The hook also skips the recovery command it emits
271
+ when it runs on its own.
272
+
273
+ | Result of the scan | What the hook does |
274
+ | --- | --- |
275
+ | Nothing found | Allows the call. |
276
+ | MEDIUM or SUSPECT only | Allows the call. On Claude Code, Codex and Cursor the model also receives a note with channel names and line numbers, never file text. Antigravity has no such channel, so the result is only logged. |
277
+ | Any HIGH finding, including an unscannable file | `ask` when a person can answer, `deny` when nobody can. The `ask` text shows the revealed hidden content to that person. The `deny` text goes to the model and holds only counts, channel names and the `--sanitize` command. |
278
+
279
+ When it asks and when it denies:
280
+
281
+ - `PROMPT_GUARD_DECISION=ask` or `deny` forces the answer.
282
+ - Cursor always gets `deny`, because its `ask` was not honoured in testing (see the comment in the code).
283
+ - Claude Code and Codex get `deny` when the payload's `permission_mode` is `bypassPermissions` or `dontAsk`, otherwise `ask`.
284
+ - Antigravity gets `deny` when a parent process was started with `--dangerously-skip-permissions` (found by running `ps`), otherwise `ask`.
285
+
286
+ On the prompt event, files named in the user's message (for example `@path`) are scanned and a
287
+ warning is added for the model when a file has a HIGH or SUSPECT finding (a MEDIUM finding alone adds no
288
+ warning on this event). The user's message is never blocked.
289
+
290
+ The hook **fails open on its own errors**: if its input is not valid JSON or the hook itself
291
+ crashes, it prints an allow answer and logs `allow-after-error`. Only a file that was found and
292
+ could not be scanned is treated as HIGH.
293
+
294
+ ### Settings
295
+
296
+ | Environment variable | Default | Effect |
297
+ | --- | --- | --- |
298
+ | `PROMPT_GUARD_TRUSTED_DIRS` | empty (no trusted directory) | Directories, separated by `:` (`;` on Windows), whose text files (`.md`, `.txt`, ...) the hook does not scan, for example your own instruction folders that mention injection phrases on purpose. Binary document formats are always scanned. |
299
+ | `PROMPT_GUARD_DECISION` | unset | `ask` or `deny` forces the answer for HIGH findings. |
300
+ | `PROMPT_GUARD_LOG` | per host, below | Path of the log file. |
301
+ | `PROMPT_GUARD_CLAUDE_LOG`, `PROMPT_GUARD_CLAUDE_DECISION` | unset | Older names of the two settings above; still read. |
302
+
303
+ ### What it logs, and where
304
+
305
+ One JSON line per hook call, metadata only. Fields: timestamp, host, event, tool name,
306
+ permission mode, the names of the payload keys, the paths of the files it scanned, counts of
307
+ findings and the outcome (`allow-no-target`, `allow-clean`, `allow-note`, `ask`, `deny`,
308
+ `prompt-clean`, `prompt-context`, `prompt-already-warned`, `prompt-no-target` or
309
+ `allow-after-error` with the error type and message). It never writes file contents. File paths are
310
+ written, so the log can reveal which files you opened.
311
+
312
+ | Host | Default log file |
313
+ | --- | --- |
314
+ | Claude Code | `~/.claude/logs/prompt-guard-hook.log` |
315
+ | Codex | `~/.codex/logs/prompt-guard-hook.log` |
316
+ | Cursor | `~/.cursor/logs/prompt-guard-hook.log` |
317
+ | Antigravity | `~/.gemini/logs/prompt-guard-hook.log` |
318
+
319
+ The file is moved to `<name>.1` when it passes 5 MB. On Antigravity the hook also keeps
320
+ `prompt-guard-seen.json` next to the log (hashes of conversation id, file path and modification
321
+ time) so it warns once per conversation and file version.
322
+
323
+ ### No network calls
324
+
325
+ The scanner and hook run offline. The test suite replaces socket functions with functions
326
+ that fail and runs the scanner over the fixtures. The hook starts `ps` for Antigravity only.
327
+ Installation from PyPI or Git can contact PyPI and GitHub.
328
+
329
+ ## Resource limits
330
+
331
+ A limit is never a way to get a file reported as clean: a file that goes over one is **unscannable**
332
+ (`[ERROR]` on the command line, a HIGH `scan_error` in the hook). The limits are constants in
333
+ `src/prompt_guard/limits.py`.
334
+
335
+ | Limit | Value |
336
+ | --- | --- |
337
+ | Any file read into memory (all formats) | 100 MB |
338
+ | One member of a `.docx`, `.pptx` or `.xlsx` archive (declared uncompressed size, checked before reading) | 50 MB |
339
+ | All members of one archive added up | 200 MB |
340
+ | Members in one archive | 10,000 |
341
+ | One PNG `zTXt` or `iTXt` chunk after decompression | 1 MB; a larger chunk makes the file unscannable (it is not truncated, because the part that was cut off could hold the payload) |
342
+ | A gzip-wrapped PDF after unpacking | 256 MB |
343
+
344
+ The patterns that look for `<!-- ... -->`, `\iffalse ... \fi`, hidden HTML in Markdown, `[//]: # (...)`,
345
+ CSS rules and XMP packets run in linear time, so a file full of unclosed openers cannot stall the scan.
346
+ One case is still slow: a page with thousands of nested hidden elements makes the HTML and Markdown
347
+ checks take minutes, because each hidden element's text is collected separately.
348
+
349
+ ## PDF reading
350
+
351
+ PDF support uses two permissively licensed libraries instead of PyMuPDF (AGPL):
352
+
353
+ - **pdfminer.six** reads the page content: every character with its font size, colour, fill
354
+ transparency and position, filled shapes (to tell white text on a dark banner from hidden text),
355
+ form XObjects, and the appearance streams of annotations and form fields.
356
+ - **pypdf** reads the objects around it: Info and XMP metadata, outline titles, attachments,
357
+ optional-content layers, annotations and form field values.
358
+
359
+ The PDF reader in `src/prompt_guard/pdf_extract.py` combines those results. The checks live in
360
+ `src/prompt_guard/scan.py`.
361
+ The limitations of this approach are listed in [SKILL.md](https://github.com/lzffelix/prompt-guard/blob/main/SKILL.md).
362
+
363
+ ## Dependencies and licenses
364
+
365
+ Runtime (all permissive; no GPL, AGPL or LGPL package):
366
+
367
+ | Package | Version in `uv.lock` | License | Needed by |
368
+ | --- | --- | --- | --- |
369
+ | beautifulsoup4 | 4.15.0 | MIT | direct |
370
+ | soupsieve | 4.1.4 | MIT | beautifulsoup4 |
371
+ | typing-extensions | 4.16.0 | PSF-2.0 | beautifulsoup4 |
372
+ | pdfminer.six | 20260107 | MIT | direct |
373
+ | charset-normalizer | 3.5.3 | MIT | pdfminer.six |
374
+ | cryptography | 51.0.1 | Apache-2.0 OR BSD-3-Clause | pdfminer.six |
375
+ | cffi | 2.1.1 | MIT-0 | cryptography |
376
+ | pycparser | 3.12 | BSD-3-Clause | cffi |
377
+ | pypdf | 7.2.0 | BSD-3-Clause | direct |
378
+
379
+ Development only (the `dev` dependency group, never imported by the scanner): pytest (MIT) with
380
+ pluggy (MIT), iniconfig (MIT), packaging (Apache-2.0 OR BSD-2-Clause) and pygments (BSD-2-Clause);
381
+ reportlab (BSD) with pillow (MIT-CMU) and charset-normalizer (MIT); colorama (BSD-3-Clause, pulled in by
382
+ pytest on Windows only).
383
+
384
+ The optional third-party `snyk-agent-scan` (Apache-2.0) is not a dependency; see SKILL.md.
385
+
386
+ ## Tests
387
+
388
+ ```bash
389
+ uv sync --managed-python --frozen
390
+ uv run --managed-python --frozen pytest
391
+ ```
392
+
393
+ The suite builds its PDFs, Office files, HTML, SVG, Markdown and images on the fly. It checks each
394
+ PDF check on small files made to trip it and on clean files that must stay clean, every other
395
+ format, the sanitized view, unscannable files, the hook (answers per host, trusted directories,
396
+ logging) and that no network code is reached.
397
+
398
+ ## Limitations
399
+
400
+ The scanner extracts text and metadata from supported formats. It has no OCR, legacy Office
401
+ binary parser or general archive scanner. Pattern matching can miss unfamiliar wording and flag
402
+ legitimate content. The hook can extract literal paths and expand ordinary globs or environment
403
+ variables. It cannot resolve a path generated dynamically by a script. See the format-specific
404
+ limits in [SKILL.md](https://github.com/lzffelix/prompt-guard/blob/main/SKILL.md).
405
+
406
+ ## Documentation and contributing
407
+
408
+ - [Documentation index](https://github.com/lzffelix/prompt-guard/blob/main/docs/README.md)
409
+ - [Synthetic evaluation benchmark](https://github.com/lzffelix/prompt-guard/blob/main/docs/EVALUATION.md) (see also `scripts/evaluate.py`)
410
+ - [Releasing and PyPI Trusted Publishing notes](https://github.com/lzffelix/prompt-guard/blob/main/docs/RELEASING.md)
411
+ - [Contributing](https://github.com/lzffelix/prompt-guard/blob/main/CONTRIBUTING.md) and [Code of Conduct](https://github.com/lzffelix/prompt-guard/blob/main/CODE_OF_CONDUCT.md)
412
+
413
+ ## Security and license
414
+
415
+ Report vulnerabilities privately as described in [SECURITY.md](https://github.com/lzffelix/prompt-guard/blob/main/SECURITY.md).
416
+ The project is licensed under the [MIT License](https://github.com/lzffelix/prompt-guard/blob/main/LICENSE).
417
+
418
+ ## False positives on real documents
419
+
420
+ Measured on 2026-12-05 with scanner commit `82ac330`. Corpus: 446 documents from the maintainer's own course, study and downloaded files. Fixed seed 20261205; up to 60 files per format, one folder at a time round-robin, deduplicated by content hash. Left out: files over 100 MB, names that suggest private or financial content, agent files and this project's own test files. Candidates found / sampled: pdf 410/60, docx 262/60, pptx 0/0, xlsx 26/26, html 186/60, md 1012/60, txt 100/60, svg 244/60, png+jpg 862/60. Each file counts once, under its highest severity. Only counts were kept; no file names or text.
421
+
422
+ | Format | Files | Clean | LOW | SUSPECT | MEDIUM | HIGH | False positive among flagged |
423
+ | --- | --- | --- | --- | --- | --- | --- | --- |
424
+ | pdf | 60 | 57 | 0 | 0 | 3 | 0 | 3 of 3 |
425
+ | docx | 60 | 49 | 2 | 0 | 5 | 4 | 9 of 9 |
426
+ | xlsx | 26 | 26 | 0 | 0 | 0 | 0 | none flagged |
427
+ | html | 60 | 32 | 3 | 0 | 12 | 13 | 25 of 25 |
428
+ | md | 60 | 57 | 0 | 3 | 0 | 0 | 3 of 3 |
429
+ | txt | 60 | 60 | 0 | 0 | 0 | 0 | none flagged |
430
+ | svg | 60 | 32 | 27 | 0 | 1 | 0 | 1 of 1 |
431
+ | png+jpg | 60 | 54 | 0 | 0 | 5 | 1 | 3 of 6 (2 true positives, 1 unclear) |
432
+ | **All** | **446** | **367** | **32** | **3** | **26** | **18** | **44 of 47** |
433
+
434
+ 18 of 446 files (4.0%, Wilson 95% interval 2.6% to 6.3%) have a HIGH finding, so the hook would ask about them. All 18 were false positives (interval for 18 of 18: 82% to 100%); 7 of the 13 HTML files are versions of one page. With MEDIUM and SUSPECT, 47 files (10.5%, 8.0% to 13.7%) were flagged: 44 false positives, 2 true positives and 1 unclear. The 2 true positives are PNGs with a chat-persona prompt in their metadata (MEDIUM, no injection phrase). The unclear one is a JPEG with 24 opaque bytes after the image. No file could not be scanned. Per file, the scan took 0.11 s at the median and 1.35 s at most. This is the maintainer's own folders, not a benchmark.
435
+
436
+ Causes of the 44 false positives (one cause per file):
437
+
438
+ - 16 HTML files: elements of a web app or template that script or the page hides (menu item, map panel, popup, settings block, empty-state text, sidebar, collapsed "more" block, small control).
439
+ - 7 files: white or transparent text on a coloured banner, button, table header or gradient (html 4, docx 2, svg 1).
440
+ - 5 files: a stray zero-width space or byte-order mark (docx 4, and the header of an image's XMP packet 1).
441
+ - 4 files: long metadata that is not prose (Office custom properties 2, XMP 2).
442
+ - 3 HTML files: a `display: none` rule inside `@media print`.
443
+ - 3 Markdown files: documentation that names a credential folder.
444
+ - 3 PDF files: a page number outside the page box, a fully transparent emoji, a reader's own annotation.
445
+ - 3 single files: a bilingual page that hides one language, long alt text on screenshots, a one-character hidden Word run.
446
+
447
+ ### After tightening (2026-12-05)
448
+
449
+ Re-measured on HTML and DOCX (60 files each) after tightening, with `HIGH` false positives before vs after:
450
+
451
+ - **Same seed (`20261205`, optimistic — rules were designed on it):** rebuilt candidate pools (203 HTML, 282 DOCX) gave HTML `12 -> 3` HIGH of 60 (`3` remaining: saved web-app `display:none`/`opacity:0` blocks without visible toggle controls) and DOCX `1 -> 0` HIGH of 60 (`2 -> 0` MEDIUM). Rebuilt baseline counts differed slightly from the `82ac330` run above (13 and 4 HIGH).
452
+ - **Out of sample (seed `20261206`, excluding first sample):** HTML `3 -> 1` HIGH of 60 (1 remaining: saved web-app `display:none` blocks); DOCX `3 -> 3` HIGH of 60 (1 unscannable OLE2 `.doc`-format file + 2 files with 2–4 boundary `U+200B` characters). All HIGH and MEDIUM findings in both sets were triaged as false positives (0 true positives).
453
+
454
+ Changed rules (injection phrases and AI-instruction wording stay HIGH in every case):
455
+
456
+ - HTML script-toggled UI: added `aria-controls`/`data-target` container roles, nested `hidden` sub-elements, and short `hidden` buttons/status elements (LOW), plus script-referenced `hidden` UI regions (MEDIUM).
457
+ - HTML short UI labels: hidden HTML elements under 8 alpha words without instruction wording drop to MEDIUM.
458
+ - HTML `@media print`: print-only `display: none` rules are not treated as screen-hidden.
459
+ - HTML/DOCX white or gradient-clipped text: white text on non-white backgrounds/shading/table headers and `background-clip: text` gradients are not flagged as hidden.
460
+ - DOCX 1-char runs & non-prose metadata: contiguous hidden runs join per paragraph (1-char benign runs -> LOW); long non-prose OOXML properties -> LOW.
461
+ - Control characters: boundary `U+FEFF` BOM -> LOW; single boundary `U+200B` -> MEDIUM; word-splitting or BiDi controls stay HIGH.
462
+
463
+ On 2026-12-05, up to 10 `U+200B` characters per text unit, including inside words, became MEDIUM when the only other scanned control is an optional leading BOM and neither the original nor the control-stripped text matches injection or AI-instruction patterns. In reconstructed DOCX samples of 60 files each, seeds `20261205` and `20261206` both went from 3 to 2 HIGH and from 1 to 2 MEDIUM. Both changed files were triaged as false positives. Findings in the other sampled formats for seed `20261205` were unchanged.
464
+
465
+ Limits: the first sample is optimistic because rules were designed on it; detection recall is tested only on synthetic fixtures; the corpus is one person's documents; triage was done by one reviewer, an AI agent, reading findings locally.