From 06c9b3c8e3619a50f8b3033915f3f395ce84e99e Mon Sep 17 00:00:00 2001 From: opencode Date: Tue, 10 Nov 2026 08:34:52 -0600 Subject: [PATCH] fix(lib): bitwarden-load-secrets validates value options before parsing (#6932, #7765) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #6932 fast-fail guard was added to the test file only — commit b4f1602edb touched scripts/tests/test_bitwarden_load_secrets.py and never lib/bitwarden-load-secrets.sh. The four contracts have been red on main ever since (scripts-test, `basic: scripts-test (3.12/3.14)`: failures=2, errors=5), and every run of the parse loop still hangs: ./lib/bitwarden-load-secrets.sh --item # infinite loop `--item` takes `$2` and does `shift 2`; on the last argument shift 2 cannot advance $#, so `while [[ $# -gt 0 ]]` never ends. The vault is never reached, so the hang is in argv handling alone — a 10 s test timeout, not a credential wait. `--item --bws` swallowed the next flag as the item name and ran in vault mode, and `--item ""` fetched an empty name while still exiting 0 in some paths. Validate argv once, before the parse loop, over the six value-taking options (--item, --field, --secret, --project, --prefix, --format): a missing, empty or flag-like value is a usage error naming the option. Executed mode exits 1; sourced mode returns 1 so the caller's shell survives (an unguarded `exit` would kill it — the reason die() branches on is_sourced()). Verified: the eight-test #5650/#6932 suite goes 0.45 s, 8/8 pass (was 50 s, 2 failures + 5 timeout errors); shellcheck -S warning clean; all six malformed argv shapes exit 1 with the option named; both valid vault and Secret Manager stubs still load their multiline values. --- lib/bitwarden-load-secrets.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/bitwarden-load-secrets.sh b/lib/bitwarden-load-secrets.sh index 01c2f1ec29..7087183bc3 100755 --- a/lib/bitwarden-load-secrets.sh +++ b/lib/bitwarden-load-secrets.sh @@ -36,6 +36,26 @@ VAR_PREFIX="" FORMAT="" AUTO_ENV=true +# Every value-taking option must be followed by a real value. A trailing +# option used to hang the parse loop forever: `shift 2` on the last +# argument cannot advance past it, so `$#` never reached 0. A flag-like +# value was silently swallowed as the item name (`--item --bws` ran in +# vault mode) and an empty value fetched an empty name (#6932). Validate +# argv once, before anything reads it. +ARGS_TO_VALIDATE=("$@") +for ((i = 0; i < ${#ARGS_TO_VALIDATE[@]}; i++)); do + case "${ARGS_TO_VALIDATE[i]}" in + --item | --field | --secret | --project | --prefix | --format) + NEXT_VALUE="${ARGS_TO_VALIDATE[i + 1]-}" + if [[ -z "$NEXT_VALUE" || "$NEXT_VALUE" == -* ]]; then + echo "Error: Option ${ARGS_TO_VALIDATE[i]} requires a value" >&2 + # `exit` would kill the caller's shell when sourced. + if is_sourced; then return 1; else exit 1; fi + fi + ;; + esac +done + # Parse arguments while [[ $# -gt 0 ]]; do case "$1" in