|
| 1 | +#!/usr/bin/env bash |
| 2 | +# OpenCode 1.18.x embeds Bun 1.3.14, which copies bundled native libraries to a |
| 3 | +# new randomly named temp file on every load and never deletes them |
| 4 | +# (anomalyco/opencode#42700, #49283). Every ODS-managed OpenCode launcher must |
| 5 | +# confine those copies to an ODS-owned BUN_TMPDIR that is emptied on start. |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 9 | +SERVICE="$ROOT_DIR/opencode/opencode-web.service" |
| 10 | +MACOS_INSTALLER="$ROOT_DIR/installers/macos/install-macos.sh" |
| 11 | +MACOS_CONSTANTS="$ROOT_DIR/installers/macos/lib/constants.sh" |
| 12 | +WINDOWS_PHASE="$ROOT_DIR/installers/windows/phases/07-devtools.ps1" |
| 13 | + |
| 14 | +pass() { echo "[PASS] $1"; } |
| 15 | +fail() { echo "[FAIL] $1" >&2; exit 1; } |
| 16 | +mode_of() { python3 -c 'import os, stat, sys; print(oct(stat.S_IMODE(os.stat(sys.argv[1]).st_mode)))' "$1"; } |
| 17 | +entries_in() { find "$1" -mindepth 1 -maxdepth 1 | wc -l | tr -d ' '; } |
| 18 | +leak_into() { printf 'stale native library copy' > "$1/.0123456789abcdef-00000000.so"; } |
| 19 | + |
| 20 | +scratch="$(mktemp -d)" |
| 21 | +trap 'rm -rf "$scratch"' EXIT |
| 22 | + |
| 23 | +echo "=== OpenCode BUN_TMPDIR confinement ===" |
| 24 | + |
| 25 | +# --- Linux systemd user unit, rendered like installers/phases/07-devtools.sh --- |
| 26 | +home="$scratch/home with space" |
| 27 | +mkdir -p "$home" |
| 28 | +unit="$scratch/opencode-web.service" |
| 29 | +sed -e "s|__HOME__|$home|g" -e 's|__OPENCODE_BIN__|/opt/oc/opencode|g' \ |
| 30 | + -e 's|__OPENCODE_BIN_DIR__|/opt/oc|g' "$SERVICE" > "$unit" |
| 31 | +linux_dir="$home/.cache/ods/opencode-bun-tmp" |
| 32 | + |
| 33 | +grep -Fxq "Environment=\"BUN_TMPDIR=$linux_dir\"" "$unit" \ |
| 34 | + || fail "Linux unit must set an ODS-owned BUN_TMPDIR" |
| 35 | +if grep -Eq '^Environment="?TMPDIR=' "$unit"; then |
| 36 | + fail "Linux unit must leave TMPDIR for OpenCode tools unchanged" |
| 37 | +fi |
| 38 | +grep -q '^PrivateTmp=true$' "$unit" || fail "Linux unit must keep PrivateTmp=true" |
| 39 | + |
| 40 | +# Run the unit's own ExecStartPre/ExecStopPost commands (plain double-quoted |
| 41 | +# arguments, which systemd and sh split identically). |
| 42 | +run_directives() { |
| 43 | + local key="$1" line found=0 |
| 44 | + while IFS= read -r line; do |
| 45 | + eval "${line#"$key="}" |
| 46 | + found=1 |
| 47 | + done < <(grep "^$key=" "$unit") |
| 48 | + [[ "$found" == 1 ]] || fail "Linux unit is missing $key" |
| 49 | +} |
| 50 | + |
| 51 | +mkdir -p "$linux_dir" |
| 52 | +leak_into "$linux_dir" |
| 53 | +run_directives ExecStartPre |
| 54 | +[[ -d "$linux_dir" && "$(entries_in "$linux_dir")" == 0 ]] \ |
| 55 | + || fail "Linux ExecStartPre must leave an empty BUN_TMPDIR" |
| 56 | +[[ "$(mode_of "$linux_dir")" == "0o700" ]] || fail "Linux BUN_TMPDIR must be private (0700)" |
| 57 | +leak_into "$linux_dir" |
| 58 | +run_directives ExecStopPost |
| 59 | +[[ ! -e "$linux_dir" ]] || fail "Linux ExecStopPost must remove BUN_TMPDIR" |
| 60 | +pass "Linux unit empties BUN_TMPDIR before start and removes it after stop" |
| 61 | + |
| 62 | +# --- macOS LaunchAgent, rendered from the installer heredoc --- |
| 63 | +# shellcheck disable=SC2016 # literal $HOME in the constants file |
| 64 | +grep -Fxq 'OPENCODE_BUN_TMPDIR="$HOME/Library/Caches/ODS/opencode-bun-tmp"' "$MACOS_CONSTANTS" \ |
| 65 | + || fail "macOS BUN_TMPDIR must live in the user's ODS cache" |
| 66 | +mac_dir="$scratch/mac bun tmp" |
| 67 | +fake_bin="$scratch/fake-opencode" |
| 68 | +record="$scratch/fake-opencode.record" |
| 69 | +cat > "$fake_bin" <<'FAKE' |
| 70 | +#!/bin/sh |
| 71 | +{ |
| 72 | + printf 'BUN_TMPDIR=%s\n' "$BUN_TMPDIR" |
| 73 | + printf 'ENTRIES=%s\n' "$(find "$BUN_TMPDIR" -mindepth 1 -maxdepth 1 | wc -l | tr -d ' ')" |
| 74 | + printf 'ARGS=%s\n' "$*" |
| 75 | +} > "$ODS_TEST_RECORD" |
| 76 | +printf 'fresh copy' > "$BUN_TMPDIR/.fedcba9876543210-00000000.so" |
| 77 | +FAKE |
| 78 | +chmod +x "$fake_bin" |
| 79 | + |
| 80 | +{ |
| 81 | + echo 'cat <<PLIST_EOF' |
| 82 | + awk '/<<PLIST_EOF$/{inside=1; next} /^PLIST_EOF$/{inside=0} inside' "$MACOS_INSTALLER" |
| 83 | + echo 'PLIST_EOF' |
| 84 | +} > "$scratch/render-plist.sh" |
| 85 | +OPENCODE_PLIST_LABEL="com.ods.opencode-web" OPENCODE_BIN="$fake_bin" \ |
| 86 | + OPENCODE_BUN_TMPDIR="$mac_dir" INSTALL_DIR="$scratch" HOME="$scratch" \ |
| 87 | + OPENCODE_LAUNCHD_PATH="/usr/bin:/bin" bash "$scratch/render-plist.sh" > "$scratch/opencode.plist" |
| 88 | + |
| 89 | +# plistlib also proves the rendered XML (escaping, comments) is a valid plist. |
| 90 | +python3 - "$scratch/opencode.plist" > "$scratch/program-args.txt" <<'PY' |
| 91 | +import plistlib, sys |
| 92 | +with open(sys.argv[1], 'rb') as handle: |
| 93 | + plist = plistlib.load(handle) |
| 94 | +for value in plist['ProgramArguments']: |
| 95 | + print(value) |
| 96 | +PY |
| 97 | +program_args=() |
| 98 | +while IFS= read -r value; do |
| 99 | + program_args+=("$value") |
| 100 | +done < "$scratch/program-args.txt" |
| 101 | +[[ "${program_args[0]}" == "/bin/sh" && "${program_args[1]}" == "-c" \ |
| 102 | + && "${program_args[3]}" == "ods-opencode-web" && "${program_args[4]}" == "$mac_dir" \ |
| 103 | + && "${program_args[5]}" == "$fake_bin" ]] \ |
| 104 | + || fail "macOS LaunchAgent must start OpenCode through the BUN_TMPDIR wrapper" |
| 105 | +[[ "${program_args[*]:6}" == "web --port 3003 --hostname 127.0.0.1" ]] \ |
| 106 | + || fail "macOS LaunchAgent must keep the loopback web command" |
| 107 | + |
| 108 | +mkdir -p "$mac_dir" |
| 109 | +leak_into "$mac_dir" |
| 110 | +for start in 1 2; do |
| 111 | + ODS_TEST_RECORD="$record" "${program_args[@]}" |
| 112 | + grep -Fxq "BUN_TMPDIR=$mac_dir" "$record" || fail "macOS start $start must export BUN_TMPDIR" |
| 113 | + grep -Fxq "ENTRIES=0" "$record" || fail "macOS start $start must begin with an empty BUN_TMPDIR" |
| 114 | + grep -Fxq "ARGS=web --port 3003 --hostname 127.0.0.1" "$record" \ |
| 115 | + || fail "macOS start $start must pass the web command unchanged" |
| 116 | +done |
| 117 | +[[ "$(entries_in "$mac_dir")" == 1 ]] || fail "macOS restarts must not accumulate copies" |
| 118 | +[[ "$(mode_of "$mac_dir")" == "0o700" ]] || fail "macOS BUN_TMPDIR must be private (0700)" |
| 119 | +pass "macOS LaunchAgent empties BUN_TMPDIR on every start and execs OpenCode" |
| 120 | + |
| 121 | +# --- Windows scheduled-task launcher (literal PowerShell text) --- |
| 122 | +# shellcheck disable=SC2016 |
| 123 | +set_line="$(grep -n 'env:BUN_TMPDIR = `\$bunTmp' "$WINDOWS_PHASE" | cut -d: -f1)" |
| 124 | +# shellcheck disable=SC2016 |
| 125 | +remove_line="$(grep -n 'Remove-Item -LiteralPath `\$bunTmp -Recurse -Force' "$WINDOWS_PHASE" | cut -d: -f1)" |
| 126 | +launch_line="$(grep -n "& '\$_ocExeLiteral' web --port" "$WINDOWS_PHASE" | cut -d: -f1)" |
| 127 | +grep -q "Join-Path \`\$env:LOCALAPPDATA 'ODS\\\\opencode-bun-tmp'" "$WINDOWS_PHASE" \ |
| 128 | + || fail "Windows launcher must use an ODS-owned BUN_TMPDIR" |
| 129 | +[[ -n "$set_line" && -n "$remove_line" && -n "$launch_line" \ |
| 130 | + && "$remove_line" -lt "$set_line" && "$set_line" -lt "$launch_line" ]] \ |
| 131 | + || fail "Windows launcher must empty and export BUN_TMPDIR before starting OpenCode" |
| 132 | +pass "Windows launcher empties BUN_TMPDIR before starting OpenCode" |
| 133 | + |
| 134 | +echo "[PASS] OpenCode BUN_TMPDIR confinement" |
0 commit comments