From 22d0abe38ebdb4dbf66ab0b32c84e8ae676c507d Mon Sep 17 00:00:00 2001 From: w3cj Date: Mon, 30 Nov 2026 23:15:59 -0700 Subject: [PATCH] fix(agentlogin): FuzzCapture looks for tokens in what placeholders carry, not their fixed parts The nightly fuzz run failed FuzzCapture for four days on one input: a 26-character access token, "eyJhbGciOiJub25lIiwidHlwIj", which is a prefix of fakeJWT's fixed header, with an id_token carrying account claims (an email and an OpenAI account). The VM's reply held only placeholders; the check matched the token against devvm's own header in the id_token's placeholder. The earlier fix skipped a placeholder only when its claims were devvm's mark and an expiry alone, so a placeholder that carries an account's claims, as Codex's and opencode's do, still had its header checked. The test's oracle was wrong, not devvm. It now looks for each token in every string of the VM's reply, keys and nested values included, rather than in its JSON, so no token matches across quotes and braces or misses one that JSON escapes. A JWT-shaped placeholder (fakeJWT's header and mark) stands for its decoded claims, without devvm's mark and expiry and without any claim that one of the reply's own JWTs has with the same value, the account the placeholders carry by design. Plain fields keep the strict rule: only a non-token field of the reply excuses a token. The oracle still catches a leak: accountClaims made to copy the token into a claim fails FuzzCapture on six seeds. Eight minutes of fuzzing found nothing more. The corpus gets the crash input, ed0b7ccd0f55eed0, and placeholder-claims-json, a token shaped like the claims' JSON ('"id_token":{"email":"a@b"'), which fails a check on the reply's JSON. Co-Authored-By: Claude Opus 5.5 --- internal/agentlogin/capture_test.go | 93 ++++++++++++------- .../fuzz/FuzzCapture/ed0b7ccd0f55eed0 | 5 + .../fuzz/FuzzCapture/placeholder-claims-json | 5 + 3 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 internal/agentlogin/testdata/fuzz/FuzzCapture/ed0b7ccd0f55eed0 create mode 100644 internal/agentlogin/testdata/fuzz/FuzzCapture/placeholder-claims-json diff --git a/internal/agentlogin/capture_test.go b/internal/agentlogin/capture_test.go index b0de7af5..82645dc4 100644 --- a/internal/agentlogin/capture_test.go +++ b/internal/agentlogin/capture_test.go @@ -507,60 +507,87 @@ func FuzzCapture(f *testing.F) { if err := json.Unmarshal(out, &vm); err != nil || vm == nil { t.Fatalf("the VM's reply isn't a JSON object: %q", out) } - rest := withoutPlaceholders(t, vm) - for _, tok := range tokens { - if strings.Contains(rest, tok) && !keptElsewhere(r, tok) { - t.Fatalf("the VM's reply holds the token %q: %s", tok, out) + for _, s := range withoutPlaceholders(r, vm) { + for _, tok := range tokens { + if strings.Contains(s, tok) && !keptElsewhere(r, tok) { + t.Fatalf("the VM's reply holds the token %q: %s", tok, out) + } } } }) } -// withoutPlaceholders is the VM's reply as JSON without devvm's own placeholders, which a token -// can match by chance (a short one is a prefix of fakeJWT's header): devvm-proxy-managed, and a -// JWT-shaped placeholder whose header and claims are fakeJWT's, devvm's mark and an expiry. -func withoutPlaceholders(t *testing.T, vm map[string]any) string { - t.Helper() +// withoutPlaceholders is every string in the VM's reply, its keys and values, nested ones +// included, but what devvm itself wrote in its placeholders, which a token can match by chance (a +// short one is a prefix of fakeJWT's header, whatever claims follow it): devvm-proxy-managed goes, +// and a JWT-shaped placeholder, fakeJWT's header and mark, leaves only its claims, decoded, but +// devvm's own (its mark and an expiry) and those that one of r's JWTs has as they are, the +// account the VM's placeholders carry by design. Strings, not the reply's JSON, so that no token +// matches across the JSON's quotes and braces, or misses one that JSON escapes. +func withoutPlaceholders(r, vm map[string]any) []string { header := strings.SplitN(fakeJWT(nil), ".", 2)[0] - rest := map[string]any{} + var out []string for k, v := range vm { + out = append(out, k) s, _ := v.(string) - parts := strings.Split(s, ".") - if s == managed || len(parts) == 3 && parts[0] == header && parts[2] == jwtMark && devvmClaims(parts[1]) { + if s == managed { continue } - rest[k] = v + if parts := strings.Split(s, "."); len(parts) == 3 && parts[0] == header && parts[2] == jwtMark { + if c, ok := placeholderClaims(r, parts[1]); ok { + v = c + } + } + out = appendStrings(out, v) } - b, err := json.Marshal(rest) - if err != nil { - t.Fatal(err) - } - return string(b) + return out } -// devvmClaims reports whether seg, a JWT's encoded claims, holds only the claims devvm's -// placeholders have: its mark and an expiry. -func devvmClaims(seg string) bool { +// placeholderClaims is seg, a JWT-shaped placeholder's encoded claims, decoded, without the claims +// devvm adds to every placeholder (its mark and an expiry) and those that one of r's JWTs has +// with the same value. It's false when seg isn't claims. +func placeholderClaims(r map[string]any, seg string) (map[string]any, bool) { raw, err := base64.RawURLEncoding.DecodeString(seg) if err != nil { - return false + return nil, false } var c map[string]any - if json.Unmarshal(raw, &c) != nil { - return false + if json.Unmarshal(raw, &c) != nil || c == nil { + return nil, false } - for k, v := range c { - switch s, _ := v.(string); { - case k == "devvm" && strings.HasPrefix(s, "placeholder-"): - case k == "exp": - if _, ok := v.(float64); !ok { - return false + if s, _ := c["devvm"].(string); strings.HasPrefix(s, "placeholder-") { + delete(c, "devvm") + } + if _, ok := c["exp"].(float64); ok { + delete(c, "exp") + } + for _, v := range r { + s, _ := v.(string) + for k, want := range jwtClaims(redact.NewSecret(s)) { + if got, ok := c[k]; ok && reflect.DeepEqual(got, want) { + delete(c, k) } - default: - return false } } - return true + return c, true +} + +// appendStrings appends every string in v, decoded JSON, to out: a string, an object's keys and +// values, and an array's items. +func appendStrings(out []string, v any) []string { + switch v := v.(type) { + case string: + out = append(out, v) + case map[string]any: + for k, e := range v { + out = appendStrings(append(out, k), e) + } + case []any: + for _, e := range v { + out = appendStrings(out, e) + } + } + return out } // keptElsewhere reports whether a field of r besides its tokens holds tok, which the VM's reply diff --git a/internal/agentlogin/testdata/fuzz/FuzzCapture/ed0b7ccd0f55eed0 b/internal/agentlogin/testdata/fuzz/FuzzCapture/ed0b7ccd0f55eed0 new file mode 100644 index 00000000..8b568f55 --- /dev/null +++ b/internal/agentlogin/testdata/fuzz/FuzzCapture/ed0b7ccd0f55eed0 @@ -0,0 +1,5 @@ +go test fuzz v1 +byte('\x01') +string(" ") +[]byte("grant_type=authorization_code") +[]byte("{\"access_token\":\"eyJhbGciOiJub25lIiwidHlwIj\",\"\":\"\",\"id_token\":\".eyJlbWFpbCI6ImFAYiIsImV4cCI6MTc5NDkyMDU2NiwiaHR0cHM6Ly9hcGkub3BlbmFpLmNvbS9hdXRoIjp7ImNoYXRncHRfYWNjb3VudF9pZCI6ImFjY3QtMSIsImNoYXRncHRfcGxhbl90eXBlIjoicHJvIn19.\",\"refresh_token\":\"r\"}") diff --git a/internal/agentlogin/testdata/fuzz/FuzzCapture/placeholder-claims-json b/internal/agentlogin/testdata/fuzz/FuzzCapture/placeholder-claims-json new file mode 100644 index 00000000..84b3d8b1 --- /dev/null +++ b/internal/agentlogin/testdata/fuzz/FuzzCapture/placeholder-claims-json @@ -0,0 +1,5 @@ +go test fuzz v1 +byte('\x01') +string(" ") +[]byte("grant_type=authorization_code") +[]byte("{\"access_token\":\"\\\"id_token\\\":{\\\"email\\\":\\\"a@b\\\"\",\"id_token\":\".eyJlbWFpbCI6ImFAYiIsImV4cCI6MTc5NDkyMDU2NiwiaHR0cHM6Ly9hcGkub3BlbmFpLmNvbS9hdXRoIjp7ImNoYXRncHRfYWNjb3VudF9pZCI6ImFjY3QtMSIsImNoYXRncHRfcGxhbl90eXBlIjoicHJvIn19.\",\"refresh_token\":\"r\"}")