install-jq/scripts/unixish.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

120 lines
2.1 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.6\"."
exit 1
fi
_base_url='https://github.com/stedolan/jq/releases/download'
_os=
_arch=
_bin_name=
_dl_name=
_dl_path=
_dl_url=
case $RUNNER_OS in
[Ll]inux)
_os='linux'
;;
mac[Oo][Ss])
_os='osx'
;;
*)
echo "Cannot handle OS of type $RUNNER_OS"
echo "Expected one of: [ Linux macOS ]"
exit 1
;;
esac
case $RUNNER_ARCH in
'X86')
_arch='386'
;;
'X64')
_arch='amd64'
;;
'ARM')
_arch='arm'
;;
'ARM64')
_arch='arm64'
;;
*)
echo "Cannot handle arch of type $RUNNER_ARCH"
echo "Expected one of: [ X86 X64 ARM ARM64 ]"
exit 1
;;
esac
# determine binary name
if [ "${_os}" = "linux" ]; then
case "${_arch}" in
'386')
_bin_name="jq-linux32"
;;
'amd64')
_bin_name="jq-linux64"
;;
*)
echo "Cannot handle \"$RUNNER_ARCH\" architecture for os \"$RUNNER_OS\""
exit 1
;;
esac
else
case "${_arch}" in
'amd64')
_bin_name="jq-osx-amd64"
;;
*)
echo "Cannot handle \"$RUNNER_ARCH\" architecture for os \"$RUNNER_OS\""
exit 1
;;
esac
fi
_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::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::'