Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
163 changes: 147 additions & 16 deletions scripts/setup-cloud-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -29,37 +33,130 @@ 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 | --check-hash-based-pycs)
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 | --check-hash-based-pycs) prev="$arg" ;;
-c | --) break ;;
-*) prev="" ;;
*) break ;;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
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

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)
Expand All @@ -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
Loading