From bd0fd5c98801947ccb954aac2d90872e6a77960b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 15:34:00 +0000 Subject: [PATCH 1/3] fix: refuse Bash commands that print a credential in runner sessions A runner session wrote a commit message inside bash -c "..." that quoted the Coder external-auth access-token command in backticks. The shell ran it, the owner's GitHub token went into a commit pushed to this repository, and GitHub's secret scanning revoked it. AGENTS.md already said to pass commit messages through a file, but that session had started in another repository and never read it. Runner sessions now get a managed PreToolUse hook that refuses Bash commands printing a credential (Coder external-auth tokens, gh auth token, git credential fill), whatever repository the session works in. AGENTS.md says why messages go through a file, and to never print a credential. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UrTUMxoyzqiN3JtHacV96S --- AGENTS.md | 3 ++- .../claude-self-hosted-runner/files/README.md | 4 ++++ .../files/claude-deny-token-commands | 21 +++++++++++++++++++ .../modules/claude-self-hosted-runner/main.tf | 8 +++++++ .../claude-self-hosted-runner/outputs.tf | 2 ++ .../claude-self-hosted-runner/files/README.md | 4 ++++ .../files/claude-deny-token-commands | 21 +++++++++++++++++++ modules/claude-self-hosted-runner/main.tf | 8 +++++++ modules/claude-self-hosted-runner/outputs.tf | 2 ++ 9 files changed, 72 insertions(+), 1 deletion(-) create mode 100755 freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands create mode 100755 modules/claude-self-hosted-runner/files/claude-deny-token-commands diff --git a/AGENTS.md b/AGENTS.md index f935eb0..c116c2d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -89,7 +89,8 @@ The `freeform` template is unique: it keeps ddev-router running so multiple DDEV ## Tool Preferences - Use `jq` (not `python3 -m json.tool`) for JSON pretty-printing and querying -- For `git commit -m` and `gh pr create --body`, write the message to a file first (e.g. under the scratchpad directory) and pass it via `git commit -F ` / `gh pr create --body-file `, rather than inlining a heredoc on the command line. Multi-paragraph messages containing backticks, apostrophes, or other shell-special characters reliably break inline heredoc quoting. +- For `git commit -m` and `gh pr create --body`, write the message to a file first (e.g. under the scratchpad directory) and pass it via `git commit -F ` / `gh pr create --body-file `, rather than inlining a heredoc on the command line. Multi-paragraph messages containing backticks, apostrophes, or other shell-special characters reliably break inline heredoc quoting. Write the file with the editor tool, not a shell command. Inside a double-quoted string, such as `bash -c "..."`, backticks and `$(...)` run the command they quote. A commit message that quoted `coder external-auth access-token github` that way once pushed the owner's GitHub token to this repository, and GitHub revoked it. +- Never run a command that prints a credential (`coder external-auth access-token`, `gh auth token`, `git credential fill`), even to check that one exists. Runner sessions refuse these commands through a hook (`modules/claude-self-hosted-runner/files/claude-deny-token-commands`). ## Before Pushing / Pre-push Checklist diff --git a/freeform/modules/claude-self-hosted-runner/files/README.md b/freeform/modules/claude-self-hosted-runner/files/README.md index 8f71f36..9917a80 100644 --- a/freeform/modules/claude-self-hosted-runner/files/README.md +++ b/freeform/modules/claude-self-hosted-runner/files/README.md @@ -61,6 +61,10 @@ access-token github`). No `gh auth login` is needed: link GitHub once at refuses commands that write, but that only guards against mistakes: the token can do whatever the GitHub app behind Coder's external auth allows. +Sessions can't run commands that print a credential, such as +`gh auth token` or `coder external-auth access-token`. A hook refuses them, +so a token can't end up in a commit, a PR or a log. + SSH remotes use this workspace's SSH agent: your forwarded agent, or your Coder key, which is the same in all your workspaces. SSH pushes act as you in every session. The workspace adds GitHub's host keys to diff --git a/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands new file mode 100755 index 0000000..a53e398 --- /dev/null +++ b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# PreToolUse hook for Bash: refuse commands that print a credential. +# +# A session once wrote a commit message inside bash -c "...", quoting +# `coder external-auth access-token github` in backticks. The shell ran it and +# put the owner's GitHub token into a pushed commit. Nothing a session does +# needs these commands' output in its context; tools that need a token (such +# as gh-read) fetch it themselves, outside the Bash tool's command string. +set -uo pipefail + +cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" +[ -n "$cmd" ] || exit 0 + +pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill' +if printf '%s' "$cmd" | grep -qE "$pattern"; then + cat >&2 <<'EOF' +Blocked: this command would print a credential (a GitHub or Coder token) into the session, where it can end up in a commit, a PR or a log. Don't run it, and don't quote it in backticks or $() inside a double-quoted string, which runs it. To read GitHub outside the session's repositories, use gh-read; it fetches the token itself. To put text that mentions such a command into a commit or PR, write the text to a file and pass it with git commit -F or --body-file. +EOF + exit 2 +fi +exit 0 diff --git a/freeform/modules/claude-self-hosted-runner/main.tf b/freeform/modules/claude-self-hosted-runner/main.tf index 894dffc..c9faad0 100644 --- a/freeform/modules/claude-self-hosted-runner/main.tf +++ b/freeform/modules/claude-self-hosted-runner/main.tf @@ -50,6 +50,14 @@ locals { allow = ["Bash(gh-read:*)"] } hooks = { + # Refuses Bash commands that print a credential; see the script. + PreToolUse = [{ + matcher = "Bash" + hooks = [{ + type = "command" + command = "/home/coder/.local/bin/claude-deny-token-commands" + }] + }] SessionStart = [{ # Not "compact" or "clear": by then the changes are the session's own. matcher = "startup|resume" diff --git a/freeform/modules/claude-self-hosted-runner/outputs.tf b/freeform/modules/claude-self-hosted-runner/outputs.tf index efe2bb2..4eb844e 100644 --- a/freeform/modules/claude-self-hosted-runner/outputs.tf +++ b/freeform/modules/claude-self-hosted-runner/outputs.tf @@ -57,6 +57,8 @@ output "startup_script" { chmod +x ~/.local/bin/claude-runner-sweep echo '${base64encode(file("${path.module}/files/claude-session-identity"))}' | base64 -d > ~/.local/bin/claude-session-identity chmod +x ~/.local/bin/claude-session-identity + echo '${base64encode(file("${path.module}/files/claude-deny-token-commands"))}' | base64 -d > ~/.local/bin/claude-deny-token-commands + chmod +x ~/.local/bin/claude-deny-token-commands sudo mkdir -p /etc/claude-code/managed-settings.d echo '${base64encode(local.managed_settings)}' | base64 -d | sudo tee /etc/claude-code/managed-settings.d/coder-runner.json >/dev/null mkcert -install >/dev/null 2>&1 || true diff --git a/modules/claude-self-hosted-runner/files/README.md b/modules/claude-self-hosted-runner/files/README.md index 8f71f36..9917a80 100644 --- a/modules/claude-self-hosted-runner/files/README.md +++ b/modules/claude-self-hosted-runner/files/README.md @@ -61,6 +61,10 @@ access-token github`). No `gh auth login` is needed: link GitHub once at refuses commands that write, but that only guards against mistakes: the token can do whatever the GitHub app behind Coder's external auth allows. +Sessions can't run commands that print a credential, such as +`gh auth token` or `coder external-auth access-token`. A hook refuses them, +so a token can't end up in a commit, a PR or a log. + SSH remotes use this workspace's SSH agent: your forwarded agent, or your Coder key, which is the same in all your workspaces. SSH pushes act as you in every session. The workspace adds GitHub's host keys to diff --git a/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/modules/claude-self-hosted-runner/files/claude-deny-token-commands new file mode 100755 index 0000000..a53e398 --- /dev/null +++ b/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# PreToolUse hook for Bash: refuse commands that print a credential. +# +# A session once wrote a commit message inside bash -c "...", quoting +# `coder external-auth access-token github` in backticks. The shell ran it and +# put the owner's GitHub token into a pushed commit. Nothing a session does +# needs these commands' output in its context; tools that need a token (such +# as gh-read) fetch it themselves, outside the Bash tool's command string. +set -uo pipefail + +cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" +[ -n "$cmd" ] || exit 0 + +pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill' +if printf '%s' "$cmd" | grep -qE "$pattern"; then + cat >&2 <<'EOF' +Blocked: this command would print a credential (a GitHub or Coder token) into the session, where it can end up in a commit, a PR or a log. Don't run it, and don't quote it in backticks or $() inside a double-quoted string, which runs it. To read GitHub outside the session's repositories, use gh-read; it fetches the token itself. To put text that mentions such a command into a commit or PR, write the text to a file and pass it with git commit -F or --body-file. +EOF + exit 2 +fi +exit 0 diff --git a/modules/claude-self-hosted-runner/main.tf b/modules/claude-self-hosted-runner/main.tf index 894dffc..c9faad0 100644 --- a/modules/claude-self-hosted-runner/main.tf +++ b/modules/claude-self-hosted-runner/main.tf @@ -50,6 +50,14 @@ locals { allow = ["Bash(gh-read:*)"] } hooks = { + # Refuses Bash commands that print a credential; see the script. + PreToolUse = [{ + matcher = "Bash" + hooks = [{ + type = "command" + command = "/home/coder/.local/bin/claude-deny-token-commands" + }] + }] SessionStart = [{ # Not "compact" or "clear": by then the changes are the session's own. matcher = "startup|resume" diff --git a/modules/claude-self-hosted-runner/outputs.tf b/modules/claude-self-hosted-runner/outputs.tf index efe2bb2..4eb844e 100644 --- a/modules/claude-self-hosted-runner/outputs.tf +++ b/modules/claude-self-hosted-runner/outputs.tf @@ -57,6 +57,8 @@ output "startup_script" { chmod +x ~/.local/bin/claude-runner-sweep echo '${base64encode(file("${path.module}/files/claude-session-identity"))}' | base64 -d > ~/.local/bin/claude-session-identity chmod +x ~/.local/bin/claude-session-identity + echo '${base64encode(file("${path.module}/files/claude-deny-token-commands"))}' | base64 -d > ~/.local/bin/claude-deny-token-commands + chmod +x ~/.local/bin/claude-deny-token-commands sudo mkdir -p /etc/claude-code/managed-settings.d echo '${base64encode(local.managed_settings)}' | base64 -d | sudo tee /etc/claude-code/managed-settings.d/coder-runner.json >/dev/null mkcert -install >/dev/null 2>&1 || true From 7bc5a25e5223704dc48cc10fe1d2e200083e82e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 16:12:39 +0000 Subject: [PATCH 2/3] fix(hook): also refuse Keychain secret reads and the gh-read token file gh-read can now take a machine token from the macOS Keychain or ~/.config/gh-read/token, so the hook refuses security find-generic-password, find-internet-password and commands naming that file. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UrTUMxoyzqiN3JtHacV96S --- .../claude-self-hosted-runner/files/claude-deny-token-commands | 2 +- .../claude-self-hosted-runner/files/claude-deny-token-commands | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands index a53e398..6e83e44 100755 --- a/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands +++ b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -11,7 +11,7 @@ set -uo pipefail cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" [ -n "$cmd" ] || exit 0 -pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill' +pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill|find-generic-password|find-internet-password|gh-read/token' if printf '%s' "$cmd" | grep -qE "$pattern"; then cat >&2 <<'EOF' Blocked: this command would print a credential (a GitHub or Coder token) into the session, where it can end up in a commit, a PR or a log. Don't run it, and don't quote it in backticks or $() inside a double-quoted string, which runs it. To read GitHub outside the session's repositories, use gh-read; it fetches the token itself. To put text that mentions such a command into a commit or PR, write the text to a file and pass it with git commit -F or --body-file. diff --git a/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/modules/claude-self-hosted-runner/files/claude-deny-token-commands index a53e398..6e83e44 100755 --- a/modules/claude-self-hosted-runner/files/claude-deny-token-commands +++ b/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -11,7 +11,7 @@ set -uo pipefail cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" [ -n "$cmd" ] || exit 0 -pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill' +pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill|find-generic-password|find-internet-password|gh-read/token' if printf '%s' "$cmd" | grep -qE "$pattern"; then cat >&2 <<'EOF' Blocked: this command would print a credential (a GitHub or Coder token) into the session, where it can end up in a commit, a PR or a log. Don't run it, and don't quote it in backticks or $() inside a double-quoted string, which runs it. To read GitHub outside the session's repositories, use gh-read; it fetches the token itself. To put text that mentions such a command into a commit or PR, write the text to a file and pass it with git commit -F or --body-file. From 0428d83d0ad9f0a49d448f22289cd0c6e046c6f5 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 16:30:37 +0000 Subject: [PATCH 3/3] fix(hook): check the raw hook input when jq is missing Without jq the hook read an empty command and let everything through. It now falls back to matching the raw JSON input, so credential commands are still refused on a machine without jq, such as a fresh macOS runner. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UrTUMxoyzqiN3JtHacV96S --- .../files/claude-deny-token-commands | 7 ++++++- .../files/claude-deny-token-commands | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands index 6e83e44..db7019a 100755 --- a/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands +++ b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -8,7 +8,12 @@ # as gh-read) fetch it themselves, outside the Bash tool's command string. set -uo pipefail -cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" +input="$(cat)" +# Without jq (or with input jq can't parse), check the raw hook input instead +# of letting the command through. +if ! cmd="$(printf '%s' "$input" | jq -r '.tool_input.command // empty' 2>/dev/null)"; then + cmd="$input" +fi [ -n "$cmd" ] || exit 0 pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill|find-generic-password|find-internet-password|gh-read/token' diff --git a/modules/claude-self-hosted-runner/files/claude-deny-token-commands b/modules/claude-self-hosted-runner/files/claude-deny-token-commands index 6e83e44..db7019a 100755 --- a/modules/claude-self-hosted-runner/files/claude-deny-token-commands +++ b/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -8,7 +8,12 @@ # as gh-read) fetch it themselves, outside the Bash tool's command string. set -uo pipefail -cmd="$(jq -r '.tool_input.command // empty' 2>/dev/null)" +input="$(cat)" +# Without jq (or with input jq can't parse), check the raw hook input instead +# of letting the command through. +if ! cmd="$(printf '%s' "$input" | jq -r '.tool_input.command // empty' 2>/dev/null)"; then + cmd="$input" +fi [ -n "$cmd" ] || exit 0 pattern='external-auth[[:space:]]+access-token|gh[[:space:]]+auth[[:space:]]+token|git[[:space:]]+credential[[:space:]]+fill|find-generic-password|find-internet-password|gh-read/token'