Skip to content
Closed
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
2 changes: 1 addition & 1 deletion docs/content/docs/internals/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ On every tier, an HTTPS server's headers become bindings for its host, which mus

devvm also takes the keys out of the VM's copies of the agents' settings files, which are `~/.claude/settings.json`, `~/.codex/config.toml`, opencode's `.json`, `.jsonc` and `.toml` files and pi's `settings.json`, `models.json` and `mcp.json`. Those are JSON, JSONC or TOML, and devvm parses each file whole and writes the VM's copy from what it parsed, so a JSONC file's copy is JSON and a TOML file's copy has no comments. devvm replaces these with `devvm-redacted`, and names what it replaced (`config_redacted`):

- Every value under a key that looks like a secret's (`*key*`, `*token*`, `*secret*`, `*password*`, `Authorization`), and every string in a list or a table under such a key, but key bindings, such as opencode's `keybinds`.
- Every value under a key that looks like a secret's — `key`, `token`, `secret`, `password` or `passwd` as a word of its own (`apiKey`, `oauth_token`, `tokens`, `apiKeys`), or the whole key `Authorization` — and every string in a list or a table under such a key, but key bindings, such as opencode's `keybinds`. A name that only starts with one of those words, such as a marketplace named `tokeneconomizer`, keeps its values.
- Any string that starts like a known key or token, such as `sk-`, `ghp_` or `eyJ`, wherever it is.
- Every value in an env or headers table, whatever its name: `env`, `environment`, `headers`, `http_headers`, and Codex's `shell_environment_policy.set`. A number of up to five digits, `true` and `false` stay, because they're switches and ports. In Claude Code's own `env`, the top-level one of `settings.json`, its [configuration variables](https://code.claude.com/docs/en/env-vars) stay too: `ANTHROPIC_MODEL`, `ANTHROPIC_SMALL_FAST_MODEL`, the `ANTHROPIC_DEFAULT_*_MODEL` and `ANTHROPIC_CUSTOM_MODEL_OPTION` families, `CLAUDE_CODE_*`, `DISABLE_*`, `MAX_*` and `*_TIMEOUT_MS`. A variable whose name has `KEY`, `TOKEN` as a word, `SECRET`, `PASS`, `AUTH`, `CREDENTIAL`, `COOKIE` or `HEADER` in it is redacted all the same, such as `CLAUDE_CODE_OAUTH_TOKEN`, and so is its value when it's a URL.
- In a URL anywhere in the file, a string's whole value or a part of other text, its user info and its secret-looking query values and path segments.
Expand Down
66 changes: 59 additions & 7 deletions internal/agentlogin/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
)

var (
// The key names: *key*, *token*, *secret*, *password*, Authorization (any case).
redactName = regexp.MustCompile(`(?i)(key|token|secret|passw|^authorization$|^proxy-authorization$|^cookie$)`)
envName = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)
envRef = regexp.MustCompile(`^(\{(env|file):[^}]*\}|\$\{?[A-Za-z_][A-Za-z0-9_]*\}?)$`)
// A value that names where a secret is rather than holding it: an env
// var's name, or a reference to one ({env:X}, ${X}).
envName = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)
envRef = regexp.MustCompile(`^(\{(env|file):[^}]*\}|\$\{?[A-Za-z_][A-Za-z0-9_]*\}?)$`)
// A switch or a port, kept in env tables: true, false or a number of at most five digits.
plainValue = regexp.MustCompile(`^(?i:true|false|[0-9]{1,5})$`)
// An env variable whose name says it holds a credential, or headers, wherever it is: its
Expand All @@ -36,6 +36,58 @@ var (
`CLAUDE_CODE_[A-Z0-9_]+|DISABLE_[A-Z0-9_]+|MAX_[A-Z0-9_]+|[A-Z0-9_]+_TIMEOUT_MS)$`)
)

// secretKeyName reports whether k names a secret: a stem as a word of its own,
// at a camelCase or separator boundary, or at the key's end, with a trailing s
// (apiKeys, tokens) allowed — but not as the start of a longer lowercase word,
// as in tokeneconomizer, a marketplace name rather than a token. Authorization
// and cookie only as the whole key.
func secretKeyName(k string) bool {
lower := strings.ToLower(k)
switch lower {
case "authorization", "proxy-authorization", "cookie":
return true
}
// The words that name a secret in a settings key.
stems := []string{"key", "token", "secret", "password", "passwd"}
for _, stem := range stems {
for i := 0; i+len(stem) <= len(lower); {
j := strings.Index(lower[i:], stem)
if j < 0 {
break
}
i += j
if secretStemAt(k, i, i+len(stem)) {
return true
}
i++
}
}
return false
}

