|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBuildToolArgumentsPreviewMaskJSONSensitiveFields(t *testing.T) { |
| 9 | + t.Parallel() |
| 10 | + |
| 11 | + raw := `{"api_key":"sk-123","password":"p@ss","nested":{"secret":"abc"},"safe":"ok"}` |
| 12 | + preview := buildToolArgumentsPreview(raw) |
| 13 | + if strings.Contains(preview, "sk-123") { |
| 14 | + t.Fatalf("preview leaked api_key: %q", preview) |
| 15 | + } |
| 16 | + if strings.Contains(preview, "p@ss") { |
| 17 | + t.Fatalf("preview leaked password: %q", preview) |
| 18 | + } |
| 19 | + if strings.Contains(preview, `"secret":"abc"`) { |
| 20 | + t.Fatalf("preview leaked nested secret: %q", preview) |
| 21 | + } |
| 22 | + if !strings.Contains(preview, `"api_key":"***"`) { |
| 23 | + t.Fatalf("preview should mask api_key: %q", preview) |
| 24 | + } |
| 25 | + if !strings.Contains(preview, `"password":"***"`) { |
| 26 | + t.Fatalf("preview should mask password: %q", preview) |
| 27 | + } |
| 28 | + if !strings.Contains(preview, `"secret":"***"`) { |
| 29 | + t.Fatalf("preview should mask nested secret: %q", preview) |
| 30 | + } |
| 31 | + if !strings.Contains(preview, `"safe":"ok"`) { |
| 32 | + t.Fatalf("preview should keep non-sensitive keys: %q", preview) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestBuildToolArgumentsPreviewMaskNonJSONFallback(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + preview := buildToolArgumentsPreview(`token=abc password:xyz arg=ok`) |
| 40 | + if strings.Contains(preview, "abc") || strings.Contains(preview, "xyz") { |
| 41 | + t.Fatalf("preview leaked fallback credentials: %q", preview) |
| 42 | + } |
| 43 | + if !strings.Contains(preview, "token=***") { |
| 44 | + t.Fatalf("preview should mask token in fallback mode: %q", preview) |
| 45 | + } |
| 46 | + if !strings.Contains(preview, "password=***") { |
| 47 | + t.Fatalf("preview should mask password in fallback mode: %q", preview) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestBuildToolArgumentsPreviewTruncate(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + raw := strings.Repeat("a", hookToolArgumentsPreviewMaxChars+20) |
| 55 | + preview := buildToolArgumentsPreview(raw) |
| 56 | + if len([]rune(preview)) != hookToolArgumentsPreviewMaxChars { |
| 57 | + t.Fatalf("preview length=%d, want %d", len([]rune(preview)), hookToolArgumentsPreviewMaxChars) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestIsSensitiveHookToolArgumentKey(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + cases := []struct { |
| 65 | + key string |
| 66 | + want bool |
| 67 | + }{ |
| 68 | + {key: "api_key", want: true}, |
| 69 | + {key: "accessKey", want: true}, |
| 70 | + {key: "authorization", want: true}, |
| 71 | + {key: "auth_token", want: true}, |
| 72 | + {key: "password", want: true}, |
| 73 | + {key: "author", want: false}, |
| 74 | + {key: "tool_name", want: false}, |
| 75 | + } |
| 76 | + for _, tc := range cases { |
| 77 | + if got := isSensitiveHookToolArgumentKey(tc.key); got != tc.want { |
| 78 | + t.Fatalf("isSensitiveHookToolArgumentKey(%q)=%v, want %v", tc.key, got, tc.want) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments