A content-safety scanning layer for OKF (Open Knowledge Format) generation pipelines. okf-guard acts as a trusted middleware that inspects raw source documents (like PDFs, Word docs, and spreadsheets) for hidden content and prompt-injection attacks before that content is bundled into a trusted knowledge base for AI consumption.
It prevents attackers from smuggling malicious instructions into the context windows of GenAI applications by exploiting features like white-on-white text, invisible rendering modes, or hidden spreadsheet rows.
Depending on how you plan to use okf-guard, there are two recommended ways to install it to avoid common Python permission errors (like WinError 2 or Permission denied).
If you just want to run the okfguard command in your terminal to scan files, the safest and best way to install it globally is using pipx. This automatically isolates the heavy dependencies (like PDF and Word parsers) so they don't break your system Python.
Use these commands one by one (for Windows PowerShell):
- Install pipx
python -m pip install --user pipx
- Add pipx to your PATH
Note: Close PowerShell and open a new PowerShell window after this command.
python -m pipx ensurepath - Install OKF-Guard with all document parsers
This installs OKF-Guard in its own isolated environment.
pipx install "okf-guard[all]" - Check that it installed correctly
If you see the OKF-Guard help/usage information, you're done! ✅
okfguard --help
(If pipx is not recognized after restarting, try running python -m pipx install "okf-guard[all]" instead).
Important
For Option 1, you do NOT need to activate a venv. pipx creates and manages its own isolated environment.
If you are writing Python code and want to import the sanitize() function into your own AI or RAG pipeline, you should install it inside your project's virtual environment.
Follow these commands in order (for Windows PowerShell):
- Go to your project folder
cd "C:\path\to\your\project" - Create a virtual environment (if you don't already have one)
python -m venv venv - Activate the virtual environment
You should now see something like
.\venv\Scripts\Activate.ps1
(venv) PS C:\... - Upgrade pip
python -m pip install --upgrade pip
- Install OKF-Guard with all document parsers
(This is the recommended command if you want support for PDF, Word, Excel, PowerPoint, HTML, etc.)
pip install "okf-guard[all]" - Verify the installation
pip show okf-guard - Test that Python can import it
If that prints
python -c "import okf_guard; print('OKF-Guard installed successfully')"
OKF-Guard installed successfully, you're ready! ✅
Important
Unlike Option 1, Option 2 does require your virtual environment to be activated before installing the package.
The simplest way to use okf-guard is the single top-level sanitize() function, which automatically infers the format from the file extension, runs the detection pipeline, and generates OKF v0.2-compliant provenance fields.
from okfguard import sanitize
# Automatically detects format, extracts text, flags issues, and makes a decision
result = sanitize("suspicious_document.pdf")
print(f"Action: {result.action}") # "pass", "quarantine", or "block"
print(f"Risk Score: {result.risk_score}") # e.g., 0.85
print(f"Clean Text: {result.clean_text}") # Only the text visible to a human
# Inspect the flags that contributed to the score
for flag in result.flags:
print(f"[{flag.type}] at {flag.location}: {flag.snippet} (Confidence: {flag.confidence})")
# Extract OKF v0.2 frontmatter fields to stamp your output bundle
print(result.provenance_fields)If you need finer control over the pipeline, you can run the components manually:
from okfguard.adapters.pdf import PDFAdapter
from okfguard.core.detector import detect
from okfguard.core.decision import calculate_action
from okfguard.core.provenance import generate_provenance
from okfguard.core.models import Config
adapter = PDFAdapter()
extracted = adapter.extract("suspicious_document.pdf")
flags = detect(extracted)
config = Config(threshold_quarantine=0.3, threshold_block=0.7)
risk_score, action = calculate_action(flags, config)
provenance = generate_provenance(extracted, flags, risk_score, action, config)okf-guard provides a built-in CLI for scanning files and directories manually or in CI/CD pipelines.
# Scan a single file
$ okfguard scan document.docx
--- document.docx ---
Action: BLOCK
Risk Score: 0.900
Flags (2):
- [hidden_text] paragraph 4, run 2 — font.hidden
Confidence: 0.90
Snippet: 'Ignore all previous instructions...'
- [injection_pattern] hidden span, offset 0-34 [instruction_override]
Confidence: 0.85
Snippet: 'Ignore all previous instructions...'Use --json for machine-readable newline-delimited JSON output, which is perfect for streaming logs.
# Scan a whole directory recursively, outputting JSON
$ okfguard scan -r /docs/uploads --json > scan_log.jsonUse okfguard review to interactively triage quarantined files from a JSON log:
$ okfguard review --json-log scan_log.jsonExit Codes:
0: Pass1: Quarantine2: Block3: Error (file not found, missing dependency, or parsing crash)
okf-guard is built around three core principles:
- Screen Before Write: Malicious content must be stopped before it enters the knowledge base. Once an injection is embedded in a trusted RAG database, it's virtually impossible to cleanly remove.
- Hidden Content + Pattern Matching: We don't just rely on regexes. We look for the mechanisms of smuggling (e.g., zero-width characters, white-on-white text, off-canvas shapes, hidden spreadsheet rows). If content is hidden from a human but visible to a parser, it is inherently suspicious.
- Conservative Defaults: v0.1.0 has no LLM-based secondary review layer, so the default thresholds (0.4 for quarantine, 0.92 for block) are tuned to prefer false positives over false negatives. A single explicit hidden-content signal alone is quarantined for human review rather than auto-blocked; only corroborated signals (hidden content plus a pattern match, or hidden content in multiple places) escalate to a block. It is safer to quarantine an ambiguously formatted document for human review than to let an attack slip through.
- Images & OCR: We do not perform OCR on images or scan for steganographic payloads hidden in image pixels.
- Macros & Executables: We extract and scan content. We do not perform static or dynamic analysis of VBA macros, embedded JavaScript, or active content.
- LLM-based Judgment: All detection in v0.1.0 is deterministic and rule-based. We do not use an LLM to evaluate the "intent" of a prompt injection.
- Reliable PDF Rendermode 3 Extraction: While the PDF format formally defines text rendering mode 3 as "invisible", the underlying
pdfminer.sixandpdfplumberstack currently used does not reliably expose this rendering mode at the character level across all PDF producers. The check exists in the codebase in case it is encountered, but our v0.1.0 test suite could not reliably produce a verifiable mode 3 test fixture. As a result, PDF hidden-text detection practically relies on the (fully-tested) color-matching and off-page position checks. - File System Management: We do not quarantine, delete, or move files on disk. We return a decision; your pipeline must enforce it.
We welcome issues and pull requests! The landscape of prompt-injection attacks evolves rapidly.
We specifically invite contributions to our injection pattern bank (src/okfguard/rules/injection_patterns.py). If you discover new injection phrasing in the wild, please open a PR to add it to the detection engine.
This project is licensed under the Apache 2.0 License.