fix(agent-setup, github-app-token): guard against inherited GH_CONFIG_DIR outside AGENT_HOME #2

Open
Flea Flicker wants to merge 1 commits from gb_flea/skills:gandalf/gh-config-dir-isolation into main
3 changed files with 13 additions and 2 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ Validates the `AGENT_HOME` environment variable, derives `GH_CONFIG_DIR` as `$AG
```bash
bash agent-setup/scripts/setup.sh
source ~/.env
source "$AGENT_HOME/.env"
```
## Output
+6
View File
@@ -5,6 +5,12 @@ die() { echo "ERROR: $*" >&2; exit 1; }
[[ -z "${AGENT_HOME:-}" ]] && die "AGENT_HOME is not set"
# Validate: never accept an inherited GH_CONFIG_DIR that points outside AGENT_HOME
if [[ -n "${GH_CONFIG_DIR:-}" && "$GH_CONFIG_DIR" != "$AGENT_HOME"* ]]; then
echo "WARN: Inherited GH_CONFIG_DIR '$GH_CONFIG_DIR' is outside AGENT_HOME. Overriding." >&2
unset GH_CONFIG_DIR
fi
# Derive GH_CONFIG_DIR — gh stores config at ~/.config/gh by default,
# so we mirror that structure under AGENT_HOME
export GH_CONFIG_DIR="$AGENT_HOME/.github"
+6 -1
View File
@@ -64,10 +64,15 @@ fi
mkdir -p "$GH_TOKEN_DIR"
GH_TOKEN_FILE="$GH_TOKEN_DIR/.gh-token"
# Validate GH_CONFIG_DIR is inside AGENT_HOME (prevents writing the token to a foreign workspace)
if [[ -n "${GH_CONFIG_DIR:-}" && -n "${AGENT_HOME:-}" && "$GH_CONFIG_DIR" != "$AGENT_HOME"* ]]; then
die "GH_CONFIG_DIR '$GH_CONFIG_DIR' is outside AGENT_HOME '${AGENT_HOME}'. Refusing to write token to a foreign workspace."
fi
printf '%s' "$TOKEN" > "$GH_TOKEN_FILE"
chmod 600 "$GH_TOKEN_FILE"
# --- Authenticate gh CLI ---
gh auth login --with-token < "$GH_TOKEN_FILE"
GH_CONFIG_DIR="$GH_TOKEN_DIR" gh auth login --with-token < "$GH_TOKEN_FILE"
echo "Authenticated. Token written to $GH_TOKEN_FILE (expires in 1 hour)."