diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index 3c9e97b35..321f05be5 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -5,6 +5,7 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" LOCAL_BIN="${HOME}/.local/bin" +mkdir -p "${LOCAL_BIN}" export PATH="${LOCAL_BIN}:${PATH}:/usr/sbin:/sbin" with_db=false @@ -20,6 +21,32 @@ done log() { printf '==> %s\n' "$*"; } +# The Trunk launcher verifies the CLI against the sha256 pins in .trunk/trunk.yaml. +TRUNK_LAUNCHER_VERSION=1.3.5 +TRUNK_LAUNCHER_SHA256=700f8bbedfd226595999ea2202b9a7af75ba8398dd711f5f159f5bbba6cbf322 + +DOWNLOAD_DIR="$(mktemp -d)" +trap 'rm -rf "${DOWNLOAD_DIR}"' EXIT + +sha256_of() { + if command -v sha256sum >/dev/null; then sha256sum "$1"; else shasum -a 256 "$1"; fi | + cut -d' ' -f1 +} + +fetch_verified() { + local url="$1" want="$2" dest="$3" have + if [[ -z ${want} ]]; then + echo "No published checksum for ${url}" >&2 + exit 1 + fi + curl -fsSL "${url}" -o "${dest}" + have="$(sha256_of "${dest}")" + if [[ ${have} != "${want}" ]]; then + echo "Checksum mismatch for ${url}: expected '${want}', got '${have}'" >&2 + exit 1 + fi +} + as_root() { if [[ ${EUID} -eq 0 ]]; then "$@"; else sudo "$@"; fi } @@ -41,15 +68,29 @@ apt_install() { } setup_uv() { - local want have + local want have platform target url sum want="$(sed -n 's/^ARG UV_VERSION=//p' "${ROOT}/docker/Dockerfile")" have="$(uv --version 2>/dev/null || true)" have="${have#uv }" have="${have%% *}" if [[ ${have} == "${want}" ]]; then return 0; fi + platform="$(uname -sm)" + case "${platform}" in + "Linux x86_64") target=x86_64-unknown-linux-gnu ;; + "Linux aarch64") target=aarch64-unknown-linux-gnu ;; + "Darwin x86_64") target=x86_64-apple-darwin ;; + "Darwin arm64") target=aarch64-apple-darwin ;; + *) + echo "No uv build for ${platform}; install uv ${want} by hand" >&2 + exit 1 + ;; + esac + url="https://github.com/astral-sh/uv/releases/download/${want}/uv-${target}.tar.gz" log "Installing uv ${want} (found ${have:-none})" - curl -LsSf "https://astral.sh/uv/${want}/install.sh" | - env UV_UNMANAGED_INSTALL="${LOCAL_BIN}" sh >/dev/null + sum="$(curl -fsSL "${url}.sha256")" + fetch_verified "${url}" "${sum%% *}" "${DOWNLOAD_DIR}/uv.tar.gz" + tar -xzf "${DOWNLOAD_DIR}/uv.tar.gz" -C "${DOWNLOAD_DIR}" --strip-components=1 + install -m 755 "${DOWNLOAD_DIR}/uv" "${DOWNLOAD_DIR}/uvx" "${LOCAL_BIN}/" hash -r } @@ -61,7 +102,7 @@ setup_backend() { } setup_node() { - local want have full os arch dest + local want have full os arch dest file sum want="$(tr -d '[:space:]' <"${ROOT}/frontend/.nvmrc")" have="$(node --version 2>/dev/null || true)" if [[ ${have} == "v${want}."* ]]; then return 0; fi @@ -75,9 +116,11 @@ setup_node() { esac dest="${HOME}/.local/share/node/${full}" log "Installing Node ${full} (found ${have:-none})" - mkdir -p "${dest}" "${LOCAL_BIN}" - curl -fsSL "https://nodejs.org/dist/${full}/node-${full}-${os}-${arch}.tar.xz" | - tar -xJ -C "${dest}" --strip-components=1 + mkdir -p "${dest}" + file="node-${full}-${os}-${arch}.tar.xz" + sum="$(curl -fsSL "https://nodejs.org/dist/${full}/SHASUMS256.txt" | awk -v f="${file}" '$2 == f { print $1 }')" + fetch_verified "https://nodejs.org/dist/${full}/${file}" "${sum}" "${DOWNLOAD_DIR}/${file}" + tar -xJf "${DOWNLOAD_DIR}/${file}" -C "${dest}" --strip-components=1 ln -sf "${dest}/bin/node" "${dest}/bin/npm" "${dest}/bin/npx" "${LOCAL_BIN}/" hash -r } @@ -95,9 +138,9 @@ setup_frontend() { setup_trunk() { command -v trunk >/dev/null && return 0 log "Installing Trunk and its linters" - mkdir -p "${LOCAL_BIN}" - curl -fsSL https://trunk.io/releases/trunk -o "${LOCAL_BIN}/trunk" - chmod +x "${LOCAL_BIN}/trunk" + fetch_verified "https://trunk.io/releases/launcher/${TRUNK_LAUNCHER_VERSION}/trunk" \ + "${TRUNK_LAUNCHER_SHA256}" "${DOWNLOAD_DIR}/trunk" + install -m 755 "${DOWNLOAD_DIR}/trunk" "${LOCAL_BIN}/trunk" hash -r (cd "${ROOT}" && trunk install --ci >/dev/null 2>&1) || log "Trunk could not download its linters (network policy?); trunk check will retry on first use"