@axiom-lattice/microsandbox-service 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +168 -0
- package/dist/{chunk-CFXAJNQX.mjs → chunk-B4GFAS4B.mjs} +2 -2
- package/dist/{chunk-4T3IR5DG.mjs → chunk-LJIZCKJ4.mjs} +164 -100
- package/dist/chunk-LJIZCKJ4.mjs.map +1 -0
- package/dist/cli.mjs +2 -2
- package/dist/index.mjs +1 -1
- package/dist/server.mjs +2 -2
- package/package.json +1 -1
- package/src/__tests__/volume-fs.test.ts +496 -0
- package/src/controllers/volume-fs.ts +13 -31
- package/src/lib/volume-path.ts +107 -0
- package/src/schemas/volume-fs.ts +11 -1
- package/dist/chunk-4T3IR5DG.mjs.map +0 -1
- /package/dist/{chunk-CFXAJNQX.mjs.map → chunk-B4GFAS4B.mjs.map} +0 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { FastifyInstance } from "fastify";
|
|
5
|
+
import { Volume } from "microsandbox";
|
|
6
|
+
import { buildApp } from "../app";
|
|
7
|
+
import { assertVolumeName } from "../lib/volume-path";
|
|
8
|
+
import type { RuntimeService } from "../types/runtime-service";
|
|
9
|
+
|
|
10
|
+
jest.mock("microsandbox", () => ({
|
|
11
|
+
Sandbox: {},
|
|
12
|
+
Volume: {
|
|
13
|
+
get: jest.fn(),
|
|
14
|
+
builder: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
describe("microsandbox volume fs routes", () => {
|
|
19
|
+
let volumeRoot: string;
|
|
20
|
+
let outsideRoot: string;
|
|
21
|
+
let app: FastifyInstance;
|
|
22
|
+
|
|
23
|
+
beforeEach(async () => {
|
|
24
|
+
volumeRoot = await fs.mkdtemp(path.join(os.tmpdir(), "microsandbox-volume-fs-"));
|
|
25
|
+
outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), "microsandbox-volume-outside-"));
|
|
26
|
+
(Volume.get as jest.Mock).mockResolvedValue({ name: "test-volume" });
|
|
27
|
+
(Volume.builder as jest.Mock).mockReturnValue({
|
|
28
|
+
create: jest.fn().mockResolvedValue({ path: volumeRoot }),
|
|
29
|
+
});
|
|
30
|
+
app = await buildApp({ runtimeService: {} as RuntimeService });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
await app.close();
|
|
35
|
+
jest.clearAllMocks();
|
|
36
|
+
jest.restoreAllMocks();
|
|
37
|
+
await fs.rm(volumeRoot, { recursive: true, force: true });
|
|
38
|
+
await fs.rm(outsideRoot, { recursive: true, force: true });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function post(operation: string, payload?: unknown, volumeName = "test-volume") {
|
|
42
|
+
return app.inject({
|
|
43
|
+
method: "POST",
|
|
44
|
+
url: `/api/volumes/${volumeName}/fs/${operation}`,
|
|
45
|
+
payload: payload as Record<string, unknown> | undefined,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function download(pathValue?: string, volumeName = "test-volume") {
|
|
50
|
+
const query = pathValue === undefined ? "" : `?path=${encodeURIComponent(pathValue)}`;
|
|
51
|
+
return app.inject({
|
|
52
|
+
method: "GET",
|
|
53
|
+
url: `/api/volumes/${volumeName}/fs/download${query}`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function expectError(
|
|
58
|
+
response: Awaited<ReturnType<typeof app.inject>>,
|
|
59
|
+
statusCode: number,
|
|
60
|
+
code: string
|
|
61
|
+
): void {
|
|
62
|
+
expect(response.statusCode).toBe(statusCode);
|
|
63
|
+
expect(response.json()).toMatchObject({ success: false, error: { code } });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
describe("read", () => {
|
|
67
|
+
it("reads an existing file and echoes the guest path", async () => {
|
|
68
|
+
await fs.writeFile(path.join(volumeRoot, "hello.txt"), "hello", "utf-8");
|
|
69
|
+
|
|
70
|
+
const response = await post("read", { path: "hello.txt" });
|
|
71
|
+
|
|
72
|
+
expect(response.statusCode).toBe(200);
|
|
73
|
+
expect(response.json()).toEqual({
|
|
74
|
+
success: true,
|
|
75
|
+
data: { path: "hello.txt", content: "hello" },
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("returns a read error for a missing file", async () => {
|
|
80
|
+
const response = await post("read", { path: "missing.txt" });
|
|
81
|
+
expectError(response, 404, "VOLUME_READ_ERROR");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("returns a validation error when path is missing", async () => {
|
|
85
|
+
const response = await post("read", {});
|
|
86
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("normalizes ~ and ~/ prefixes to the volume root", async () => {
|
|
90
|
+
await fs.writeFile(path.join(volumeRoot, "tilde.txt"), "tilde", "utf-8");
|
|
91
|
+
|
|
92
|
+
for (const guestPath of ["~/tilde.txt"]) {
|
|
93
|
+
const response = await post("read", { path: guestPath });
|
|
94
|
+
expect(response.statusCode).toBe(200);
|
|
95
|
+
expect((response.json() as { data: { content: string } }).data.content).toBe("tilde");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const root = await post("read", { path: "~" });
|
|
99
|
+
expectError(root, 404, "VOLUME_READ_ERROR");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("write", () => {
|
|
104
|
+
it("creates nested directories and writes content", async () => {
|
|
105
|
+
const response = await post("write", {
|
|
106
|
+
path: "nested/deep/file.txt",
|
|
107
|
+
content: "written",
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(response.statusCode).toBe(200);
|
|
111
|
+
expect(response.json()).toEqual({
|
|
112
|
+
success: true,
|
|
113
|
+
data: { path: "nested/deep/file.txt" },
|
|
114
|
+
});
|
|
115
|
+
await expect(
|
|
116
|
+
fs.readFile(path.join(volumeRoot, "nested/deep/file.txt"), "utf-8")
|
|
117
|
+
).resolves.toBe("written");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("overwrites an existing file", async () => {
|
|
121
|
+
const target = path.join(volumeRoot, "overwrite.txt");
|
|
122
|
+
await fs.writeFile(target, "old", "utf-8");
|
|
123
|
+
|
|
124
|
+
const response = await post("write", { path: "overwrite.txt", content: "new" });
|
|
125
|
+
|
|
126
|
+
expect(response.statusCode).toBe(200);
|
|
127
|
+
await expect(fs.readFile(target, "utf-8")).resolves.toBe("new");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it.each([
|
|
131
|
+
["missing content", { path: "file.txt" }],
|
|
132
|
+
["non-string path", { path: 1, content: "x" }],
|
|
133
|
+
["missing body", undefined],
|
|
134
|
+
])("returns a validation error for %s", async (_label, payload) => {
|
|
135
|
+
const response = await post("write", payload);
|
|
136
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("delete", () => {
|
|
141
|
+
it("deletes one existing regular file", async () => {
|
|
142
|
+
await fs.writeFile(path.join(volumeRoot, "delete.txt"), "content");
|
|
143
|
+
|
|
144
|
+
const response = await post("delete", { path: "delete.txt" });
|
|
145
|
+
|
|
146
|
+
expect(response.statusCode).toBe(200);
|
|
147
|
+
expect(response.json()).toEqual({ success: true, data: { path: "delete.txt" } });
|
|
148
|
+
await expect(fs.access(path.join(volumeRoot, "delete.txt"))).rejects.toThrow();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("rejects a file reached through an outside symlinked parent", async () => {
|
|
152
|
+
await fs.writeFile(path.join(outsideRoot, "outside.txt"), "outside");
|
|
153
|
+
await fs.symlink(outsideRoot, path.join(volumeRoot, "outside-link"), "dir");
|
|
154
|
+
|
|
155
|
+
const response = await post("delete", { path: "outside-link/outside.txt" });
|
|
156
|
+
|
|
157
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
158
|
+
await expect(fs.readFile(path.join(outsideRoot, "outside.txt"), "utf-8")).resolves.toBe(
|
|
159
|
+
"outside"
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("list", () => {
|
|
165
|
+
it("lists file, directory and symlink entries with metadata", async () => {
|
|
166
|
+
await fs.writeFile(path.join(volumeRoot, "file.txt"), "hello", "utf-8");
|
|
167
|
+
await fs.mkdir(path.join(volumeRoot, "sub"));
|
|
168
|
+
await fs.symlink("file.txt", path.join(volumeRoot, "link.txt"));
|
|
169
|
+
|
|
170
|
+
const response = await post("list", { path: "" });
|
|
171
|
+
|
|
172
|
+
expect(response.statusCode).toBe(200);
|
|
173
|
+
const body = response.json() as {
|
|
174
|
+
success: true;
|
|
175
|
+
data: {
|
|
176
|
+
entries: Array<{
|
|
177
|
+
path: string;
|
|
178
|
+
kind: string;
|
|
179
|
+
size: number;
|
|
180
|
+
mode: number;
|
|
181
|
+
modified: string | null;
|
|
182
|
+
created: string | null;
|
|
183
|
+
}>;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
const entries = new Map(body.data.entries.map((entry) => [entry.path, entry]));
|
|
187
|
+
|
|
188
|
+
expect(entries.get("file.txt")).toMatchObject({
|
|
189
|
+
kind: "file",
|
|
190
|
+
size: 5,
|
|
191
|
+
mode: 0,
|
|
192
|
+
});
|
|
193
|
+
expect(entries.get("sub")).toMatchObject({ kind: "directory" });
|
|
194
|
+
expect(entries.get("link.txt")).toMatchObject({ kind: "symlink" });
|
|
195
|
+
|
|
196
|
+
const file = entries.get("file.txt");
|
|
197
|
+
expect(file?.modified).toEqual(expect.any(String));
|
|
198
|
+
expect(Number.isNaN(Date.parse(file?.modified ?? ""))).toBe(false);
|
|
199
|
+
if (file?.created !== null) {
|
|
200
|
+
expect(Number.isNaN(Date.parse(file?.created ?? ""))).toBe(false);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("treats empty, absolute and tilde guest paths as the volume root", async () => {
|
|
205
|
+
await fs.writeFile(path.join(volumeRoot, "root.txt"), "root", "utf-8");
|
|
206
|
+
|
|
207
|
+
for (const guestPath of ["", "/", "~", "~/"]) {
|
|
208
|
+
const response = await post("list", { path: guestPath });
|
|
209
|
+
expect(response.statusCode).toBe(200);
|
|
210
|
+
const body = response.json() as { data: { entries: Array<{ path: string }> } };
|
|
211
|
+
expect(body.data.entries.map((entry) => path.posix.basename(entry.path))).toContain(
|
|
212
|
+
"root.txt"
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("returns a list error for a missing path", async () => {
|
|
218
|
+
const response = await post("list", { path: "missing" });
|
|
219
|
+
expectError(response, 404, "VOLUME_LIST_ERROR");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("returns a validation error when path is missing", async () => {
|
|
223
|
+
const response = await post("list", {});
|
|
224
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("download", () => {
|
|
229
|
+
it("round-trips binary content as base64", async () => {
|
|
230
|
+
const bytes = Buffer.from([0, 1, 2, 3, 250, 251, 255]);
|
|
231
|
+
await fs.writeFile(path.join(volumeRoot, "binary.bin"), bytes);
|
|
232
|
+
|
|
233
|
+
const response = await download("binary.bin");
|
|
234
|
+
|
|
235
|
+
expect(response.statusCode).toBe(200);
|
|
236
|
+
const body = response.json() as {
|
|
237
|
+
data: { path: string; contentBase64: string };
|
|
238
|
+
};
|
|
239
|
+
expect(body.data.path).toBe("binary.bin");
|
|
240
|
+
expect(body.data.contentBase64).toBe(bytes.toString("base64"));
|
|
241
|
+
expect(Buffer.from(body.data.contentBase64, "base64").equals(bytes)).toBe(true);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("returns a download error for a missing file", async () => {
|
|
245
|
+
const response = await download("missing.bin");
|
|
246
|
+
expectError(response, 404, "VOLUME_DOWNLOAD_ERROR");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("returns a validation error when the query path is missing", async () => {
|
|
250
|
+
const response = await download();
|
|
251
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe("upload", () => {
|
|
256
|
+
it("writes base64 content into nested directories", async () => {
|
|
257
|
+
const bytes = Buffer.from([9, 8, 7, 0, 255]);
|
|
258
|
+
const response = await post("upload", {
|
|
259
|
+
path: "upload/nested/blob.bin",
|
|
260
|
+
contentBase64: bytes.toString("base64"),
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
expect(response.statusCode).toBe(200);
|
|
264
|
+
expect(response.json()).toEqual({
|
|
265
|
+
success: true,
|
|
266
|
+
data: { path: "upload/nested/blob.bin" },
|
|
267
|
+
});
|
|
268
|
+
const written = await fs.readFile(path.join(volumeRoot, "upload/nested/blob.bin"));
|
|
269
|
+
expect(written.equals(bytes)).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it.each([
|
|
273
|
+
["invalid base64", { path: "bad.bin", contentBase64: "***not-base64***" }],
|
|
274
|
+
["empty base64", { path: "empty.bin", contentBase64: "" }],
|
|
275
|
+
["missing body", undefined],
|
|
276
|
+
["missing path", { contentBase64: "aGVsbG8=" }],
|
|
277
|
+
])("returns a validation error for %s", async (_label, payload) => {
|
|
278
|
+
const response = await post("upload", payload);
|
|
279
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
describe("mkdir", () => {
|
|
284
|
+
it("creates a directory", async () => {
|
|
285
|
+
const response = await post("mkdir", { path: "created" });
|
|
286
|
+
|
|
287
|
+
expect(response.statusCode).toBe(200);
|
|
288
|
+
expect(response.json()).toEqual({ success: true, data: { path: "created" } });
|
|
289
|
+
expect((await fs.stat(path.join(volumeRoot, "created"))).isDirectory()).toBe(true);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("creates nested directories", async () => {
|
|
293
|
+
const response = await post("mkdir", { path: "nested/deep/dir" });
|
|
294
|
+
|
|
295
|
+
expect(response.statusCode).toBe(200);
|
|
296
|
+
expect((await fs.stat(path.join(volumeRoot, "nested/deep/dir"))).isDirectory()).toBe(true);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("is idempotent for an existing directory", async () => {
|
|
300
|
+
await fs.mkdir(path.join(volumeRoot, "existing"));
|
|
301
|
+
|
|
302
|
+
const response = await post("mkdir", { path: "existing" });
|
|
303
|
+
|
|
304
|
+
expect(response.statusCode).toBe(200);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it.each([
|
|
308
|
+
["missing path", {}],
|
|
309
|
+
["non-string path", { path: 1 }],
|
|
310
|
+
["missing body", undefined],
|
|
311
|
+
])("returns a validation error for %s", async (_label, payload) => {
|
|
312
|
+
const response = await post("mkdir", payload);
|
|
313
|
+
expectError(response, 400, "VALIDATION_ERROR");
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe("volume names", () => {
|
|
318
|
+
it.each(["test-volume", "volume_1", "a.b-c9"])(
|
|
319
|
+
"accepts valid volume name %p",
|
|
320
|
+
async (volumeName) => {
|
|
321
|
+
await fs.writeFile(path.join(volumeRoot, "any.txt"), "any", "utf-8");
|
|
322
|
+
|
|
323
|
+
const response = await post("read", { path: "any.txt" }, volumeName);
|
|
324
|
+
|
|
325
|
+
expect(response.statusCode).toBe(200);
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
it.each(["a%2Fb", "a%5Cb", "%2Ehidden", "-leading-dash", "bad%24name"])(
|
|
330
|
+
"rejects invalid volume name %s before host path resolution",
|
|
331
|
+
async (volumeName) => {
|
|
332
|
+
const response = await post("read", { path: "any.txt" }, volumeName);
|
|
333
|
+
|
|
334
|
+
expectError(response, 400, "INVALID_VOLUME_NAME");
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
it.each([".", "..", "a/b", "a\\b", "", "bad$name", "-leading"])(
|
|
339
|
+
"rejects invalid volume name %p",
|
|
340
|
+
(volumeName) => {
|
|
341
|
+
expect(() => assertVolumeName(volumeName)).toThrow("Invalid volume name");
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
it.each(["test-volume", "volume_1", "a.b-c9"])(
|
|
346
|
+
"accepts valid volume name %p",
|
|
347
|
+
(volumeName) => {
|
|
348
|
+
expect(() => assertVolumeName(volumeName)).not.toThrow();
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
describe("lexical traversal", () => {
|
|
354
|
+
it.each([
|
|
355
|
+
"../outside.txt",
|
|
356
|
+
"nested/../../outside.txt",
|
|
357
|
+
"a/../b",
|
|
358
|
+
"..\\outside.txt",
|
|
359
|
+
"nested\\file.txt",
|
|
360
|
+
])("rejects guest path %s", async (guestPath) => {
|
|
361
|
+
await fs.writeFile(path.join(volumeRoot, "outside.txt"), "inside", "utf-8");
|
|
362
|
+
|
|
363
|
+
const response = await post("read", { path: guestPath });
|
|
364
|
+
|
|
365
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("rejects traversal on write before touching the filesystem", async () => {
|
|
369
|
+
const escaped = path.join(path.dirname(volumeRoot), "escaped.txt");
|
|
370
|
+
|
|
371
|
+
const response = await post("write", { path: "../escaped.txt", content: "escaped" });
|
|
372
|
+
|
|
373
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
374
|
+
await expect(fs.access(escaped)).rejects.toThrow();
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("rejects traversal on list, download and mkdir", async () => {
|
|
378
|
+
expectError(await post("list", { path: "../" }), 403, "PATH_TRAVERSAL");
|
|
379
|
+
expectError(await download("../outside.bin"), 403, "PATH_TRAVERSAL");
|
|
380
|
+
expectError(await post("mkdir", { path: "../created" }), 403, "PATH_TRAVERSAL");
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("rejects NUL bytes in guest paths", async () => {
|
|
384
|
+
const response = await post("read", { path: "hello\0.txt" });
|
|
385
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe("symlink confinement", () => {
|
|
390
|
+
it("rejects reading through an outside symlinked parent directory", async () => {
|
|
391
|
+
await fs.mkdir(path.join(outsideRoot, "dir"));
|
|
392
|
+
await fs.writeFile(path.join(outsideRoot, "dir/secret.txt"), "secret", "utf-8");
|
|
393
|
+
await fs.symlink(outsideRoot, path.join(volumeRoot, "escape"));
|
|
394
|
+
|
|
395
|
+
const response = await post("read", { path: "escape/dir/secret.txt" });
|
|
396
|
+
|
|
397
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("rejects downloading through an outside symlinked parent directory", async () => {
|
|
401
|
+
await fs.writeFile(path.join(outsideRoot, "secret.bin"), "secret", "utf-8");
|
|
402
|
+
await fs.symlink(outsideRoot, path.join(volumeRoot, "escape"));
|
|
403
|
+
|
|
404
|
+
const response = await download("escape/secret.bin");
|
|
405
|
+
|
|
406
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("rejects reading and overwriting a symlink that points outside the root", async () => {
|
|
410
|
+
const outsideFile = path.join(outsideRoot, "target.txt");
|
|
411
|
+
await fs.writeFile(outsideFile, "outside", "utf-8");
|
|
412
|
+
await fs.symlink(outsideFile, path.join(volumeRoot, "link.txt"));
|
|
413
|
+
|
|
414
|
+
const read = await post("read", { path: "link.txt" });
|
|
415
|
+
expectError(read, 403, "PATH_TRAVERSAL");
|
|
416
|
+
|
|
417
|
+
const write = await post("write", { path: "link.txt", content: "overwritten" });
|
|
418
|
+
expectError(write, 403, "PATH_TRAVERSAL");
|
|
419
|
+
|
|
420
|
+
const upload = await post("upload", {
|
|
421
|
+
path: "link.txt",
|
|
422
|
+
contentBase64: Buffer.from("uploaded").toString("base64"),
|
|
423
|
+
});
|
|
424
|
+
expectError(upload, 403, "PATH_TRAVERSAL");
|
|
425
|
+
|
|
426
|
+
await expect(fs.readFile(outsideFile, "utf-8")).resolves.toBe("outside");
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it("rejects listing an outside symlinked directory without exposing entries", async () => {
|
|
430
|
+
await fs.writeFile(path.join(outsideRoot, "secret.txt"), "secret", "utf-8");
|
|
431
|
+
await fs.symlink(outsideRoot, path.join(volumeRoot, "outside-dir"));
|
|
432
|
+
|
|
433
|
+
const response = await post("list", { path: "outside-dir" });
|
|
434
|
+
|
|
435
|
+
expectError(response, 403, "PATH_TRAVERSAL");
|
|
436
|
+
expect(response.body).not.toContain("secret.txt");
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("rejects write, upload and mkdir through an outside symlinked parent before creating anything", async () => {
|
|
440
|
+
await fs.symlink(outsideRoot, path.join(volumeRoot, "escape"));
|
|
441
|
+
|
|
442
|
+
const write = await post("write", {
|
|
443
|
+
path: "escape/created-dir/escaped.txt",
|
|
444
|
+
content: "escaped",
|
|
445
|
+
});
|
|
446
|
+
expectError(write, 403, "PATH_TRAVERSAL");
|
|
447
|
+
await expect(fs.access(path.join(outsideRoot, "created-dir"))).rejects.toThrow();
|
|
448
|
+
|
|
449
|
+
const upload = await post("upload", {
|
|
450
|
+
path: "escape/upload-dir/blob.bin",
|
|
451
|
+
contentBase64: Buffer.from("escaped").toString("base64"),
|
|
452
|
+
});
|
|
453
|
+
expectError(upload, 403, "PATH_TRAVERSAL");
|
|
454
|
+
await expect(fs.access(path.join(outsideRoot, "upload-dir"))).rejects.toThrow();
|
|
455
|
+
|
|
456
|
+
const mkdir = await post("mkdir", { path: "escape/mkdir-dir" });
|
|
457
|
+
expectError(mkdir, 403, "PATH_TRAVERSAL");
|
|
458
|
+
await expect(fs.access(path.join(outsideRoot, "mkdir-dir"))).rejects.toThrow();
|
|
459
|
+
|
|
460
|
+
await expect(fs.access(path.join(outsideRoot, "escaped.txt"))).rejects.toThrow();
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("keeps inside-root symlink reads and writes working", async () => {
|
|
464
|
+
await fs.writeFile(path.join(volumeRoot, "target.txt"), "original", "utf-8");
|
|
465
|
+
await fs.symlink("target.txt", path.join(volumeRoot, "inside-link.txt"));
|
|
466
|
+
|
|
467
|
+
const read = await post("read", { path: "inside-link.txt" });
|
|
468
|
+
expect(read.statusCode).toBe(200);
|
|
469
|
+
expect((read.json() as { data: { content: string } }).data.content).toBe("original");
|
|
470
|
+
|
|
471
|
+
const write = await post("write", {
|
|
472
|
+
path: "inside-link.txt",
|
|
473
|
+
content: "updated",
|
|
474
|
+
});
|
|
475
|
+
expect(write.statusCode).toBe(200);
|
|
476
|
+
await expect(fs.readFile(path.join(volumeRoot, "target.txt"), "utf-8")).resolves.toBe(
|
|
477
|
+
"updated"
|
|
478
|
+
);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("keeps inside-root symlinked directory listing and mkdir working", async () => {
|
|
482
|
+
await fs.mkdir(path.join(volumeRoot, "real-dir"));
|
|
483
|
+
await fs.writeFile(path.join(volumeRoot, "real-dir/child.txt"), "child", "utf-8");
|
|
484
|
+
await fs.symlink("real-dir", path.join(volumeRoot, "dir-link"));
|
|
485
|
+
|
|
486
|
+
const list = await post("list", { path: "dir-link" });
|
|
487
|
+
expect(list.statusCode).toBe(200);
|
|
488
|
+
const body = list.json() as { data: { entries: Array<{ path: string }> } };
|
|
489
|
+
expect(body.data.entries.map((entry) => entry.path)).toContain("dir-link/child.txt");
|
|
490
|
+
|
|
491
|
+
const mkdir = await post("mkdir", { path: "dir-link/new-dir" });
|
|
492
|
+
expect(mkdir.statusCode).toBe(200);
|
|
493
|
+
expect((await fs.stat(path.join(volumeRoot, "real-dir/new-dir"))).isDirectory()).toBe(true);
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
});
|
|
@@ -6,6 +6,12 @@ import { Volume } from "microsandbox";
|
|
|
6
6
|
import z from "zod";
|
|
7
7
|
import { HttpError } from "../lib/errors";
|
|
8
8
|
import { ok } from "../lib/http";
|
|
9
|
+
import {
|
|
10
|
+
assertVolumeName,
|
|
11
|
+
resolveContainedGuestPath,
|
|
12
|
+
resolveGuestPath,
|
|
13
|
+
resolveWritableGuestPath,
|
|
14
|
+
} from "../lib/volume-path";
|
|
9
15
|
import {
|
|
10
16
|
volumeFsDeleteSchema,
|
|
11
17
|
volumeFsReadSchema,
|
|
@@ -21,16 +27,9 @@ interface VolumeFsParams {
|
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
const MSB_DATA_DIR = path.join(os.homedir(), ".microsandbox");
|
|
24
|
-
const VOLUME_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
25
30
|
|
|
26
31
|
type VolumeFileStat = Awaited<ReturnType<typeof fs.lstat>>;
|
|
27
32
|
|
|
28
|
-
function assertVolumeName(name: string): void {
|
|
29
|
-
if (name === "." || name === ".." || !VOLUME_NAME_PATTERN.test(name)) {
|
|
30
|
-
throw new HttpError(400, "INVALID_VOLUME_NAME", "Invalid volume name");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
33
|
function assertRegularVolumeFile(guestPath: string, stat: VolumeFileStat): void {
|
|
35
34
|
if (stat.isSymbolicLink()) {
|
|
36
35
|
throw new HttpError(
|
|
@@ -72,23 +71,6 @@ async function resolveVolumeHostPath(name: string): Promise<string> {
|
|
|
72
71
|
}
|
|
73
72
|
}
|
|
74
73
|
|
|
75
|
-
function resolveGuestPath(hostRoot: string, guestPath: string): string {
|
|
76
|
-
const normalized = guestPath === "~" ? "" : guestPath.replace(/^~\//, "");
|
|
77
|
-
const resolved = path.join(hostRoot, path.normalize(normalized).replace(/^(\.\.(\/|\\|$))+/, ""));
|
|
78
|
-
if (!resolved.startsWith(hostRoot + path.sep) && resolved !== hostRoot) {
|
|
79
|
-
throw new HttpError(403, "PATH_TRAVERSAL", "Path traversal detected");
|
|
80
|
-
}
|
|
81
|
-
return resolved;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function resolveDeleteGuestPath(hostRoot: string, guestPath: string): string {
|
|
85
|
-
const pathSegments = guestPath.replace(/\\/g, "/").split("/");
|
|
86
|
-
if (pathSegments.includes("..")) {
|
|
87
|
-
throw new HttpError(403, "PATH_TRAVERSAL", "Path traversal detected");
|
|
88
|
-
}
|
|
89
|
-
return resolveGuestPath(hostRoot, guestPath);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
74
|
async function assertDeleteParentContained(hostRoot: string, fullPath: string): Promise<void> {
|
|
93
75
|
const [rootPath, parentPath] = await Promise.all([
|
|
94
76
|
fs.realpath(hostRoot),
|
|
@@ -141,7 +123,7 @@ export function createVolumeFsController(): {
|
|
|
141
123
|
try {
|
|
142
124
|
const { path: guestPath } = volumeFsReadSchema.parse(request.body ?? {});
|
|
143
125
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
144
|
-
const fullPath =
|
|
126
|
+
const fullPath = await resolveContainedGuestPath(hostRoot, guestPath);
|
|
145
127
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
146
128
|
return reply.send(ok({ path: guestPath, content }));
|
|
147
129
|
} catch (err: unknown) {
|
|
@@ -159,7 +141,7 @@ export function createVolumeFsController(): {
|
|
|
159
141
|
try {
|
|
160
142
|
const { path: guestPath, content } = volumeFsWriteSchema.parse(request.body ?? {});
|
|
161
143
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
162
|
-
const fullPath =
|
|
144
|
+
const fullPath = await resolveWritableGuestPath(hostRoot, guestPath);
|
|
163
145
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
164
146
|
await fs.writeFile(fullPath, content, "utf-8");
|
|
165
147
|
return reply.send(ok({ path: guestPath }));
|
|
@@ -178,7 +160,7 @@ export function createVolumeFsController(): {
|
|
|
178
160
|
try {
|
|
179
161
|
const { path: guestPath } = volumeFsDeleteSchema.parse(request.body ?? {});
|
|
180
162
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
181
|
-
const fullPath =
|
|
163
|
+
const fullPath = resolveGuestPath(hostRoot, guestPath);
|
|
182
164
|
await assertDeleteParentContained(hostRoot, fullPath);
|
|
183
165
|
const stat = await fs.lstat(fullPath);
|
|
184
166
|
assertRegularVolumeFile(guestPath, stat);
|
|
@@ -222,7 +204,7 @@ export function createVolumeFsController(): {
|
|
|
222
204
|
try {
|
|
223
205
|
const { path: guestPath } = volumeFsListSchema.parse(request.body ?? {});
|
|
224
206
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
225
|
-
const fullPath =
|
|
207
|
+
const fullPath = await resolveContainedGuestPath(hostRoot, guestPath);
|
|
226
208
|
const dirents = await fs.readdir(fullPath, { withFileTypes: true });
|
|
227
209
|
const entries = [];
|
|
228
210
|
for (const d of dirents) {
|
|
@@ -262,7 +244,7 @@ export function createVolumeFsController(): {
|
|
|
262
244
|
try {
|
|
263
245
|
const { path: guestPath } = volumeFsDownloadSchema.parse(request.query ?? {});
|
|
264
246
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
265
|
-
const fullPath =
|
|
247
|
+
const fullPath = await resolveContainedGuestPath(hostRoot, guestPath);
|
|
266
248
|
const buf = await fs.readFile(fullPath);
|
|
267
249
|
const contentBase64 = buf.toString("base64");
|
|
268
250
|
return reply.send(ok({ path: guestPath, contentBase64 }));
|
|
@@ -281,7 +263,7 @@ export function createVolumeFsController(): {
|
|
|
281
263
|
try {
|
|
282
264
|
const { path: guestPath, contentBase64 } = volumeFsUploadSchema.parse(request.body ?? {});
|
|
283
265
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
284
|
-
const fullPath =
|
|
266
|
+
const fullPath = await resolveWritableGuestPath(hostRoot, guestPath);
|
|
285
267
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
286
268
|
const data = Buffer.from(contentBase64, "base64");
|
|
287
269
|
await fs.writeFile(fullPath, data);
|
|
@@ -301,7 +283,7 @@ export function createVolumeFsController(): {
|
|
|
301
283
|
try {
|
|
302
284
|
const { path: guestPath } = volumeFsMkdirSchema.parse(request.body ?? {});
|
|
303
285
|
const hostRoot = await resolveVolumeHostPath(name);
|
|
304
|
-
const fullPath =
|
|
286
|
+
const fullPath = await resolveWritableGuestPath(hostRoot, guestPath);
|
|
305
287
|
await fs.mkdir(fullPath, { recursive: true });
|
|
306
288
|
return reply.send(ok({ path: guestPath }));
|
|
307
289
|
} catch (err: unknown) {
|