mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +00:00
feat(contributor-scan): a scan skips its runs directory when it lies inside the scanned path (3.1, follow-up)
Since part A a native scan writes its run under the user cache or wherever `OPENULTRASAST_RUNS_DIR` points. A scan of a path that contains that directory, a home directory or a tree with the runs directory set inside it, took the files under it for source: those of earlier runs and those of its own run. On the code as it was, a source file under the runs directory inside the target was read and its finding reported (`file_targets=3 findings=2` for a tree of two files and one such copy). By the maintainer's decision, recorded as step 2c of the handover, the scan skips the runs directory only: `config.runs_dir`, the default location or the override. The other directories under `~/.cache/openultrasast` are read as before, and so is every tree whose runs directory lies outside it. `preprocess_repository` and `enumerate_source_files` in src/openultrasast/preprocess.py take a `skip` directory, and the preprocess stage of a scan (`_map_scan_targets` in cli.py) passes `Path(config.runs_dir)`. The skip applies only where the runs directory lies inside the scanned path: a target that is the runs directory or lies inside it is read as it was, so a runs directory set to `.` does not silence a scan. Both paths are compared resolved, so a symlinked or relative name is found, and the excluded path is built under the root as the walk spells it, so a relative root works too. Every other caller of the two functions passes no `skip` and is unchanged. No run of this package keeps a source file today (the independent reviewer's count: a quick run holds 15 files, all `.json`, `.jsonl`, `.md` or `.sarif`; the two benchmark runs in the real cache hold `.json` and `.toml`). The copy in the tests is a constructed case. Tests: four tests failed on the code as it was and pass after the change: the runs directory inside the scanned path, by the override, by the default location and by the override through a symlink, and the rest of the cache still read. Two more pin the narrow reading, a target that is the runs directory or lies inside it is read as it was; they failed on a first cut that skipped more than was decided. A direct call with a relative root pins the path comparison. The nine test files around the change pass (113 passed), as do the 37 files that exercise the scan path (362 passed); ruff, mypy, the three gates and compileall pass. Runs of the real CLI on one scratch subject, the code at HEAD against this tree: with the runs directory inside the target the copy under it is no longer read (`file_targets=4 findings=3` for five source files); with the runs directory a parent of the target, or the target itself, both read the same five files and report the same four findings. An independent reviewer approved the change in three rounds; its first round found that the first cut also silenced a scan whose target lay inside the runs directory, which this cut narrows to the decided case.
This commit is contained in:
@@ -750,7 +750,12 @@ def _map_scan_targets(
|
||||
write_static_hints(static_hints, run.root / "mapping" / "static_hints.json")
|
||||
snapshot, targets = runtime.run_stage(
|
||||
"preprocess",
|
||||
lambda: preprocess_repository(run.target, run.root / "preprocess" / "file_targets.json", static_hints),
|
||||
lambda: preprocess_repository(
|
||||
run.target,
|
||||
run.root / "preprocess" / "file_targets.json",
|
||||
static_hints,
|
||||
skip=Path(config.runs_dir),
|
||||
),
|
||||
)
|
||||
# A language nothing here covers is named, never a silent zero (pre-push 17.2, Requirement 9.2).
|
||||
for language, count in uncovered_languages(targets, ruleset, engine=mode != "quick"):
|
||||
|
||||
@@ -62,9 +62,10 @@ def preprocess_repository(
|
||||
root: Path,
|
||||
output_path: Path | None = None,
|
||||
static_hints: list[object] | None = None,
|
||||
skip: Path | None = None,
|
||||
) -> tuple[RepoSnapshot, list[FileTarget]]:
|
||||
resolved = root.resolve()
|
||||
files = enumerate_source_files(resolved)
|
||||
files = enumerate_source_files(resolved, skip)
|
||||
targets = [build_file_target(resolved, path) for path in files]
|
||||
if static_hints:
|
||||
from .mapping import attach_static_hints
|
||||
@@ -93,9 +94,15 @@ def write_preprocess_artifact(snapshot: RepoSnapshot, targets: list[FileTarget],
|
||||
output_path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n")
|
||||
|
||||
|
||||
def enumerate_source_files(root: Path) -> list[Path]:
|
||||
def enumerate_source_files(root: Path, skip: Path | None = None) -> list[Path]:
|
||||
"""Every source file under `root`, in repository path order.
|
||||
|
||||
`skip` names a directory the caller keeps its own output in, the runs directory of a scan. When it lies
|
||||
inside `root`, nothing under it is taken for source, so a scan reads neither an earlier run nor its own.
|
||||
"""
|
||||
patterns = _load_ignore_patterns(root)
|
||||
tracked = _tracked_files(root) if patterns else None
|
||||
excluded = _excluded_directory(root, skip)
|
||||
paths: list[Path] = []
|
||||
for path in root.rglob("*"):
|
||||
if (
|
||||
@@ -104,6 +111,7 @@ def enumerate_source_files(root: Path) -> list[Path]:
|
||||
or _has_ignored_dir(root, path)
|
||||
or _is_ignored(root, path, patterns, tracked)
|
||||
or _is_vendored(root, path)
|
||||
or (excluded is not None and path.is_relative_to(excluded))
|
||||
):
|
||||
continue
|
||||
if detect_language(path) != "unknown":
|
||||
@@ -111,6 +119,17 @@ def enumerate_source_files(root: Path) -> list[Path]:
|
||||
return sorted(paths, key=lambda item: item.relative_to(root).as_posix())
|
||||
|
||||
|
||||
def _excluded_directory(root: Path, skip: Path | None) -> Path | None:
|
||||
"""`skip` as a path under `root`, or None when there is nothing to skip: no `skip` was given, or it is
|
||||
`root` itself or lies above it, which leaves the tree read as it was."""
|
||||
if skip is None:
|
||||
return None
|
||||
resolved, named = root.resolve(), skip.resolve()
|
||||
if named == resolved or not named.is_relative_to(resolved):
|
||||
return None
|
||||
return root / named.relative_to(resolved)
|
||||
|
||||
|
||||
def build_file_target(root: Path, path: Path) -> FileTarget:
|
||||
language = detect_language(path)
|
||||
text = _read_text(path)
|
||||
|
||||
@@ -14,6 +14,7 @@ import pytest
|
||||
from openultrasast import cli
|
||||
from openultrasast.cli import main
|
||||
from openultrasast.config import ResolvedConfig
|
||||
from openultrasast.preprocess import enumerate_source_files
|
||||
|
||||
LEDGER = ".openultrasast/calibration/false_positive_learnings.json"
|
||||
LEDGER_DIRECTORIES = {".openultrasast", ".openultrasast/calibration"}
|
||||
@@ -225,6 +226,94 @@ def test_a_relative_runs_dir_override_is_taken_from_the_working_directory_not_th
|
||||
assert not (repo / ".runs").exists()
|
||||
|
||||
|
||||
def _runs_inside(repo: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, form: str) -> Path:
|
||||
"""The runs directory inside the scanned tree: by the override, by the default location, or by the override
|
||||
through a symlink (as macOS keeps `/tmp`)."""
|
||||
monkeypatch.delenv("OPENULTRASAST_RUNS_DIR", raising=False)
|
||||
runs = repo / (".cache/openultrasast/runs" if form == "default_location" else ".runs")
|
||||
runs.mkdir(parents=True)
|
||||
if form == "default_location":
|
||||
monkeypatch.setenv("XDG_CACHE_HOME", str(repo / ".cache"))
|
||||
elif form == "symlinked_override":
|
||||
link = tmp_path / "runs-link"
|
||||
link.symlink_to(runs, target_is_directory=True)
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(link))
|
||||
else:
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(runs))
|
||||
return runs
|
||||
|
||||
|
||||
def _files_read(run: Path) -> list[str]:
|
||||
return [target["path"] for target in json.loads((run / "preprocess" / "file_targets.json").read_text())["file_targets"]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("form", ["override", "default_location", "symlinked_override"])
|
||||
def test_a_scan_skips_the_runs_directory_when_it_lies_inside_the_scanned_path(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], form: str
|
||||
) -> None:
|
||||
repo = _target(tmp_path)
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, form)
|
||||
# A constructed case, since no run of this package keeps a source file: one with the finding under study,
|
||||
# under the runs directory.
|
||||
(runs / "earlier").mkdir()
|
||||
(runs / "earlier" / "copy.py").write_text((repo / "app.py").read_text())
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
(run,) = [path for path in runs.iterdir() if path.name != "earlier"]
|
||||
assert _files_read(run) == ["app.py", "pkg/lib.py"]
|
||||
assert re.findall(r"^findings=(\d+)$", capsys.readouterr().out, flags=re.MULTILINE) == ["1"]
|
||||
|
||||
|
||||
def test_only_the_runs_directory_is_skipped_the_rest_of_the_cache_is_read(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
repo = _target(tmp_path)
|
||||
runs = _runs_inside(repo, tmp_path, monkeypatch, "default_location")
|
||||
copy = (repo / "app.py").read_text()
|
||||
# The last two are not the runs directory either: a sibling that shares its name's prefix, and a directory
|
||||
# of that name elsewhere in the tree.
|
||||
for kept in (runs / "earlier", runs.parent / "other", runs.parent / "runs-other", repo / "elsewhere" / "runs"):
|
||||
kept.mkdir(parents=True)
|
||||
(kept / "copy.py").write_text(copy)
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
(run,) = [path for path in runs.iterdir() if path.name != "earlier"]
|
||||
assert _files_read(run) == [
|
||||
".cache/openultrasast/other/copy.py",
|
||||
".cache/openultrasast/runs-other/copy.py",
|
||||
"app.py",
|
||||
"elsewhere/runs/copy.py",
|
||||
"pkg/lib.py",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("where", ["a_parent_of_the_target", "the_target_itself"])
|
||||
def test_a_target_that_lies_inside_the_runs_directory_is_read_as_it_was(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], where: str
|
||||
) -> None:
|
||||
# The runs directory is skipped where it lies inside the scanned path, not the other way round: set to a
|
||||
# working directory or to `.`, it must not silence the scan.
|
||||
repo = _target(tmp_path)
|
||||
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(tmp_path if where == "a_parent_of_the_target" else repo))
|
||||
|
||||
assert _scan(repo) == 0
|
||||
|
||||
(printed,) = re.findall(r"^run_dir=(.+)$", capsys.readouterr().out, flags=re.MULTILINE)
|
||||
assert _files_read(Path(printed)) == ["app.py", "pkg/lib.py"]
|
||||
|
||||
|
||||
def test_the_runs_directory_is_found_under_a_relative_root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
# The scan passes a resolved root; a direct caller need not, and the skip must hold for both.
|
||||
(tmp_path / "runs" / "earlier").mkdir(parents=True)
|
||||
for name in ("app.py", "runs/earlier/copy.py"):
|
||||
(tmp_path / name).write_text("x = 1\n")
|
||||
monkeypatch.chdir(tmp_path)
|
||||
|
||||
assert [path.as_posix() for path in enumerate_source_files(Path("."), Path("runs"))] == ["app.py"]
|
||||
|
||||
|
||||
def test_no_test_can_write_a_scan_run_into_the_developers_cache(tmp_path_factory: pytest.TempPathFactory) -> None:
|
||||
"""tests/conftest.py redirects the override for every test, so one that forgets to name a location cannot
|
||||
write into the real `~/.cache/openultrasast/runs`."""
|
||||
|
||||
Reference in New Issue
Block a user