From d8b4cac023cdc93e188d3d9e2083789f2b23c664 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:24:11 +0100 Subject: [PATCH] fix(shell): the wrapper says so when it cannot make its runs directory (item 5b) The maintainer's answer 4 of 2026-11-27, "Guard it" (.kiro/handover.md:1409-1413), as offered: "a clear message when HOME is unset, and a failed mkdir stops the wrapper with exit 1; a test first." The fault it answers, in the same words: "With HOME unset the wrapper now fails ($HOME/.cache becomes /.cache), where it wrote ./.ousast before; a failed mkdir is not checked, as before." What changed - ops/shell/ousast.sh, the container path: the wrapper returns 1 with "ousast: HOME is not set, so there is no ~/.cache/openultrasast/runs to write the runs to" when HOME is unset or empty, and with "ousast: cannot create , where the runs are written" when mkdir -p fails (mkdir's own line comes first). - The two lines that set `runs` and ran mkdir -p moved up to that place, from after the build. The placement is the session's reading of "stops the wrapper": a cold build pulls ~2 GB, and a run that cannot write its runs should say so before it waits. - The native path names no HOME and is not touched. Tests (tests/test_shell_wrapper.py) - test_the_container_path_stops_when_home_is_not_set: exit 1, that one line on stderr, nothing on stdout, so neither the build nor the container is started. - test_a_runs_directory_that_cannot_be_made_stops_the_wrapper: a file where $HOME/.cache should be; exit 1, the wrapper's line last on stderr, nothing on stdout. - The helper `ousast` takes `without_home`. The docker stub answers `image inspect` with exit 1, so the wrapper takes its build branch in each test that reaches the build (the second and the third); before that, "nothing on stdout" said nothing about the build. - RED, on an archive of HEAD with the final test file (s10_c4_red5.out): 2 failed, 3 passed, each `assert 0 == 1`. For the reason of the fault: the wrapper builds, mkdir is refused ("/.cache: Permission denied", run as uid 1000) or fails ("Not a directory"), the wrapper goes on to the container (with reports=/.cache/... when HOME is unset), and the exit status is 0. - GREEN, on the working tree (s10_c4_green5.out): 5 passed. - A mutant with the guard behind the build fails both new tests on the build's line (/tmp/opencode/s10_c4_mutant.py, output s10_c4_mut.out), so the tests pin the placement; with another shebang in STUB it fails the same way (s10_c4_nopin.out). - Gates (s10_c4e_exits.txt): ten zeros; the three gate outputs equal the baseline's. Measured outside the stubs (/tmp/opencode/s10_c4_probe.sh, output s10_c4_probe.out): real bash on this machine, the native path switched off so that no probe can scan. HOME unset, HOME empty, and bash under set -u with HOME unset each print the one line and exit 1; a file at $HOME/.cache prints mkdir's line, then the wrapper's, exit 1. By reading, not measured - zsh can source the wrapper as well. The block uses `[ -z "${HOME:-}" ]`, `local`, `if ! mkdir -p`, `echo ... >&2` and `return 1`; zsh is not installed here, so no probe ran. - A run as root with HOME unset had another shape on HEAD: mkdir -p /.cache succeeds there, so the runs went to the filesystem root with no error. The guard closes it the same way. This session's runs are as uid 1000. Found and not fixed, for the maintainer - The helper's second parameter is `executables`. The third test passes "scan" there (tests/test_shell_wrapper.py:65, HEAD:63), which makes four stubs named s, c, a and n beside docker's. Nothing calls them. The new tests pass an empty tuple. - A runs directory that exists but cannot be written to passes the guard: mkdir -p exits 0 on it (s10_c4_readonly_runs.out), and the wrapper goes on to docker. The decision says "a failed mkdir". - The container path reads $HOME/.cache alone and ignores XDG_CACHE_HOME, which the native path honours (src/openultrasast/config.py:198-203); README.md:189-190 and docs/examples.md:3-5 state the rule for every run. From before this commit. - ops/shell/ousast.ps1:44-45 has the same shape, a New-Item whose result is not checked, after the build. Read, not run: pwsh is not installed. - The wrapper's message for an unset HOME (ops/shell/ousast.sh:53) describes the container path, which reads $HOME alone. Without HOME, Python's Path.home() and bash's `~` still give /home/mc on this machine (s10_c4_pathhome.out), and a native scan's default runs directory comes from the former unless XDG_CACHE_HOME is set to an absolute path (src/openultrasast/config.py:198-203 and :216-226). As decided. Review - Review: four rounds against 7396887, all by one reviewer subagent. Round one approved the change with seven remarks; six were carried into the test file and this message, and the seventh is the last bullet above. A probe of the reviewer started one container by mistake, against the brief: it failed at creating its run directory, and nothing of it remains (/tmp/opencode/s10_c4_after_review_docker.out). Round two approved the code and the tests on one condition, met in this message ("in every test" became "in each test that reaches the build"), and remarked that the docker stub's shebang was its second line. Round three approved the tree as it stood and remarked that the stub's inserted line depended on the shebang's text. Round four approved the final tree. Docker was not run in rounds two to four. --- ops/shell/ousast.sh | 14 +++++++++-- tests/test_shell_wrapper.py | 47 +++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/ops/shell/ousast.sh b/ops/shell/ousast.sh index b90859f2..ffea08b9 100644 --- a/ops/shell/ousast.sh +++ b/ops/shell/ousast.sh @@ -47,6 +47,18 @@ ousast() { return 1 fi + # The runs go under HOME. Both checks come before the build: on a cold one it pulls ~2 GB, and a run that + # cannot write its runs should say so first. + if [ -z "${HOME:-}" ]; then + echo "ousast: HOME is not set, so there is no ~/.cache/openultrasast/runs to write the runs to" >&2 + return 1 + fi + local runs="$HOME/.cache/openultrasast/runs" + if ! mkdir -p "$runs"; then + echo "ousast: cannot create $runs, where the runs are written" >&2 + return 1 + fi + # Build once, the first time. The run says why it waits, then the build's progress shows: on a cold build it # is the only sign that a long download is running. if ! docker image inspect "openultrasast:${OUSAST_VERSION:-dev}" >/dev/null 2>&1; then @@ -72,8 +84,6 @@ ousast() { fi done - local runs="$HOME/.cache/openultrasast/runs" - mkdir -p "$runs" # /reports is the runs directory itself here, not a reports directory with runs under it: the override puts # the run beside a native one, where the pruning of the default location finds it. TARGET="$PWD" OUSAST_REPORTS="$runs" \ diff --git a/tests/test_shell_wrapper.py b/tests/test_shell_wrapper.py index c7bb668c..351cf062 100644 --- a/tests/test_shell_wrapper.py +++ b/tests/test_shell_wrapper.py @@ -12,10 +12,10 @@ echo "{name} cwd=$PWD args=$*${{OUSAST_REPORTS:+ reports=$OUSAST_REPORTS}}" """ -def ousast(tmp_path, executables, *arguments): - """`ousast ` in tmp_path/project, with tmp_path as HOME. 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.""" +def ousast(tmp_path, executables, *arguments, without_home=False): + """`ousast ` in tmp_path/project, with tmp_path as HOME unless `without_home` is passed. 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.""" checkout = tmp_path / "checkout" checkout.mkdir() (checkout / "docker-compose.yml").write_text("services: {}\n") @@ -26,18 +26,20 @@ def ousast(tmp_path, executables, *arguments): bin_dir = tmp_path / "bin" bin_dir.mkdir() for name in ("docker", *executables): - (bin_dir / name).write_text(STUB.format(name=name)) + stub = STUB.format(name=name) + if name == "docker": + # `image inspect` fails, as for an image that is not built, so the wrapper takes its build branch. + shebang, body = stub.split("\n", 1) + stub = f'{shebang}\n[ "$1" = image ] && exit 1\n{body}' + (bin_dir / name).write_text(stub) (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(checkout), "HOME": str(tmp_path)}, - capture_output=True, - text=True, - ) + environment = {"PATH": f"{bin_dir}:/usr/bin:/bin", "OUSAST_HOME": str(checkout), "HOME": str(tmp_path)} + if without_home: + del environment["HOME"] + return subprocess.run(["bash", "-c", script], cwd=project, env=environment, capture_output=True, text=True) def test_a_native_scan_runs_in_the_callers_directory(tmp_path): @@ -68,3 +70,24 @@ def test_the_container_writes_its_runs_to_the_users_cache(tmp_path): assert words[0] == "docker" and words[-1] == f"reports={runs}" # Without the override the run lands under runs/runs, where no pruning looks. assert words.index("OPENULTRASAST_RUNS_DIR=/reports") < words.index("ousast") + + +def test_the_container_path_stops_when_home_is_not_set(tmp_path): + """With HOME unset, `$HOME/.cache` became `/.cache`: the wrapper went on to the container just the same.""" + result = ousast(tmp_path, (), "scan", ".", without_home=True) + message = "ousast: HOME is not set, so there is no ~/.cache/openultrasast/runs to write the runs to" + assert result.returncode == 1, result.stderr + assert result.stderr.splitlines() == [message] + # Neither the build nor the container is started. + assert result.stdout == "" + + +def test_a_runs_directory_that_cannot_be_made_stops_the_wrapper(tmp_path): + """`mkdir -p` was not checked: the wrapper went on to the container with a runs directory that did not exist.""" + (tmp_path / ".cache").write_text("") + result = ousast(tmp_path, (), "scan", ".") + runs = tmp_path / ".cache" / "openultrasast" / "runs" + assert result.returncode == 1, result.stderr + # mkdir says why first; the wrapper's line is the last. + assert result.stderr.splitlines()[-1] == f"ousast: cannot create {runs}, where the runs are written" + assert result.stdout == ""