mirror of
https://github.com/trailofbits/skills.git
synced 2026-09-14 14:28:48 +08:00
c199e0cc7d
* Narrow the modern-python shims to the commands uv run replaces Closes #207. The shims sit on PATH, so they intercept every subprocess any tool spawns, not just what Claude types. Two of the intercepted invocations were not package management at all, and blocking them broke real tooling. `uv pip` now passes through when it carries --project, --directory or --target. Those say a tool is building an environment it owns, where `uv add` is not the available advice: prek installs every hook with `uv pip install --project / --directory <cache>`, so the refusal made `git commit` fail in any repo whose hooks need a Python environment. A bare `uv pip install requests` is still refused. `python -c`, `python -m <module>` and `python -` now reach the real interpreter. None of them resolves a script against a project's dependencies, which is what `uv run` exists to do, and `uv run python3 -` is not a drop-in replacement inside a pipeline. `python -m pip` stays intercepted, as do bare `python` and `python script.py`. Passing anything through is new for the python shim, which previously ended every branch in exit 1, so it gains the same skip-my-own-dir PATH walk the uv shim already had. That walk now uses parameter expansion rather than basename, because the one case where it must report failure is a PATH holding nothing but the shim, where shelling out to coreutils fails first with a confusing error. Verified by A/B on the two symptoms #207 reports, running each suite against the old shim and the new one: - zeroize-audit's rust-regression smoke test: FAILED at line 72 before, "Rust regression smoke checks passed." after. - prek hook installation from a cold cache: refused before, "check json Passed" after. bats goes from 19 cases to 38. Five python cases inverted rather than being deleted: the ones asserting that -c and -m are refused now assert they run. AGENTS.md's note on `make shell-suites` is corrected rather than removed — the #207 interceptions are gone, but the target still fails because variant-analysis invokes `python3 <script>.py`, which the shim intercepts by design. That one belongs to variant-analysis. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Decide on the mode selector, not on argument position Two gaps in the narrowing, both from review. `uv pip install --help` documents `-t, --target <TARGET>`, so the short form has to be exempt alongside the long one. Without it the same tool-managed install was allowed or refused depending on spelling. The python shim read only $1 to find the mode selector, so `python -u -c 'code'` was refused while `python -c 'code'` ran, even though they are the same invocation. It now steps over interpreter flags to find the selector, giving `-W`, `-X` and `--check-hash-based-pycs` the two slots they take. `-u -m pip` is still refused, and so is `-u script.py`: a script path is what `uv run` replaces regardless of what precedes it. bats 38 -> 43. Both #207 regressions re-verified after the restructure: zeroize-audit's smoke test passes and prek installs hooks from a cold cache. Not fixed here, deliberately: `uv --no-progress pip install requests` still slips past the refusal, because the subcommand check reads $1 as well. Parsing that correctly means knowing which uv global flags take a value, and getting it wrong would refuse a command that works today. The failure mode is a missed nudge rather than a breakage — the real uv runs and behaves correctly — so it does not belong in a change whose purpose is to refuse less. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
103 lines
3.4 KiB
Bash
Executable File
103 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# PATH shim for python/python3 — intercepts the invocations `uv run` replaces and
|
|
# passes the rest through to the real interpreter. Works for both names via $0.
|
|
#
|
|
# Parameter expansion rather than basename/dirname on purpose: this shim has to work
|
|
# when PATH holds nothing but its own directory, which is exactly the case where it
|
|
# must report that no real interpreter was found. Shelling out to coreutils there
|
|
# fails first, with a confusing error about basename.
|
|
cmd="${0##*/}"
|
|
|
|
# What is intercepted, and what is not (#207):
|
|
#
|
|
# `python` and `python script.py` are what `uv run` exists to replace — they resolve a
|
|
# script against a project's dependencies, and running them bare gets the system
|
|
# interpreter with none of them.
|
|
#
|
|
# `-c`, `-m` and `-` are not that. They read a program from the command line, a module
|
|
# already on the path, or stdin, and none of them resolves a script's dependencies.
|
|
# `uv run python3 -` is also not a drop-in replacement inside a pipeline, so redirecting
|
|
# it there broke real scripts — zeroize-audit's smoke test among them.
|
|
#
|
|
# `-m pip` stays intercepted: that IS package management, and it is the foot-gun.
|
|
|
|
# Hand off to the real interpreter, skipping this shim's directory in PATH.
|
|
exec_real() {
|
|
local shim_dir path_entries dir resolved
|
|
shim_dir="$(cd "${0%/*}" && pwd)"
|
|
IFS=: read -ra path_entries <<<"${PATH:-}"
|
|
for dir in "${path_entries[@]}"; do
|
|
resolved="$(cd "$dir" 2>/dev/null && pwd)" || continue
|
|
[[ "$resolved" == "$shim_dir" ]] && continue
|
|
if [[ -x "$dir/$cmd" ]]; then
|
|
exec "$dir/$cmd" "$@"
|
|
fi
|
|
done
|
|
echo "ERROR: real $cmd binary not found on PATH" >&2
|
|
exit 127
|
|
}
|
|
|
|
# Canonical rationale for the suggestion's shape (the README,
|
|
# setup-shims.sh, and python-shim.bats point here):
|
|
#
|
|
# Suggestions always use the exact name `python`, never `python3`: uv
|
|
# special-cases the `python` command (uv >= 0.4.0) and executes its resolved
|
|
# interpreter directly instead of a PATH lookup, so the suggested command
|
|
# works even outside a project, where `uv run python3` would resolve back
|
|
# to this shim.
|
|
#
|
|
# Arguments are requoted with %q so the suggestion stays runnable when they
|
|
# contain spaces or shell metacharacters.
|
|
args=""
|
|
if (($#)); then
|
|
args="$(printf ' %q' "$@")"
|
|
fi
|
|
|
|
# Find the mode selector, stepping over any interpreter flags in front of it. Reading
|
|
# only $1 would make the decision depend on argument order: `python -u -c 'code'` means
|
|
# exactly what `python -c 'code'` means, and refusing one while allowing the other is an
|
|
# accident, not a rule. `-W`, `-X` and `--check-hash-based-pycs` take a separate value,
|
|
# so they consume two slots; everything else beginning with `-` consumes one.
|
|
mode=""
|
|
argv=("$@")
|
|
i=0
|
|
while ((i < ${#argv[@]})); do
|
|
case "${argv[i]}" in
|
|
-c | -m | -)
|
|
mode="${argv[i]}"
|
|
break
|
|
;;
|
|
-W | -X | --check-hash-based-pycs)
|
|
((i += 2))
|
|
;;
|
|
-*)
|
|
((i += 1))
|
|
;;
|
|
*)
|
|
# A script path. This is the case `uv run` exists to replace.
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "$mode" in
|
|
-m)
|
|
if [[ "${argv[i + 1]:-}" == "pip" ]]; then
|
|
echo "ERROR: \`$cmd -m pip\` is not supported. Use:" >&2
|
|
echo " uv add <package> # add a dependency" >&2
|
|
echo " uv remove <package> # remove a dependency" >&2
|
|
exit 1
|
|
fi
|
|
exec_real "$@"
|
|
;;
|
|
-c | -)
|
|
exec_real "$@"
|
|
;;
|
|
*)
|
|
echo "ERROR: Use \`uv run python$args\` instead of \`$cmd$args\`" >&2
|
|
exit 1
|
|
;;
|
|
esac
|