Skip to content

Commit 7718503

Browse files
test(server): verify draft media access boundaries
1 parent c8c1cd8 commit 7718503

1 file changed

Lines changed: 83 additions & 5 deletions

File tree

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,93 @@ describe("AssetAccess", () => {
337337
const asset = yield* resolveAsset(token, suffix.slice(separator + 1));
338338
expect(asset).toMatchObject({ kind: "file", path: yield* fs.realPath(filePath), mimeType });
339339
if (asset?.kind !== "file") throw new Error("Expected draft media file");
340-
const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset));
341-
expect(yield* Effect.promise(() => response.text())).toBe("media bytes");
342-
expect(
343-
yield* resolveAsset(token, name === "video.mp4" ? "audio.wav" : "video.mp4"),
344-
).toBeNull();
340+
for (const [range, status, body] of [
341+
[undefined, 200, "media bytes"],
342+
["bytes=2-5", 206, "dia "],
343+
] as const) {
344+
const resolved = yield* resolveAsset(token, suffix.slice(separator + 1));
345+
if (resolved?.kind !== "file") throw new Error("Expected draft media file");
346+
const response = HttpServerResponse.toWeb(yield* assetFileResponse(resolved, range));
347+
expect(response.status).toBe(status);
348+
expect(response.headers.get("content-type")).toBe(mimeType);
349+
expect(response.headers.get("content-length")).toBe(String(body.length));
350+
if (range) expect(response.headers.get("content-range")).toBe("bytes 2-5/11");
351+
expect(yield* Effect.promise(() => response.text())).toBe(body);
352+
}
353+
for (const requestedPath of [
354+
name === "video.mp4" ? "audio.wav" : "video.mp4",
355+
`../${name}`,
356+
]) {
357+
expect(yield* resolveAsset(token, requestedPath)).toBeNull();
358+
}
359+
expect(yield* resolveAsset(`${token}tampered`, name)).toBeNull();
345360
}
346361
}).pipe(Effect.provide(layerTest)),
347362
);
348363

364+
it.effect(
365+
"rejects draft media escapes and disallowed types without enabling non-draft media",
366+
() =>
367+
Effect.gen(function* () {
368+
const fs = yield* FileSystem.FileSystem;
369+
const path = yield* Path.Path;
370+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-draft-boundary-" });
371+
const root = path.join(directory, "workspace");
372+
yield* fs.makeDirectory(root);
373+
for (const name of ["video.mp4", "audio.wav", "secret.txt"]) {
374+
yield* fs.writeFileString(path.join(root, name), "workspace bytes");
375+
yield* fs.writeFileString(path.join(directory, name), "outside bytes");
376+
}
377+
for (const [requestedPath, tag] of [
378+
["../video.mp4", "AssetWorkspacePathValidationError"],
379+
["../audio.wav", "AssetWorkspacePathValidationError"],
380+
["secret.txt", "AssetPreviewTypeValidationError"],
381+
] as const) {
382+
const error = yield* issueAssetUrl({
383+
resource: { _tag: "draft-workspace-file", cwd: root, path: requestedPath },
384+
workspaceRoot: root,
385+
}).pipe(Effect.flip);
386+
expect(error._tag).toBe(tag);
387+
}
388+
for (const name of ["video.mp4", "audio.wav"]) {
389+
const error = yield* issueAssetUrl({
390+
resource: { _tag: "workspace-file", threadId: ThreadId.make("thread-1"), path: name },
391+
workspaceRoot: root,
392+
}).pipe(Effect.flip);
393+
expect(error._tag).toBe("AssetPreviewTypeValidationError");
394+
}
395+
}).pipe(Effect.provide(layerTest)),
396+
);
397+
398+
it.effect.skipIf(!symlinksSupported)(
399+
"rejects draft media symlinks escaping the workspace or targeting disallowed types",
400+
() =>
401+
Effect.gen(function* () {
402+
const fs = yield* FileSystem.FileSystem;
403+
const path = yield* Path.Path;
404+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-draft-symlink-" });
405+
const root = path.join(directory, "workspace");
406+
yield* fs.makeDirectory(root);
407+
const outside = path.join(directory, "outside.mp4");
408+
const secret = path.join(root, "secret.txt");
409+
yield* fs.writeFileString(outside, "outside bytes");
410+
yield* fs.writeFileString(secret, "private bytes");
411+
for (const [name, target] of [
412+
["escape.mp4", outside],
413+
["disguised.wav", secret],
414+
] as const) {
415+
yield* fs.symlink(target, path.join(root, name));
416+
const error = yield* issueAssetUrl({
417+
resource: { _tag: "draft-workspace-file", cwd: root, path: name },
418+
workspaceRoot: root,
419+
}).pipe(Effect.flip);
420+
expect(["AssetWorkspaceAssetNotFoundError", "AssetPreviewTypeValidationError"]).toContain(
421+
error._tag,
422+
);
423+
}
424+
}).pipe(Effect.provide(layerTest)),
425+
);
426+
349427
it.effect("resolves home-relative media paths independently of the workspace", () =>
350428
Effect.gen(function* () {
351429
const fs = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)