Skip to content

Commit fb87faa

Browse files
fix(security): parse refreshed installer urls robustly
1 parent 57afc58 commit fb87faa

5 files changed

Lines changed: 71 additions & 7 deletions

File tree

scripts/generated/internal_checksums.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
# Used by check-manifest-drift.sh to detect unauthorized changes.
99

1010
declare -gA ACFS_INTERNAL_CHECKSUMS=(
11-
[scripts/lib/security.sh]="1fc1cb591fd62ac56d12c5af183de9674e501a74d478b3dd60901167262e919a"
11+
[scripts/lib/security.sh]="d82c1b79d17a0063e4d60cb12587f3550e25fa114a4261bd57bc350bcf297cad"
1212
[scripts/lib/agents.sh]="0d4e7666b7a7267203445c364ad2d4997775826175700627abe83c70afce2269"
13-
[scripts/lib/update.sh]="8a43e4fcfef29df3eadb0c69a091a66670d0834cc3b7e8691835f6acd5e418a5"
13+
[scripts/lib/update.sh]="10621235fd8472ef4ab0a1c75c20bce2488b27b6664144ed923516972cecee23"
1414
[scripts/lib/doctor.sh]="89cbdcf2c6b5a88404857a8885508f686c9aeba04e69a95b4e99475ba9148f42"
1515
[scripts/lib/doctor_fix.sh]="0010bcf607fce4cb5447a7f5daee0bdf507b03839f97be746a0afdab97e1b249"
1616
[scripts/lib/autofix.sh]="ae7cc5e0b3af3f170d647945d3daee9a341c9276c270fe06895ab9aaf26ba805"

scripts/lib/security.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,21 @@ load_checksums() {
855855

856856
# Match url value for the current tool — override KNOWN_INSTALLERS so
857857
# stale URLs baked into an older security.sh are corrected when
858-
# checksums.yaml is refreshed from GitHub.
859-
if [[ -n "$current_tool" ]] && [[ "$line" =~ url:[[:space:]]*\"(https://[^\"]+)\" ]]; then
860-
KNOWN_INSTALLERS["$current_tool"]="${BASH_REMATCH[1]}"
858+
# checksums.yaml is refreshed from GitHub. Accept quoted or unquoted
859+
# YAML scalars, matching install.sh's bootstrap parser.
860+
if [[ -n "$current_tool" ]] && [[ "$line" =~ ^[[:space:]]*url:[[:space:]]*(.*)$ ]]; then
861+
local url_value="${BASH_REMATCH[1]}"
862+
url_value="${url_value%%#*}"
863+
url_value="${url_value%"${url_value##*[![:space:]]}"}"
864+
url_value="${url_value#"${url_value%%[![:space:]]*}"}"
865+
url_value="${url_value%\"}"
866+
url_value="${url_value#\"}"
867+
url_value="${url_value%\'}"
868+
url_value="${url_value#\'}"
869+
870+
if [[ "$url_value" =~ ^https://[^[:space:]]+$ ]]; then
871+
KNOWN_INSTALLERS["$current_tool"]="$url_value"
872+
fi
861873
fi
862874

863875
# Match sha256 value for the current tool.

scripts/lib/update.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2858,8 +2858,17 @@ update_sync_known_installer_urls_from_checksums() {
28582858
fi
28592859
fi
28602860

2861-
if [[ -n "$current_tool" ]] && [[ "$line" =~ url:[[:space:]]*\"(https://[^\"]+)\" ]]; then
2861+
if [[ -n "$current_tool" ]] && [[ "$line" =~ ^[[:space:]]*url:[[:space:]]*(.*)$ ]]; then
28622862
local refreshed_url="${BASH_REMATCH[1]}"
2863+
refreshed_url="${refreshed_url%%#*}"
2864+
refreshed_url="${refreshed_url%"${refreshed_url##*[![:space:]]}"}"
2865+
refreshed_url="${refreshed_url#"${refreshed_url%%[![:space:]]*}"}"
2866+
refreshed_url="${refreshed_url%\"}"
2867+
refreshed_url="${refreshed_url#\"}"
2868+
refreshed_url="${refreshed_url%\'}"
2869+
refreshed_url="${refreshed_url#\'}"
2870+
[[ "$refreshed_url" =~ ^https://[^[:space:]]+$ ]] || continue
2871+
28632872
local previous_url="${KNOWN_INSTALLERS[$current_tool]:-}"
28642873
if [[ "$previous_url" != "$refreshed_url" ]]; then
28652874
KNOWN_INSTALLERS["$current_tool"]="$refreshed_url"

tests/unit/lib/test_security.bats

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,19 @@ stub_acfs_curl_response() {
270270
# Need full 64-char sha256 for regex
271271
local sha1="1111111111111111111111111111111111111111111111111111111111111111"
272272
local sha2="2222222222222222222222222222222222222222222222222222222222222222"
273+
local sha3="3333333333333333333333333333333333333333333333333333333333333333"
273274

274275
cat > "$CHECKSUMS_FILE" <<EOF
275276
installers:
276277
tool1:
277278
url: "https://example.com/1"
278279
sha256: "$sha1"
279280
tool2:
280-
url: "https://example.com/2"
281+
url: 'https://example.com/2'
281282
sha256: "$sha2"
283+
tool3:
284+
url: https://example.com/3
285+
sha256: "$sha3"
282286
EOF
283287

284288
echo "DEBUG: CHECKSUMS_FILE=$CHECKSUMS_FILE" >&2
@@ -300,4 +304,12 @@ EOF
300304
local val2
301305
val2=$(get_checksum "tool2")
302306
assert_equal "$val2" "$sha2"
307+
308+
local val3
309+
val3=$(get_checksum "tool3")
310+
assert_equal "$val3" "$sha3"
311+
312+
assert_equal "${KNOWN_INSTALLERS[tool1]}" "https://example.com/1"
313+
assert_equal "${KNOWN_INSTALLERS[tool2]}" "https://example.com/2"
314+
assert_equal "${KNOWN_INSTALLERS[tool3]}" "https://example.com/3"
303315
}

tests/unit/lib/test_update.bats

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,37 @@ EOF
17441744
assert_output --partial "declare -a"
17451745
}
17461746

1747+
@test "update_sync_known_installer_urls_from_checksums: accepts quoted and unquoted urls" {
1748+
local checksums_file
1749+
checksums_file="$HOME/checksums.yaml"
1750+
1751+
cat > "$checksums_file" <<'EOF'
1752+
installers:
1753+
double_quoted:
1754+
url: "https://example.com/double.sh"
1755+
sha256: "2222222222222222222222222222222222222222222222222222222222222222"
1756+
single_quoted:
1757+
url: 'https://example.com/single.sh'
1758+
sha256: "3333333333333333333333333333333333333333333333333333333333333333"
1759+
unquoted:
1760+
url: https://example.com/unquoted.sh
1761+
sha256: "4444444444444444444444444444444444444444444444444444444444444444"
1762+
EOF
1763+
1764+
declare -gA KNOWN_INSTALLERS=(
1765+
[double_quoted]="https://example.invalid/old-double.sh"
1766+
[single_quoted]="https://example.invalid/old-single.sh"
1767+
[unquoted]="https://example.invalid/old-unquoted.sh"
1768+
)
1769+
1770+
update_sync_known_installer_urls_from_checksums "$checksums_file"
1771+
assert_equal "$?" "0"
1772+
1773+
assert_equal "${KNOWN_INSTALLERS[double_quoted]}" "https://example.com/double.sh"
1774+
assert_equal "${KNOWN_INSTALLERS[single_quoted]}" "https://example.com/single.sh"
1775+
assert_equal "${KNOWN_INSTALLERS[unquoted]}" "https://example.com/unquoted.sh"
1776+
}
1777+
17471778
@test "install.sh verifier refetches installer when fresh checksums change URL" {
17481779
local installer="$PROJECT_ROOT/install.sh"
17491780
local old_url="https://raw.githubusercontent.com/Dicklesworthstone/mcp_agent_mail/main/install.sh"

0 commit comments

Comments
 (0)