From acb682fe5c8162a78883ad272dd94a195debd4ee Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:36:43 -0600 Subject: [PATCH] lib(sops): make sops_encrypt and bitwarden-to-sops init writes atomic (#6555) sops_encrypt wrote ciphertext with a plain redirect: the shell truncated the target before sops ran, so a failed encryption destroyed the previous ciphertext, and the unconditional success echo masked the failure with status 0 in both the script and sourced forms. The bitwarden-to-sops init path had the same redirect shape: set -e stopped the script but the zero-byte target it created routed every later run to the sops set update branch instead of retrying init. Both writers now follow the lib/bws-to-sops.sh oracle: encrypt to a mktemp file beside the target, require sops exit 0 and non-empty output, then mv over the target. sops_encrypt returns non-zero and prints no success message on failure; a failed init leaves no file at the target so the next run retries init. Regression coverage: lib/sops-atomic-writers.test.sh stubs sops (fail/ok) and bws, and asserts byte-identity of a pre-existing target after failure, non-zero status in script and sourced forms, replacement on success, and no target file after a failed init. 23/23 pass on the fix; the suite fails 7 checks against the pre-fix files. Wired into scripts-test.yml. --- .github/workflows/scripts-test.yml | 2 + lib/bitwarden-to-sops.sh | 14 ++- lib/sops-atomic-writers.test.sh | 142 +++++++++++++++++++++++++++++ lib/sops_encrypt.sh | 27 +++++- 4 files changed, 183 insertions(+), 2 deletions(-) create mode 100755 lib/sops-atomic-writers.test.sh diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index 05af993466..a8e761acc9 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -38,3 +38,5 @@ jobs: run: python3 -m unittest discover -s scripts/tests -p 'test_bitwarden_load_secrets.py' -v - name: Run power-guard timing validation tests (#6477) run: python3 -m unittest discover -s nixos/services/power-guard -p 'test_*.py' -v + - name: Run sops atomic writers regression tests (#6555) + run: bash lib/sops-atomic-writers.test.sh diff --git a/lib/bitwarden-to-sops.sh b/lib/bitwarden-to-sops.sh index c0e25959f5..67a330b40b 100755 --- a/lib/bitwarden-to-sops.sh +++ b/lib/bitwarden-to-sops.sh @@ -367,7 +367,19 @@ if [ ! -f "$TARGET_FILE" ]; then fi done <<<"$PAIRS" - sops -e --filename-override "$TARGET_FILE" <(echo -n "$TMP_YAML") >"$TARGET_FILE" + # Encrypt to a temp file beside the target and rename it into place only + # after sops exits 0 with non-empty output (#6555). Writing with + # `>"$TARGET_FILE"` created the target before sops ran, so a failed first + # encryption left a zero-byte file behind; every later run then took the + # `sops set` update branch below instead of retrying this init branch. + # Temp-then-rename follows lib/bws-to-sops.sh. + TMP_ENC="$(mktemp "$(dirname "$TARGET_FILE")/.bitwarden-to-sops.XXXXXX")" + if ! sops -e --filename-override "$TARGET_FILE" <(echo -n "$TMP_YAML") >"$TMP_ENC" || [ ! -s "$TMP_ENC" ]; then + rm -f "$TMP_ENC" + echo "Error: initial SOPS encryption failed; $TARGET_FILE was not created." >&2 + exit 1 + fi + mv "$TMP_ENC" "$TARGET_FILE" echo "Created and encrypted $TARGET_FILE successfully." else diff --git a/lib/sops-atomic-writers.test.sh b/lib/sops-atomic-writers.test.sh new file mode 100755 index 0000000000..99cd2c0554 --- /dev/null +++ b/lib/sops-atomic-writers.test.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env bash +# Tests for the atomic sops writers (#6555): lib/sops_encrypt.sh and the +# init path of lib/bitwarden-to-sops.sh. +# +# Oracle: byte-identity. After a failed encryption, a pre-existing target +# must be byte-identical to before the run (or, for a first-run init, must +# not exist at all); after a successful encryption the target holds exactly +# the new ciphertext. The reference implementation for the expected shape +# is lib/bws-to-sops.sh (mktemp beside the target, mv only on success). +# sops is stubbed on PATH: SOPS_STUB_MODE=fail models any sops failure +# (bad key, sops error, interruption) — exit 1, no output; =ok models +# success by copying its input to stdout as the "ciphertext". bws is +# stubbed with a fixed dummy secret; no live credentials are used. +set -uo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" +ENC="$SCRIPT_DIR/sops_encrypt.sh" +BW="$SCRIPT_DIR/bitwarden-to-sops.sh" +[ -f "$ENC" ] || { echo "FAIL: $ENC not found" >&2; exit 1; } +[ -f "$BW" ] || { echo "FAIL: $BW not found" >&2; exit 1; } +command -v jq >/dev/null 2>&1 || { echo "FAIL: jq not found in PATH" >&2; exit 1; } + +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT +STUB_BIN="$TMP/bin" +mkdir -p "$STUB_BIN" "$TMP/home" + +cat >"$STUB_BIN/sops" <<'STUB' +#!/usr/bin/env bash +# Stub sops: fail mode exits 1 with no output; ok mode "encrypts" by +# copying its input (the last argument, a process-substitution fd) to +# stdout. +if [ "${SOPS_STUB_MODE:-ok}" = "fail" ]; then + echo "stub sops: simulated encryption failure" >&2 + exit 1 +fi +last="${!#}" +cat "$last" +STUB + +cat >"$STUB_BIN/bws" <<'STUB' +#!/usr/bin/env bash +# Stub bws: only `secret get ` is supported. +if [ "${1:-}" = "secret" ] && [ "${2:-}" = "get" ]; then + printf '{"key":"MY_SECRET","value":"dummy-value"}\n' + exit 0 +fi +echo "stub bws: unsupported invocation: $*" >&2 +exit 2 +STUB +chmod +x "$STUB_BIN/sops" "$STUB_BIN/bws" + +FAILURES=0 +pass() { echo "PASS: $1"; } +fail() { echo "FAIL: $1" >&2; FAILURES=$((FAILURES + 1)); } + +# check : pass when the condition command succeeds. +check() { + local name="$1" + shift + if "$@" >/dev/null 2>&1; then pass "$name"; else fail "$name"; fi +} + +leftover_temps() { # -> count of leftover temp files + find "$1" -maxdepth 1 -name "$2.*" | wc -l +} + +# ---------------------------------------------------------------- sops_encrypt, script form +enc_case() { # + local name="$1" mode="$2" dir="$TMP/enc-$1" + mkdir -p "$dir" + local target="$dir/out.enc" sentinel="$dir/sentinel" + printf 'PREVIOUS-CIPHERTEXT' >"$sentinel" + cp "$sentinel" "$target" + local out rc + out="$(printf 'new-plaintext' | SOPS_STUB_MODE="$mode" PATH="$STUB_BIN:$PATH" bash "$ENC" "$target" 2>&1)" + rc=$? + if [ "$mode" = "fail" ]; then + [ "$rc" -ne 0 ] && pass "sops_encrypt script/$name: non-zero exit" || fail "sops_encrypt script/$name: exit was $rc, want non-zero" + cmp -s "$sentinel" "$target" && pass "sops_encrypt script/$name: target byte-identical" || fail "sops_encrypt script/$name: target changed on failure" + case "$out" in *"Encrypted output saved"*) fail "sops_encrypt script/$name: success message printed on failure" ;; *) pass "sops_encrypt script/$name: no success message on failure" ;; esac + else + [ "$rc" -eq 0 ] && pass "sops_encrypt script/$name: zero exit" || fail "sops_encrypt script/$name: exit was $rc, want 0" + printf 'new-plaintext' | cmp -s - "$target" && pass "sops_encrypt script/$name: target replaced with new ciphertext" || fail "sops_encrypt script/$name: target does not hold the new ciphertext" + case "$out" in *"Encrypted output saved"*) pass "sops_encrypt script/$name: success message on success" ;; *) fail "sops_encrypt script/$name: success message missing on success" ;; esac + fi + [ "$(leftover_temps "$dir" '.sops_encrypt')" -eq 0 ] && pass "sops_encrypt script/$name: no leftover temp files" || fail "sops_encrypt script/$name: leftover temp files" +} + +# ---------------------------------------------------------------- sops_encrypt, sourced form +enc_sourced_case() { # + local name="$1" mode="$2" dir="$TMP/encs-$1" + mkdir -p "$dir" + local target="$dir/out.enc" sentinel="$dir/sentinel" + printf 'PREVIOUS-CIPHERTEXT' >"$sentinel" + cp "$sentinel" "$target" + local out rc + out="$(SOPS_STUB_MODE="$mode" PATH="$STUB_BIN:$PATH" bash -c 'source "$1"; printf "new-plaintext" | sops_encrypt "$2"' _ "$ENC" "$target" 2>&1)" + rc=$? + if [ "$mode" = "fail" ]; then + [ "$rc" -ne 0 ] && pass "sops_encrypt sourced/$name: non-zero return" || fail "sops_encrypt sourced/$name: return was $rc, want non-zero" + cmp -s "$sentinel" "$target" && pass "sops_encrypt sourced/$name: target byte-identical" || fail "sops_encrypt sourced/$name: target changed on failure" + case "$out" in *"Encrypted output saved"*) fail "sops_encrypt sourced/$name: success message printed on failure" ;; *) pass "sops_encrypt sourced/$name: no success message on failure" ;; esac + else + [ "$rc" -eq 0 ] && pass "sops_encrypt sourced/$name: zero return" || fail "sops_encrypt sourced/$name: return was $rc, want 0" + printf 'new-plaintext' | cmp -s - "$target" && pass "sops_encrypt sourced/$name: target replaced with new ciphertext" || fail "sops_encrypt sourced/$name: target does not hold the new ciphertext" + fi + [ "$(leftover_temps "$dir" '.sops_encrypt')" -eq 0 ] && pass "sops_encrypt sourced/$name: no leftover temp files" || fail "sops_encrypt sourced/$name: leftover temp files" +} + +# ---------------------------------------------------------------- bitwarden-to-sops init path +bw_init_case() { # + local name="$1" mode="$2" dir="$TMP/bw-$1" + mkdir -p "$dir" + local target="$dir/new-target.yaml" + local out rc + out="$(HOME="$TMP/home" BWS_ACCESS_TOKEN=dummy SOPS_STUB_MODE="$mode" PATH="$STUB_BIN:$PATH" bash "$BW" --bws-secret test-secret --target "$target" --no-auto-env 2>&1)" + rc=$? + if [ "$mode" = "fail" ]; then + [ "$rc" -ne 0 ] && pass "bitwarden-to-sops init/$name: non-zero exit" || fail "bitwarden-to-sops init/$name: exit was $rc, want non-zero" + [ ! -e "$target" ] && pass "bitwarden-to-sops init/$name: no file at target after failed init" || fail "bitwarden-to-sops init/$name: target exists after failed init (later runs would take the update branch)" + case "$out" in *"Created and encrypted"*) fail "bitwarden-to-sops init/$name: success message printed on failure" ;; *) pass "bitwarden-to-sops init/$name: no success message on failure" ;; esac + else + [ "$rc" -eq 0 ] && pass "bitwarden-to-sops init/$name: zero exit" || fail "bitwarden-to-sops init/$name: exit was $rc, want 0: $out" + [ -s "$target" ] && pass "bitwarden-to-sops init/$name: target created non-empty" || fail "bitwarden-to-sops init/$name: target missing or empty after successful init" + grep -q 'my_secret: "dummy-value"' "$target" 2>/dev/null && pass "bitwarden-to-sops init/$name: target holds the encrypted payload" || fail "bitwarden-to-sops init/$name: target does not hold the expected payload" + fi + [ "$(leftover_temps "$dir" '.bitwarden-to-sops')" -eq 0 ] && pass "bitwarden-to-sops init/$name: no leftover temp files" || fail "bitwarden-to-sops init/$name: leftover temp files" +} + +enc_case fail fail +enc_case ok ok +enc_sourced_case fail fail +enc_sourced_case ok ok +bw_init_case fail fail +bw_init_case ok ok + +if [ "$FAILURES" -ne 0 ]; then + echo "sops-atomic-writers: $FAILURES check(s) failed" >&2 + exit 1 +fi +echo "sops-atomic-writers: all checks passed" diff --git a/lib/sops_encrypt.sh b/lib/sops_encrypt.sh index 1b90978da6..d8476f9162 100755 --- a/lib/sops_encrypt.sh +++ b/lib/sops_encrypt.sh @@ -13,7 +13,32 @@ sops_encrypt() { return 1 fi - sops --encrypt <(cat -) >"$output_file" + # Encrypt to a temp file in the target's directory and rename it over the + # output only after sops exits 0 with non-empty output (#6555). Writing + # with `>"$output_file"` truncated the previous ciphertext before sops + # ran, so a failed encryption destroyed it — and the unconditional + # success message below still reported success with status 0. The + # temp-then-rename shape follows lib/bws-to-sops.sh (mktemp beside the + # target, mv only on success). + local output_dir tmp_file + output_dir="$(dirname "$output_file")" + if ! tmp_file="$(mktemp "$output_dir/.sops_encrypt.XXXXXX")"; then + echo "Error: could not create a temp file in $output_dir" >&2 + return 1 + fi + + if ! sops --encrypt <(cat -) >"$tmp_file" || [ ! -s "$tmp_file" ]; then + rm -f "$tmp_file" + echo "Error: SOPS encryption failed; $output_file left unchanged." >&2 + return 1 + fi + + if ! mv "$tmp_file" "$output_file"; then + rm -f "$tmp_file" + echo "Error: could not replace $output_file with the encrypted output" >&2 + return 1 + fi + echo "Encrypted output saved to $output_file" }