fix(contributor-scan): the wrapper's native path runs in the caller's directory (item 5a)

`ousast scan .` through ops/shell/ousast.sh with a native install scanned the wrong
tree. The native path changed into the checkout before it passed the arguments on, so
`.` was the OpenUltraSAST checkout and a relative file name was looked up there. The
eighth session showed it with a stub interpreter in place of `.venv/bin/python`
(`cwd=/tmp/opencode/s8_fakehome args=-m openultrasast.cli scan .`). The check also
looked for `joern-parse` only, while the batch queries run `joern`: with `joern-parse`
alone on PATH every batch query failed and a standard scan still exited 0 (2026-11-26,
14 findings where the working launcher gives 15).

The native path now runs the checkout's interpreter by its absolute path, in the
caller's directory, as the container path does, and it is taken only when `joern` is on
PATH as well. So `.`, relative names and relative options mean the caller's files. The
reviewer found, and a run confirmed, what dropping the `cd` brings: `-m` alone puts the
working directory first on the interpreter's path, so a file of the scanned tree could
stand in for a module the CLI imports (a `json.py` in the caller's directory: `ousast
--help` printed SHADOW-EXECUTED and exited 3). The interpreter is started with `-P`,
which keeps that directory off the path; the flag needs Python 3.11, the project's
floor (pyproject.toml).

Changes a caller sees, beyond the one the decision named:
- The checkout's .env is no longer read on the native path; the CLI reads .env from the
  working directory, which is now the caller's. ops/README.md says so.
- `ousast pairs` and `ousast repos` no longer find their defaults outside the
  checkout, because `benchmarks/` is read relative to the working directory. Measured
  for `pairs`: through the wrapper natively, and on the container path, `pairs --slice
  vibe-py` ends with "pair catalog is not a file: benchmarks/pairs/catalog.toml",
  exit 1, so the container path already behaved this way. `repos` by reading. Run from
  the checkout root they find their defaults again (by reading; not run). The header's
  `ousast pairs --slice vibe-py` example is removed. This is a reading the maintainer
  may correct.
- The message for a machine without Docker names both executables.

Tests: the new tests/test_shell_wrapper.py runs the wrapper by bash with a stub of the
interpreter, `joern`, `joern-parse` and `docker` ahead of /usr/bin and /bin on PATH.
Its two tests failed before the change (2 failed: the interpreter ran in the fake
checkout, and the native path was taken without `joern`), and the first failed again
until `-P` was passed (1 failed). With the real venv and Joern 4.0.625 on PATH, from a
directory without .env: `ousast scan examples --mode quick` on a fresh copy of libpng's
contrib/examples gives file_targets=4, findings=4, exit 0, before and after the flag,
the figures of the eighth session. The eight tests of tests/test_docs_site.py and
tests/test_removed_plane_references.py pass, `mkdocs build --strict` exits 0, ruff
format, ruff check, mypy, the three gates and compileall pass, and the gates print what
they printed before the change. An independent reviewer approved the change in the
second round; its two findings of the first round led to the `-P` flag and to the
removal of the header example.

Found by the reviewer and left alone, outside this decision: the background child of
`ousast pre-push` (src/openultrasast/push/background.py) starts the CLI with `-m` and
without `-P`, from the pushed repository.
This commit is contained in:
Marius Ciepluch
2026-11-26 17:29:16 +01:00
parent ea8d3d2e6e
commit a6e8e2615a
3 changed files with 69 additions and 8 deletions
+57
View File
@@ -0,0 +1,57 @@
"""The shell wrapper, `ops/shell/ousast.sh`, run by bash against stubs: no Joern, no venv, no Docker."""
import shlex
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
WRAPPER = ROOT / "ops/shell/ousast.sh"
STUB = """#!/bin/sh
echo "{name} cwd=$PWD args=$*"
"""
def ousast(tmp_path, executables, *arguments):
"""`ousast <arguments>` in tmp_path/project. OUSAST_HOME is a checkout that holds only what the wrapper looks
for, the interpreter under its .venv is a stub, and so is each executable named, and docker, on PATH."""
home = tmp_path / "checkout"
home.mkdir()
(home / "docker-compose.yml").write_text("services: {}\n")
python = home / ".venv" / "bin" / "python"
python.parent.mkdir(parents=True)
python.write_text(STUB.format(name="python"))
python.chmod(0o755)
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
for name in ("docker", *executables):
(bin_dir / name).write_text(STUB.format(name=name))
(bin_dir / name).chmod(0o755)
project = tmp_path / "project"
project.mkdir()
script = f'source "{WRAPPER}" && ousast {" ".join(shlex.quote(a) for a in arguments)}'
return subprocess.run(
["bash", "-c", script],
cwd=project,
env={"PATH": f"{bin_dir}:/usr/bin:/bin", "OUSAST_HOME": str(home)},
capture_output=True,
text=True,
)
def test_a_native_scan_runs_in_the_callers_directory(tmp_path):
"""The native path changed into the checkout first, so `ousast scan .` scanned the checkout, not the caller.
`-P` is passed because `-m` alone puts the working directory first on the interpreter's path, where a file
of the scanned tree could stand in for a module the CLI imports."""
result = ousast(tmp_path, ["joern", "joern-parse"], "scan", ".")
assert result.returncode == 0, result.stderr
assert result.stdout.splitlines() == [f"python cwd={tmp_path / 'project'} args=-P -m openultrasast.cli scan ."]
def test_the_native_path_needs_joern_as_well_as_joern_parse(tmp_path):
"""The batch queries run `joern`; with `joern-parse` alone every one fails, and the scan still exits 0."""
result = ousast(tmp_path, ["joern-parse"], "scan", ".")
assert result.returncode == 0, result.stderr
calls = [line.split()[0] for line in result.stdout.splitlines()]
assert "python" not in calls and "docker" in calls