From 5c6ec94f4fe00069db8e7928d6af81a89e9add74 Mon Sep 17 00:00:00 2001 From: "pithead-developer[bot]" <328836012+pithead-developer[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:06:37 -0600 Subject: [PATCH 1/5] fix: CLI restore puts config.json, .env and the Caddyfile back when a later step fails (#4450) restore installed the three fixed files before copying the data directories, and a failed copy left the archive's files in place. It now keeps a copy of each live file beside it, moves the copies back on any later failure, and says so when a copy cannot be put back. Co-Authored-By: Claude Fable 5.2 --- docs/operations.md | 5 ++++ lib/pithead/16a-restore-safety.sh | 32 +++++++++++++++++++++-- tests/stack/test-cli-restore-hardening.sh | 27 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/docs/operations.md b/docs/operations.md index c37ece399a..3287dd9935 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -827,6 +827,11 @@ preserves the destination's file in `data/monero`, `data/tari`, and `data/p2pool install may already have synced chain data, and replacing it would force a resync. It still adds files that only the archive has. The wizard replaces its Tor and dashboard directories normally. +If `./pithead restore` fails while it copies a data directory, it puts `config.json`, `.env` and the +`Caddyfile` back as they were before the restore. Data directories it had already copied keep the +archive's files. A previous file it cannot put back stays beside its original name as a +`.restore-old` copy, and restore says so. + When the administrative archive contains `mining_data.db`, restore removes the destination's WAL, shared-memory and rollback-journal sidecars before copying the archived database and its sidecars. A crashed destination writer's committed rows cannot override the archived rows. diff --git a/lib/pithead/16a-restore-safety.sh b/lib/pithead/16a-restore-safety.sh index c63b14859e..577a8f1edb 100644 --- a/lib/pithead/16a-restore-safety.sh +++ b/lib/pithead/16a-restore-safety.sh @@ -63,6 +63,24 @@ restore_discard_stage() { local stage="${RESTORE_STAGE_DIR:-}" RESTORE_STAGE_DIR="" [ -z "$stage" ] || rm -rf -- "$stage" + restore_fixed_backups_finish rollback || + warn "Restore could not put some previous files back — they are kept beside their original names as .restore-old copies." +} + +restore_fixed_backups_finish() { + local entry path backup rc=0 + for entry in "${RESTORE_FIXED_BACKUPS[@]}"; do + path="${entry%%$'\t'*}" backup="${entry#*$'\t'}" + if [ "$1" = keep ]; then + [ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, the copy of $path from before the restore — delete it by hand." + elif [ -n "$backup" ]; then + sudo mv -f -- "$backup" "$path" || rc=1 + else + sudo rm -f -- "$path" || warn "Restore could not remove $path, which it had added — it still holds the archive's version." + fi + done + RESTORE_FIXED_BACKUPS=() + return "$rc" } restore_destination_safe() { # @@ -255,7 +273,8 @@ restore_recheck_destinations() { } restore_commit_stage() { - local path rel owner_uid owner_gid + local path rel owner_uid owner_gid backup + RESTORE_FIXED_BACKUPS=() if ! owner_uid=$(id -u "$REAL_USER") || ! owner_gid=$(id -g "$REAL_USER"); then restore_discard_stage error "Restore could not resolve the invoking operator's ownership — nothing was restored." @@ -269,7 +288,15 @@ restore_commit_stage() { # appliance's life (#2329). local mode=600 [ "$path" != "$PWD/Caddyfile" ] || mode=644 - [ ! -e "$RESTORE_STAGE_DIR/$rel" ] || sudo install -o "$owner_uid" -g "$owner_gid" -m "$mode" "$RESTORE_STAGE_DIR/$rel" "$path" || { + [ -e "$RESTORE_STAGE_DIR/$rel" ] || continue + backup="" + if [ -e "$path" ] && ! { backup=$(sudo mktemp "$path.restore-old.XXXXXXXXXX") && sudo cp -a -- "$path" "$backup"; }; then + [ -z "$backup" ] || sudo rm -f -- "$backup" || : + restore_discard_stage + error "Restore could not keep a copy of $path — nothing more was restored." + fi + RESTORE_FIXED_BACKUPS+=("$path"$'\t'"$backup") + sudo install -o "$owner_uid" -g "$owner_gid" -m "$mode" "$RESTORE_STAGE_DIR/$rel" "$path" || { restore_discard_stage error "Restore failed while committing $path; inspect the destination before retrying." } @@ -290,6 +317,7 @@ restore_commit_stage() { error "Restore failed while committing $path; inspect the destination before retrying." } done + restore_fixed_backups_finish keep # The archive wins ordinary collisions, but Tor's circuit history must never survive a # restore. Its onion keys are separate files and remain untouched. sudo rm -f -- "${RESTORE_ALLOWED_DIRS[3]}/state" || { diff --git a/tests/stack/test-cli-restore-hardening.sh b/tests/stack/test-cli-restore-hardening.sh index 47d7918e53..eaf3f78c56 100644 --- a/tests/stack/test-cli-restore-hardening.sh +++ b/tests/stack/test-cli-restore-hardening.sh @@ -131,6 +131,33 @@ assert_eq "restored Caddyfile is world-readable" "$(file_mode "$BK/Caddyfile")" assert_eq "restored config belongs to the invoking operator" "$(file_uid "$BK/config.json")" "$(id -u)" assert_eq "restored onion key is owner-only" "$(file_mode "$BK/data/tor/hs_ed25519_secret_key")" 600 assert_eq "restored database is owner-only" "$(file_mode "$BK/data/dashboard/dashboard.db")" 600 +assert_eq "successful restore leaves no copies of the fixed files" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" "" +jq '.dashboard.energy.cost_per_kwh = 0.456' "$BK/config.json" >"$CR/pre-config" && cp "$CR/pre-config" "$BK/config.json" +printf 'PRE_RESTORE_MARK=1\n' >>"$BK/.env" +printf 'CADDY-PRE-RESTORE\n' >"$BK/Caddyfile" +cp "$BK/.env" "$CR/pre-env" +mkdir "$CR/fail-bin" +printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_COPY_DEST" ] || exit 1\nexec /bin/cp "$@"\n' >"$CR/fail-bin/cp" +chmod +x "$CR/fail-bin/cp" +out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" PATH="$CR/fail-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)" +assert_rc "restore reports a failed data copy" "$?" 1 +assert_contains "failed data copy names the dashboard directory" "$out" "committing $BK/data/dashboard" +assert_eq "failed data copy puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")" +assert_eq "failed data copy puts the live env back" "$(cat "$BK/.env")" "$(cat "$CR/pre-env")" +assert_eq "failed data copy puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE +assert_eq "rolled-back config stays owner-only" "$(file_mode "$BK/config.json")" 600 +assert_eq "failed data copy leaves no copies of the fixed files" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" "" +printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_MV_DEST" ] || exit 1\nexec /bin/mv "$@"\n' >"$CR/fail-bin/mv" +chmod +x "$CR/fail-bin/mv" +out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" FAIL_MV_DEST="$BK/.env" PATH="$CR/fail-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)" +assert_rc "failed put-back keeps the failed restore's exit code" "$?" 1 +assert_contains "failed put-back keeps the data copy error" "$out" "committing $BK/data/dashboard" +assert_contains "failed put-back names the .restore-old copies" "$out" "kept beside their original names as .restore-old copies" +assert_eq "failed put-back keeps the previous env beside its name" "$(cat "$BK"/.env.restore-old.*)" "$(cat "$CR/pre-env")" +assert_eq "failed put-back still puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")" +assert_eq "failed put-back still puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE +mv "$BK"/.env.restore-old.* "$BK/.env" +rm -f "$CR/fail-bin/mv" # Generated files in a backup are compatibility inputs, never runtime policy. A valid config plus # stale .env/Caddyfile must land the normal writers' output, while opaque generated identity and From 472403495421985a6f8645bfff9e91f3d0244737 Mon Sep 17 00:00:00 2001 From: "pithead-developer[bot]" <4933481+pithead-developer[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:28:32 -0600 Subject: [PATCH 2/5] fix: restore warns on failed per-file rollback and tests added-file removal (#4450) Address reviewer findings: move the two explanatory comments to docs, add a per-file warning when the restore cannot put a file back, and add the missing test for the rollback branch where a file did not exist before the restore (backup is empty). Co-Authored-By: Claude Code --- docs/operations.md | 5 +++++ lib/pithead/16a-restore-safety.sh | 12 ++++-------- tests/stack/test-cli-restore-hardening.sh | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/operations.md b/docs/operations.md index 3287dd9935..b9c458db20 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -827,6 +827,11 @@ preserves the destination's file in `data/monero`, `data/tari`, and `data/p2pool install may already have synced chain data, and replacing it would force a resync. It still adds files that only the archive has. The wizard replaces its Tor and dashboard directories normally. +The fixed files have specific permissions: `config.json` and `.env` carry secrets and stay owner-only +(600); the `Caddyfile` is restored as world-readable (644) because the `caddy` container runs as root +with `cap_drop: ALL` and no `CAP_DAC_OVERRIDE`, so a 600 file would leave it permission-denied on +its bind mount for the rest of the appliance's life (#2329). + If `./pithead restore` fails while it copies a data directory, it puts `config.json`, `.env` and the `Caddyfile` back as they were before the restore. Data directories it had already copied keep the archive's files. A previous file it cannot put back stays beside its original name as a diff --git a/lib/pithead/16a-restore-safety.sh b/lib/pithead/16a-restore-safety.sh index 577a8f1edb..c9c636ab0a 100644 --- a/lib/pithead/16a-restore-safety.sh +++ b/lib/pithead/16a-restore-safety.sh @@ -74,7 +74,10 @@ restore_fixed_backups_finish() { if [ "$1" = keep ]; then [ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, the copy of $path from before the restore — delete it by hand." elif [ -n "$backup" ]; then - sudo mv -f -- "$backup" "$path" || rc=1 + sudo mv -f -- "$backup" "$path" || { + rc=1 + warn "Restore could not put $path back — it is kept as $backup." + } else sudo rm -f -- "$path" || warn "Restore could not remove $path, which it had added — it still holds the archive's version." fi @@ -281,11 +284,6 @@ restore_commit_stage() { fi for path in "${RESTORE_FIXED_PATHS[@]}"; do rel="${path#/}" - # config.json and .env carry secrets and stay owner-only (600); the Caddyfile does not, and - # the caddy container reads it as a cap_drop:ALL root with no CAP_DAC_OVERRIDE, so it must - # come back world-readable (644) like generate_caddyfile's normal apply-path render — a 600 - # restore leaves caddy permission-denied on its bind-mounted Caddyfile for the rest of the - # appliance's life (#2329). local mode=600 [ "$path" != "$PWD/Caddyfile" ] || mode=644 [ -e "$RESTORE_STAGE_DIR/$rel" ] || continue @@ -318,8 +316,6 @@ restore_commit_stage() { } done restore_fixed_backups_finish keep - # The archive wins ordinary collisions, but Tor's circuit history must never survive a - # restore. Its onion keys are separate files and remain untouched. sudo rm -f -- "${RESTORE_ALLOWED_DIRS[3]}/state" || { restore_discard_stage error "Restore committed data but could not discard Tor circuit state; keep the stack stopped and remove the state before starting Tor." diff --git a/tests/stack/test-cli-restore-hardening.sh b/tests/stack/test-cli-restore-hardening.sh index eaf3f78c56..1bdb66b2c2 100644 --- a/tests/stack/test-cli-restore-hardening.sh +++ b/tests/stack/test-cli-restore-hardening.sh @@ -159,6 +159,21 @@ assert_eq "failed put-back still puts the live Caddyfile back" "$(cat "$BK/Caddy mv "$BK"/.env.restore-old.* "$BK/.env" rm -f "$CR/fail-bin/mv" +printf 'EXISTING-ENV\n' >"$BK/.env" +printf 'EXISTING-CADDY\n' >"$BK/Caddyfile" +rm -f "$BK/config.json" +cr_archive "$CR/archive-no-config.tar.gz" +mkdir "$CR/fail-bin2" +printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_COPY_DEST" ] || exit 1\nexec /bin/cp "$@"\n' >"$CR/fail-bin2/cp" +chmod +x "$CR/fail-bin2/cp" +out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" PATH="$CR/fail-bin2:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)" +assert_rc "failed data copy with missing config.json exits 1" "$?" 1 +assert_not_contains "missing config.json is not restored on failure" "$(test -f "$BK/config.json" && echo yes)" yes +assert_eq "existing .env survives put-back" "$(cat "$BK/.env")" EXISTING-ENV +assert_eq "existing Caddyfile survives put-back" "$(cat "$BK/Caddyfile")" EXISTING-CADDY +assert_eq "rollback of added file leaves no .restore-old copy" "$(find "$BK" -maxdepth 1 -name 'config.json.restore-old.*')" "" +rm -f "$CR/fail-bin2/cp" + # Generated files in a backup are compatibility inputs, never runtime policy. A valid config plus # stale .env/Caddyfile must land the normal writers' output, while opaque generated identity and # secrets still round-trip. From 78d6990477816de70e80a0316c2dfc7b11459347 Mon Sep 17 00:00:00 2001 From: "pithead-developer[bot]" <4933481+pithead-developer[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:59:32 -0600 Subject: [PATCH 3/5] fix: restore sandbox config.json for tests after added-file rollback test The test for an empty backup (config.json did not exist before restore) removed it from $BK/config.json. Subsequent tests expected it present. Restore it from $CR/pre-config before the next test block. --- tests/stack/test-cli-restore-hardening.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/stack/test-cli-restore-hardening.sh b/tests/stack/test-cli-restore-hardening.sh index 1bdb66b2c2..1545585329 100644 --- a/tests/stack/test-cli-restore-hardening.sh +++ b/tests/stack/test-cli-restore-hardening.sh @@ -174,6 +174,9 @@ assert_eq "existing Caddyfile survives put-back" "$(cat "$BK/Caddyfile")" EXISTI assert_eq "rollback of added file leaves no .restore-old copy" "$(find "$BK" -maxdepth 1 -name 'config.json.restore-old.*')" "" rm -f "$CR/fail-bin2/cp" +cp "$CR/pre-config" "$BK/config.json" +chmod 644 "$BK/config.json" + # Generated files in a backup are compatibility inputs, never runtime policy. A valid config plus # stale .env/Caddyfile must land the normal writers' output, while opaque generated identity and # secrets still round-trip. From d581499f11c80883a96d8fbd4b0662f0f5681320 Mon Sep 17 00:00:00 2001 From: "pithead-developer[bot]" <4933481+pithead-developer[bot]@users.noreply.github.com> Date: Sun, 29 Nov 2026 13:01:19 -0600 Subject: [PATCH 4/5] Keep restore rollback safe on Bash 3.2 and describe its scope Co-Authored-By: Claude Opus 5.5 --- docs/operations.md | 5 +++-- lib/pithead/16a-restore-safety.sh | 2 +- tests/stack/test-cli-restore-hardening.sh | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/operations.md b/docs/operations.md index 0c24951302..7a2d0441f9 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -832,8 +832,9 @@ The fixed files have specific permissions: `config.json` and `.env` carry secret with `cap_drop: ALL` and no `CAP_DAC_OVERRIDE`, so a 600 file would leave it permission-denied on its bind mount for the rest of the appliance's life (#2329). -If `./pithead restore` fails while it copies a data directory, it puts `config.json`, `.env` and the -`Caddyfile` back as they were before the restore. Data directories it had already copied keep the +If `./pithead restore` fails once it has started writing files (installing a fixed file, clearing the +dashboard database sidecars or copying a data directory), it puts `config.json`, `.env` and the +`Caddyfile` back as they were before the restore and removes any of them that the restore added. Data directories it had already copied keep the archive's files. A previous file it cannot put back stays beside its original name as a `.restore-old` copy, and restore says so. diff --git a/lib/pithead/16a-restore-safety.sh b/lib/pithead/16a-restore-safety.sh index b53b3772f4..e96c6ac28b 100644 --- a/lib/pithead/16a-restore-safety.sh +++ b/lib/pithead/16a-restore-safety.sh @@ -69,7 +69,7 @@ restore_discard_stage() { restore_fixed_backups_finish() { local entry path backup rc=0 - for entry in "${RESTORE_FIXED_BACKUPS[@]}"; do + for entry in ${RESTORE_FIXED_BACKUPS[@]+"${RESTORE_FIXED_BACKUPS[@]}"}; do path="${entry%%$'\t'*}" backup="${entry#*$'\t'}" if [ "$1" = keep ]; then [ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, the copy of $path from before the restore — delete it by hand." diff --git a/tests/stack/test-cli-restore-hardening.sh b/tests/stack/test-cli-restore-hardening.sh index 520529ec31..2148dbae27 100644 --- a/tests/stack/test-cli-restore-hardening.sh +++ b/tests/stack/test-cli-restore-hardening.sh @@ -162,7 +162,6 @@ rm -f "$CR/fail-bin/mv" printf 'EXISTING-ENV\n' >"$BK/.env" printf 'EXISTING-CADDY\n' >"$BK/Caddyfile" rm -f "$BK/config.json" -cr_archive "$CR/archive-no-config.tar.gz" mkdir "$CR/fail-bin2" printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_COPY_DEST" ] || exit 1\nexec /bin/cp "$@"\n' >"$CR/fail-bin2/cp" chmod +x "$CR/fail-bin2/cp" From d29b3c0d396e39bf3bfa4897f47bcb5cafd3830a Mon Sep 17 00:00:00 2001 From: "pithead-developer[bot]" <4933481+pithead-developer[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:21:48 -0600 Subject: [PATCH 5/5] Keep restore's rollback list to its own run and finish copy cleanup after a signal The EXIT trap now starts from an empty rollback list, so an inherited RESTORE_FIXED_BACKUPS can no longer move or remove files. A signal while the .restore-old copies are being removed after a successful restore finishes removing them instead of putting part of the old files back. The put-back runs before the staging directory is removed, a partial copy that cannot be removed is named, and the copies are ignored by git. Tests pin the per-file put-back warning and the Caddyfile mode, and docs/operations.md states what a failed restore does and does not undo. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 3 ++ docs/operations.md | 15 ++++++--- lib/pithead/16a-restore-safety.sh | 16 +++++---- lib/pithead/17-backup-restore.sh | 4 +-- tests/stack/test-cli-restore-hardening.sh | 41 +++++++++++++++++++++++ 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 0dc6b4030f..b57d67db70 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ .env .env.new .env.new.* +.env.restore-old.* +config.json.restore-old.* +Caddyfile.restore-old.* .env.dryrun .env.apply-incomplete .env.caddy-restart-pending diff --git a/docs/operations.md b/docs/operations.md index 7a2d0441f9..a4592a46d4 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -832,11 +832,16 @@ The fixed files have specific permissions: `config.json` and `.env` carry secret with `cap_drop: ALL` and no `CAP_DAC_OVERRIDE`, so a 600 file would leave it permission-denied on its bind mount for the rest of the appliance's life (#2329). -If `./pithead restore` fails once it has started writing files (installing a fixed file, clearing the -dashboard database sidecars or copying a data directory), it puts `config.json`, `.env` and the -`Caddyfile` back as they were before the restore and removes any of them that the restore added. Data directories it had already copied keep the -archive's files. A previous file it cannot put back stays beside its original name as a -`.restore-old` copy, and restore says so. +`./pithead restore` copies the archive once into a private staging directory, so every check and +the extraction read the same bytes; that directory is removed when restore exits. + +If `./pithead restore` fails, or is interrupted, while it installs a fixed file, clears the dashboard +database sidecars or copies a data directory, it puts `config.json`, `.env` and the `Caddyfile` back +as they were before the restore and removes any of them that the restore added. It does not undo the +data: directories copied before the failure, and the one being copied when it failed, keep the +archive's files, and dashboard database sidecars it had removed stay removed. A failure after the +last data directory is copied leaves the restored files in place. A previous file restore cannot put +back stays beside its original name as a `.restore-old` copy, and restore names that copy. When the administrative archive contains `mining_data.db`, restore removes the destination's WAL, shared-memory and rollback-journal sidecars before copying the archived database and its diff --git a/lib/pithead/16a-restore-safety.sh b/lib/pithead/16a-restore-safety.sh index e96c6ac28b..f5877c84ec 100644 --- a/lib/pithead/16a-restore-safety.sh +++ b/lib/pithead/16a-restore-safety.sh @@ -62,16 +62,18 @@ restore_member_allowed() { restore_discard_stage() { local stage="${RESTORE_STAGE_DIR:-}" RESTORE_STAGE_DIR="" - [ -z "$stage" ] || rm -rf -- "$stage" restore_fixed_backups_finish rollback || warn "Restore could not put some previous files back — they are kept beside their original names as .restore-old copies." + [ -z "$stage" ] || rm -rf -- "$stage" } restore_fixed_backups_finish() { - local entry path backup rc=0 + local entry path backup rc=0 action="$1" + [ "${RESTORE_FIXED_KEEP:-}" != 1 ] || action=keep + [ "$action" != keep ] || RESTORE_FIXED_KEEP=1 for entry in ${RESTORE_FIXED_BACKUPS[@]+"${RESTORE_FIXED_BACKUPS[@]}"}; do path="${entry%%$'\t'*}" backup="${entry#*$'\t'}" - if [ "$1" = keep ]; then + if [ "$action" = keep ]; then [ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, the copy of $path from before the restore — delete it by hand." elif [ -n "$backup" ]; then sudo mv -f -- "$backup" "$path" || { @@ -82,7 +84,7 @@ restore_fixed_backups_finish() { sudo rm -f -- "$path" || warn "Restore could not remove $path, which it had added — it still holds the archive's version." fi done - RESTORE_FIXED_BACKUPS=() + RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP="" return "$rc" } @@ -277,7 +279,7 @@ restore_recheck_destinations() { restore_commit_stage() { local path rel owner_uid owner_gid backup - RESTORE_FIXED_BACKUPS=() + RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP="" if ! owner_uid=$(id -u "$REAL_USER") || ! owner_gid=$(id -g "$REAL_USER"); then restore_discard_stage error "Restore could not resolve the invoking operator's ownership — nothing was restored." @@ -289,9 +291,9 @@ restore_commit_stage() { [ -e "$RESTORE_STAGE_DIR/$rel" ] || continue backup="" if [ -e "$path" ] && ! { backup=$(sudo mktemp "$path.restore-old.XXXXXXXXXX") && sudo cp -a -- "$path" "$backup"; }; then - [ -z "$backup" ] || sudo rm -f -- "$backup" || : + [ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, a partial copy of $path — delete it by hand." restore_discard_stage - error "Restore could not keep a copy of $path — nothing more was restored." + error "Restore could not keep a copy of $path — nothing was restored." fi RESTORE_FIXED_BACKUPS+=("$path"$'\t'"$backup") sudo_install_file "$RESTORE_STAGE_DIR/$rel" "$path" "$mode" "$owner_uid:$owner_gid" || { diff --git a/lib/pithead/17-backup-restore.sh b/lib/pithead/17-backup-restore.sh index 729bcdce2a..b8c35bbe35 100644 --- a/lib/pithead/17-backup-restore.sh +++ b/lib/pithead/17-backup-restore.sh @@ -244,9 +244,7 @@ stack_restore() { archive=$(cd "$(dirname "$archive")" && printf '%s/%s' "$PWD" "$(basename "$archive")") archive_source="$archive" - # Snapshot once so every audit and the extraction read the same bytes. Clear inherited state - # before arming cleanup: only a directory created by this invocation may be removed. - RESTORE_STAGE_DIR="" + RESTORE_STAGE_DIR="" RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP="" trap restore_discard_stage EXIT RESTORE_STAGE_DIR=$(mktemp -d) || error "Could not create a private restore staging directory." if ! (umask 077 && cp -- "$archive" "$RESTORE_STAGE_DIR/.archive" && chmod 600 "$RESTORE_STAGE_DIR/.archive"); then diff --git a/tests/stack/test-cli-restore-hardening.sh b/tests/stack/test-cli-restore-hardening.sh index 2148dbae27..e8edfa9507 100644 --- a/tests/stack/test-cli-restore-hardening.sh +++ b/tests/stack/test-cli-restore-hardening.sh @@ -146,6 +146,7 @@ assert_eq "failed data copy puts the live config back" "$(cat "$BK/config.json") assert_eq "failed data copy puts the live env back" "$(cat "$BK/.env")" "$(cat "$CR/pre-env")" assert_eq "failed data copy puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE assert_eq "rolled-back config stays owner-only" "$(file_mode "$BK/config.json")" 600 +assert_eq "rolled-back Caddyfile stays world-readable" "$(file_mode "$BK/Caddyfile")" 644 assert_eq "failed data copy leaves no copies of the fixed files" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" "" printf '#!/usr/bin/env bash\ncase "$*" in *.restore-old.*" $FAIL_MV_DEST") exit 1 ;; esac\nexec /bin/mv "$@"\n' >"$CR/fail-bin/mv" chmod +x "$CR/fail-bin/mv" @@ -153,6 +154,7 @@ out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" FAIL_MV_DEST="$BK/.env" assert_rc "failed put-back keeps the failed restore's exit code" "$?" 1 assert_contains "failed put-back keeps the data copy error" "$out" "committing $BK/data/dashboard" assert_contains "failed put-back names the .restore-old copies" "$out" "kept beside their original names as .restore-old copies" +assert_contains "failed put-back names where the env copy is" "$out" "Restore could not put $BK/.env back — it is kept as $BK/.env.restore-old." assert_eq "failed put-back keeps the previous env beside its name" "$(cat "$BK"/.env.restore-old.*)" "$(cat "$CR/pre-env")" assert_eq "failed put-back still puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")" assert_eq "failed put-back still puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE @@ -173,6 +175,45 @@ assert_eq "existing Caddyfile survives put-back" "$(cat "$BK/Caddyfile")" EXISTI assert_eq "rollback of added file leaves no .restore-old copy" "$(find "$BK" -maxdepth 1 -name 'config.json.restore-old.*')" "" rm -f "$CR/fail-bin2/cp" +cp "$CR/pre-config" "$BK/config.json" +mkdir "$CR/fail-bin3" +printf '%s\n' '#!/usr/bin/env bash' 'case "${!#}" in *.env.restore-old.*) exit 1 ;; esac' 'exec /bin/cp "$@"' >"$CR/fail-bin3/cp" +printf '%s\n' '#!/usr/bin/env bash' 'case "$*" in *.env.restore-old.*) exit 1 ;; esac' 'exec /bin/rm "$@"' >"$CR/fail-bin3/rm" +chmod +x "$CR/fail-bin3/cp" "$CR/fail-bin3/rm" +out="$(cd "$BK" && PATH="$CR/fail-bin3:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)" +assert_rc "a failed copy of a fixed file stops the restore" "$?" 1 +assert_contains "a failed copy of a fixed file says nothing was restored" "$out" "Restore could not keep a copy of $BK/.env — nothing was restored." +assert_contains "a partial copy that cannot be removed is named" "$out" "Could not remove $BK/.env.restore-old." +assert_eq "a failed copy of a fixed file puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")" +assert_eq "a failed copy of a fixed file leaves the live env alone" "$(cat "$BK/.env")" EXISTING-ENV +/bin/rm -f "$BK"/.env.restore-old.* + +mkdir "$CR/term-bin" +printf '%s\n' '#!/usr/bin/env bash' '/bin/rm "$@" || exit' 'case "$*" in *config.json.restore-old.*) ;; *) exit 0 ;; esac' \ + '[ ! -e "$TERM_MARK" ] || exit 0' ': >"$TERM_MARK"' 'pid=$PPID' \ + 'while [ "${pid:-0}" -gt 1 ]; do' \ + ' case "$(ps -o args= -p "$pid")" in *"pithead restore"*) kill -TERM "$pid"; exit 0 ;; esac' \ + ' pid=$(ps -o ppid= -p "$pid" | tr -d " ")' 'done' >"$CR/term-bin/rm" +chmod +x "$CR/term-bin/rm" +out="$(cd "$BK" && TERM_MARK="$CR/term-sent" PATH="$CR/term-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)" +assert_rc "a signal while the copies are removed ends the restore" "$?" 143 +assert_not_contains "a signal while the copies are removed puts nothing back" "$out" "could not put" +assert_not_contains "a signal while the copies are removed keeps the restored env" "$(cat "$BK/.env")" EXISTING-ENV +assert_not_contains "a signal while the copies are removed keeps the restored Caddyfile" "$(cat "$BK/Caddyfile")" EXISTING-CADDY +assert_eq "a signal while the copies are removed leaves no copies" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" "" + +printf 'victim-a\n' >"$CR/victim-a" +printf 'victim-b\n' >"$CR/victim-b" +printf 'not an archive\n' >"$CR/junk.tar.gz" +out="$(cd "$BK" && RESTORE_FIXED_BACKUPS="$CR/victim-a"$'\t'"$CR/victim-b" RESTORE_FIXED_KEEP=1 PATH="$BK/bin:$PATH" ./pithead restore -y "$CR/junk.tar.gz" 2>&1)" +assert_rc "restore refuses a file that is not a backup" "$?" 1 +assert_eq "an inherited rollback list cannot replace a file" "$(cat "$CR/victim-a" 2>/dev/null)" victim-a +assert_eq "an inherited rollback list cannot remove a file" "$(cat "$CR/victim-b" 2>/dev/null)" victim-b +assert_eq "copies of the fixed files are ignored by git" \ + "$(git -C "$ROOT" check-ignore --no-index .env.restore-old.x config.json.restore-old.x Caddyfile.restore-old.x | wc -l | tr -d ' ')" 3 + +printf 'EXISTING-ENV\n' >"$BK/.env" +printf 'EXISTING-CADDY\n' >"$BK/Caddyfile" cp "$CR/pre-config" "$BK/config.json" chmod 644 "$BK/config.json"