Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .agentcortex/context/archive/INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Index of all archived work logs, categorized by module, pattern, and key decisio
- `src/ghostcheck/checks/ai_marker.py` → `feat-older-issues-bundle.md` (Implemented AI-Generated Code Marker plugin)
- `src/ghostcheck/checks/` → `fix-bug-bundle.md` (Resolved outstanding bugs in diff scanner, severity engine, mcp auditor, entropy scanner, and hallucination checker)
- `src/ghostcheck/checks/data_exfiltration_detector.py` → `feat-data-exfiltration.md` (AI Data Exfiltration Detector checking LLM prompt, MCP tool leakage, and public writes)
- `src/ghostcheck/checks/context_inflation_detector.py` → `feat-context-inflation-20260701.md` (Context Inflation / Prompt Flooding Detector scanner plugin)
- `src/ghostcheck/presets/manager.py` → `feat-context-inflation-20260701.md` (Integrated context_inflation into Next.js, Flutter, Django, FastAPI, Terraform presets)

## By Pattern

Expand All @@ -32,6 +34,8 @@ Index of all archived work logs, categorized by module, pattern, and key decisio
- `[data-exfiltration]` → `feat-data-exfiltration.md`
- `[shannon-entropy-refinement]` → `feat-data-exfiltration.md`
- `[ts-syntax-fallback]` → `feat-data-exfiltration.md`
- `[context-inflation]` → `feat-context-inflation-20260701.md`
- `[n-gram-performance]` → `feat-context-inflation-20260701.md`

## By Decision

Expand All @@ -50,4 +54,6 @@ Index of all archived work logs, categorized by module, pattern, and key decisio
- `[dynamic-test-key-generation]` → Dynamically construct mock API keys at test runtime to prevent triggering GitHub Advanced Security Secret Scanning alerts (`feat-older-issues-bundle.md`)
- `[shannon-entropy-key-token-filter]` → Run Shannon entropy checking only on regex-filtered key token matches to prevent false positives on CJK natural languages (`feat-data-exfiltration.md`)
- `[typescript-syntax-fallback-scanning]` → Gracefully fallback to text-based scanning on typescript AST parsing failures (`feat-data-exfiltration.md`)
- `[ngram-repetition-optimized-comparison]` → Use index-based sliding comparisons for n-gram checks instead of full list comprehension tuple allocations to ensure O(1) memory complexity (`feat-context-inflation-20260701.md`)
- `[zw-unicode-isolates-expansion]` → Include bidirectional isolates (\u2066–\u2069), word joiners, and Mongolian vowel separators to prevent Trojan Source-style prompt injection bypasses (`feat-context-inflation-20260701.md`)

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Work Log: feat-context-inflation

- Branch: main
- Classification: feature
- Classified by: Antigravity
- Frozen: true
- Created Date: 2026-07-01
- Owner: wen
- Guardrails Mode: Full
- Recommended Skills: test-driven-development (Drive implementation with tests), production-readiness (Ensure scanner logs and handles errors robustly)

## Session Info
- Agent: Gemini 3.5 Flash (High)
- Session: 2026-07-01T19:42:00+08:00
- Platform: Antigravity

## Drift Log
- Skip Attempt: NO
- Gate Fail Reason: N/A
- Token Leak: NO

## Risks
- False Positives: Standard markdown files or formatting dividers (like `---` or long lines of stars) might be flagged as padding token spam. (Mitigation: Exclude programming-language and structured file extensions from divider spam checks).
- Performance: Scanning large text files for regex/repetition could block. (Mitigation: Optimized repetition algorithm to perform rolling index checks with zero list-slicing or tuple creation overhead, keeping memory complexity at O(1)).

