Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>` / `gh pr create --body-file <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 <file>` / `gh pr create --body-file <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

Expand Down
4 changes: 4 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions freeform/modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions freeform/modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions modules/claude-self-hosted-runner/files/claude-deny-token-commands
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading