Skip to content

Commit 3828e77

Browse files
fix(security): preserve checksums on failed reload
1 parent 9fae6a7 commit 3828e77

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

scripts/generated/internal_checksums.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Used by check-manifest-drift.sh to detect unauthorized changes.
99

1010
declare -gA ACFS_INTERNAL_CHECKSUMS=(
11-
[scripts/lib/security.sh]="8fa466969cb0ec945266296900bbbc6ea04bf9a41b521458799ad9ac3dec0fd1"
11+
[scripts/lib/security.sh]="4f63909711279bc5fafd8f0abd2e608c428312584e6e6a7e72da2447bdabab15"
1212
[scripts/lib/agents.sh]="66fac24c48c9ce7d17ae213ff2f8669a1902e77f01266f4eeaccdcef09e02856"
1313
[scripts/lib/update.sh]="70903e24c0a0fc3711d754d19e475be8e9aae59fb5ad0e66c8c5c3535885dffa"
1414
[scripts/lib/doctor.sh]="8b055f242330f1e9571bb25d006ff09cf1f8bcbe81ba07260959e37c1b0f9e2b"

scripts/lib/security.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ load_checksums() {
879879
local in_installers=false
880880
local installers_indent=0
881881
local tool_indent=""
882+
local tool=""
883+
local -A parsed_checksums=()
884+
local -A parsed_installers=()
882885
# Use ACFS colors if available, preserving empty-string NO_COLOR behavior.
883886
local warn_color="${ACFS_YELLOW-\033[0;33m}"
884887
local nc_color="${ACFS_NC-\033[0m}"
@@ -888,9 +891,6 @@ load_checksums() {
888891
return 1
889892
fi
890893

891-
# Clear any previously loaded checksums (avoid stale entries if reloaded).
892-
LOADED_CHECKSUMS=()
893-
894894
# Lightweight YAML parsing for our specific format:
895895
#
896896
# installers:
@@ -957,21 +957,31 @@ load_checksums() {
957957
url_value="${url_value#\'}"
958958

959959
if [[ "$url_value" =~ ^https://[^[:space:]]+$ ]]; then
960-
KNOWN_INSTALLERS["$current_tool"]="$url_value"
960+
parsed_installers["$current_tool"]="$url_value"
961961
fi
962962
fi
963963

964964
# Match sha256 value for the current tool.
965965
if [[ -n "$current_tool" ]] && [[ "$line" =~ sha256:[[:space:]]*['\"]?([0-9A-Fa-f]{64})['\"]? ]]; then
966-
LOADED_CHECKSUMS["$current_tool"]="${BASH_REMATCH[1],,}"
966+
parsed_checksums["$current_tool"]="${BASH_REMATCH[1],,}"
967967
fi
968968
done < "$file"
969969

970-
if [[ ${#LOADED_CHECKSUMS[@]} -eq 0 ]]; then
970+
if [[ ${#parsed_checksums[@]} -eq 0 ]]; then
971971
printf "${warn_color}Warning:${nc_color} No valid installer checksums found in: %s\n" "$file" >&2
972972
return 1
973973
fi
974974

975+
# Commit parsed data only after validating that the new file has usable
976+
# checksum entries, so a malformed refresh cannot erase previous state.
977+
LOADED_CHECKSUMS=()
978+
for tool in "${!parsed_checksums[@]}"; do
979+
LOADED_CHECKSUMS["$tool"]="${parsed_checksums[$tool]}"
980+
if [[ -n "${parsed_installers[$tool]:-}" ]]; then
981+
KNOWN_INSTALLERS["$tool"]="${parsed_installers[$tool]}"
982+
fi
983+
done
984+
975985
return 0
976986
}
977987

tests/unit/lib/test_security.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,37 @@ EOF
456456
assert_equal "${KNOWN_INSTALLERS[tool2]}" "https://example.com/2"
457457
assert_equal "${KNOWN_INSTALLERS[tool3]}" "https://example.com/3"
458458
}
459+
460+
@test "load_checksums: failed reload preserves previous checksum state" {
461+
local good_sha="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
462+
local bad_file
463+
bad_file="$(create_temp_file)"
464+
465+
declare -gA LOADED_CHECKSUMS=()
466+
KNOWN_INSTALLERS["txn_tool"]="https://example.com/old"
467+
468+
cat > "$CHECKSUMS_FILE" <<EOF
469+
installers:
470+
txn_tool:
471+
url: "https://example.com/good"
472+
sha256: "$good_sha"
473+
EOF
474+
475+
load_checksums
476+
assert_equal "$?" "0"
477+
assert_equal "$(get_checksum "txn_tool")" "$good_sha"
478+
assert_equal "${KNOWN_INSTALLERS["txn_tool"]}" "https://example.com/good"
479+
480+
cat > "$bad_file" <<'EOF'
481+
installers:
482+
txn_tool:
483+
url: "https://example.com/bad"
484+
sha256: "not-a-valid-sha"
485+
EOF
486+
487+
if load_checksums "$bad_file"; then
488+
fail "malformed checksums reload unexpectedly succeeded"
489+
fi
490+
assert_equal "$(get_checksum "txn_tool")" "$good_sha"
491+
assert_equal "${KNOWN_INSTALLERS["txn_tool"]}" "https://example.com/good"
492+
}

0 commit comments

Comments
 (0)