diff --git a/changelog.d/fixed-admin-mcp-read-scrub-false-positives.md b/changelog.d/fixed-admin-mcp-read-scrub-false-positives.md new file mode 100644 index 0000000000..2a5a66ae21 --- /dev/null +++ b/changelog.d/fixed-admin-mcp-read-scrub-false-positives.md @@ -0,0 +1 @@ +- Admin MCP reads no longer mask values that cannot hold a credential. A read masked everything under a key containing `token`, `secret`, `password`, `credential` or `authorization` (booleans and `*tokens` counts excepted), so `settings_read` returned `[masked:token]` for a numeric rate (`tokens_per_coin`); `[masked:authorization]` for the whole per-repository self-authorization hold policy (`repoSelfAuthorizationHold`); and a mask for a secret that is not set at all, which read the same as one that is. `contributors_list` hid `token_expires_at`, `token_issued_at` and `token_last_used_at`. These are now shown: an empty value or `null`, an empty list or object, a timestamp, a token count or rate, and the self-authorization hold settings. Everything else under those keys is masked as before, kept values are still scrubbed by shape, and the stricter rule that decides what a write preview may store is unchanged. diff --git a/src/docs/admin-mcp.md b/src/docs/admin-mcp.md index f823ac7bb4..30ba6d3983 100644 --- a/src/docs/admin-mcp.md +++ b/src/docs/admin-mcp.md @@ -259,6 +259,30 @@ configuration and its provenance), an agent's export bundle (the `agent` and presentational. A test fails when the dashboard serves a new `GET /api/config` route that is neither a section nor one of these exclusions. +### What a read masks + +Every read result is scrubbed in two ways. Strings are scrubbed by shape: +GitHub tokens, JWTs, bearer tokens, private keys and similar are replaced +wherever they appear. Values are also masked by key: a value under a key that +contains `token`, `secret`, `password`, `credential` or `authorization` is +replaced by `[masked:...]`, unless it cannot hold a credential: + +- `true` or `false`, `null`, or an empty string, list or object; +- a timestamp (`token_expires_at`); +- a token count or rate (`totalTokens`, `tokens_per_coin`); +- the self-authorization hold settings (`selfAuthorizationHold`, + `repoSelfAuthorizationHold`), which are a merge policy and not a header. + +The name of an environment variable under a key such as `token_env` is a +string like any other there, so it is masked too when it is set. + +So `[masked:token]` means a value is there and was withheld, and an empty +value means nothing is set. A list or object with content under such a key is +masked whole (for example `credentialWarnings` when there are warnings); read +it on the dashboard. The one exception is a token count block, an object under +a count key such as `tokens`: it is read like any other object, and the keys +inside it are judged by the same rules. + Meta tools: - `exclusion_catalogue` lists what the admin MCP will never do, and why. diff --git a/src/pkg/adminmcp/adminmcp.go b/src/pkg/adminmcp/adminmcp.go index 38cb05a6a1..83071277d4 100644 --- a/src/pkg/adminmcp/adminmcp.go +++ b/src/pkg/adminmcp/adminmcp.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/hivecommons/hive/crux/logscrub" ) @@ -827,7 +828,7 @@ func maskSensitive(b []byte) []byte { case map[string]any: out := map[string]any{} for k, val := range x { - if sensitiveKey(k) && !maskExempt(k, val) { + if sensitiveKey(k) && !readMaskExempt(k, val) { out[k] = "[masked:" + maskLabel(k) + "]" } else { out[k] = walk(val) @@ -857,7 +858,111 @@ func maskSensitive(b []byte) []byte { func sensitiveKey(k string) bool { k = strings.ToLower(k) - return strings.Contains(k, "token") || strings.Contains(k, "secret") || strings.Contains(k, "password") || strings.Contains(k, "credential") || strings.Contains(k, "authorization") + return strings.Contains(k, "token") || strings.Contains(k, "secret") || strings.Contains(k, "password") || strings.Contains(k, "credential") || authorizationKey(k) +} + +// authorizationKey reports whether a lower-cased key names an Authorization +// header or its value. The self-authorization hold (selfAuthorizationHold, +// repoSelfAuthorizationHold, self_authorization_hold) is a merge policy, the +// rule on whether a hive may approve its own pull requests, and not a header. +func authorizationKey(lower string) bool { + if !strings.Contains(lower, "authorization") { + return false + } + squashed := strings.ReplaceAll(strings.ReplaceAll(lower, "_", ""), "-", "") + return !strings.HasSuffix(squashed, "selfauthorizationhold") +} + +// readMaskExempt reports whether a value under a sensitive-looking key can be +// shown in a read result because it cannot hold a credential. It adds to +// maskExempt, which the pending-confirmation scrub shares and which stays as +// strict as it was: +// +// - nothing at all: null, "", {} and []. Masking those made a secret that +// is not set look set. +// - a timestamp (token_expires_at). +// - a token count or rate with its unit after the word (tokens_per_coin). +// +// A string kept this way is still scrubbed by shape like any other string. +func readMaskExempt(k string, v any) bool { + if maskExempt(k, v) { + return true + } + switch x := v.(type) { + case nil: + return true + case string: + return x == "" || isTimestamp(x) + case []any: + return len(x) == 0 + case map[string]any: + return len(x) == 0 || tokenRateKey(k) + case float64: + return tokenRateKey(k) + default: + return false + } +} + +func isTimestamp(s string) bool { + _, err := time.Parse(time.RFC3339Nano, s) + return err == nil +} + +// keyWords splits a JSON key into lower-case words at "_", "-", "." and +// camelCase boundaries: "webhookSecretEnv" is webhook, secret, env. +func keyWords(k string) []string { + var words []string + var cur []rune + flush := func() { + if len(cur) > 0 { + words = append(words, strings.ToLower(string(cur))) + cur = cur[:0] + } + } + var prev rune + for _, r := range k { + switch { + case r == '_' || r == '-' || r == '.' || r == ' ': + flush() + case unicode.IsUpper(r) && (unicode.IsLower(prev) || unicode.IsDigit(prev)): + flush() + cur = append(cur, r) + default: + cur = append(cur, r) + } + prev = r + } + flush() + return words +} + +// tokenRateKey reports whether k has "tokens" as a whole word with more after +// it (tokens_per_coin, tokensIn): a count or rate like the keys tokenCountKey +// accepts, with the unit after the word. A credential qualifier before the +// word (access_tokens_revoked) or another sensitive word in the key rules it +// out. +func tokenRateKey(k string) bool { + lower := strings.ToLower(k) + for _, word := range []string{"secret", "password", "credential", "authorization"} { + if strings.Contains(lower, word) { + return false + } + } + words := keyWords(k) + for i, word := range words { + if word != "tokens" { + continue + } + stem := strings.Join(words[:i], "") + for _, q := range credentialTokenQualifiers { + if strings.HasSuffix(stem, q) { + return false + } + } + return true + } + return false } // credentialTokenQualifiers mark a plural "*tokens" key as a collection of diff --git a/src/pkg/adminmcp/scrub_read_test.go b/src/pkg/adminmcp/scrub_read_test.go new file mode 100644 index 0000000000..3bf563f9c5 --- /dev/null +++ b/src/pkg/adminmcp/scrub_read_test.go @@ -0,0 +1,121 @@ +package adminmcp + +import ( + "reflect" + "testing" +) + +// A read masked every value under a key containing token, secret, password, +// credential or authorization. settings_read then hid a numeric token rate, +// token timestamps and a whole merge-policy block, and showed "[masked:token]" +// for a secret that is not set at all. +func TestScrubShowsValuesThatCannotBeASecret(t *testing.T) { + hold := map[string]any{"org/repo": map[string]any{"enabled": true, "source": "config"}} + for key, value := range map[string]any{ + // counts and rates + "tokens_per_coin": float64(1000), + "tokensIn": float64(5), + "max_tokens_per_day": float64(100), + "tokens_by_model": map[string]any{"opus": float64(7)}, + // timestamps + "token_expires_at": "2026-12-01T00:00:00Z", + "token_issued_at": "2026-11-01T10:00:00.123456789-05:00", + "token_last_used_at": "2026-11-06T12:13:51Z", + // nothing to mask + "auth_token": "", + "registration_token": nil, + "credentialWarnings": []any{}, + "client_secret": map[string]any{}, + // the self-authorization hold is a merge policy + "repoSelfAuthorizationHold": hold, + "self_authorization_hold": map[string]any{"enabled": false}, + } { + out := Scrub(map[string]any{key: value}).(map[string]any) + if !reflect.DeepEqual(out[key], value) { + t.Errorf("%s: read shows %#v, want %#v", key, out[key], value) + } + } +} + +func TestScrubStillMasksWhatCouldBeASecret(t *testing.T) { + for name, tc := range map[string]struct { + key string + value any + }{ + "token": {"token", "opaque-value"}, + "auth token": {"auth_token", "abc123"}, + "registration token": {"registration_token", "r-1"}, + "password": {"password", "hunter2"}, + "client secret": {"client_secret", "s3"}, + "authorization header": {"Authorization", "opaque"}, + "proxy authorization": {"proxy_authorization", "opaque"}, + "authorization hold, no self": {"authorizationHold", "opaque"}, + "hold key that names a token": {"self_authorization_hold_token", "opaque"}, + "env key, variable name": {"token_env", "GITLAB_TOKEN"}, + "env key, camel case": {"webhookSecretEnv", "HIVE_WEBHOOK_SECRET"}, + "env key, lower-case value": {"token_env", "a3f1c9d2e4b5"}, + "env-shaped value, not env key": {"api_token", "GITLAB_TOKEN"}, + "credential count": {"access_tokens_per_user", float64(5)}, + "refresh token map": {"refresh_tokens_by_user", map[string]any{"a": "b"}}, + "tokens with secret": {"tokens_secret", map[string]any{"a": "b"}}, + "numeric under token secret": {"token_secret", float64(123456)}, + "number under password": {"password", float64(12345678)}, + "list of tokens": {"refresh_tokens", []any{"opaque"}}, + "warnings with text": {"credentialWarnings", []any{"a warning"}}, + "credential block": {"credentialInjection", map[string]any{"enabled": true}}, + "almost a timestamp": {"token", "2026-13-45T00:00:00Z"}, + "date only": {"token", "2026-11-06"}, + } { + out := Scrub(map[string]any{tc.key: tc.value}).(map[string]any) + got, _ := out[tc.key].(string) + if len(got) < len("[masked:") || got[:len("[masked:")] != "[masked:" { + t.Errorf("%s: %s is shown as %#v, want it masked", name, tc.key, out[tc.key]) + } + } +} + +// A value kept because of its key is still scrubbed by shape. +func TestScrubStillScrubsKeptValuesByShape(t *testing.T) { + out := Scrub(map[string]any{ + "repoSelfAuthorizationHold": map[string]any{"org/repo": map[string]any{"reason": "see Bearer abcdefghijklmnopqrstuvwxyz012345", "api_token": "opaque"}}, + }).(map[string]any) + repo := out["repoSelfAuthorizationHold"].(map[string]any)["org/repo"].(map[string]any) + if repo["api_token"] != "[masked:token]" { + t.Errorf("a token inside a shown block is not masked: %#v", repo) + } + if reason, _ := repo["reason"].(string); reason == "see Bearer abcdefghijklmnopqrstuvwxyz012345" { + t.Errorf("a bearer token inside a shown block is not scrubbed: %q", reason) + } +} + +// The pending-confirmation scrub decides what a write may store. It is not +// loosened: these stay refused there. +func TestPendingScrubIsAsStrictAsBefore(t *testing.T) { + out := scrubPendingMap(map[string]any{ + "token_env": "GITLAB_TOKEN", + "tokens_per_coin": 1000, + "token_expires_at": "2026-12-01T00:00:00Z", + "api_key": "", + "otelHeaders": map[string]any{}, + }).(map[string]any) + for key, value := range out { + if text, _ := value.(string); len(text) < len("[masked:") || text[:len("[masked:")] != "[masked:" { + t.Errorf("pending scrub now keeps %s = %#v", key, value) + } + } +} + +func TestKeyWords(t *testing.T) { + for key, want := range map[string][]string{ + "webhookSecretEnv": {"webhook", "secret", "env"}, + "token_env": {"token", "env"}, + "TOKENS_PER_COIN": {"tokens", "per", "coin"}, + "tokens-in": {"tokens", "in"}, + "tokensecret": {"tokensecret"}, + "oauth2Token": {"oauth2", "token"}, + } { + if got := keyWords(key); !reflect.DeepEqual(got, want) { + t.Errorf("keyWords(%q) = %v, want %v", key, got, want) + } + } +}