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>
144 lines
4.6 KiB
Bash
144 lines
4.6 KiB
Bash
#!/usr/bin/env bats
|
|
# Tests for python/python3 PATH shim
|
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
SHIM="${BATS_TEST_DIRNAME}/python"
|
|
SHIM3="${BATS_TEST_DIRNAME}/python3"
|
|
|
|
# Pass-through cases need to reach a real interpreter. /usr/bin holds python3 on both
|
|
# macOS and the CI image, and pinning PATH to it keeps the test off any shim that
|
|
# happens to be installed on the machine running this — including an older copy of
|
|
# this very shim, which would otherwise answer instead of the real binary.
|
|
REAL_PATH="/usr/bin:/bin"
|
|
|
|
real_python3_or_skip() {
|
|
[[ -x /usr/bin/python3 ]] || skip "no /usr/bin/python3 to pass through to"
|
|
}
|
|
|
|
# --------------------------------------------------------------- still intercepted
|
|
|
|
@test "exits non-zero for bare python" {
|
|
run "$SHIM"
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv run python"* ]]
|
|
}
|
|
|
|
@test "exits non-zero for python script.py" {
|
|
run "$SHIM" script.py
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv run python script.py"* ]]
|
|
}
|
|
|
|
@test "exits non-zero for python -m pip install" {
|
|
run "$SHIM" -m pip install requests
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv add"* ]]
|
|
[[ "$output" == *"uv remove"* ]]
|
|
}
|
|
|
|
@test "python3 -m pip suggests uv add" {
|
|
run "$SHIM3" -m pip install foo
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv add"* ]]
|
|
}
|
|
|
|
@test "works when invoked as python3 via symlink" {
|
|
run "$SHIM3"
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *'instead of `python3'* ]]
|
|
}
|
|
|
|
# The suggestion must use the exact name `python`, never `python3`; see
|
|
# the header comment in ./python for the full rationale.
|
|
@test "suggests exact 'uv run python', not python3, when invoked as python3" {
|
|
run "$SHIM3" script.py
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"Use \`uv run python script.py\`"* ]]
|
|
[[ "$output" != *"uv run python3"* ]]
|
|
}
|
|
|
|
@test "bare invocation suggests uv run python without trailing space" {
|
|
run "$SHIM"
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"Use \`uv run python\` instead of \`python\`"* ]]
|
|
}
|
|
|
|
# %q output can differ across bash versions, so build the expectation with
|
|
# the same requoting the shim uses, after checking it actually escapes.
|
|
@test "suggestion requotes a script path so it stays copy-paste runnable" {
|
|
run "$SHIM" 'my script.py'
|
|
[[ $status -ne 0 ]]
|
|
quoted="$(printf '%q' 'my script.py')"
|
|
[[ "$quoted" != 'my script.py' ]]
|
|
[[ "$output" == *"Use \`uv run python $quoted\`"* ]]
|
|
}
|
|
|
|
# ------------------------------------------------------------- deliberately allowed
|
|
#
|
|
# None of these resolves a script against a project's dependencies, which is the thing
|
|
# `uv run` exists to do, so redirecting them was wrong. See #207 and ./python's header.
|
|
|
|
@test "python -c runs the code instead of refusing" {
|
|
real_python3_or_skip
|
|
run env PATH="$REAL_PATH" "$SHIM3" -c 'print(1+1)'
|
|
[[ $status -eq 0 ]]
|
|
[[ "$output" == "2" ]]
|
|
}
|
|
|
|
@test "python -m <module> reaches the module instead of refusing" {
|
|
real_python3_or_skip
|
|
run env PATH="$REAL_PATH" "$SHIM3" -m json.tool --help
|
|
[[ $status -eq 0 ]]
|
|
[[ "$output" == *"json.tool"* ]]
|
|
}
|
|
|
|
@test "python - reads the program from stdin" {
|
|
real_python3_or_skip
|
|
run bash -c "echo 'print(3+3)' | env PATH='$REAL_PATH' '$SHIM3' -"
|
|
[[ $status -eq 0 ]]
|
|
[[ "$output" == "6" ]]
|
|
}
|
|
|
|
# An interpreter flag before the mode selector must not change the decision: `-u -c` is
|
|
# the same invocation as `-c`. Reading only $1 made the answer depend on argument order.
|
|
|
|
@test "an interpreter flag before -c does not resurrect the refusal" {
|
|
real_python3_or_skip
|
|
run env PATH="$REAL_PATH" "$SHIM3" -u -c 'print(1+1)'
|
|
[[ $status -eq 0 ]]
|
|
[[ "$output" == "2" ]]
|
|
}
|
|
|
|
@test "a value-taking interpreter flag before -c is stepped over correctly" {
|
|
real_python3_or_skip
|
|
run env PATH="$REAL_PATH" "$SHIM3" -X utf8 -c 'print(1+1)'
|
|
[[ $status -eq 0 ]]
|
|
[[ "$output" == "2" ]]
|
|
}
|
|
|
|
@test "a flag before a script path still refuses" {
|
|
run "$SHIM3" -u script.py
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv run python"* ]]
|
|
}
|
|
|
|
@test "a flag before -m pip still refuses" {
|
|
run "$SHIM3" -u -m pip install foo
|
|
[[ $status -ne 0 ]]
|
|
[[ "$output" == *"uv add"* ]]
|
|
}
|
|
|
|
@test "exits 127 with error when the real interpreter is not found" {
|
|
# A PATH holding the shim and nothing else cannot test this: the shebang is
|
|
# `/usr/bin/env bash`, so env exits 127 looking for bash and the shim never runs —
|
|
# the right status for the wrong reason. Give it bash and no python3.
|
|
local only_bash="$BATS_TEST_TMPDIR/only-bash"
|
|
mkdir -p "$only_bash"
|
|
ln -sf "$(command -v bash)" "$only_bash/bash"
|
|
[[ ! -x "$only_bash/python3" ]]
|
|
|
|
run -127 env PATH="${BATS_TEST_DIRNAME}:$only_bash" "$SHIM3" -c 'print(1)'
|
|
[[ "$output" == *"real python3 binary not found"* ]]
|
|
}
|