Skip to content

[FEATURE] RAG Data Poisoning Linter (E7-F1) #20

Description

@KbWen

Problem Description

In RAG (Retrieval-Augmented Generation) applications, markdown documents, text snippets, and documentation pages are retrieved and injected into the LLM context. Attackers can poison these files with hidden prompt injections (e.g., using zero-width characters, white-colored text, HTML hidden elements, or indirect prompt commands) to hijack the agent when the document is retrieved.

Current gap: GhostCheck already detects prompt injection and zero-width characters, but only in agent rule files and prompt templates — not in general documentation/knowledge base files that feed RAG pipelines. This scanner fills that gap.

Scope Definition

Important: GhostCheck is a static scanner, not a runtime firewall. This scanner performs pre-ingestion document linting — scanning source documents before they enter a vector database. It does NOT connect to vector DBs, monitor runtime retrieval, or detect semantic/embedding-level manipulation.

In Scope (unique value — not duplicating existing scanners)

  • Hidden HTML content: style="display:none", style="color:white", visibility:hidden, font-size:0 — injection payloads hidden via CSS
  • HTML comment-embedded injection: <!-- ignore previous instructions --> payloads inside HTML comments
  • Zero-width / bidi characters in docs: Reuse pattern from agent_rules.py (\u200B-\u200F, \uFEFF, \u202A-\u202E) but apply to RAG-scoped files
  • Prompt injection phrases in knowledge base docs: Reuse patterns from prompt_template_scanner.py (suspicious_jailbreak_phrasing) but apply to RAG-scoped files
  • Markdown link hijacking: Detect suspicious markdown links that redirect to attacker-controlled URLs disguised as legitimate references

Out of Scope (already covered or infeasible)

  • General prompt injection detection → Already in prompt_template_scanner.py (for template files)
  • Zero-width chars in agent rules → Already in agent_rules.py (for rule files)
  • High-entropy / base64 detection → Already in entropy_scanner.py
  • Vector DB / runtime analysis → Out of static analysis scope
  • Embedding manipulation detection → Requires ML, not regex

RAG File Scope Detection

The scanner should identify RAG-relevant files using both heuristic detection and configurable paths:

Heuristic (default): Scan .md, .txt, .html, .csv, .json files found in directories matching:

  • docs/, knowledge_base/, knowledge-base/, kb/, data/, corpus/, rag/, context/, wiki/

Configurable override in ghostcheck.toml:

[rag]
paths = ["docs/", "my_custom_kb/", "data/sources/"]
extensions = [".md", ".txt", ".html", ".csv", ".json"]

Implementation Details

Files involved:

  • src/ghostcheck/checks/rag_poison_scanner.py (New) — inherits from BaseScannerPlugin
  • tests/test_rag_poison_scanner.py (New)

Pattern reuse strategy: Import shared patterns from existing checkers rather than duplicating:

# Example: reuse zero-width pattern from agent_rules concepts
ZERO_WIDTH_PATTERN = r'[\u200B-\u200F\uFEFF\u202A-\u202E]'

False positive mitigation: Integrate with context_auditor.py to suppress findings where injection phrases appear in educational/warning/negative contexts (e.g., "Attackers use phrases like 'ignore previous instructions'").

Acceptance Criteria

  • Implement RAGPoisonScanner inheriting from BaseScannerPlugin
  • Detect hidden HTML content (display:none, color:white, visibility:hidden, font-size:0)
  • Detect HTML comment-embedded injection payloads
  • Detect zero-width / bidi characters in RAG-scoped documents
  • Detect prompt injection phrases in non-template knowledge base documents
  • Integrate context_auditor.py for false positive suppression in educational docs
  • Support both heuristic directory detection and ghostcheck.toml configuration
  • Add 5+ unit tests covering: clean docs, poisoned docs, false-positive regression (educational content)
  • Seamless integration with ghostcheck scan
  • Update framework presets to include RAG document scanning when applicable

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions