pii-scan: refuse an invalid pattern instead of scanning clean, and read '+++' as a header only after '---'

review-pii-guard N2 (a broken private regex made grep exit 2 and the scan
report clean) and N3 (an added line starting '++ ' was taken for a file
header). Tests for both; the env README names BRIDGE_ID_KEY and
BRIDGE_SECRET_KEY.
This commit is contained in:
acamilo 2026-09-29 16:55:48 +00:00
parent 05324293ee
commit 2c8596eaa2
3 changed files with 24 additions and 5 deletions

4
infra/env/README.md vendored
View file

@ -53,6 +53,8 @@ requires `CTID`, `HOSTNAME`, `IP`, `GAME` and `PUSH_TARGET`; `ROLE` defaults to
Secrets. `PASS_KEY` holds the **name** of a `pass` entry, never a key. The Twitch app Secrets. `PASS_KEY` holds the **name** of a `pass` entry, never a key. The Twitch app
id/secret arrive as the systemd credential `twitch-app`, installed by id/secret arrive as the systemd credential `twitch-app`, installed by
`infra/06-secrets.sh` from `pass` on the operator box; the OAuth tokens live in the `infra/06-secrets.sh` from `pass` on the operator box, reading the two entry **names** from
`BRIDGE_ID_KEY` and `BRIDGE_SECRET_KEY` in the real env file (it skips `twitch-app` with a
warning when they are unset); the OAuth tokens live in the
container as `/var/lib/flybridge/tokens.json`, mode 0600. `ROM_SHA256` is a digest, and container as `/var/lib/flybridge/tokens.json`, mode 0600. `ROM_SHA256` is a digest, and
the ROM it describes is never committed, copied into the repo, or shown on stream. the ROM it describes is never committed, copied into the repo, or shown on stream.

View file

@ -168,5 +168,13 @@ git -C "$W" checkout -q main
cp "$T/other-hook" "$W/.git/hooks/pre-push" cp "$T/other-hook" "$W/.git/hooks/pre-push"
expect 1 "install-hooks refuses to clobber a foreign hook" -- "$W/tools/install-hooks.sh" expect 1 "install-hooks refuses to clobber a foreign hook" -- "$W/tools/install-hooks.sh"
# --- review-pii-guard N2/N3 (2026-09-29) ----------------------------------------------
printf 'broken - (unclosed\n' > "$T/private/bad-patterns.txt"
expect 2 "an invalid private regex refuses to scan (never 'clean')" -- env FLY_PII_PATTERNS="$T/private/bad-patterns.txt" "$SCAN" --tree
printf '++ %s\n' "$LAN" > "$W/src/plusplus.txt"
git -C "$W" add -A && git -C "$W" commit -qm "a line that starts with ++"
expect 1 "an added line starting '++ ' is still scanned" -- "$SCAN" --range HEAD~1..HEAD
git -C "$W" rm -qf src/plusplus.txt && git -C "$W" commit -qm "drop it"
echo "===" echo "==="
if [ "$FAILS" -eq 0 ]; then echo "pii-scan-test: ALL PASSED"; else echo "pii-scan-test: ${FAILS} FAILED"; exit 1; fi if [ "$FAILS" -eq 0 ]; then echo "pii-scan-test: ALL PASSED"; else echo "pii-scan-test: ${FAILS} FAILED"; exit 1; fi

View file

@ -73,7 +73,15 @@ trap 'rm -rf "$TMP"' EXIT
# itself; they are not proper nouns. # itself; they are not proper nouns.
# -------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------
NAMES=(); FLAGS=(); RES=(); KIND=() NAMES=(); FLAGS=(); RES=(); KIND=()
pat() { NAMES+=("$1"); FLAGS+=("$2"); RES+=("$3"); KIND+=(re); } # Every pattern is compiled once up front: grep exits 2 on a bad regex, and a scan that
# silently skipped a broken private pattern would report "clean" (fail open).
pat() {
local rc=0 f=-E
[ "$2" = -i ] && f=-iE
grep $f -e "$3" </dev/null >/dev/null 2>&1 || rc=$?
[ "$rc" -le 1 ] || die "pattern '$1' is not a valid extended regex; refusing to scan"
NAMES+=("$1"); FLAGS+=("$2"); RES+=("$3"); KIND+=(re)
}
# name flags extended regex # name flags extended regex
pat lan-address -i '\b192\.168\.[0-9]{1,3}\.[0-9]{1,3}\b|\b2600:[0-9a-f]' pat lan-address -i '\b192\.168\.[0-9]{1,3}\.[0-9]{1,3}\b|\b2600:[0-9a-f]'
@ -234,9 +242,10 @@ scan_range() {
git -C "$REPO_ROOT" log -p --no-color --no-ext-diff --no-renames --unified=0 \ git -C "$REPO_ROOT" log -p --no-color --no-ext-diff --no-renames --unified=0 \
--format='PIISCAN-COMMIT %H' "$@" -- . ':!data' ':!package-lock.json' \ --format='PIISCAN-COMMIT %H' "$@" -- . ':!data' ':!package-lock.json' \
| awk -v corpus="$corpus" -v index_f="$index" ' | awk -v corpus="$corpus" -v index_f="$index" '
/^PIISCAN-COMMIT / { commit = $2; path = ""; next } /^PIISCAN-COMMIT / { commit = $2; path = ""; hdr = 0; next }
/^\+\+\+ / { path = substr($0, 5); sub(/^b\//, "", path); if (path == "/dev/null") path = ""; next } /^--- / { hdr = 1; next }
/^@@ / { if (match($0, /\+[0-9]+/)) ln = substr($0, RSTART + 1, RLENGTH - 1) + 0; next } /^\+\+\+ / && hdr { path = substr($0, 5); sub(/^b\//, "", path); if (path == "/dev/null") path = ""; hdr = 0; next }
/^@@ / { hdr = 0; if (match($0, /\+[0-9]+/)) ln = substr($0, RSTART + 1, RLENGTH - 1) + 0; next }
/^\+/ { if (path != "") { print substr($0, 2) > corpus; print commit "\t" path "\t" ln > index_f }; ln++; next } /^\+/ { if (path != "") { print substr($0, 2) > corpus; print commit "\t" path "\t" ln > index_f }; ln++; next }
' '
touch "$corpus" "$index" touch "$corpus" "$index"