// secretStemAt reports whether the stem at k[i:j] stands as a word: what
// follows it ends it (the key's end, a separator, an uppercase letter, a digit,
// or a trailing s before one of those), and what precedes it ends the word
// before it — or the stem ends the key, as in apikey.
func secretStemAt(k string, i, j int) bool {
if j < len(k) {
switch next := k[j]; {
case next == 's' && j+1 < len(k) && k[j+1] >= 'a' && k[j+1] <= 'z':
return false // a longer word starting with s, as in keystore
case next >= 'a' && next <= 'z' && next != 's':
return false // a longer lowercase word, as in tokeneconomizer
}
}
if i == 0 || j == len(k) {
return true
}
prev := k[i-1]
if (prev < 'a' || prev > 'z') && (prev < 'A' || prev > 'Z') {
return true
}
return k[i] >= 'A' && k[i] <= 'Z' // a camelCase boundary, as in apiKey
}

// agentSetting: the value at path, under key k, is one of Claude Code's configuration variables in
// its settings' top-level env (claudeEnv), and its name doesn't say it's a credential (credName).
func agentSetting(path, k string) bool {
Expand Down Expand Up @@ -126,9 +178,9 @@ func (r *redaction) value(v any, path, k string, secret bool) any {
func secretIn(path, k, ck string, x any) bool {
if _, ok := x.(map[string]any); ok {
name := strings.ToLower(ck)
return redactName.MatchString(ck) && !strings.Contains(name, "keybind") && !strings.Contains(name, "keymap")
return secretKeyName(ck) && !strings.Contains(name, "keybind") && !strings.Contains(name, "keymap")
}
return redactName.MatchString(ck) || wholeTable(path, k)
return secretKeyName(ck) || wholeTable(path, k)
}

// str returns s, at path under key k, as the VM's copy holds it. A URL in a table of secrets, such
Expand All @@ -137,7 +189,7 @@ func secretIn(path, k, ck string, x any) bool {
// keys and credentials go (scrubText).
func (r *redaction) str(s, path, k string, secret bool) string {
url := strings.Contains(s, "://")
named := redactName.MatchString(k) || credName.MatchString(k)
named := secretKeyName(k) || credName.MatchString(k)
keep := redactKeep(s) || plainValue.MatchString(s) || (url && !named) || agentSetting(path, k)
if (secret && !keep) || secretShape.MatchString(s) { // a known key's shape, wherever it is
r.paths = append(r.paths, path)
Expand Down
57 changes: 57 additions & 0 deletions internal/agentlogin/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,63 @@ func TestRedactFreeText(t *testing.T) {
}
}

// A marketplace or plugin name that only starts with a secret word —
// tokeneconomizer, the TokenEconomizer plugin — keeps its settings, while a
// real secret key under it still goes.
func TestRedactKeepsMarketplaceName(t *testing.T) {
in := `{
"extraKnownMarketplaces": {
"tokeneconomizer": {"source": {"source": "github"}},
"tokeneconomizerApiKey": "DUMMY-UNDER-MARKETPLACE"
},
"marketplaces": {"tokeneconomizer": {"source_type": "github"}}
}`
out, names := redacted(t, "json", in)
for _, s := range []string{`"source": "github"`, `"source_type": "github"`} {
if !strings.Contains(out, s) {
t.Errorf("copy lost %s:\n%s", s, out)
}
}
if strings.Contains(out, "DUMMY-UNDER-MARKETPLACE") {
t.Errorf("copy holds the secret under the marketplace name:\n%s", out)
}
if want := "extraKnownMarketplaces.tokeneconomizerApiKey\n"; names != want {
t.Errorf("names = %q, want %q", names, want)
}

tomlIn := "[marketplaces.tokeneconomizer]\nsource_type = \"github\"\n"
out, _ = redacted(t, "toml", tomlIn)
if !strings.Contains(out, `"github"`) {
t.Errorf("TOML copy lost the marketplace source type:\n%s", out)
}
}

// secretKeyName: a secret word as a word of its own, at a camelCase or
// separator boundary, or at the key's end — not as the start of a longer
// lowercase word.
func TestSecretKeyName(t *testing.T) {
for _, k := range []string{
"token", "apiKey", "apikey", "api_key", "API_KEY", "accessToken", "oauth_token",
"tokens", "apiKeys", "keys", "clientSecret", "client_secret", "mySecret",
"password", "passwd", "userPassword", "privateKey", "refresh_token",
"Authorization", "Proxy-Authorization", "cookie", "GITHUB_TOKEN",
"ANTHROPIC_API_KEY", "SECRET_X", "tokenList", "mytoken", "thekey",
"tokeneconomizerApiKey", "secretKey",
} {
if !secretKeyName(k) {
t.Errorf("secretKeyName(%q) = false", k)
}
}
for _, k := range []string{
"tokeneconomizer", "tokenizer", "keystore", "keyboard", "secretary",
"tokenize", "secretive", "model", "preferred_auth_method", "baseURL",
} {
if secretKeyName(k) {
t.Errorf("secretKeyName(%q) = true", k)
}
}
}

// opencode.json (JSONC) and Claude Code's settings.json.
func TestRedactJSON(t *testing.T) {
in := `{
Expand Down
Loading