|
1 | 1 | package detector |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 | ) |
6 | 9 |
|
@@ -42,3 +45,148 @@ func TestResult_HasThreats(t *testing.T) { |
42 | 45 | }) |
43 | 46 | } |
44 | 47 | } |
| 48 | + |
| 49 | +func TestParseStructuredResult_ReasonBounds(t *testing.T) { |
| 50 | + build := func(reasons string) []byte { |
| 51 | + return []byte(`{"prompt_injection":false,"secret_leak":false,"malicious_patch":false,"reasons":[` + reasons + `]}`) |
| 52 | + } |
| 53 | + |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + reasons string |
| 57 | + wantErr bool |
| 58 | + }{ |
| 59 | + {"single reason", `"looks fine"`, false}, |
| 60 | + {"max length reason", `"` + strings.Repeat("x", MaxReasonRunes) + `"`, false}, |
| 61 | + {"max count reasons", strings.TrimSuffix(strings.Repeat(`"r",`, MaxReasons), ","), false}, |
| 62 | + {"empty reason", `""`, true}, |
| 63 | + {"whitespace-only reason", `" \t "`, true}, |
| 64 | + {"over-long reason", `"` + strings.Repeat("x", MaxReasonRunes+1) + `"`, true}, |
| 65 | + {"too many reasons", strings.TrimSuffix(strings.Repeat(`"r",`, MaxReasons+1), ","), true}, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.name, func(t *testing.T) { |
| 70 | + _, err := ParseStructuredResult(build(tt.reasons)) |
| 71 | + if tt.wantErr && err == nil { |
| 72 | + t.Fatalf("expected error for %s", tt.name) |
| 73 | + } |
| 74 | + if !tt.wantErr && err != nil { |
| 75 | + t.Fatalf("unexpected error for %s: %v", tt.name, err) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestParseStructuredResult_ReasonRunesCountedAsRunes(t *testing.T) { |
| 82 | + // MaxReasonRunes bounds characters, not bytes: a multi-byte reason at the |
| 83 | + // limit must be accepted even though its byte length exceeds the limit. |
| 84 | + data := []byte(`{"prompt_injection":false,"secret_leak":false,"malicious_patch":false,"reasons":["` + |
| 85 | + strings.Repeat("é", MaxReasonRunes) + `"]}`) |
| 86 | + if _, err := ParseStructuredResult(data); err != nil { |
| 87 | + t.Fatalf("unexpected error for multi-byte reason at the rune limit: %v", err) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestReadResultFile_RejectsOversizeFile(t *testing.T) { |
| 92 | + dir := t.TempDir() |
| 93 | + path := filepath.Join(dir, "detection_result.json") |
| 94 | + // A syntactically valid but oversized file must be rejected on size before |
| 95 | + // it is parsed, so a hostile file cannot be read into memory in full. |
| 96 | + payload := `{"prompt_injection":false,"secret_leak":false,"malicious_patch":false,"reasons":["` + |
| 97 | + strings.Repeat("x", MaxResultFileBytes) + `"]}` |
| 98 | + if err := os.WriteFile(path, []byte(payload), 0o600); err != nil { |
| 99 | + t.Fatalf("writing oversize result file: %v", err) |
| 100 | + } |
| 101 | + _, err := ReadResultFile(path) |
| 102 | + if err == nil { |
| 103 | + t.Fatal("expected oversize result file to be rejected") |
| 104 | + } |
| 105 | + if !strings.Contains(err.Error(), "exceeds the maximum size") { |
| 106 | + t.Fatalf("expected size error, got: %v", err) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestReadResultFile_RoundTripsWrittenResult(t *testing.T) { |
| 111 | + dir := t.TempDir() |
| 112 | + path := filepath.Join(dir, "detection_result.json") |
| 113 | + // Every result the detector writes must pass its own read-side validation. |
| 114 | + want := BuildResultFromReport(true, false, false, []string{"injected instruction in issue body"}) |
| 115 | + if err := WriteResultFile(path, want); err != nil { |
| 116 | + t.Fatalf("writing result: %v", err) |
| 117 | + } |
| 118 | + got, err := ReadResultFile(path) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("reading back written result: %v", err) |
| 121 | + } |
| 122 | + if !got.HasThreats() || len(got.Reasons) != 1 || got.Reasons[0] != want.Reasons[0] { |
| 123 | + t.Fatalf("round-trip mismatch: %+v", got) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestValidateReportFields_BoundsReasons(t *testing.T) { |
| 128 | + if msg := ValidateReportFields(false, false, false, []any{" "}); msg == "" { |
| 129 | + t.Fatal("expected whitespace-only reason to be rejected at report time") |
| 130 | + } |
| 131 | + if msg := ValidateReportFields(true, false, false, []any{"real reason"}); msg != "" { |
| 132 | + t.Fatalf("unexpected rejection: %s", msg) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// TestWriteResultFile_RejectsUnreadableResult verifies the write/read symmetry |
| 137 | +// required by TD-10b: a result that would be rejected on read must be rejected |
| 138 | +// on write, and must not leave a file behind. Without this, a caller that |
| 139 | +// bypassed the report-time validation could persist a result file that only |
| 140 | +// fails much later, at conclude time, as a misleading parse error. |
| 141 | +func TestWriteResultFile_RejectsUnreadableResult(t *testing.T) { |
| 142 | + tests := []struct { |
| 143 | + name string |
| 144 | + result *Result |
| 145 | + }{ |
| 146 | + {"over-long reason", &Result{PromptInjection: true, Reasons: []string{strings.Repeat("x", MaxReasonRunes+1)}}}, |
| 147 | + {"blank reason", &Result{PromptInjection: true, Reasons: []string{" "}}}, |
| 148 | + {"empty reason", &Result{PromptInjection: true, Reasons: []string{""}}}, |
| 149 | + {"too many reasons", &Result{PromptInjection: true, Reasons: make([]string, MaxReasons+1)}}, |
| 150 | + } |
| 151 | + |
| 152 | + for _, tt := range tests { |
| 153 | + t.Run(tt.name, func(t *testing.T) { |
| 154 | + dir := t.TempDir() |
| 155 | + path := filepath.Join(dir, "detection_result.json") |
| 156 | + if err := WriteResultFile(path, tt.result); err == nil { |
| 157 | + t.Fatal("expected write to be rejected") |
| 158 | + } |
| 159 | + if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 160 | + t.Fatalf("rejected write must not leave a result file behind (stat err = %v)", err) |
| 161 | + } |
| 162 | + // The atomic-write temp file must be cleaned up too. |
| 163 | + entries, err := os.ReadDir(dir) |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("ReadDir error = %v", err) |
| 166 | + } |
| 167 | + if len(entries) != 0 { |
| 168 | + t.Fatalf("rejected write left %d file(s) behind: %v", len(entries), entries) |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// TestWriteResultFile_AcceptsBoundaryResult verifies the symmetry does not |
| 175 | +// over-reject: a result exactly at the documented limits must round-trip. |
| 176 | +func TestWriteResultFile_AcceptsBoundaryResult(t *testing.T) { |
| 177 | + path := filepath.Join(t.TempDir(), "detection_result.json") |
| 178 | + reasons := make([]string, MaxReasons) |
| 179 | + for i := range reasons { |
| 180 | + reasons[i] = strings.Repeat("y", MaxReasonRunes) |
| 181 | + } |
| 182 | + if err := WriteResultFile(path, &Result{SecretLeak: true, Reasons: reasons}); err != nil { |
| 183 | + t.Fatalf("result at the documented limits must be writable: %v", err) |
| 184 | + } |
| 185 | + got, err := ReadResultFile(path) |
| 186 | + if err != nil { |
| 187 | + t.Fatalf("result at the documented limits must be readable: %v", err) |
| 188 | + } |
| 189 | + if len(got.Reasons) != MaxReasons { |
| 190 | + t.Fatalf("reasons = %d, want %d", len(got.Reasons), MaxReasons) |
| 191 | + } |
| 192 | +} |
0 commit comments