## Decisions
- [Approved Spec] Implemented Context Inflation / Prompt Flooding Detector according to [context-inflation-detector.md](file:///c:/Users/wen/.gemini/antigravity/scratch/security-tools/docs/specs/context-inflation-detector.md).
- [Tenth Man & Premortem Remediation] Hardened the detector against evasion vectors and performance degradation as flagged by Tenth Man Auditor and Premortem Analyst:
- Added binary density check instead of simple null-byte binary skip to prevent comment-based null-byte bypasses.
- Implemented partial scanning (first 1MB / last 1MB) for files > 10MB to prevent both OOM crashes and size-based scanner bypasses.
- Implemented 10,000 character line chunking instead of truncation to prevent ReDoS while retaining all text content.
- Added CJK language support by running character-level n-gram checking when CJK text is detected.
- Extended n-grams checks to cover up to 6-grams and added variations of LLM special tokens.
- Raised line and word repetition limits to 30 to minimize false positives on mock test arrays.

## Evidence
- Unit Tests: Added `tests/test_context_inflation_detector.py` containing 19 test cases covering ZW runs, ZW totals, whitespaces, n-grams (1-gram to 6-grams), line repetitions, padding tokens (standard and LLM-special), divider spam, CJK repetitions, null-byte density, huge file partial scans, line chunking, and preset manager integration.
- Test Run Results: 300 passed, 0 failed, 0 warnings.
- Manual CLI validation: Passed successfully on mock files.

## Observability
- Errors are raised via CLI standard outputs and logged via the standard logging module.
- Rollback detection: A simple git revert can be used if the scanner causes blocking false alerts. Rollback is confirmed successful when the CI/CD pipeline tests pass.

## Lessons
- [context-inflation-performance] Use index-based sliding comparisons for n-gram checks instead of full list comprehension tuple allocations to ensure O(1) memory overhead on large files.
- [context-inflation-unicode] Ensure zero-width scanning includes the full set of Unicode directional isolates (\u2066–\u2069), Mongolian vowel separators, and word joiners to prevent Trojan Source-style prompt injection bypasses.
- [context-inflation-divider-fp] Exclude common code and structured file extensions from divider spam checks to eliminate false positives on header banners and comment blocks.
- [context-inflation-density] Avoid using binary null-byte checks in text scanners, as it enables simple null-byte injection bypasses. Use a control character density check instead.
- [context-inflation-cjk] Standard regex word boundaries fail for non-space-separated CJK languages. Treat each CJK character as a token for repetition scanning.

## Resume
- State: TESTED
- Completed:
- Implemented ContextInflationDetector in checks/context_inflation_detector.py
- Integrated context_inflation into default enabled modules and all presets (next.js, flutter, django, fastapi, terraform) in scanner.py and presets/manager.py
- Registered self-scan exemptions for context_inflation rules
- Wrote 19 comprehensive unit tests in tests/test_context_inflation_detector.py
- Executed independent peer-review audit, Tenth Man review, and Premortem analysis to harden the engine against bypasses and performance degradation.
- Next: `/ship` to deliver the feature
- Context: Context Inflation / Prompt Flooding Detector is fully implemented, verified, reviewed, and ready for shipping.

### Read Map
Files to read:
- [src/ghostcheck/checks/context_inflation_detector.py](file:///c:/Users/wen/.gemini/antigravity/scratch/security-tools/src/ghostcheck/checks/context_inflation_detector.py) → Full (core detection logic)
- [tests/test_context_inflation_detector.py](file:///c:/Users/wen/.gemini/antigravity/scratch/security-tools/tests/test_context_inflation_detector.py) → Full (test suite)

### Skip List
- None

### Context Snapshot
Implemented Context Inflation / Prompt Flooding Detector to detect ZW character flooding, whitespace padding, word/line repetitions, and padding token spams. Resolved peer review, Tenth Man, and Premortem feedback to support up to 6-gram repetition with O(1) memory complexity, CJK character-level scanning, null-byte density pre-filtering, and line-chunking.

### Backlog Status
- Active Backlog: [docs/specs/_product-backlog.md](file:///c:/Users/wen/.gemini/antigravity/scratch/security-tools/docs/specs/_product-backlog.md)
- Current Feature: Context Inflation / Prompt Flooding Detector (Shipped)
- Remaining: 11 pending, 0 deferred
- Next Recommended: User choice or E9-F2 LLM Egress Firewall Auditor
12 changes: 12 additions & 0 deletions .agentcortex/context/current_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `[prompt-template-scanner] docs/specs/prompt_template_scanner.md [Frozen] [Updated: 2026-06-09]`
- `[ai-marker] docs/specs/ai_marker.md [Frozen] [Updated: 2026-06-09]`
- `[data-exfiltration] docs/specs/data-exfiltration.md [Frozen] [Updated: 2026-06-26]`
- `[context-inflation] docs/specs/context-inflation-detector.md [Frozen] [Updated: 2026-07-01]`
- When reading specs: only open files tagged with the current task's module.
- **Canonical Commands**:
- `/spec-intake`: Import external specs (from other LLMs, documents, or natural language). Handles large product specs via decomposition. Runs before `/bootstrap`.
Expand Down Expand Up @@ -82,9 +83,20 @@ GLOBAL-CANDIDATE [Patch Path Fallback]: When `apply_patch` is unstable on this W
- [port-cross-refs]: When porting a skill across repos, re-validate its `§X.Y` cross-refs and `runtime_anchor` paths against the TARGET repo's section numbering (agentic-os §12.5/§5.2a ≠ security-tools §2.1/§5.2).
- [Parentheses-Depth-Extraction]: Replaced simple non-greedy regex matching with dynamic parentheses depth balancing in fallback text scanner to support nested function calls.
- [Masked-Context-Exemption]: When writing scanner self-exemptions checking line contexts, always account for both the raw string representation and the masked representation (e.g. `abcd******************wxyz`), as masking happens prior to the final post-processing filter.
- [context-inflation-performance]: Use index-based sliding comparisons for n-gram checks instead of full list comprehension tuple allocations to ensure O(1) memory overhead on large files.
- [context-inflation-unicode]: Ensure zero-width scanning includes the full set of Unicode directional isolates (\u2066–\u2069), Mongolian vowel separators, and word joiners to prevent Trojan Source-style prompt injection bypasses.
- [context-inflation-divider-fp]: Exclude common code and structured file extensions from divider spam checks to eliminate false positives on header banners and comment blocks.

## Ship History

### Ship-feat/usability-and-dx-hardening-2026-07-03
- Usability and DX hardening shipped: Added two-stage parent parsing to allow global flags anywhere on CLI, implemented `--fail-on` exit threshold configuration, restricted JS/Python AST scanners using file extension boundaries, added post-scan deduplication, enabled inline ignores for AST findings, warned on ignored target scans, and configured pre-commit hook to scan staged files. Corrected entropy checks to evaluate raw values before masking. Mitigated downstream false positives ("很容易誤判") by fixing GPA-06 command arg placeholder bypasses and excluding config/lock extensions (`.lock`, `.yaml`, `.yml`, `.toml`, `.ini`, `.xml`) from ContextInflationDetector. Hardened AST scanners against non-string input types and secured dynamic context fetching by masking raw secrets. Resolved parallel expert peer reviews and Tenth Man bypasses (implemented clean 10MB file truncation instead of skipping to close large file bypasses, partial scans for structured configs, Hangul filler ZW expansions, short phrase n-gram repeat checks, and lazy token lowercasing optimization).
- Tests: Pass (324/324 tests passed).

### Ship-feat/context-inflation-detector-2026-07-01
- Feature shipped: Context Inflation and Prompt Flooding Detector checking invisible characters (including bidirectional isolates and formatting overrides), whitespace padding, n-gram repetitions (up to 10-grams), consecutive line repetitions (threshold 15), and padding token spams (including LLM-specific tokens). Aligned and integrated across all framework presets (Next.js, Flutter, Django, FastAPI, Terraform).
- Tests: Pass (19/19 module tests passed, 305/305 total tests passed, Grade A pre-commit score).

### Ship-feat/data-exfiltration-hardening-2026-06-26
- Feature shipped: Hardened AI Data Exfiltration Detector against static bypasses (decimal/hex IP SSRF, nested subscript taints, path construction, getattr resolution, and shutil.move) and implemented a fully hardened JS AST visitor and JS Validation Scanner.
- Tests: Pass (281/281 tests passed, Grade A self-scan score 100/100).
Expand Down
6 changes: 3 additions & 3 deletions docs/specs/_product-backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ GhostCheck 的核心差異化:**不只是另一個 SAST 工具,而是第一
|---|---------|------|------|------|------|
| E8-F1 | **Memory Poisoning Audit** | P1 | v1.2.0 | 🟡 | 掃描 Agent 的持久化記憶系統(Vector DB / JSON Profile),偵測潛伏中的惡意指令或偏見。 |
| E8-F2 | **Swarm Cascading Risk Analysis** | P2 | v1.3.0 | 🟡 | 分析 Multi-agent 工作流中的通訊拓補,找出單點 Agent 被劫持後可能導致的級聯失效點。 |
| E8-F3 | **Lethal Trifecta Detector** | P0 | v1.2.0 | 🟡 | 自動偵測「私有資料存取+不受信輸入+工具執行」的危險組合,強制調高安全等級與審核要求。 |
| E8-F3 | **Lethal Trifecta Detector** | P0 | v1.2.0 | | 自動偵測「私有資料存取+不受信輸入+工具執行」的危險組合,強制調高安全等級與審核要求。 |
| E8-F4 | **Tool Metadata Poisoning Linter** | P1 | v1.2.0 | 🟡 | 深度掃描 MCP Server 或 Plugin 的 Metadata/Description,防止 Hidden Prompt 注入至 LLM 推理過程。 |
| E8-F5 | **Agentic Kill-Switch Compliance** | P0 | v1.3.0 | 🟡 | 審核專案中是否實作了實體的斷路器機制(Token Cap/File Limit/Human-Confirm),防止 Autonomous 跑飛。 |
| E8-F6 | **MCP Registry & Provenance Guard** | P2 | v1.3.0 | 🟡 | 建立 MCP Server 信任鏈驗證,檢查第三方工具的數位簽署、來源聲譽與已知惡意黑名單。 |
Expand All @@ -157,7 +157,7 @@ GhostCheck 的核心差異化:**不只是另一個 SAST 工具,而是第一

| # | Feature | 優先 | 版本 | 狀態 | 說明 |
|---|---------|------|------|------|------|
| E9-F1 | **Silent Package Installation Detector** | P0 | v1.2.0 | 🟡 | 偵測 AI Agent 是否在背景靜默執行套件安裝(如 `pip install` / `npm install` 且未鎖定版本),防範相依性劫持。 |
| E9-F1 | **Silent Package Installation Detector** | P0 | v1.2.0 | | 偵測 AI Agent 是否在背景靜默執行套件安裝(如 `pip install` / `npm install` 且未鎖定版本),防範相依性劫持。 |
| E9-F2 | **LLM Egress Firewall Auditor** | P1 | v1.2.0 | 🟡 | 審計專案是否設定了出站流量限制(Egress Firewall),防範 Agent 透過未授權的 HTTP 請求外洩資料。 |
| E9-F3 | **Shadow AI Env Leakage Scanner** | P1 | v1.3.0 | 🟡 | 掃描環境變數,偵測是否有敏感的 LLM API Keys 在子程序中被意外匯出或暴露給非特權指令。 |

Expand All @@ -170,7 +170,7 @@ GhostCheck 的核心差異化:**不只是另一個 SAST 工具,而是第一
| # | Feature | 優先 | 版本 | 狀態 | 說明 |
|---|---------|------|------|------|------|
| E10-F1 | **Vector DB Metadata Poisoning Auditor** | P1 | v1.2.0 | 🟡 | 偵測匯入向量資料庫(如 Chroma, Pinecone)的 metadata 中是否夾帶 Prompt Injection 指令。 |
| E10-F2 | **Context Inflation / Prompt Flooding Detector** | P0 | v1.2.0 | 🟡 | 偵測利用重複大量垃圾字元意圖撐滿上下文視窗(Context Window),以使模型遺忘 System Prompt 的攻擊。 |
| E10-F2 | **Context Inflation / Prompt Flooding Detector** | P0 | v1.2.0 | | 偵測利用重複大量垃圾字元意圖撐滿上下文視窗(Context Window),以使模型遺忘 System Prompt 的攻擊。 |

---

Expand Down
57 changes: 57 additions & 0 deletions docs/specs/context-inflation-detector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
status: frozen
title: Context Inflation / Prompt Flooding Detector
source: external
source_doc: _product-backlog.md (E10-F2)
created: 2026-07-01
---

# Context Inflation / Prompt Flooding Detector

## Goal
Implement a security scanner plugin (`ContextInflationDetector`) to detect Context Inflation and Prompt Flooding attacks. These attacks attempt to bypass LLM system instructions or safety filters by flooding the context window with repetitive text, large blocks of whitespace, or invisible zero-width characters.

## Acceptance Criteria
1. **E10-F2 Alignment**: Scan files to detect context inflation and prompt flooding patterns.
2. **Invisible Character Flooding Detection**:
- Detect consecutive sequences of zero-width or invisible Unicode characters (e.g., `\u200b`, `\u200c`, `\u200d`, `\u200e`, `\u200f`, `\ufeff`, `\u202a`–`\u202e` RTL/LTR overrides, zero-width spaces).
- Trigger `CRITICAL` finding if a single file contains more than 50 consecutive zero-width/invisible characters, or more than 200 total zero-width/invisible characters (excluding common Markdown syntax or standard formatting if applicable, but strictly flags malicious obfuscation).
3. **Whitespace Padding / Large Gap Detection**:
- Detect huge blocks of whitespaces, tabs, or newlines designed to push text out of the context window or user screen.
- Trigger `MEDIUM` finding if there are more than 1000 consecutive whitespace/newline characters without non-whitespace content.
4. **Word Repetition Flooding Detection**:
- Detect cases where a single word or short phrase (1-3 words) is repeated consecutively or near-consecutively (e.g., "ignore ignore ignore", "hello hello hello").
- Trigger `HIGH` finding if a word/phrase is repeated consecutively more than 30 times.
5. **Repetitive Line Flooding Detection**:
- Detect identical lines repeated consecutively.
- Trigger `HIGH` finding if the same line (ignoring leading/trailing whitespace) is repeated consecutively more than 15 times.
6. **Padding Token Spamming Detection**:
- Detect excessive repetitions of padding patterns (e.g., `<pad>`, `[PAD]`, `<unk>`, `...`, `---`, `***`, `===`).
- Trigger `MEDIUM` finding if a file contains more than 50 occurrences of standard padding patterns or dividers in close proximity or within a single file.
7. **Scanner Registry & Integration**:
- The plugin must be integrated into `PluginManager` and registered under the name `context_inflation_detector`.
- Appropriate test cases must verify all detection mechanisms against mock payloads.

## Non-goals
- Parsing ASTs for this check: since context inflation and prompt flooding are character/line-level text attacks, a fast text-based scan is sufficient and more performant than AST parsing.
- Correcting or sanitizing the files: the scanner only audits and reports findings; it does not modify the scanned files.

## Constraints
- **Performance**: The linter must perform fast pre-filtering. If none of the inflation characteristics (like zero-width characters, long whitespace blocks, or high repetitions) are present, it should skip the file immediately.
- **Encoding**: Must handle UTF-8 and non-UTF-8 files gracefully without crashing, utilizing safe decoding fallbacks (similar to other scanners in GhostCheck).

## API / Data Contract
The scanner must return findings in the standard GhostCheck finding format:
```json
{
"file": "path/to/file",
"line": 12,
"name": "context_inflation_detected",
"severity": "CRITICAL | HIGH | MEDIUM",
"message": "Detailed description of the detected inflation pattern",
"suggestion": "How to resolve the issue"
}
```

## File Relationship
INDEPENDENT
Loading
Loading