Skip to content

Commit ee5f6e2

Browse files
fix(scripts): use mktemp for temp paths (closes 3 panic-attack path-traversal findings) (#185)
## Summary Three scripts wrote to or read from hard-coded `/tmp/*` paths — panic-attack flags as path-traversal / TOCTOU low (any local user on a shared CI or workstation can race the file between write and read, or pre-create a symlink to clobber the target). - `scripts/balance_corpus.sh` — write listing to `$(mktemp)` + trap rm - `scripts/balance_corpus_fast.sh` — write listing to `$(mktemp)` + trap rm - `scripts/gen-provers-a2ml.sh` — accept variant-list path as `$1` (was hard-coded `/tmp/provers-list.txt`); callers should pass `$(mktemp)` or any path they control. Adds usage banner + early-exit on missing/unreadable arg. ## Test plan - [x] `bash -n` syntax-check passes on all three scripts - [x] `gen-provers-a2ml.sh` against a populated `$(mktemp)` emits the expected `[prover.<Variant>]` blocks with snake_case slugs and correct `count = N` header - [x] empty-arg invocation prints usage to stderr and exits 2 instead of silently producing `count = 0` - [ ] CI green (rust-ci / governance / scans) Does not touch in-flight PRs (#176/#178/#179/#182 — none of them edit the scripts directory). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 88cd8dc commit ee5f6e2

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

scripts/balance_corpus.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ fi
2424
REPLICA_DIR="$CORPUS_DIR/replica"
2525
mkdir -p "$REPLICA_DIR"
2626

27-
# Get a list of all files with the specified extension
28-
find "$CORPUS_DIR" -name "*$EXTENSION" > /tmp/corpus_files.txt
27+
# Get a list of all files with the specified extension.
28+
# Use mktemp for the listing file: predictable /tmp/* paths are a
29+
# panic-attack low (path-traversal / TOCTOU on multi-user runners).
30+
CORPUS_LIST="$(mktemp)"
31+
trap 'rm -f "$CORPUS_LIST"' EXIT
32+
find "$CORPUS_DIR" -name "*$EXTENSION" > "$CORPUS_LIST"
2933

3034
# Read the list into an array
31-
mapfile -t FILES < /tmp/corpus_files.txt
35+
mapfile -t FILES < "$CORPUS_LIST"
3236

3337
# Replicate files in batches to avoid timeout
3438
echo "Starting replication of $FILES_TO_ADD files for $CORPUS_DIR..."

scripts/balance_corpus_fast.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ fi
2424
REPLICA_DIR="$CORPUS_DIR/replica"
2525
mkdir -p "$REPLICA_DIR"
2626

27-
# Get a list of all files with the specified extension
28-
find "$CORPUS_DIR" -name "*$EXTENSION" > /tmp/corpus_files.txt
27+
# Get a list of all files with the specified extension.
28+
# Use mktemp for the listing file: predictable /tmp/* paths are a
29+
# panic-attack low (path-traversal / TOCTOU on multi-user runners).
30+
CORPUS_LIST="$(mktemp)"
31+
trap 'rm -f "$CORPUS_LIST"' EXIT
32+
find "$CORPUS_DIR" -name "*$EXTENSION" > "$CORPUS_LIST"
2933

3034
# Read the list into an array
31-
mapfile -t FILES < /tmp/corpus_files.txt
35+
mapfile -t FILES < "$CORPUS_LIST"
3236

3337
# Replicate files in smaller batches to avoid timeout
3438
echo "Starting replication of $FILES_TO_ADD files for $CORPUS_DIR..."

scripts/gen-provers-a2ml.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
#!/bin/bash
22
# Generate provers.a2ml from a variant list.
33
# Snake-case conversion: PascalCase -> snake_case (matches serde default).
4+
#
5+
# Usage: gen-provers-a2ml.sh <variant-list-file>
6+
#
7+
# The variant-list path is a CLI argument (was the hard-coded
8+
# /tmp/provers-list.txt); predictable /tmp/* paths are a panic-attack
9+
# low (path-traversal / TOCTOU on multi-user runners). Callers should
10+
# pass `$(mktemp)` or any path they control.
411
set -euo pipefail
512

13+
VARIANT_LIST="${1:-}"
14+
if [ -z "$VARIANT_LIST" ] || [ ! -r "$VARIANT_LIST" ]; then
15+
echo "usage: $0 <variant-list-file>" >&2
16+
echo " (one PascalCase variant per line; e.g. \"$(mktemp)\" populated by caller)" >&2
17+
exit 2
18+
fi
19+
620
cat << 'HEADER'
721
# SPDX-License-Identifier: MPL-2.0
822
# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
@@ -28,7 +42,7 @@ source = "src/rust/provers/mod.rs::ProverKind"
2842
date = "2026-04-24"
2943
HEADER
3044

31-
printf "count = %d\n\n" "$(wc -l < /tmp/provers-list.txt)"
45+
printf "count = %d\n\n" "$(wc -l < "$VARIANT_LIST")"
3246

3347
while IFS= read -r variant; do
3448
# snake_case: lowercase + insert underscore before uppercase letter that
@@ -38,4 +52,4 @@ while IFS= read -r variant; do
3852
echo "[prover.${variant}]"
3953
echo "slug = \"${slug}\""
4054
echo ""
41-
done < /tmp/provers-list.txt
55+
done < "$VARIANT_LIST"

0 commit comments

Comments
 (0)