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\"}")