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..db7019a --- /dev/null +++ b/freeform/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -0,0 +1,26 @@ +#!/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 + +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' +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..db7019a --- /dev/null +++ b/modules/claude-self-hosted-runner/files/claude-deny-token-commands @@ -0,0 +1,26 @@ +#!/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 + +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' +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