install-jq/scripts/windowsish-17.ps1
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

124 lines
3.3 KiB
PowerShell
Executable file

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
Write-Host "::group::Prep"
if (-not ($Env:JQ_VERSION -match '^[0-9]+\.[0-9]+(\.[0-9]+)*$')) {
Write-Host "Invalid JQ_VERSION: '$Env:JQ_VERSION'. Expected a version string like '1.7.1'."
exit 1
}
$_base_url = "https://github.com/jqlang/jq/releases/download"
$_arch_env = ($Env:RUNNER_ARCH).ToLower()
# validate input and prepare some vars
switch ($_arch_env)
{
"x86" {
$_arch = "i386"
}
"386" {
$_arch = "i386"
}
"x64" {
$_arch = "amd64"
}
"x86_64" {
$_arch = "amd64"
}
"amd64" {
$_arch = "amd64"
}
default {
Write-Host "Cannot handle arch of type $Env:RUNNER_ARCH"
Write-Host "Expected one of: [ x86 386 x64 x86_64 amd64 ]"
exit 1
}
}
# build bin name
$_bin_name = "jq-windows-${_arch}.exe"
# build download vars
$_dl_name = "${_bin_name}"
$_dl_path = "$Env:RUNNER_TEMP\${_dl_name}"
$_dl_url = "${_base_url}/jq-$Env:JQ_VERSION/${_dl_name}"
Write-Host "::endgroup::"
# download artifact
Write-Host "::group::Downloading jq"
Write-Host "Src: ${_dl_url}"
Write-Host "Dst: ${_dl_path}"
Invoke-WebRequest -Uri "${_dl_url}" -OutFile "${_dl_path}"
Write-Host "::endgroup::"
Write-Host "::group::Verifying checksum"
$_sha_url = "${_base_url}/jq-$Env:JQ_VERSION/sha256sum.txt"
$_sha_path = "$Env:RUNNER_TEMP\jq-$Env:JQ_VERSION.sha256sum.txt"
$_sha_ok = $true
try {
Invoke-WebRequest -Uri "${_sha_url}" -OutFile "${_sha_path}"
} catch {
$_sha_ok = $false
if ($_.Exception.Response -and ([int]$_.Exception.Response.StatusCode -eq 404)) {
$_sha_ok = $false
} else {
Write-Host "Failed to download sha256sum.txt from ${_sha_url}"
throw
}
}
if ($_sha_ok -and (Test-Path -LiteralPath "${_sha_path}")) {
$_expected = $null
foreach ($_line in Get-Content -LiteralPath "${_sha_path}") {
$_parts = $_line -split '\s+'
if ($_parts.Length -ge 2 -and $_parts[1] -eq "${_bin_name}") {
$_expected = $_parts[0]
break
}
}
if (-not $_expected) {
Write-Host "Could not find checksum for '${_bin_name}' in ${_sha_url}"
exit 1
}
$_actual = (Get-FileHash -Algorithm SHA256 -LiteralPath "${_dl_path}").Hash.ToLower()
if ($_expected.ToLower() -ne $_actual) {
Write-Host "Checksum verification failed for '${_bin_name}'"
Write-Host "Expected: $_expected"
Write-Host "Actual: $_actual"
exit 1
}
Write-Host "Checksum verified for '${_bin_name}'"
} else {
Write-Host "WARNING: No sha256sum.txt found at ${_sha_url}; skipping checksum verification."
}
Write-Host "::endgroup::"
# install into tool cache
Write-Host "::group::Copying to tool cache"
Write-Host "Creating tool cache directory $Env:RUNNER_TOOL_CACHE\jq\"
New-Item "$Env:RUNNER_TOOL_CACHE\jq\" -ItemType Directory -Force
Write-Host "Installing into tool cache:"
Write-Host "Src: $Env:RUNNER_TEMP\${_bin_name}"
Write-Host "Dst: $Env:RUNNER_TOOL_CACHE\jq\jq.exe"
Move-Item -Force -LiteralPath "$Env:RUNNER_TEMP\${_bin_name}" -Destination "$Env:RUNNER_TOOL_CACHE\jq\jq.exe"
Write-Host "Adding $Env:RUNNER_TOOL_CACHE\jq\ to path..."
Add-Content "$Env:GITHUB_PATH" "$Env:RUNNER_TOOL_CACHE\jq\"
Write-Host "::endgroup::"