From d17fb7d4803e3f6791871cc7979381d1ec9ec0a4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 17 Nov 2026 14:38:06 +0000 Subject: [PATCH 1/2] chore: sync defense-in-depth files from agentic Copy the setup script and the Safe Chain section from jaredwray/agentic at 01b485d. Co-authored-by: Jared Wray --- AGENTS.md | 1 + scripts/setup-cloud-environment.sh | 163 ++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 16 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index dd78805..8ee1964 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -55,6 +55,7 @@ Never bypass the shims. - Keep `~/.safe-chain/shims` first on `PATH`. - Run `npm`, `npx`, `pnpm`, and `pnpx` only through the shims. Do not run a different copy by its full path. +- Run `pip`, `pip3`, `pipx`, `uv`, `uvx`, `poetry`, and `python -m pip` only through the shims. - Do not install a package with `curl | sh` or with a package manager that has no shim. - If Safe Chain blocks a package, stop. Do not use a different command, path, or package to get the same code. Tell the user the package name and the Safe Chain message. diff --git a/scripts/setup-cloud-environment.sh b/scripts/setup-cloud-environment.sh index cdb159d..4b5d63b 100755 --- a/scripts/setup-cloud-environment.sh +++ b/scripts/setup-cloud-environment.sh @@ -2,6 +2,10 @@ # setup-cloud-environment.sh — Aikido Safe Chain bootstrap for Codespaces, Cursor Cloud Agents, # Claude Code on the web, and Codex cloud. # +# Safe Chain shims the npm and Python package managers. This script installs it, then runs the +# frozen install of every tracked lockfile: pnpm-lock.yaml, uv.lock, and poetry.lock. A root +# with only requirements files gets the shims and no install. +# # Fail closed: never install dependencies unless Safe Chain shims are on PATH. # Copied into the target repo as scripts/setup-cloud-environment.sh. @@ -29,13 +33,62 @@ persist_shim_path() { # refuse to run until Safe Chain's setup-ci replaces them. if [[ -n "${CLAUDE_ENV_FILE:-}" ]]; then mkdir -p "$SAFE_CHAIN_SHIMS" - for cmd in npm npx pnpm pnpx; do + for cmd in npm npx pnpm pnpx pip pip3 pipx uv uvx poetry pdm; do if [[ ! -e "${SAFE_CHAIN_SHIMS}/${cmd}" ]]; then printf '#!/bin/sh\necho "error: Safe Chain is not set up, so %s is blocked; run bash ./scripts/setup-cloud-environment.sh" >&2\nexit 1\n' \ "$cmd" > "${SAFE_CHAIN_SHIMS}/${cmd}" chmod +x "${SAFE_CHAIN_SHIMS}/${cmd}" fi done + # `python -m pip` never reaches the pip stub, so python and python3 get one that runs Python + # but refuses a package manager run as a module. + python_stub=$(cat <<'STUB' +#!/usr/bin/env bash +shims="__SHIMS__" +blocked() { + echo "error: Safe Chain is not set up, so __CMD__ -m $1 is blocked; run bash ./scripts/setup-cloud-environment.sh" >&2 + exit 1 +} +prev="" +for arg in "$@"; do + case "$prev" in + -m) + case "${arg%%.*}" in pip | pipx | uv | poetry | pdm) blocked "$arg" ;; esac + break + ;; + -W | -X) + prev="" + continue + ;; + esac + case "$arg" in + -m?*) + module="${arg#-m}" + case "${module%%.*}" in pip | pipx | uv | poetry | pdm) blocked "$module" ;; esac + break + ;; + -m | -[!-]*m) prev="-m" ;; + -W | -X) prev="$arg" ;; + -c | --) break ;; + -*) prev="" ;; + *) break ;; + esac +done +real=$(PATH="${PATH//"$shims":/}"; command -v __CMD__) +if [[ -z "$real" || "$real" == "$shims/__CMD__" ]]; then + echo "error: could not find __CMD__ outside the Safe Chain shims" >&2 + exit 1 +fi +exec "$real" "$@" +STUB + ) + python_stub=${python_stub//__SHIMS__/$SAFE_CHAIN_SHIMS} + for cmd in python python3; do + if [[ ! -e "${SAFE_CHAIN_SHIMS}/${cmd}" ]]; then + printf '%s\n' "${python_stub//__CMD__/$cmd}" > "${SAFE_CHAIN_SHIMS}/${cmd}" + chmod +x "${SAFE_CHAIN_SHIMS}/${cmd}" + fi + done persist_shim_path "$CLAUDE_ENV_FILE" fi @@ -43,23 +96,67 @@ if git_root=$(git rev-parse --show-toplevel 2>/dev/null); then cd "$git_root" fi -if [[ ! -f pnpm-lock.yaml ]]; then - echo "error: pnpm-lock.yaml is required; refusing to install without a frozen lockfile" >&2 +# Tracked files with these names at any depth; outside a git checkout, the current directory only. +find_files() { + local name file + if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + for name in "$@"; do + git ls-files -- ":(glob)**/${name}" + done + else + for name in "$@"; do + for file in $name; do + if [[ -e "$file" ]]; then + printf '%s\n' "$file" + fi + done + done + fi +} + +lockfiles=() +while IFS= read -r lockfile; do + lockfiles+=("$lockfile") +done < <(find_files pnpm-lock.yaml uv.lock poetry.lock) +requirements=$(find_files 'requirements*.txt' 'requirements*.in') + +if [[ ${#lockfiles[@]} -eq 0 && -z "$requirements" ]]; then + echo "error: no pnpm-lock.yaml, uv.lock, poetry.lock, or requirements file found; refusing to install without a frozen lockfile" >&2 exit 1 fi -if ! command -v pnpm >/dev/null \ - && [[ -f package.json ]] \ - && grep -q '"packageManager"' package.json \ - && command -v corepack >/dev/null; then - mkdir -p "$SAFE_CHAIN_BIN" - corepack enable --install-directory "$SAFE_CHAIN_BIN" pnpm - export PATH="${SAFE_CHAIN_BIN}:${PATH}" -fi +# Print the first lockfile with this name; fail when there is none. +first_lock() { + local lockfile + for lockfile in ${lockfiles[@]+"${lockfiles[@]}"}; do + if [[ "${lockfile##*/}" == "$1" ]]; then + printf '%s\n' "$lockfile" + return 0 + fi + done + return 1 +} -if ! command -v pnpm >/dev/null; then - echo "error: pnpm is required on PATH before Safe Chain can wrap it" >&2 - exit 1 +# True when the tool is on PATH outside the Safe Chain shims. +real_tool() { + PATH="${PATH//${SAFE_CHAIN_SHIMS}:/}" command -v "$1" >/dev/null +} + +if pnpm_lock=$(first_lock pnpm-lock.yaml); then + pnpm_manifest="$(dirname "$pnpm_lock")/package.json" + if ! real_tool pnpm \ + && [[ -f "$pnpm_manifest" ]] \ + && grep -q '"packageManager"' "$pnpm_manifest" \ + && command -v corepack >/dev/null; then + mkdir -p "$SAFE_CHAIN_BIN" + corepack enable --install-directory "$SAFE_CHAIN_BIN" pnpm + export PATH="${SAFE_CHAIN_BIN}:${PATH}" + fi + + if ! real_tool pnpm; then + echo "error: pnpm is required on PATH before Safe Chain can wrap it" >&2 + exit 1 + fi fi installer=$(mktemp) @@ -86,5 +183,39 @@ if [[ -n "${GITHUB_PATH:-}" ]]; then printf '%s\n' "$SAFE_CHAIN_BIN" >> "$GITHUB_PATH" fi -pnpm safe-chain-verify -pnpm install --frozen-lockfile +# uv and Poetry can come from pipx once the shims are in place, so that install is protected too. +for tool in uv poetry; do + if ! lockfile=$(first_lock "${tool}.lock"); then + continue + fi + if ! real_tool "$tool" && real_tool pipx; then + mkdir -p "$SAFE_CHAIN_BIN" + PIPX_BIN_DIR="$SAFE_CHAIN_BIN" pipx install "$tool" + fi + if ! real_tool "$tool"; then + echo "error: ${tool} is required on PATH to install from ${lockfile}" >&2 + exit 1 + fi +done + +if [[ ${#lockfiles[@]} -eq 0 ]]; then + pip safe-chain-verify +fi + +for lockfile in ${lockfiles[@]+"${lockfiles[@]}"}; do + root=$(dirname "$lockfile") + case "${lockfile##*/}" in + pnpm-lock.yaml) + pnpm safe-chain-verify + (cd "$root" && pnpm install --frozen-lockfile) + ;; + uv.lock) + uv safe-chain-verify + (cd "$root" && uv sync --locked) + ;; + poetry.lock) + poetry safe-chain-verify + (cd "$root" && poetry sync) + ;; + esac +done From ae5c439497eb3bed05fedd1883de8fe952052f9b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 17 Nov 2026 14:43:01 +0000 Subject: [PATCH 2/2] fix: block pip after a Python hash option The Python stub now skips the value of --check-hash-based-pycs before it scans for -m pip. Co-authored-by: Jared Wray --- scripts/setup-cloud-environment.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup-cloud-environment.sh b/scripts/setup-cloud-environment.sh index 4b5d63b..efa4404 100755 --- a/scripts/setup-cloud-environment.sh +++ b/scripts/setup-cloud-environment.sh @@ -56,7 +56,7 @@ for arg in "$@"; do case "${arg%%.*}" in pip | pipx | uv | poetry | pdm) blocked "$arg" ;; esac break ;; - -W | -X) + -W | -X | --check-hash-based-pycs) prev="" continue ;; @@ -68,7 +68,7 @@ for arg in "$@"; do break ;; -m | -[!-]*m) prev="-m" ;; - -W | -X) prev="$arg" ;; + -W | -X | --check-hash-based-pycs) prev="$arg" ;; -c | --) break ;; -*) prev="" ;; *) break ;;