Files
Open_Ultra_SAST/ops/shell/ousast.sh
T
Marius Ciepluch d8b4cac023 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 <runs>, 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.
2026-11-27 18:24:11 +01:00

99 lines
4.7 KiB
Bash

# OpenUltraSAST as a local command. Source this from ~/.bashrc or ~/.zshrc:
#
# source /path/to/OpenUltraSAST/ops/shell/ousast.sh
#
# Then use it like any other linter, from any directory:
#
# ousast scan .
# ousast scan src/api
#
# 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.
#
# The one thing worth knowing: your working directory is mounted READ-ONLY, and findings are written to
# ~/.cache/openultrasast/runs, where a native scan writes them too. That directory is mounted at /reports, the
# only writable mount, so nothing is written to the directory being scanned. A tool that reads your source to
# tell you about it has no business being able to change it.
# Resolve this script's own location so the function works from anywhere, under bash or zsh.
if [ -n "${BASH_SOURCE[0]:-}" ]; then
_OUSAST_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
elif [ -n "${(%):-%N}" ] 2>/dev/null; then
_OUSAST_HOME="$(cd "$(dirname "${(%):-%N}")/../.." && pwd)"
fi
export OUSAST_HOME="${OUSAST_HOME:-$_OUSAST_HOME}"
ousast() {
local compose="$OUSAST_HOME/docker-compose.yml"
if [ ! -f "$compose" ]; then
echo "ousast: cannot find $compose (set OUSAST_HOME to the checkout)" >&2
return 1
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. 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 and joern-parse were found" >&2
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
echo "ousast: building the analysis image once (this pulls ~2 GB of engine; later runs are instant)" >&2
( cd "$OUSAST_HOME" && docker compose build ) || return 1
fi
# Rewrite paths that live under the current directory to their mounted equivalent, so `ousast scan src/api`
# means what the user expects. Anything outside the mount is left alone and warned about rather than
# silently rewritten into a path the container cannot see.
local args=() a abs
for a in "$@"; do
if [ -e "$a" ]; then
abs="$(cd "$(dirname "$a")" 2>/dev/null && pwd)/$(basename "$a")"
case "$abs" in
"$PWD") args+=("/target") ;;
"$PWD"/*) args+=("/target${abs#"$PWD"}") ;;
*) echo "ousast: $a is outside $PWD and is not mounted; passing it through unchanged" >&2
args+=("$a") ;;
esac
else
args+=("$a")
fi
done
# /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" \
OUSAST_NETWORK="${OUSAST_NETWORK:-none}" \
docker compose -f "$compose" run --rm -e OPENULTRASAST_RUNS_DIR=/reports ousast "${args[@]}"
}
# The LLM judge needs the network and a key; without them the model layer still entails and only the
# `suspicion` band goes unasked. This makes that opt-in explicit rather than a surprise.
ousast-with-judge() {
OUSAST_NETWORK=bridge ousast "$@"
}