Skip to content

Commit eff5618

Browse files
committed
fix: check the wheel's sha256 in install-wheel.sh
The script downloaded the wheel without checking it. It now compares the file with the sha256 digest that the GitHub API lists for the release asset, and deletes it and fails on a mismatch or when no digest is listed. The fallback without jq also matched only compact JSON, while the API returns it with a space after each colon, so it never found a wheel.
1 parent b91f372 commit eff5618

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

‎docs/install-wheel.sh‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,26 @@ find_wheel_asset() {
179179
select(.name | test("^(" + $tool + "|" + $tool_us + ")-")) |
180180
select(.name | contains($platform)) |
181181
if $version != "" then select(.name | contains($version)) else . end |
182-
.browser_download_url + "|" + .name + "|" + (.size | tostring)
182+
.browser_download_url + "|" + .name + "|" + (.size | tostring) + "|" + (.digest // "")
183183
' | head -n1
184184
else
185-
# Fallback: use grep and sed (less reliable but works without jq)
186-
echo "$json_data" | grep -o '"browser_download_url":"[^"]*"' | sed 's/"browser_download_url":"//; s/"//' | while read -r url; do
185+
# Fallback: use grep and sed (less reliable but works without jq).
186+
# In each asset the API lists "digest" before "browser_download_url".
187+
local digest=""
188+
echo "$json_data" | grep -oE '"(digest|browser_download_url)": *"[^"]*"' | sed -E 's/^"([a-z_]+)": *"(.*)"$/\1 \2/' | while read -r key value; do
189+
if [[ "$key" == "digest" ]]; then
190+
digest="$value"
191+
continue
192+
fi
193+
local url="$value"
187194
local filename=$(echo "$url" | sed 's|.*/||')
188195
if [[ "$filename" == *".whl" ]] && [[ "$filename" == "$tool"* || "$filename" == "$tool_underscore"* ]] && [[ "$filename" == *"$platform_tag"* ]]; then
189196
if [[ -z "$version" ]] || [[ "$filename" == *"$version"* ]]; then
190-
echo "$url|$filename|unknown"
197+
echo "$url|$filename|unknown|$digest"
191198
break
192199
fi
193200
fi
201+
digest=""
194202
done
195203
fi
196204
}
@@ -221,6 +229,34 @@ download_file() {
221229
fi
222230
}
223231

232+
# Check a downloaded file against the sha256 digest GitHub lists for the asset
233+
verify_sha256() {
234+
local file="$1"
235+
local digest="$2"
236+
local expected="${digest#sha256:}"
237+
local actual
238+
239+
if [[ "$digest" != sha256:* ]]; then
240+
rm -f "$file"
241+
error "The release lists no sha256 digest for $(basename "$file"), so it cannot be checked"
242+
fi
243+
244+
if command_exists sha256sum; then
245+
actual=$(sha256sum "$file" | cut -d' ' -f1)
246+
elif command_exists shasum; then
247+
actual=$(shasum -a 256 "$file" | cut -d' ' -f1)
248+
else
249+
rm -f "$file"
250+
error "Neither sha256sum nor shasum found. One of them is needed to check the download."
251+
fi
252+
253+
if [[ "$actual" != "$expected" ]]; then
254+
rm -f "$file"
255+
error "Checksum mismatch for $(basename "$file"): expected sha256 $expected, got $actual"
256+
fi
257+
success "sha256 checked: $expected"
258+
}
259+
224260
# List available platforms
225261
list_platforms() {
226262
local json_data="$1"
@@ -239,7 +275,7 @@ list_platforms() {
239275
done
240276
else
241277
# Fallback without jq
242-
echo "$json_data" | grep -o '"name":"[^"]*\.whl"' | sed 's/"name":"//; s/"//' | grep -E "^($tool|${tool//-/_})-" | sed 's/.*py2\.py3-none-//; s/\.whl$//' | sort -u | while read -r platform; do
278+
echo "$json_data" | grep -oE '"name": *"[^"]*\.whl"' | sed -E 's/"name": *"//; s/"//' | grep -E "^($tool|${tool//-/_})-" | sed 's/.*py2\.py3-none-//; s/\.whl$//' | sort -u | while read -r platform; do
243279
echo " $platform"
244280
done
245281
fi
@@ -325,7 +361,7 @@ main() {
325361
if command_exists jq; then
326362
release_tag=$(echo "$release_info" | jq -r '.tag_name')
327363
else
328-
release_tag=$(echo "$release_info" | grep -o '"tag_name":"[^"]*"' | sed 's/"tag_name":"//; s/"//')
364+
release_tag=$(echo "$release_info" | grep -oE '"tag_name": *"[^"]*"' | sed -E 's/"tag_name": *"//; s/"//')
329365
fi
330366

331367
if [[ "$list_platforms_flag" == true ]]; then
@@ -357,14 +393,15 @@ main() {
357393
fi
358394

359395
# Parse wheel info
360-
IFS='|' read -r download_url filename file_size <<< "$wheel_info"
396+
IFS='|' read -r download_url filename file_size digest <<< "$wheel_info"
361397

362398
# Create output directory
363399
mkdir -p "$output_dir"
364400

365401
# Download the wheel
366402
local output_file="$output_dir/$filename"
367403
if download_file "$download_url" "$output_file"; then
404+
verify_sha256 "$output_file" "$digest"
368405
echo
369406
success "Wheel downloaded successfully!"
370407
echo "File: $output_file"

0 commit comments

Comments
 (0)