|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "net/http" |
6 | 7 | "net/http/httptest" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
9 | 10 | "strings" |
10 | 11 | "testing" |
| 12 | + "time" |
11 | 13 |
|
12 | 14 | "github.com/BackendStack21/odek/internal/danger" |
13 | 15 | ) |
@@ -316,3 +318,160 @@ func TestJsonQuery_WrapsStringValue(t *testing.T) { |
316 | 318 | t.Fatalf("json_query string value should be wrapped in untrusted_content, got: %q", r.Value) |
317 | 319 | } |
318 | 320 | } |
| 321 | + |
| 322 | + |
| 323 | +// ── 6. Shell / parallel_shell must cap command output ──────────────────── |
| 324 | + |
| 325 | +func TestShell_CapsOutputSize(t *testing.T) { |
| 326 | + dir := t.TempDir() |
| 327 | + path := filepath.Join(dir, "huge.txt") |
| 328 | + os.WriteFile(path, []byte(strings.Repeat("x", 15*1024*1024)), 0644) |
| 329 | + |
| 330 | + tool := &shellTool{} |
| 331 | + tool.SetContext(context.Background()) |
| 332 | + result, err := tool.Call(fmt.Sprintf(`{"command":"cat %s","description":"read huge file"}`, path)) |
| 333 | + if err != nil { |
| 334 | + t.Fatalf("Call() error: %v", err) |
| 335 | + } |
| 336 | + |
| 337 | + body := unwrapUntrusted(result) |
| 338 | + if len(body) > 1024*1024+200 { |
| 339 | + t.Fatalf("shell returned %d bytes, expected cap near 1 MiB", len(body)) |
| 340 | + } |
| 341 | +} |
| 342 | + |
| 343 | +func TestParallelShell_CapsOutputSize(t *testing.T) { |
| 344 | + dir := t.TempDir() |
| 345 | + path := filepath.Join(dir, "huge.txt") |
| 346 | + os.WriteFile(path, []byte(strings.Repeat("x", 15*1024*1024)), 0644) |
| 347 | + |
| 348 | + tool := ¶llelShellTool{} |
| 349 | + result := callJSON(t, tool, fmt.Sprintf(`{"commands":[{"command":"cat %s"}]}`, path)) |
| 350 | + var r struct { |
| 351 | + Results []struct { |
| 352 | + Stdout string `json:"stdout"` |
| 353 | + Stderr string `json:"stderr"` |
| 354 | + Error string `json:"error,omitempty"` |
| 355 | + } `json:"results"` |
| 356 | + } |
| 357 | + mustUnmarshal(t, result, &r) |
| 358 | + if len(r.Results) != 1 { |
| 359 | + t.Fatalf("expected 1 result, got %d", len(r.Results)) |
| 360 | + } |
| 361 | + out := r.Results[0].Stdout + r.Results[0].Stderr |
| 362 | + if len(out) > 1024*1024+200 { |
| 363 | + t.Fatalf("parallel_shell returned %d bytes, expected cap near 1 MiB", len(out)) |
| 364 | + } |
| 365 | +} |
| 366 | + |
| 367 | +// ── 7. Browser must enforce an HTTP request timeout ────────────────────── |
| 368 | + |
| 369 | +func TestBrowser_NavigateTimeout(t *testing.T) { |
| 370 | + orig := browserRequestTimeout |
| 371 | + browserRequestTimeout = 100 * time.Millisecond |
| 372 | + defer func() { browserRequestTimeout = orig }() |
| 373 | + |
| 374 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 375 | + time.Sleep(300 * time.Millisecond) |
| 376 | + fmt.Fprint(w, "<html><body>page</body></html>") |
| 377 | + })) |
| 378 | + defer srv.Close() |
| 379 | + |
| 380 | + tool := newBrowserTool(danger.DangerousConfig{}) |
| 381 | + result := callJSON(t, tool, fmt.Sprintf(`{"action":"navigate","url":%q}`, srv.URL)) |
| 382 | + var r struct { |
| 383 | + Error string `json:"error,omitempty"` |
| 384 | + } |
| 385 | + mustUnmarshal(t, result, &r) |
| 386 | + if r.Error == "" || !strings.Contains(strings.ToLower(r.Error), "timeout") { |
| 387 | + t.Fatalf("browser should time out on a slow server, got: %q", r.Error) |
| 388 | + } |
| 389 | +} |
| 390 | + |
| 391 | +// ── 8. batch_patch must reject huge files and wrap diff output ─────────── |
| 392 | + |
| 393 | +func TestBatchPatch_RejectsHugeFile(t *testing.T) { |
| 394 | + dir := t.TempDir() |
| 395 | + path := filepath.Join(dir, "huge.txt") |
| 396 | + os.WriteFile(path, []byte(strings.Repeat("x", 15*1024*1024)), 0644) |
| 397 | + |
| 398 | + tool := &batchPatchTool{} |
| 399 | + result := callJSON(t, tool, fmt.Sprintf(`{"patches":[{"path":%q,"old_string":"xxx","new_string":"yyy"}]}`, path)) |
| 400 | + var r struct { |
| 401 | + Results []struct { |
| 402 | + Success bool `json:"success"` |
| 403 | + Error string `json:"error,omitempty"` |
| 404 | + } `json:"results"` |
| 405 | + } |
| 406 | + mustUnmarshal(t, result, &r) |
| 407 | + if len(r.Results) != 1 { |
| 408 | + t.Fatalf("expected 1 result, got %d", len(r.Results)) |
| 409 | + } |
| 410 | + if r.Results[0].Success { |
| 411 | + t.Fatal("batch_patch should reject a 15 MiB file") |
| 412 | + } |
| 413 | + if r.Results[0].Error == "" { |
| 414 | + t.Fatal("batch_patch should return an error for a 15 MiB file") |
| 415 | + } |
| 416 | +} |
| 417 | + |
| 418 | +func TestBatchPatch_WrapsDiff(t *testing.T) { |
| 419 | + dir := t.TempDir() |
| 420 | + path := filepath.Join(dir, "test.txt") |
| 421 | + os.WriteFile(path, []byte("hello world\n"), 0644) |
| 422 | + |
| 423 | + tool := &batchPatchTool{} |
| 424 | + result := callJSON(t, tool, fmt.Sprintf(`{"patches":[{"path":%q,"old_string":"hello","new_string":"goodbye"}]}`, path)) |
| 425 | + var r struct { |
| 426 | + Results []struct { |
| 427 | + Diff string `json:"diff"` |
| 428 | + } `json:"results"` |
| 429 | + } |
| 430 | + mustUnmarshal(t, result, &r) |
| 431 | + if len(r.Results) == 0 || !strings.HasPrefix(r.Results[0].Diff, "<untrusted_content_") { |
| 432 | + t.Fatalf("batch_patch diff should be wrapped in untrusted_content, got: %q", r.Results[0].Diff) |
| 433 | + } |
| 434 | +} |
| 435 | + |
| 436 | +// ── 9. Transcribe must reject huge / symlinked audio inputs ────────────── |
| 437 | + |
| 438 | +func TestTranscribe_RejectsHugeFile(t *testing.T) { |
| 439 | + dir := t.TempDir() |
| 440 | + path := filepath.Join(dir, "huge.ogg") |
| 441 | + os.WriteFile(path, make([]byte, 15*1024*1024), 0644) |
| 442 | + |
| 443 | + tool := &transcribeTool{} |
| 444 | + result := callJSON(t, tool, fmt.Sprintf(`{"path":%q}`, path)) |
| 445 | + var r struct { |
| 446 | + Error string `json:"error,omitempty"` |
| 447 | + } |
| 448 | + mustUnmarshal(t, result, &r) |
| 449 | + if !strings.Contains(r.Error, "too large") { |
| 450 | + t.Fatalf("transcribe should reject a 15 MiB file with a size error, got: %q", r.Error) |
| 451 | + } |
| 452 | +} |
| 453 | + |
| 454 | +// ── 10. Tree must cap directory width ──────────────────────────────────── |
| 455 | + |
| 456 | +func TestTree_CapsDirectoryWidth(t *testing.T) { |
| 457 | + dir := t.TempDir() |
| 458 | + for i := 0; i < 1500; i++ { |
| 459 | + os.WriteFile(filepath.Join(dir, fmt.Sprintf("file%d.txt", i)), []byte("x"), 0644) |
| 460 | + } |
| 461 | + |
| 462 | + tool := &treeTool{dangerousConfig: danger.DangerousConfig{}} |
| 463 | + result := callJSON(t, tool, fmt.Sprintf(`{"path":%q}`, dir)) |
| 464 | + var r struct { |
| 465 | + Tree struct { |
| 466 | + Children []any `json:"children"` |
| 467 | + } `json:"tree"` |
| 468 | + Error string `json:"error,omitempty"` |
| 469 | + } |
| 470 | + mustUnmarshal(t, result, &r) |
| 471 | + if r.Error != "" { |
| 472 | + t.Fatalf("tree returned error: %s", r.Error) |
| 473 | + } |
| 474 | + if len(r.Tree.Children) > 1000 { |
| 475 | + t.Fatalf("tree did not cap directory width: got %d children", len(r.Tree.Children)) |
| 476 | + } |
| 477 | +} |
0 commit comments