Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(ci): cover Path methods in the existing request-path rule (#7170)

LearningCircuit9104057a90b9
diff --git a/.semgrep/rules/ldr-security.yaml b/.semgrep/rules/ldr-security.yaml
index 646f70f1b7..5a3cf34d61 100644
--- a/.semgrep/rules/ldr-security.yaml
+++ b/.semgrep/rules/ldr-security.yaml
@@ -476,6 +476,8 @@ rules:
- pattern: io.open(file=$PATH, ...)
- pattern: aiofiles.open($PATH, ...)
- pattern: aiofiles.open(file=$PATH, ...)
+ - pattern: $PATH.read_text(...)
+ - pattern: $PATH.unlink(...)
- focus-metavariable: $PATH
- pattern: Path(...)
- pattern: PurePath(...)
diff --git a/changelog.d/+request-path-file-sinks.security.md b/changelog.d/+request-path-file-sinks.security.md
new file mode 100644
index 0000000000..47ea9a9657
--- /dev/null
+++ b/changelog.d/+request-path-file-sinks.security.md
@@ -0,0 +1 @@
+Extend the existing request-path scanner to cover Path.read_text() and Path.unlink() receivers passed through route parameters or request attributes.
diff --git a/tests/ci/standalone/test_semgrep_request_path_rule.py b/tests/ci/standalone/test_semgrep_request_path_rule.py
new file mode 100644
index 0000000000..7e9fdcc03a
--- /dev/null
+++ b/tests/ci/standalone/test_semgrep_request_path_rule.py
@@ -0,0 +1,132 @@
+"""Exercise direct Path receivers in the existing request-path rule."""
+
+# allow: no-sut-import — this test invokes the scanner and its rule file.
+
+import json
+import shutil
+import subprocess
+import tempfile
+import unittest
+from pathlib import Path
+
+
+ROOT = Path(__file__).resolve().parents[3]
+RULE = ROOT / ".semgrep" / "rules" / "ldr-security.yaml"
+RULE_ID = "request-path-traversal-risk"
+
+SOURCE = """\
+from pathlib import Path
+from fastapi import APIRouter, Request
+from fastapi.responses import FileResponse
+from werkzeug.utils import safe_join
+from local_deep_research.security.path_validator import PathValidator
+
+router = APIRouter()
+
[email protected]("/read/{path:path}")
+def read_path(path: Path):
+ return path.read_text(encoding="utf-8") # expected-finding
+
[email protected]("/alias/{path:path}")
+def read_alias(path: Path):
+ selected = path
+ return selected.read_text() # expected-finding
+
[email protected]("/remove/{path:path}")
+def remove_path(path: Path):
+ path.unlink(missing_ok=True) # expected-finding
+
[email protected]("/async/{path:path}")
+async def remove_async(path: Path):
+ path.unlink() # expected-finding
+
+def request_path(request: Request):
+ return request.state.report_path.read_text() # expected-finding
+
[email protected]("/constructed-read/{name:path}")
+def constructed_read(name: str):
+ return Path(name).read_text() # expected-finding expected-dynamic
+
[email protected]("/constructed-remove/{name:path}")
+def constructed_remove(name: str):
+ Path(name).unlink() # expected-finding expected-dynamic
+
+def internal_path(path: Path):
+ return path.read_text()
+
[email protected]("/encoding")
+def fixed_path_encoding(encoding: str):
+ return Path("/srv/data/fixed.txt").read_text(encoding=encoding)
+
[email protected]("/missing")
+def fixed_path_missing(missing_ok: bool):
+ Path("/srv/data/fixed.txt").unlink(missing_ok=missing_ok)
+
[email protected]("/validated/{name:path}")
+def validated_path(name: str):
+ target = PathValidator.validate_safe_path(name, "/srv/data")
+ return target.read_text()
+
[email protected]("/joined/{name:path}")
+def joined_path(name: str):
+ return FileResponse(safe_join("/srv/data", name))
+"""
+
+
[email protected](shutil.which("semgrep"), "Semgrep runs in scanner CI")
+class RequestPathRuleTests(unittest.TestCase):
+ def test_direct_path_receivers_preserve_existing_boundaries(self):
+ expected_lines = {
+ number
+ for number, line in enumerate(SOURCE.splitlines(), 1)
+ if "# expected-finding" in line
+ }
+ expected_findings = {(RULE_ID, line) for line in expected_lines} | {
+ ("path-traversal-risk", number)
+ for number, line in enumerate(SOURCE.splitlines(), 1)
+ if "expected-dynamic" in line
+ }
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory)
+ source = root / "routes.py"
+ source.write_text(SOURCE, encoding="utf-8")
+ report = root / "report.json"
+ result = subprocess.run(
+ [
+ "semgrep",
+ "scan",
+ "--config",
+ str(RULE),
+ "--jobs=1",
+ "--strict",
+ "--metrics=off",
+ "--disable-version-check",
+ "--json-output",
+ str(report),
+ str(source),
+ ],
+ cwd=root,
+ capture_output=True,
+ text=True,
+ timeout=90,
+ check=False,
+ )
+ self.assertEqual(
+ result.returncode, 0, result.stdout + result.stderr
+ )
+ findings = json.loads(report.read_text(encoding="utf-8"))
+ self.assertEqual(findings.get("errors"), [])
+ self.assertEqual(findings.get("skipped_rules"), [])
+ self.assertEqual(len(findings["paths"]["scanned"]), 1)
+ self.assertEqual(len(findings["results"]), len(expected_findings))
+ self.assertEqual(
+ {
+ (item["check_id"].rsplit(".", 1)[-1], item["start"]["line"])
+ for item in findings["results"]
+ },
+ expected_findings,
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()