Files
Open_Ultra_SAST/ops/shell/ousast.sh
T
Marius Ciepluch efa311a112 fix(shell): the wrapper passes the retention days into the image (item 5b)
The maintainer's second decision on answer 7, taken on 2026-11-27 while the scan path
change (312c09b) was reviewed. Put with the question tool, in this question: "Answer 7
... is built, measured and reviewed, with one consequence the decision does not cover.
The wrapper passes only OPENULTRASAST_RUNS_DIR into the image, never
OPENULTRASAST_RUNS_RETENTION_DAYS. ... How should the retention reach the image?" The
option chosen, in full: "Pass it through (Recommended)": "The wrappers pass
OPENULTRASAST_RUNS_RETENTION_DAYS into the container: one more -e in
ops/shell/ousast.sh and ops/shell/ousast.ps1, a test first. The scan in the image then
honours the host's setting, 0 included. Measured: compose passes a variable named
without a value only when the host sets it. This commit stays the scan path alone; the
wrapper is the next commit." Not chosen: "Document it", "Hold answer 7".

What changed.
- ops/shell/ousast.sh: the compose run passes -e OPENULTRASAST_RUNS_RETENTION_DAYS,
  without a value, beside the runs directory's override; the comment above it says why.
- ops/shell/ousast.ps1: the same option, edited by reading: no PowerShell is installed
  here.
- tests/test_shell_wrapper.py: test_the_container_path_passes_the_retention_into_the_image.
- tests/test_run_pruning.py: the guard test of 312c09b pinned one text,
  "-e OPENULTRASAST_RUNS_DIR=/reports ousast". With the new option between the two it
  now pins the word OPENULTRASAST_RUNS_DIR=<WRAPPER_RUNS_MOUNT> wherever it stands in
  the wrapper's text.

Measured, through the wrapper function itself. The script is /tmp/opencode/s10_c8_probe.sh
and its files are under /tmp/opencode/s10_c8_probe/, one per measurement; those of the
wrapper runs start with the command and then with the line that ran. bash sources the
working tree's ops/shell/ousast.sh and runs `ousast scan examples --mode quick` with
HOME at a scratch directory, so the runs directory the wrapper mounts at /reports is a
scratch one; the real ~/.cache/openultrasast/runs was not touched
(/tmp/opencode/s10_c8_real_runs.out). The image is openultrasast:dev (3f959b31f2f2) with
the working tree's package copied over it, under a tag that OUSAST_VERSION gives to
compose, so the wrapper finds an image and does not build. Network off. No key is in the
shell's environment; compose reads the checkout's .env by default, and that file names
both keys, which docker-compose.yml:44-45 pass on, so the containers may have had them:
not looked at, and no measurement depends on it (quick mode, network none). Compose
5.5.1. Each scratch runs directory held a run of 40 days ago, a run of 5 days ago and a
directory called notes. The stamps run from 18:54:48Z to 18:54:58Z.
- Retention unset on the host (10_changed_unset.out): "notice: removed 1 run older
  than 30 days from /reports"; the expired run is gone, the run of 5 days ago, the new
  run and notes remain (11_changed_unset_after.out).
- Retention 0 on the host (20_changed_zero.out, 21_changed_zero_after.out): no notice;
  the expired run stays, with the run of 5 days ago, the new run and notes.
- Retention 3 on the host (30_changed_three.out): "notice: removed 2 runs older
  than 3 days from /reports"; both old runs are gone (31_changed_three_after.out).
- The control: HEAD's wrapper (312c09b) with retention 0 on the host (40_head_zero.out):
  "notice: removed 1 run older than 30 days from /reports". That is the gap this
  commit closes (41_head_zero_after.out).
- What the container sees of the variable under the wrapper's two options, asked of the
  image's Python (50_variable_unset.out, 51_variable_zero.out, 52_variable_empty.out):
  unset, "set to 0" and empty, as on the host. An empty value reads as not set
  (config.py, _runs_retention_days).
- The wrapper the probe ran is the tree's, and the package it was built from equals the
  tree's (03_probe_sources.out is a bare list of digests).
- The probe image is removed (90_remove_probe.out); no container remains
  (91_containers_after.out), and openultrasast:dev alone is left (92_images_after.out).

Tests. RED: an archive of HEAD (src, ops, tests, pyproject.toml) with the new test, the
package imported from the archive and the wrapper HEAD's (s10_c8_red2.out: 1 failed,
6 passed, at "ValueError: 'OPENULTRASAST_RUNS_RETENTION_DAYS' is not in list").
GREEN on the final tree: 78 passed over tests/test_shell_wrapper.py,
tests/test_run_pruning.py, tests/test_task_images.py, tests/test_docs_site.py and
tests/test_removed_plane_references.py (s10_c8_green4.out; the command is not in the
file). Mutants, the tree with one line changed, run on the two wrapper and pruning
test files (the copies are /tmp/opencode/s10_c8_mutB and s10_c8_mutD):
- WRAPPER_RUNS_MOUNT = "/reports2" fails the guard and the first test of 312c09b
  (s10_c8_mutB2.out: 2 failed, 41 passed);
- the wrapper's word changed to OPENULTRASAST_RUNS_DIR=/reports2 fails the guard and
  the wrapper test of d1f61fb (s10_c8_mutD2.out: 2 failed, 41 passed).
Two earlier GREEN runs failed on the guard as it was (s10_c8_green.out,
s10_c8_green2.out): that is the change to tests/test_run_pruning.py.

Checks on the final tree: ruff format, ruff check, mypy, compileall and the three gates
pass, and the gates print what they printed before the change (s10_c8b_exits.txt);
bash -n on ops/shell/ousast.sh exits 0.

- Review: two rounds by a reviewer subagent, reading and re-running, no docker. Round one
  approved the four files and conditioned the commit on the message: "Network off, no
  key" was unsupported, since compose reads the checkout's .env, which names both keys,
  and the review line was a placeholder. Round two checked the tree after the optional
  remarks were taken (the guard takes the word exactly; the shell command is on two
  lines) and found the code right: GREEN 78 passed again, both mutants 2 failed and 41
  passed, the probe's second run read as stated. One condition remained: the stamps at
  line 39, 18:54:48Z to 18:54:58Z.

By reading, not measured.
- ops/shell/ousast.ps1: no PowerShell is installed here.
- No page says what the wrapper passes into the image, so none needed a change:
  README.md and docs/examples.md say that OPENULTRASAST_RUNS_RETENTION_DAYS sets the
  days and that 0 keeps every run, and through the wrapper that is now true.
- A value the host sets that is not a whole number of days now stops the scan in the
  image with the message a native scan gives (config.py, _runs_retention_days); before
  it did not reach the image.

Not checked: the whole test suite; Windows and ops/shell/ousast.ps1; whether the keys
of the checkout's .env reached the probe's containers.
2026-11-27 19:59:50 +01:00

102 lines
4.9 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 scan in the image prunes the expired runs as a native scan does. The
# retention goes in without a value: compose sets it in the container only when the host has it, so a host
# that keeps every run with 0 keeps them through the wrapper too.
TARGET="$PWD" OUSAST_REPORTS="$runs" \
OUSAST_NETWORK="${OUSAST_NETWORK:-none}" \
docker compose -f "$compose" run --rm -e OPENULTRASAST_RUNS_DIR=/reports \
-e OPENULTRASAST_RUNS_RETENTION_DAYS 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 "$@"
}