|
| 1 | +package file |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestReadUnscopedReadsPath(t *testing.T) { |
| 13 | + path := filepath.Join(t.TempDir(), "note.txt") |
| 14 | + if err := os.WriteFile(path, []byte("hello"), 0o600); err != nil { |
| 15 | + t.Fatal(err) |
| 16 | + } |
| 17 | + |
| 18 | + got, err := NewRead().Run(context.Background(), readJSON(path)) |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + if got != "hello" { |
| 23 | + t.Fatalf("Run() = %q, want hello", got) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestReadScopedReadsRelativePathFromWorkspace(t *testing.T) { |
| 28 | + workspace := t.TempDir() |
| 29 | + if err := os.WriteFile(filepath.Join(workspace, "note.txt"), []byte("hello"), 0o600); err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + got, err := NewRead(workspace).Run(context.Background(), readJSON("note.txt")) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + if got != "hello" { |
| 38 | + t.Fatalf("Run() = %q, want hello", got) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestReadScopedRejectsParentEscape(t *testing.T) { |
| 43 | + base := t.TempDir() |
| 44 | + workspace := filepath.Join(base, "workspace") |
| 45 | + if err := os.Mkdir(workspace, 0o700); err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + if err := os.WriteFile(filepath.Join(base, "secret.txt"), []byte("secret"), 0o600); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + _, err := NewRead(workspace).Run(context.Background(), readJSON("../secret.txt")) |
| 53 | + if err == nil { |
| 54 | + t.Fatal("expected workspace escape error") |
| 55 | + } |
| 56 | + if !strings.Contains(err.Error(), "outside workspace") { |
| 57 | + t.Fatalf("error = %q, want outside workspace", err) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestReadScopedRejectsAbsolutePathOutsideWorkspace(t *testing.T) { |
| 62 | + workspace := t.TempDir() |
| 63 | + outside := filepath.Join(t.TempDir(), "secret.txt") |
| 64 | + if err := os.WriteFile(outside, []byte("secret"), 0o600); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + |
| 68 | + _, err := NewRead(workspace).Run(context.Background(), readJSON(outside)) |
| 69 | + if err == nil { |
| 70 | + t.Fatal("expected workspace escape error") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestReadScopedRejectsSymlinkEscape(t *testing.T) { |
| 75 | + base := t.TempDir() |
| 76 | + workspace := filepath.Join(base, "workspace") |
| 77 | + if err := os.Mkdir(workspace, 0o700); err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + secret := filepath.Join(base, "secret.txt") |
| 81 | + if err := os.WriteFile(secret, []byte("secret"), 0o600); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + if err := os.Symlink(secret, filepath.Join(workspace, "secret-link")); err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + |
| 88 | + _, err := NewRead(workspace).Run(context.Background(), readJSON("secret-link")) |
| 89 | + if err == nil { |
| 90 | + t.Fatal("expected workspace escape error") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func readJSON(path string) json.RawMessage { |
| 95 | + data, _ := json.Marshal(readInput{Path: path}) |
| 96 | + return data |
| 97 | +} |
0 commit comments