diff --git a/ops/README.md b/ops/README.md index af561053..d0095173 100644 --- a/ops/README.md +++ b/ops/README.md @@ -30,8 +30,9 @@ you expect. Anything outside the mount is passed through unchanged with a warnin rewritten into a path the container cannot see. Your directory is mounted **read-only**; findings go to `./.ousast`, the one writable mount. The image builds itself on first use and says so. -If `joern-parse` is already on your PATH and a local venv exists, the wrapper uses those instead — someone who -installed the engine deliberately should not be quietly routed through a container. Set +If `joern` and `joern-parse` are already on your PATH and a local venv exists, the wrapper uses those instead, +from your directory — someone who installed the engine deliberately should not be quietly routed through a +container. A native run reads `.env` from your directory, not from the checkout. Set `OUSAST_PREFER_NATIVE=0` to always containerise. ## Two ways to get the engine diff --git a/ops/shell/ousast.sh b/ops/shell/ousast.sh index 0b1a1ed3..4292993d 100644 --- a/ops/shell/ousast.sh +++ b/ops/shell/ousast.sh @@ -6,7 +6,6 @@ # # ousast scan . # ousast scan src/api -# ousast pairs --slice vibe-py # # Docker is an implementation detail on purpose. The engine is a JVM and ~2 GB of Joern, and a contributor # evaluating this tool should not have to install either to try it. What they get instead is a command. @@ -31,15 +30,19 @@ ousast() { fi # A native install wins when it is there: it is faster, and someone who installed Joern deliberately - # should not be quietly routed through a container. - if [ "${OUSAST_PREFER_NATIVE:-1}" = "1" ] && command -v joern-parse >/dev/null 2>&1 \ - && [ -x "$OUSAST_HOME/.venv/bin/python" ]; then - ( cd "$OUSAST_HOME" && .venv/bin/python -m openultrasast.cli "$@" ) + # should not be quietly routed through a container. It runs in the caller's directory, as the container + # does, so `.`, relative names and relative options mean the caller's files. -P keeps that directory off + # the interpreter's path, where a file of it could stand in for a module the CLI imports. Joern is looked + # for as well: the batch queries run `joern`, and with `joern-parse` alone every one fails while the scan + # still exits 0. + if [ "${OUSAST_PREFER_NATIVE:-1}" = "1" ] && command -v joern >/dev/null 2>&1 \ + && command -v joern-parse >/dev/null 2>&1 && [ -x "$OUSAST_HOME/.venv/bin/python" ]; then + "$OUSAST_HOME/.venv/bin/python" -P -m openultrasast.cli "$@" return $? fi if ! command -v docker >/dev/null 2>&1; then - echo "ousast: docker is not installed, and no local joern-parse was found" >&2 + echo "ousast: docker is not installed, and no local joern and joern-parse were found" >&2 return 1 fi diff --git a/tests/test_shell_wrapper.py b/tests/test_shell_wrapper.py new file mode 100644 index 00000000..b13c7826 --- /dev/null +++ b/tests/test_shell_wrapper.py @@ -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 ` 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