diff --git a/CLAUDE.md b/CLAUDE.md index 45a3ddb..0d93858 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -64,7 +64,7 @@ For a machine that must not get the full wrapper. It resolves its own path the s Every `lib/*.sh` file assumes `logging.sh` is already sourced. `permissions.sh` and `path-security.sh` are foundational — other modules call their functions. -- **`credentials.sh`** — fetches `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain (service `op-service-account-claude-automation`) and `GH_TOKEN` from the Automation vault via that service account, retrying `op read` with exponential backoff. Runs automatically as a side effect of being sourced (not invoked as a discrete step later in the flow). Both are exported into the wrapper process and inherited by its children, including the agent's own shell — they are absent from the user's login shell but visible to anything the wrapper launches. If the vault fetch fails, `GH_TOKEN` is set to a deliberately invalid sentinel so `gh` fails closed, rather than left unset (which would let `gh` fall back to the keyring OAuth token and its `admin:org` scope). Must be sourced before `secrets-loader.sh`, which depends on `OP_SERVICE_ACCOUNT_TOKEN`. +- **`credentials.sh`** — fetches `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain (service `op-service-account-claude-automation`) and `GH_TOKEN` from the Automation vault via that service account, retrying `op read` with exponential backoff. Runs automatically as a side effect of being sourced (not invoked as a discrete step later in the flow). Both are exported into the wrapper process and inherited by its children, including the agent's own shell — they are absent from the user's login shell but visible to anything the wrapper launches. If the vault fetch fails, `GH_TOKEN` is set to a deliberately invalid sentinel so `gh` fails closed, rather than left unset (which would let `gh` fall back to the keyring OAuth token and its `admin:org` scope). GitHub tokens are loaded only on arich-mac; see "Personal GitHub tokens: arich-mac only" below. Must be sourced before `secrets-loader.sh`, which depends on `OP_SERVICE_ACCOUNT_TOKEN`. - **`secrets-loader.sh`** — discovers secrets files at two levels (project `.claude/secrets.op`, local `.claude/secrets.local.op`), validates permissions/paths, runs `op inject` to resolve `op://Automation/...` references against the Automation vault via service account. Invoked explicitly as `init_secrets_loader` and `inject_secrets` (see execution flow above). - **`launch-dir-check.sh`** — `check_launch_dir` warns to stderr (never blocks) when CWD is exactly `$HOME` or `$HOME/Developer` — not subdirectories, which are legitimate git repos — since per-project secrets/pre-launch hooks are skipped there and an unrelated MCP server surface from `~/.claude/.claude.json` loads instead; recommends `--add-dir` for multi-repo work - **`binary-discovery.sh`** — finds the real `claude` binary in `$PATH` excluding the wrapper itself, validates it isn't world-writable @@ -84,6 +84,14 @@ The wrapper execs `claude` through `caffeinate -is`, so the system does not idle `-i` (prevent idle sleep) holds on battery as well as AC; `-s` is AC-only. +#### Personal GitHub tokens: arich-mac only + +Andrew's rule (2026-11-09): no GitHub operation by an agent may act as a person. On any host whose `hostname -s` is not `arich-mac` (any case, exact match), `credentials.sh` loads no `GH_TOKEN`, `GH_TOKEN_NOS` or `GH_TOKEN_TWM`, and `creds_enforce_app_only_gh` removes `GH_TOKEN_NOS`, `GH_TOKEN_TWM`, `GH_TOKEN_SWM`, `GITHUB_TOKEN` and `CLAUDE_WRAPPER_VAULT_VARS`. `bin/claude-wrapper` calls it again just before `exec`, because `inject_secrets` (a `secrets.op` can name `GH_TOKEN`) and the pre-launch hook run after `credentials.sh`. Agents there act on GitHub only as the GitHub Apps, minted per call by the dotfiles gh wrapper. + +`GH_TOKEN` is set to the fetch-failed sentinel, not unset: an unset `GH_TOKEN` lets gh and the gh wrapper fall back to the keyring login, which acts as Andrew. The gh wrapper replaces `GH_TOKEN` with the App token on App-routed calls, so those still work. + +If `hostname` is missing or fails, the host counts as personal. There is no env-var override; tests put a `hostname` stub on `PATH`. The section below applies to arich-mac only. + #### GitHub token selection A fine-grained PAT is bound to exactly one GitHub resource owner at creation and cannot be repointed, so one token cannot cover a personal account and two orgs. `credentials.sh` derives the owner from the launch directory's `origin` remote and selects the matching vault item: @@ -114,7 +122,7 @@ All secret files (`.op` files) must be owner-only permissions (no group/world). | `.claude/secrets.op` | Per-project 1Password secrets (committed); references `op://Automation/...` | | `.claude/secrets.local.op` | Per-project local overrides (gitignored) | -`GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from the Automation vault item matching the launch directory's GitHub owner (see "GitHub token selection" above) — not from a flat file, and not sourced by shell startup. +On arich-mac, `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from the Automation vault item matching the launch directory's GitHub owner (see "GitHub token selection" above) — not from a flat file, and not sourced by shell startup. On other hosts it is only the invalid sentinel. ## Headroom Learned Patterns @@ -142,9 +150,9 @@ All secret files (`.op` files) must be owner-only permissions (no group/world). *~500 tokens/session saved* -- `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from one of two Automation vault items chosen by the launch directory's GitHub owner (`CCCLI-NOS`, or `GitHub - CCCLI` for everything else); no flat token files exist +- On arich-mac only, `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from one of two Automation vault items chosen by the launch directory's GitHub owner (`CCCLI-NOS`, or `GitHub - CCCLI` for everything else); no flat token files exist. Other hosts get no personal GitHub token - Per-project secrets live in `.claude/secrets.op` (committed) referencing `op://Automation/...`; resolved by `secrets-loader.sh` via `OP_SERVICE_ACCOUNT_TOKEN` (no TouchID) -- The global `~/.config/claude-code/secrets.op` no longer exists +- `secrets-loader.sh` still loads a global `~/.config/claude-code/secrets.op` when one exists, and at least one personal Mac has one that sets `GH_TOKEN`; off arich-mac the pre-exec strip removes it - `opp ` runs `op` as your personal account (unsets service account token for that subprocess); needed for Personal vault access (e.g. prep-airdrop.sh) ### Pre-commit Hooks diff --git a/README.md b/README.md index da77fe0..ea11cf9 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,11 @@ cat ~/.ssh/id_ed25519_claude_code.pub #### GitHub Token +This applies only on the company Mac, arich-mac. On any other host the wrapper +loads no personal GitHub token and removes inherited ones; `GH_TOKEN` holds +only an invalid sentinel, and agents act on GitHub as the GitHub Apps. See +`docs/SECRETS.md`. + The wrapper fetches `GH_TOKEN` itself at launch, via `lib/credentials.sh`. It reads `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain, then resolves a token from the 1Password Automation vault. No shell-startup setup is required — if diff --git a/bin/claude-wrapper b/bin/claude-wrapper index a7b8eb0..beff053 100755 --- a/bin/claude-wrapper +++ b/bin/claude-wrapper @@ -102,6 +102,10 @@ if secrets_available; then fi fi +# Off arich-mac, remove any personal GitHub token that secrets.op or the +# pre-launch hook exported after credentials.sh ran (see that file). +creds_enforce_app_only_gh + if secrets_available; then debug_log "Executing: ${CAFF_BIN} -is ${CLAUDE_BIN} [${#RC_ARGS[@]} injected args + ${#} args] with secrets loaded" else diff --git a/docs/SECRETS.md b/docs/SECRETS.md index 599741c..6809ac8 100644 --- a/docs/SECRETS.md +++ b/docs/SECRETS.md @@ -154,6 +154,17 @@ DEBUG_MODE=true ### GitHub Token Integration +Only on the company Mac, arich-mac (`hostname -s`, any case), does the wrapper +load a personal GitHub token. On every other host it loads no `GH_TOKEN`, +`GH_TOKEN_NOS` or `GH_TOKEN_TWM`, and it removes those, `GH_TOKEN_SWM` and +`GITHUB_TOKEN` if the session inherits them. It removes them again just +before it starts Claude, so a `secrets.op` entry or a pre-launch hook cannot +put one back. `GH_TOKEN` is left as the invalid sentinel +`invalid-cccli-token-vault-fetch-failed`, not unset: with no `GH_TOKEN`, gh +falls back to the keyring login, which acts as Andrew. Agents there reach +GitHub only as the GitHub Apps, which the dotfiles gh wrapper mints per call. +The rest of this section describes arich-mac. + `GH_TOKEN` is fetched by the wrapper itself at launch, via `lib/credentials.sh`. It reads `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain, then resolves the token from the 1Password Automation vault: @@ -344,7 +355,7 @@ Output shows which files loaded in order. Last file wins for duplicate variables | `GIT_COMMITTER_NAME` | `Claude Code Bot` | Git committer | | `GIT_COMMITTER_EMAIL` | `claude-code@smartwatermelon.github` | Git committer email | | `GIT_SSH_COMMAND` | `ssh -i ~/.ssh/id_ed25519_claude_code ...` | SSH key for git | -| `GH_TOKEN` | Fetched by wrapper from 1Password Automation vault via `lib/credentials.sh` | GitHub CLI token | +| `GH_TOKEN` | arich-mac: fetched by wrapper from 1Password Automation vault via `lib/credentials.sh`. Other hosts: the invalid sentinel only | GitHub CLI token | ### Loaded from secrets.op diff --git a/lib/credentials.sh b/lib/credentials.sh index fbbf3de..a533ac5 100755 --- a/lib/credentials.sh +++ b/lib/credentials.sh @@ -10,6 +10,8 @@ # rather than left unset, so gh fails closed instead of falling back to the # keyring OAuth token (which holds admin:org). See _load_gh_token. # +# GitHub tokens load only on arich-mac; elsewhere see creds_enforce_app_only_gh. +# # Requires: lib/logging.sh must be sourced first. # Must be sourced before lib/secrets-loader.sh (which needs OP_SERVICE_ACCOUNT_TOKEN). @@ -62,6 +64,14 @@ _CREDS_HAS_TIMEOUT=false command -v timeout &>/dev/null && _CREDS_HAS_TIMEOUT=true readonly _CREDS_HAS_TIMEOUT +# Personal GitHub tokens: arich-mac only (CLAUDE.md). hostname is on PATH +# for test stubs; no env override. Unreadable means not arich-mac. +_CREDS_PERSONAL_GH_ALLOWED=false +if [[ "$(hostname -s 2>/dev/null || true)" =~ ^[Aa][Rr][Ii][Cc][Hh]-[Mm][Aa][Cc]$ ]]; then + _CREDS_PERSONAL_GH_ALLOWED=true +fi +readonly _CREDS_PERSONAL_GH_ALLOWED + # ========================================================= # SERVICE ACCOUNT TOKEN # ========================================================= @@ -311,11 +321,25 @@ _load_owner_gh_tokens() { debug_log "Per-owner tokens available: ${loaded}/${#specs[@]}" } +# Off arich-mac, remove personal GitHub tokens. bin/claude-wrapper calls it +# again before exec: secrets.op and pre-launch run after this file. +creds_enforce_app_only_gh() { + "${_CREDS_PERSONAL_GH_ALLOWED}" && return 0 + unset GH_TOKEN_NOS GH_TOKEN_TWM GH_TOKEN_SWM GITHUB_TOKEN "${_CREDS_VAULT_MARKER_VAR}" + # Sentinel, not unset: unset lets gh use the keyring login (Andrew). + export GH_TOKEN="${_CREDS_GH_TOKEN_FETCH_FAILED}" +} + # ========================================================= # MAIN # ========================================================= _load_service_account_token -_load_gh_token -_load_owner_gh_tokens +if "${_CREDS_PERSONAL_GH_ALLOWED}"; then + _load_gh_token + _load_owner_gh_tokens +else + debug_log "Not arich-mac: no personal GitHub token loaded; gh acts only as the GitHub Apps" +fi +creds_enforce_app_only_gh # An inherited GH_TOKEN_SWM (an older parent session) must not reach this one. unset GH_TOKEN_SWM diff --git a/tests/test-credentials.sh b/tests/test-credentials.sh index 4941f47..57a45f5 100755 --- a/tests/test-credentials.sh +++ b/tests/test-credentials.sh @@ -49,9 +49,24 @@ make_stub_dir() { real_bin="$(type -P "${bin}" 2>/dev/null || true)" [[ -n "${real_bin}" ]] && ln -s "${real_bin}" "${stub_dir}/${bin}" done + write_hostname_stub "${stub_dir}" "${STUB_HOSTNAME}" echo "${stub_dir}" } +# Tokens load only on arich-mac, so stub dirs pose as it by default; the +# host-gate tests pick other names. +STUB_HOSTNAME="ARICH-MAC" +write_hostname_stub() { + local dir="$1" name="$2" + printf '#!/bin/sh\necho "%s"\n' "${name}" >"${dir}/hostname" + chmod +x "${dir}/hostname" +} + +# For tests that run with the real PATH: prepend this dir so `hostname` +# resolves to arich-mac and everything else to the real binaries. +ARICH_HOST_DIR="$(mktemp -d "${TEST_TMP}/host-arich.XXXXXX")" +write_hostname_stub "${ARICH_HOST_DIR}" "ARICH-MAC" + assert_equals() { local expected="$1" actual="$2" message="$3" ((TESTS_RUN += 1)) @@ -149,7 +164,8 @@ echo "=== _load_gh_token ===" # GH_TOKEN already set -> vault lookup skipped. debug_output="$( - GH_TOKEN="preset-gh-token" \ + PATH="${ARICH_HOST_DIR}:${PATH}" \ + GH_TOKEN="preset-gh-token" \ OP_SERVICE_ACCOUNT_TOKEN="dummy" \ CLAUDE_DEBUG=true \ bash -c "source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'" 2>&1 1>/dev/null @@ -353,58 +369,58 @@ EOF # A smartwatermelon launch gets the personal token (no SWM PAT, #659). # KNOWN-BAD on the old code: the SWM ref. repo_dir="$(make_repo_with_remote "git@github.com:smartwatermelon/claude-wrapper.git")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "smartwatermelon remote -> personal token ref, never a smartwatermelon PAT" repo_dir="$(make_repo_with_remote "git@github.com:nightowlstudiollc/night-owl-studio.git")" -assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}" || true)" \ "nightowlstudiollc remote -> NOS token ref" # The personal owner keeps the original ref — it still covers ~33 repos. repo_dir="$(make_repo_with_remote "git@github.com:twistedmelonman/something.git")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "twistedmelonman remote -> personal token ref" # https remotes must parse identically to scp-style ones. repo_dir="$(make_repo_with_remote "https://github.com/nightowlstudiollc/night-owl-studio.git")" -assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}" || true)" \ "https remote -> same owner as scp-style remote" # ssh://git@github.com/owner/repo is a valid remote form that a naive # scp-style-only pattern does not match. repo_dir="$(make_repo_with_remote "ssh://git@github.com/nightowlstudiollc/x.git")" -assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}" || true)" \ "ssh:// remote form -> owner parsed correctly" # An unknown org falls back to the personal token rather than guessing. It # fails loudly with a permissions error, which beats silently trying a token # that cannot work. repo_dir="$(make_repo_with_remote "git@github.com:someotherorg/repo.git")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "unknown owner -> personal token ref (fallback)" # A non-GitHub remote must yield no owner. A sed-based parser passes its input # through unchanged when the pattern does not match, which would put an entire # URL where an owner name belongs. repo_dir="$(make_repo_with_remote "https://gitlab.com/someone/thing.git")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "non-GitHub remote -> personal token ref (owner not derivable)" # A git repo with no origin remote at all. repo_dir="$(make_repo_with_remote "")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "repo without origin remote -> personal token ref" # Not a git repo — the pre-#120 behavior for sessions launched outside a repo. repo_dir="$(mktemp -d "${TEST_TMP}/plain.XXXXXX")" -assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}")" \ +assert_equals "op://Automation/GitHub - CCCLI/Token" "$(ref_selected_from "${repo_dir}" || true)" \ "non-repo directory -> personal token ref" # A subdirectory resolves to the enclosing repo's owner, since the wrapper is # frequently launched from somewhere below the git root. repo_dir="$(make_repo_with_remote "git@github.com:nightowlstudiollc/night-owl-studio.git")" mkdir -p "${repo_dir}/nested/deeper" -assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}/nested/deeper")" \ +assert_equals "op://Automation/CCCLI-NOS/token" "$(ref_selected_from "${repo_dir}/nested/deeper" || true)" \ "subdirectory of a repo -> enclosing repo's owner" # --- Per-owner token loading (_load_owner_gh_tokens) --- @@ -552,6 +568,139 @@ child:unset" "${result}" \ assert_equals "" "$(grep -F "CCCLI-SWM" "${call_log}" 2>/dev/null || true)" \ "the CCCLI-SWM vault item is never read" +# --- Host gate: no personal GitHub token off arich-mac --- +# Values here are stub literals; no assertion handles a real credential. + +echo "" +echo "=== host gate (app-only GitHub off arich-mac) ===" + +SENTINEL="invalid-cccli-token-vault-fetch-failed" + +# An op stub that hands out a distinct fake value per ref and logs each ref. +make_host_gate_stub() { + local host="$1" stub + stub="$(make_stub_dir env bash cat id security timeout)" + write_hostname_stub "${stub}" "${host}" + cat >"${stub}/op" <>"${stub}/op-calls.log" +case "\$2" in + "op://Automation/CCCLI-NOS/token") echo "tok-nos" ;; + "op://Automation/GitHub - CCCLI/Token") echo "tok-personal" ;; + *) exit 1 ;; +esac +EOF + chmod +x "${stub}/op" + printf '%s' "${stub}" +} + +# Prints the five token vars. Sourced: the session. Run: a child process. +REPORT_SCRIPT="${TEST_TMP}/report-vars.sh" +cat >"${REPORT_SCRIPT}" <<'EOF' +echo "${GH_TOKEN:-unset}/${GH_TOKEN_NOS:-unset}/${GH_TOKEN_TWM:-unset}/${GITHUB_TOKEN:-unset}/${GH_TOKEN_SWM:-unset}" +EOF +REPORT_VARS="source '${REPORT_SCRIPT}'" + +# Personal host, all inherited: none survive, no vault read. +# KNOWN-BAD on the old code: the presets were kept. +stub_dir="$(make_host_gate_stub "ASIAGO")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + GH_TOKEN="inherited-gh" GH_TOKEN_NOS="inherited-nos" GH_TOKEN_TWM="inherited-twm" \ + GITHUB_TOKEN="inherited-github" GH_TOKEN_SWM="inherited-swm" \ + bash -c "source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}; bash '${REPORT_SCRIPT}'" 2>/dev/null +)" +assert_equals "${SENTINEL}/unset/unset/unset/unset +${SENTINEL}/unset/unset/unset/unset" "${result}" \ + "personal host, all inherited -> none kept, GH_TOKEN is the sentinel, children clean" +assert_equals "" "$(grep -F "op://Automation/" "${stub_dir}/op-calls.log" 2>/dev/null || true)" \ + "personal host -> no GitHub token vault item is read" + +# Personal host, nothing inherited: nothing loaded. +# KNOWN-BAD on the old code: three tokens were fetched. +stub_dir="$(make_host_gate_stub "TILSIT")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN GH_TOKEN_SWM CLAUDE_WRAPPER_VAULT_VARS; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "${SENTINEL}/unset/unset/unset/unset" "${result}" \ + "personal host, nothing inherited -> no token loaded" + +# A missing token on a personal host is the design, not a fault: no warning. +warn_output="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'" 2>&1 1>/dev/null +)" +assert_equals "" "$(grep -F "WARNING" <<<"${warn_output}" || true)" \ + "personal host -> no warning about the absent tokens" + +# A name that only starts with arich-mac is not arich-mac. +stub_dir="$(make_host_gate_stub "arich-mac2")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "${SENTINEL}/unset/unset/unset/unset" "${result}" \ + "host arich-mac2 -> treated as personal (exact match only)" + +# No hostname command: the host cannot be shown to be arich-mac, so fail closed. +stub_dir="$(make_host_gate_stub "unused")" +rm -f "${stub_dir}/hostname" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "${SENTINEL}/unset/unset/unset/unset" "${result}" \ + "hostname unavailable -> treated as personal" + +# arich-mac, either case: unchanged (GITHUB_TOKEN kept, SWM still removed). +for host in ARICH-MAC arich-mac; do + stub_dir="$(make_host_gate_stub "${host}")" + result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + GITHUB_TOKEN="caller-github" GH_TOKEN_SWM="inherited-swm" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM CLAUDE_WRAPPER_VAULT_VARS; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}" 2>/dev/null + )" + assert_equals "tok-personal/tok-nos/tok-personal/caller-github/unset" "${result}" \ + "host ${host} -> tokens loaded as before" +done + +# arich-mac keeps a caller's presets, as before. +stub_dir="$(make_host_gate_stub "arich-mac")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + GH_TOKEN="preset-gh" GH_TOKEN_NOS="preset-nos" GH_TOKEN_TWM="preset-twm" \ + bash -c "unset GITHUB_TOKEN CLAUDE_WRAPPER_VAULT_VARS; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "preset-gh/preset-nos/preset-twm/unset/unset" "${result}" \ + "host arich-mac -> caller presets kept" + +# secrets.op or pre-launch can re-export these; the pre-exec strip removes them. +stub_dir="$(make_host_gate_stub "ASIAGO")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; export GH_TOKEN=from-secrets-op GH_TOKEN_NOS=from-hook GH_TOKEN_TWM=from-hook GITHUB_TOKEN=from-hook; creds_enforce_app_only_gh; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "${SENTINEL}/unset/unset/unset/unset" "${result}" \ + "personal host -> tokens exported after credentials.sh are removed before launch" + +stub_dir="$(make_host_gate_stub "ARICH-MAC")" +result="$( + PATH="${stub_dir}" \ + OP_SERVICE_ACCOUNT_TOKEN="dummy" \ + bash -c "unset GH_TOKEN GH_TOKEN_NOS GH_TOKEN_TWM GITHUB_TOKEN; source '${LIB_DIR}/logging.sh'; source '${LIB_DIR}/credentials.sh'; export GH_TOKEN=from-secrets-op GITHUB_TOKEN=from-hook; creds_enforce_app_only_gh; ${REPORT_VARS}" 2>/dev/null +)" +assert_equals "from-secrets-op/tok-nos/tok-personal/from-hook/unset" "${result}" \ + "arich-mac -> the pre-launch strip changes nothing" + # --- Summary --- echo "" diff --git a/tests/test-wrapper.sh b/tests/test-wrapper.sh index 87b9698..cb8d71f 100755 --- a/tests/test-wrapper.sh +++ b/tests/test-wrapper.sh @@ -20,6 +20,9 @@ REPO_ROOT="$(cd "${TEST_DIR}/.." && pwd)" WRAPPER="${REPO_ROOT}/bin/claude-wrapper" LIB_DIR="${REPO_ROOT}/lib" +# An inherited session marker would turn Test 3.6's presets into leftovers. +unset CLAUDE_WRAPPER_VAULT_VARS + # shellcheck source=tests/lib/op-guard.sh source "${TEST_DIR}/lib/op-guard.sh" @@ -605,6 +608,10 @@ EOF real_bin="$(command -v "${bin}" 2>/dev/null || true)" [[ -n "${real_bin}" ]] && ln -s "${real_bin}" "${stub_dir}/${bin}" done + # credentials.sh fetches GitHub tokens only on arich-mac; pose as that host + # so this test reaches the vault read on any machine, CI included. + printf '#!/bin/sh\necho ARICH-MAC\n' >"${stub_dir}/hostname" + chmod +x "${stub_dir}/hostname" # The per-owner vars are preset so _load_owner_gh_tokens takes its # already-set path and reads nothing. This assertion counts _load_gh_token's @@ -747,6 +754,33 @@ test_mock_integration() { else echo -e "${YELLOW}⚠${NC} Integration test skipped (wrapper exited with ${exit_code})" fi + + # Personal host: inherited tokens never reach claude. Fake presets only. + local host_dir + host_dir="${TEST_TMP}/host-personal" + mkdir -p "${host_dir}" + printf '#!/bin/sh\necho ASIAGO\n' >"${host_dir}/hostname" + chmod +x "${host_dir}/hostname" + exit_code=0 + output="$( + PATH="${host_dir}:${PATH}" \ + GH_TOKEN="inherited-gh" GH_TOKEN_NOS="inherited-nos" GH_TOKEN_TWM="inherited-twm" \ + GH_TOKEN_SWM="inherited-swm" \ + "${test_wrapper}" --version 2>&1 + )" || exit_code=$? + + # Names and yes/no only: a failure prints "Actual", maybe a real token. + if [[ ${exit_code} -eq 0 ]] && [[ "${output}" == *MOCK_CLAUDE_EXECUTED* ]]; then + local names sentinel_only="no" + names="$(grep -oE '^GH_TOKEN[A-Z_]*=' <<<"${output}" | tr '\n' ' ' || true)" + grep -qx 'GH_TOKEN=invalid-cccli-token-vault-fetch-failed' <<<"${output}" && sentinel_only="yes" + assert_equals "GH_TOKEN= " "${names}" \ + "Personal host: no GH_TOKEN_* var reaches the launched process" + assert_equals "yes" "${sentinel_only}" \ + "Personal host: GH_TOKEN reaches the launched process only as the sentinel" + else + echo -e "${YELLOW}⚠${NC} Personal-host launch test skipped (wrapper exited with ${exit_code})" + fi } test_no_old_naming_references() { @@ -808,6 +842,23 @@ test_caffeinate_launch() { fi } +test_app_only_strip_before_exec() { + echo "" + echo "Test 4.6: Strips personal GitHub tokens after secrets load, before exec" + + # Never skipped, unlike the launch check in 4.3, which needs a runnable wrapper. + local strip_line inject_line exec_line in_order="no" + strip_line="$(grep -nE '^creds_enforce_app_only_gh$' "${WRAPPER}" | tail -1 | cut -d: -f1 || true)" + inject_line="$(grep -nE 'inject_secrets|run_pre_launch_hook' "${WRAPPER}" | tail -1 | cut -d: -f1 || true)" + exec_line="$(grep -nE '^[[:space:]]*exec ' "${WRAPPER}" | head -1 | cut -d: -f1 || true)" + if [[ -n "${strip_line}" && -n "${inject_line}" && -n "${exec_line}" ]] \ + && ((inject_line < strip_line && strip_line < exec_line)); then + in_order="yes" + fi + assert_equals "yes" "${in_order}" \ + "creds_enforce_app_only_gh runs after inject_secrets/pre-launch and before exec" +} + # ============================================================================= # SECTION 5: Regression Tests - Ensure existing functionality preserved # ============================================================================= @@ -911,6 +962,7 @@ main() { test_mock_integration test_no_old_naming_references test_caffeinate_launch + test_app_only_strip_before_exec # Section 5: Regression Tests echo ""