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.

4.2 KB
Raw Blame
"""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()

@router.get("/read/{path:path}")
def read_path(path: Path):
    return path.read_text(encoding="utf-8")  # expected-finding

@router.get("/alias/{path:path}")
def read_alias(path: Path):
    selected = path
    return selected.read_text()  # expected-finding

@router.delete("/remove/{path:path}")
def remove_path(path: Path):
    path.unlink(missing_ok=True)  # expected-finding

@router.delete("/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

@router.get("/constructed-read/{name:path}")
def constructed_read(name: str):
    return Path(name).read_text()  # expected-finding expected-dynamic

@router.delete("/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()

@router.get("/encoding")
def fixed_path_encoding(encoding: str):
    return Path("/srv/data/fixed.txt").read_text(encoding=encoding)

@router.delete("/missing")
def fixed_path_missing(missing_ok: bool):
    Path("/srv/data/fixed.txt").unlink(missing_ok=missing_ok)

@router.get("/validated/{name:path}")
def validated_path(name: str):
    target = PathValidator.validate_safe_path(name, "/srv/data")
    return target.read_text()

@router.get("/joined/{name:path}")
def joined_path(name: str):
    return FileResponse(safe_join("/srv/data", name))
"""


@unittest.skipUnless(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()