🐛 fix(docker_runner): validate act_runner binary arch

This commit is contained in:
csh
2026-05-22 11:58:01 +08:00
parent 0579c2dc97
commit e5db752c55
3 changed files with 166 additions and 2 deletions
+76
View File
@@ -20,6 +20,31 @@ assert_eq() {
fi
}
make_fake_elf() {
local path=$1
local arch=$2
local machine_bytes=""
case "${arch}" in
amd64)
machine_bytes='\076\000'
;;
arm64)
machine_bytes='\267\000'
;;
arm-7)
machine_bytes='\050\000'
;;
*)
echo "FAIL: unsupported fake ELF arch ${arch}" >&2
exit 1
;;
esac
printf '\177ELF\002\001\001\000\000\000\000\000\000\000\000\000\002\000%b' \
"${machine_bytes}" > "${path}"
}
test_extract_versions_from_listing() {
local html
html=$(cat <<'EOF'
@@ -90,10 +115,61 @@ test_resolve_latest_version_or_fallback_uses_fallback() {
"resolve_latest_version_or_fallback should fall back when detected version is invalid"
}
test_detect_binary_arch_from_file() {
local temp_root amd64_file arm64_file arm7_file text_file
temp_root=$(mktemp -d)
amd64_file="${temp_root}/act_runner-amd64"
arm64_file="${temp_root}/act_runner-arm64"
arm7_file="${temp_root}/act_runner-arm7"
text_file="${temp_root}/not-elf"
make_fake_elf "${amd64_file}" "amd64"
make_fake_elf "${arm64_file}" "arm64"
make_fake_elf "${arm7_file}" "arm-7"
printf 'not an elf\n' > "${text_file}"
assert_eq "amd64" "$(detect_binary_arch_from_file "${amd64_file}")" \
"detect_binary_arch_from_file should recognize amd64 ELF binaries"
assert_eq "arm64" "$(detect_binary_arch_from_file "${arm64_file}")" \
"detect_binary_arch_from_file should recognize arm64 ELF binaries"
assert_eq "arm-7" "$(detect_binary_arch_from_file "${arm7_file}")" \
"detect_binary_arch_from_file should recognize arm-7 ELF binaries"
assert_eq "unknown" "$(detect_binary_arch_from_file "${text_file}")" \
"detect_binary_arch_from_file should reject non-ELF files"
rm -rf "${temp_root}"
}
test_binary_arch_matches_target() {
local temp_root amd64_file arm64_file
temp_root=$(mktemp -d)
amd64_file="${temp_root}/act_runner-amd64"
arm64_file="${temp_root}/act_runner-arm64"
make_fake_elf "${amd64_file}" "amd64"
make_fake_elf "${arm64_file}" "arm64"
if ! binary_arch_matches_target "${amd64_file}" "amd64"; then
echo "FAIL: binary_arch_matches_target should accept matching architectures" >&2
exit 1
fi
if binary_arch_matches_target "${arm64_file}" "amd64"; then
echo "FAIL: binary_arch_matches_target should reject mismatched architectures" >&2
exit 1
fi
rm -rf "${temp_root}"
}
test_extract_versions_from_listing
test_pick_latest_version
test_extract_version_number_accepts_v_prefix
test_resolve_latest_version_or_fallback_prefers_latest
test_resolve_latest_version_or_fallback_uses_fallback
test_detect_binary_arch_from_file
test_binary_arch_matches_target
echo "upgrade_test.sh: PASS"