install-jq/scripts/unixish-17.sh
mountaintopsolutions 50f40aa247
fix(security): verify download integrity and harden install scripts (#25)
* fix(security): verify download integrity and harden install scripts

Addresses gaps in the jq install path that could allow a corrupted or
tampered binary to be installed into the tool cache and GITHUB_PATH.

- Add SHA256 checksum verification for jq 1.7+ downloads (unixish-17.sh,
  windowsish-17.ps1) against each release's sha256sum.txt. Fails closed on
  mismatch; warns and proceeds only when no checksum file is published
  (e.g. jq 1.7.0 and the legacy stedolan 1.5/1.6 releases).
- Harden curl in unixish.sh / unixish-17.sh with --fail and --retry so a
  404/5xx response no longer exits 0 and writes an error page to disk as
  the binary.
- Validate the JQ_VERSION input (digits and dots only) across all four
  install scripts, rejecting malformed or path-traversal values before
  they reach download URLs.
- Bump the test-container image from node:20 to node:24 so JS actions run
  on a supported Node runtime.

* fix(security): use portable sha256 for macOS in unixish-17.sh

sha256sum is a GNU/coreutils binary and is absent on stock macOS (e.g.
the macos-13/macos-14 runner images), which broke checksum verification
there with "sha256sum: command not found". Newer images (macos-15/26)
ship coreutils and happened to work, but the action must not depend on
that.

Add a _compute_sha256 helper that prefers sha256sum (Linux) and falls
back to shasum -a 256 (ships with macOS via Perl) then openssl, so
integrity verification works across all supported runners.

* ci: drop macos-13 from test matrix (runner image decommissioned)

macOS 13 runner image is closing down (see
https://github.blog/changelog/2025-09-19-github-actions-macos-13-runner-image-is-closing-down/),
so macos-13 jobs sit perpetually queued and can no longer be allocated.
Remove it from the matrix; macos-14/15/26 remain.

* fix(security): address review feedback on validation and ps1 strings

Addresses CoPilot review comments on PR #25:

- Tighten JQ_VERSION validation in unixish.sh and unixish-17.sh to
  require dot-separated numeric components (rejects ".", "..", "1..7",
  "1.7.", trailing dots) via: printf '%s' "$JQ_VERSION" | grep -Eq
  '^[0-9]+\.[0-9]+(\.[0-9]+)*$'. Uses printf rather than echo so
  values like "-n" are passed through to grep rather than consumed as
  an echo flag.
- Add the missing JQ_VERSION validation to windowsish-17.ps1 (the 1.7+
  Windows path) before $_dl_url is constructed, matching the check
  already present in windowsish.ps1.
- Fix PowerShell double-quote escaping in error/checksum messages:
  \" is not a valid escape in PowerShell double-quoted strings and
  rendered literal backslashes. Use single quotes around interpolated
  values instead (e.g. 'jq-windows-amd64.exe') so output is clean.

All four install scripts now enforce the same version regex and render
clean diagnostic output.

* fix(security): fail closed on checksum download errors (non-404)

The checksum-file fetch treated any download failure as 'checksum file
not published' and silently skipped verification — a fail-open path
where a transient network error or intentional disruption to the
checksum download would bypass integrity verification while the
(possibly tampered) binary still installed.

- windowsish-17.ps1: in the catch, only treat HTTP 404 as
  'not published' (skip with warning); any other failure prints a
  message and throws to abort the install. (Adopts CoPilot's
  suggestion.)
- unixish-17.sh: same fail-closed logic — capture curl's
  %{http_code}; 200 verifies, 404 warns+skips, anything else (incl.
  000 for DNS/timeout) exits 1. Ports the fix CoPilot flagged on the
  PowerShell script to the Unix script, which had the identical gap.
2026-06-24 13:26:34 -05:00

147 lines
3.4 KiB
Shell
Executable file

#!/bin/sh
set -e
echo '::group::Prep'
# validate input and prepare some vars
if ! printf '%s' "$JQ_VERSION" | grep -Eq '^[0-9]+\.[0-9]+(\.[0-9]+)*$'; then
echo "Invalid JQ_VERSION: \"$JQ_VERSION\". Expected a version string like \"1.7.1\"."
exit 1
fi
# Portable SHA256 digest: prefer sha256sum (Linux), fall back to shasum
# (macOS, ships with the OS via Perl), then openssl. Prints only the hex
# digest and returns non-zero if no tool is available.
_compute_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
elif command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$1" | awk '{print $NF}'
else
return 1
fi
}
_base_url='https://github.com/jqlang/jq/releases/download'
_arch_env="$(echo "$RUNNER_ARCH" | tr '[:upper:]' '[:lower:]')"
_os=
_arch=
_bin_name=
_dl_name=
_dl_path=
_dl_url=
case $RUNNER_OS in
[Ll]inux)
_os='linux'
;;
mac[Oo][Ss])
_os='macos'
;;
*)
echo "Cannot handle OS of type $RUNNER_OS"
echo "Expected one of: [ Linux macOS ]"
exit 1
;;
esac
case "${_arch_env}" in
'x86'|'386')
_arch='i386'
;;
'x64'|'amd64'|'x86_64')
_arch='amd64'
;;
'arm'|'armhf')
_arch='armhf'
;;
'armel')
_arch='armel'
;;
'arm64'|'aarch64')
_arch='arm64'
;;
*)
echo "Cannot handle arch of type $RUNNER_ARCH"
echo "Expected one of: [ x86 386 x64 amd64 x86_64 arm armhf armel arm64 aarch64 ]"
exit 1
;;
esac
# build bin name
_bin_name="jq-${_os}-${_arch}"
# build download vars
_dl_name="${_bin_name}"
_dl_path="$RUNNER_TEMP/${_dl_name}"
_dl_url="${_base_url}/jq-$JQ_VERSION/${_dl_name}"
echo '::endgroup::'
echo '::group::Downloading jq'
echo "Src: ${_dl_url}"
echo "Dst: ${_dl_path}"
curl -fsSL --retry 3 "${_dl_url}" -o "${_dl_path}"
echo '::endgroup::'
echo '::group::Verifying checksum'
_sha_url="${_base_url}/jq-$JQ_VERSION/sha256sum.txt"
_sha_path="$RUNNER_TEMP/jq-$JQ_VERSION.sha256sum.txt"
_sha_http_code="$(curl -sSL -o "${_sha_path}" -w '%{http_code}' --retry 3 "${_sha_url}" || true)"
if [ "${_sha_http_code}" = '200' ]; then
_expected="$(grep -E " ${_bin_name}\$" "${_sha_path}" | head -n1 | awk '{print $1}' || true)"
if [ -z "${_expected}" ]; then
echo "Could not find checksum for \"${_bin_name}\" in ${_sha_url}"
exit 1
fi
_actual="$(_compute_sha256 "${_dl_path}")" || {
echo "No SHA256 tool available (sha256sum, shasum, openssl)."
exit 1
}
if [ "${_expected}" != "${_actual}" ]; then
echo "Checksum verification failed for \"${_bin_name}\""
echo "Expected: ${_expected}"
echo "Actual: ${_actual}"
exit 1
fi
echo "Checksum verified for \"${_bin_name}\""
elif [ "${_sha_http_code}" = '404' ]; then
echo "WARNING: No sha256sum.txt found at ${_sha_url}; skipping checksum verification."
else
echo "Failed to download sha256sum.txt from ${_sha_url} (HTTP ${_sha_http_code})"
exit 1
fi
echo '::endgroup::'
echo '::group::Copying to tool cache'
echo "Creating tool cache directory $RUNNER_TOOL_CACHE/jq"
mkdir -p "$RUNNER_TOOL_CACHE/jq"
echo "Installing into tool cache:"
echo "Src: $RUNNER_TEMP/${_bin_name}"
echo "Dst: $RUNNER_TOOL_CACHE/jq/jq"
mv "$RUNNER_TEMP/${_bin_name}" "$RUNNER_TOOL_CACHE/jq/jq"
chmod +x "$RUNNER_TOOL_CACHE/jq/jq"
echo "Adding $RUNNER_TOOL_CACHE/jq to path..."
echo "$RUNNER_TOOL_CACHE/jq" >> $GITHUB_PATH
echo '::endgroup::'