|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAtomicBatchLifecycleOutsideRootRefused(t *testing.T) { |
| 13 | + repoRoot := t.TempDir() |
| 14 | + outsideRoot := t.TempDir() |
| 15 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 16 | + s := newReadGuardServer(t, repoRoot) |
| 17 | + source := writeAtomicBatchFixture(t, repoRoot, "source.txt", "source\n") |
| 18 | + destination := filepath.Join(outsideRoot, "destination.txt") |
| 19 | + |
| 20 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 21 | + atomicFileMove(source, destination, ""), |
| 22 | + }, "file-lifecycle-outside-root") |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" || !strings.Contains(receipt.Error, "outside") { |
| 27 | + t.Fatalf("receipt = %+v", receipt) |
| 28 | + } |
| 29 | + if got := readAtomicBatchFixture(t, source); got != "source\n" { |
| 30 | + t.Fatalf("source = %q", got) |
| 31 | + } |
| 32 | + if _, err := os.Stat(destination); !errors.Is(err, os.ErrNotExist) { |
| 33 | + t.Fatalf("destination was created: %v", err) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestAtomicBatchLifecycleInvalidDigestAndOverlapRefused(t *testing.T) { |
| 38 | + t.Run("invalid digest", func(t *testing.T) { |
| 39 | + s := newAtomicBatchTestServer(t, mutationTestWatcher{}) |
| 40 | + path := writeAtomicBatchFixture(t, t.TempDir(), "source.txt", "source\n") |
| 41 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 42 | + atomicFileDelete(path, "not-a-sha256"), |
| 43 | + }, "file-lifecycle-invalid-digest") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" { |
| 48 | + t.Fatalf("receipt = %+v", receipt) |
| 49 | + } |
| 50 | + if got := readAtomicBatchFixture(t, path); got != "source\n" { |
| 51 | + t.Fatalf("source = %q", got) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("overlapping path ownership", func(t *testing.T) { |
| 56 | + s := newAtomicBatchTestServer(t, mutationTestWatcher{}) |
| 57 | + dir := t.TempDir() |
| 58 | + path := writeAtomicBatchFixture(t, dir, "source.txt", "source\n") |
| 59 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 60 | + atomicFileEdit(path, "source", "edited"), |
| 61 | + atomicFileDelete(path, ""), |
| 62 | + }, "file-lifecycle-overlap") |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" || !strings.Contains(receipt.Error, "overlaps") { |
| 67 | + t.Fatalf("receipt = %+v", receipt) |
| 68 | + } |
| 69 | + if got := readAtomicBatchFixture(t, path); got != "source\n" { |
| 70 | + t.Fatalf("source = %q", got) |
| 71 | + } |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestPrepareBatchJournalRequiresExplicitExistenceState(t *testing.T) { |
| 76 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 77 | + s := newAtomicBatchTestServer(t, mutationTestWatcher{}) |
| 78 | + path := filepath.Join(t.TempDir(), "empty.txt") |
| 79 | + receipt := batchTransactionReceipt{TransactionID: "unset-existence-state"} |
| 80 | + err := s.prepareBatchJournal(&receipt, map[string]*batchFileBuffer{ |
| 81 | + path: {absPath: path, relPath: "empty.txt"}, |
| 82 | + }, []string{path}) |
| 83 | + if err == nil || !strings.Contains(err.Error(), "existence state is unset") { |
| 84 | + t.Fatalf("prepareBatchJournal error = %v", err) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestAtomicBatchLifecycleDestinationGuards(t *testing.T) { |
| 89 | + t.Run("destination symlink", func(t *testing.T) { |
| 90 | + if os.PathSeparator == '\\' { |
| 91 | + t.Skip("symlink creation is not reliably available on Windows CI") |
| 92 | + } |
| 93 | + repoRoot := t.TempDir() |
| 94 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 95 | + s := newReadGuardServer(t, repoRoot) |
| 96 | + source := writeAtomicBatchFixture(t, repoRoot, "source.txt", "source\n") |
| 97 | + target := writeAtomicBatchFixture(t, repoRoot, "target.txt", "target\n") |
| 98 | + destination := filepath.Join(repoRoot, "destination.txt") |
| 99 | + if err := os.Symlink(target, destination); err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + |
| 103 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 104 | + atomicFileMove(source, destination, ""), |
| 105 | + }, "file-lifecycle-destination-symlink") |
| 106 | + if err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" || !strings.Contains(receipt.Error, "symlink") { |
| 110 | + t.Fatalf("receipt = %+v", receipt) |
| 111 | + } |
| 112 | + if got := readAtomicBatchFixture(t, source); got != "source\n" { |
| 113 | + t.Fatalf("source = %q", got) |
| 114 | + } |
| 115 | + if got := readAtomicBatchFixture(t, target); got != "target\n" { |
| 116 | + t.Fatalf("target = %q", got) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("symlinked destination parent", func(t *testing.T) { |
| 121 | + if os.PathSeparator == '\\' { |
| 122 | + t.Skip("symlink creation is not reliably available on Windows CI") |
| 123 | + } |
| 124 | + repoRoot := t.TempDir() |
| 125 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 126 | + s := newReadGuardServer(t, repoRoot) |
| 127 | + source := writeAtomicBatchFixture(t, repoRoot, "source.txt", "source\n") |
| 128 | + realParent := filepath.Join(repoRoot, "real-parent") |
| 129 | + if err := os.Mkdir(realParent, 0o755); err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + linkedParent := filepath.Join(repoRoot, "linked-parent") |
| 133 | + if err := os.Symlink(realParent, linkedParent); err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + destination := filepath.Join(linkedParent, "destination.txt") |
| 137 | + |
| 138 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 139 | + atomicFileMove(source, destination, ""), |
| 140 | + }, "file-lifecycle-symlinked-parent") |
| 141 | + if err != nil { |
| 142 | + t.Fatal(err) |
| 143 | + } |
| 144 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" || !strings.Contains(receipt.Error, "symlink") { |
| 145 | + t.Fatalf("receipt = %+v", receipt) |
| 146 | + } |
| 147 | + if got := readAtomicBatchFixture(t, source); got != "source\n" { |
| 148 | + t.Fatalf("source = %q", got) |
| 149 | + } |
| 150 | + if _, err := os.Stat(filepath.Join(realParent, "destination.txt")); !errors.Is(err, os.ErrNotExist) { |
| 151 | + t.Fatalf("destination was created: %v", err) |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + t.Run("dot-dot traversal destination", func(t *testing.T) { |
| 156 | + repoRoot := t.TempDir() |
| 157 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 158 | + s := newReadGuardServer(t, repoRoot) |
| 159 | + source := writeAtomicBatchFixture(t, repoRoot, "source.txt", "source\n") |
| 160 | + outsideName := "traversal-destination-" + filepath.Base(repoRoot) + ".txt" |
| 161 | + destination := repoRoot + string(os.PathSeparator) + ".." + string(os.PathSeparator) + outsideName |
| 162 | + |
| 163 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 164 | + atomicFileMove(source, destination, ""), |
| 165 | + }, "file-lifecycle-dot-dot-destination") |
| 166 | + if err != nil { |
| 167 | + t.Fatal(err) |
| 168 | + } |
| 169 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" || !strings.Contains(receipt.Error, "outside") { |
| 170 | + t.Fatalf("receipt = %+v", receipt) |
| 171 | + } |
| 172 | + if got := readAtomicBatchFixture(t, source); got != "source\n" { |
| 173 | + t.Fatalf("source = %q", got) |
| 174 | + } |
| 175 | + if _, err := os.Stat(filepath.Clean(destination)); !errors.Is(err, os.ErrNotExist) { |
| 176 | + t.Fatalf("destination was created: %v", err) |
| 177 | + } |
| 178 | + }) |
| 179 | + |
| 180 | + t.Run("delete directory", func(t *testing.T) { |
| 181 | + repoRoot := t.TempDir() |
| 182 | + t.Setenv(batchTransactionDirEnv, filepath.Join(t.TempDir(), "transactions")) |
| 183 | + s := newReadGuardServer(t, repoRoot) |
| 184 | + directory := filepath.Join(repoRoot, "directory") |
| 185 | + if err := os.Mkdir(directory, 0o755); err != nil { |
| 186 | + t.Fatal(err) |
| 187 | + } |
| 188 | + |
| 189 | + receipt, err := s.runBatchTransaction(context.Background(), []batchEditItem{ |
| 190 | + atomicFileDelete(directory, ""), |
| 191 | + }, "file-lifecycle-delete-directory") |
| 192 | + if err != nil { |
| 193 | + t.Fatal(err) |
| 194 | + } |
| 195 | + if receipt.Status != "aborted" || receipt.DiskStatus != "unchanged" { |
| 196 | + t.Fatalf("receipt = %+v", receipt) |
| 197 | + } |
| 198 | + info, err := os.Stat(directory) |
| 199 | + if err != nil || !info.IsDir() { |
| 200 | + t.Fatalf("directory was changed: info=%v err=%v", info, err) |
| 201 | + } |
| 202 | + }) |
| 203 | +} |
0 commit comments