Skip to content

lib(sops_encrypt): a failed sops encryption truncates the output file to zero bytes and still reports success — bitwarden-to-sops init leaves a poisoned zero-byte target #6555

Closed

Description

@dezren39

Summary

Two lib/ sops writers repeat the non-atomic redirect pattern that #6536 (dns save.sh) and #6554 (talos save-secrets.sh) were filed for, in places those issues do not cover:

  1. lib/sops_encrypt.sh:16 — the generic sops_encrypt helper writes ciphertext with sops --encrypt <(cat -) >"$output_file". The shell truncates $output_file before sops runs, so any encryption failure (missing/expired age key, sops error, interruption) destroys the previous ciphertext. Worse, the failure is masked: the file has no set -e, and line 17 unconditionally echoes Encrypted output saved to $output_file; the function's return status is that echo's 0. Both invocation forms are affected — running it as a script (lib/sops_encrypt.sh:28) and sourcing it as a function.
  2. lib/bitwarden-to-sops.sh:370 — the first-run init path (if [ ! -f "$TARGET_FILE" ], :359) creates a brand-new sops file with sops -e ... >"$TARGET_FILE". This script does run under set -euo pipefail (:15), so a failed sops exits non-zero — but the redirect has already created a zero-byte file at the target path. Every later run then takes the "file exists" branch (:374) and runs sops set (:378) against that empty, unencrypted placeholder instead of retrying init, so one transient failure permanently wedges the target until a human deletes the empty file by hand.

The hardened oracle already exists in the same directory: lib/bws-to-sops.sh:144-152 encrypts to a mktemp file beside the target, round-trip-verifies it with sops -d, and only then mvs it over the target.

Evidence (verified this run, laptop primary at origin/main 9c06d0c)

Probe ~/git/fleet/probe-sops-lib-atomic drives the real lib/sops_encrypt.sh with a stub sops on PATH and a pre-existing out.enc holding a sentinel:

  • Failing stub (sops exits 1), script form: exit 0, output Encrypted output saved to out.enc, and out.enc goes from the sentinel to 0 bytes.
  • Failing stub, sourced-function form: function return 0, same success message, target 0 bytes.
  • Control (succeeding stub emitting its input): exit 0, target replaced with the new content (10 bytes) — the happy path works; only the failure path destroys data and lies about it.

The bitwarden-to-sops site is verified by code read at the same commit (lines above): the redirect creates the target before sops runs, set -e stops the script but cannot un-create the file, and the [ ! -f ... ] branch test is what routes every subsequent run to the sops set update path.

Dedup: title/keyword searches for sops_encrypt, bitwarden-to-sops, and sops-truncation return no issue covering either lib/ site; #6536 covers infra/dns/save.sh only and #6554 covers infra/talos/01b.us/save-secrets.sh only (its body names dns as the sole related item).

Why it matters

sops_encrypt is the shared helper anything in lib/ (or any operator following the repo's own tooling) reaches for to persist an encrypted file; its contract is "pipe secrets in, get an encrypted file out". Today a transient sops failure both destroys the previous ciphertext and reports success, so the loss surfaces only when the file is next decrypted — the same failure shape as #6536/#6554, with the masking defect of #6554 added. The bitwarden-to-sops variant turns one transient failure into a stuck target file that misroutes all future runs.

Acceptance criteria

  • sops_encrypt encrypts to a temporary file in the target's directory and atomically renames it over the output only after sops exits 0 (and the temp file is non-empty); a failed encryption leaves any previous output byte-identical.
  • sops_encrypt propagates failure: non-zero return/exit on sops failure, and the "Encrypted output saved" message is printed only on actual success.
  • bitwarden-to-sops.sh's init path uses the same temp-then-rename shape, so a failed first encryption leaves no file at $TARGET_FILE and the next run retries the init branch.
  • Regression coverage (stub-sops probe or test): failing sops → previous output unchanged + non-zero status for both the script and sourced forms of sops_encrypt; failing init in bitwarden-to-sops → no target file created; succeeding sops → new ciphertext in place for both.

Distinct from

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/triageQueue packet kind: verify, reproduce, and route this item
    source/creatorFiled by the new-issue-creator job
    on Nov 6, 2026
  2. developing-today commented on Nov 6, 2026

    @developing-today
    Contributor
  3. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

    • Agent: new-issue-creator (new-issue-creator-code-a)
    • Session / ID: cron new-issue-creator-code-a, run 2026-11-06T16:05Z
    • Found via: filed this run
    • Task: Triage the non-atomic sops writes in lib/sops_encrypt.sh (:16, failure masked, target truncated) and lib/bitwarden-to-sops.sh init (:370, zero-byte placeholder wedges later runs); lib/bws-to-sops.sh:144-152 is the hardened temp+verify+rename oracle. Verified at origin/main 9c06d0c with laptop probe ~/git/fleet/probe-sops-lib-atomic (real script, stub sops: failure → target 0 bytes + exit 0 + success message; success control replaces content).
    • Expectation: effort s — triage verdict (confirmed or refuted with evidence); if confirmed, route a fix task applying the temp-then-rename pattern to both sites with stub-sops regression coverage.
    • Links: packet packet-triage-6555.json (repo-health-code-pilot queue)
  4. added
    work/assignedQueue worker lifecycle state
    queue/claimedQueue worker lifecycle state
    and removed on Nov 6, 2026
  5. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    Picked up — task 11218, issue #6555 (triage, direct lane).

    Lane code-r claimed task 11218 and will work the triage directly (§1b assigned_lane=direct). Re-verified fresh via gh: issue #6555 is OPEN with no hard-stop labels. Next: verify the cited code on the laptop primary at origin/main (lib/sops_encrypt.sh, lib/bitwarden-to-sops.sh, oracle lib/bws-to-sops.sh) and post a verdict.

  6. 16 remaining items

  7. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    queue/claimedQueue worker lifecycle state
    and removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 6, 2026
  8. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  9. added a commit that references this issue on Nov 6, 2026
  10. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    app/liblib/ shared shell librarieskind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus:donetask/small-fixQueue packet kind: bounded fix with reproduction and targeted tests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions