Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bcb6933
fix(media-server): check every remote URL against a private-address p…
07rjain Oct 23, 2026
163102d
chore: merge current recording verification work from main
07rjain Oct 23, 2026
c4e645d
fix(media-server): pin remote connections and complete safe media sta…
07rjain Oct 23, 2026
fdb9c6a
test(media-server): enforce one deadline across recording staging and…
07rjain Oct 23, 2026
7b49d10
test(media-server): preserve the spawn overload type in the deadline …
07rjain Oct 23, 2026
d80a37b
fix(media-server): try checked fallback addresses for media reads
07rjain Oct 23, 2026
5ab2e54
Merge remote-tracking branch 'origin/main' into split/media-url-policy
07rjain Oct 23, 2026
6933d95
chore(media-server): merge latest main into URL policy fixes
07rjain Oct 24, 2026
d4aee16
fix(media-server): align IPv6 restrictions with the S3 endpoint policy
07rjain Oct 24, 2026
c8166f9
fix(media-server): bound audio staging and retry refused connections
07rjain Oct 26, 2026
cbea23c
fix(media-server): include test configuration in Docker images
07rjain Oct 26, 2026
d6ded5b
Merge remote-tracking branch 'origin/main' into split/media-url-policy
07rjain Oct 26, 2026
d43279c
test(media-server): align merged video tests with staged resources
07rjain Oct 26, 2026
13b33fa
fix(media-server): secure upstream verification and Loom downloads
07rjain Oct 26, 2026
7c1d0b5
chore: merge main and resolve media URL policy conflicts
07rjain Nov 3, 2026
8733953
chore: merge main and preserve checked media downloads
07rjain Nov 5, 2026
ba08c78
chore: merge main and preserve media URL policy
07rjain Nov 12, 2026
e2ff26d
chore: merge main media safety changes
07rjain Nov 12, 2026
7b22f23
fix(media-server): merge main and preserve URL policy across probe wo…
07rjain Nov 16, 2026
27faa8d
Merge remote-tracking branch 'origin/main' into split/media-url-policy
07rjain Nov 16, 2026
785ef0f
test(media-server): apply URL policy in remote thumbnail rejection test
07rjain Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/media-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN grep -q MAX_DECLARED_SAMPLES node_modules/mediabunny/dist/modules/src/isobmf

COPY apps/media-server/bunfig.toml ./bunfig.toml
COPY apps/media-server/src ./src
COPY apps/media-server/capcodec.md ./capcodec.md
COPY apps/media-server/capcodec.md apps/media-server/bunfig.toml ./

RUN bun test src/__tests__/lib/audio-quality-policy.test.ts src/__tests__/lib/audio-quality.integration.test.ts \
&& bun test src/__tests__/lib/audio-quality-formats.integration.test.ts src/__tests__/lib/audio-quality-benchmark.test.ts \
Expand Down
4 changes: 3 additions & 1 deletion apps/media-server/src/__tests__/lib/job-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ describe("durable recording workers", () => {
const sending = sendWebhook(job);
updateJob(job.jobId, { phase: "complete", progress: 100 });
expect(sendWebhook(job)).toBe(sending);
// The URL policy check runs before fetch, so wait for the request to start.
while (!release) await Bun.sleep(1);
const started = performance.now();
release?.(new Response(null, { status: 503 }));
release(new Response(null, { status: 503 }));
await sending;
expect(performance.now() - started).toBeLessThan(400);
expect(maximumInFlight).toBe(1);
Expand Down
80 changes: 79 additions & 1 deletion apps/media-server/src/__tests__/lib/media-address-guard.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, test } from "bun:test";
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import {
assertMediaUrlAllowed,
classifyHostLiteral,
Expand All @@ -11,6 +11,7 @@ import {
import { getSourceSize } from "../../lib/media-common";
import { MediaInputPolicyError } from "../../lib/media-input-policy";
import { childProcessEnv } from "../../lib/media-spawn";
import { BlockedUrlError } from "../../lib/url-policy";

const NONE: ReadonlySet<string> = new Set();
const ANY: ReadonlySet<string> = new Set(["*"]);
Expand All @@ -21,12 +22,89 @@ function resolvesTo(...addresses: string[]): MediaAddressPolicy["resolveHost"] {

const servers: { stop(force?: boolean): Promise<void> }[] = [];
const savedHosts = process.env.MEDIA_SERVER_INTERNAL_MEDIA_HOSTS;
const savedAllowedHosts = process.env.MEDIA_SERVER_ALLOWED_HOSTS;
const savedAllowPrivate = process.env.MEDIA_SERVER_ALLOW_PRIVATE_URLS;
const savedS3Endpoint = process.env.S3_INTERNAL_ENDPOINT;

beforeEach(() => {
process.env.MEDIA_SERVER_ALLOW_PRIVATE_URLS = "false";
});

afterEach(async () => {
await Promise.all(servers.splice(0).map((server) => server.stop(true)));
if (savedHosts === undefined)
delete process.env.MEDIA_SERVER_INTERNAL_MEDIA_HOSTS;
else process.env.MEDIA_SERVER_INTERNAL_MEDIA_HOSTS = savedHosts;
for (const [key, value] of [
["MEDIA_SERVER_ALLOWED_HOSTS", savedAllowedHosts],
["MEDIA_SERVER_ALLOW_PRIVATE_URLS", savedAllowPrivate],
["S3_INTERNAL_ENDPOINT", savedS3Endpoint],
] as const) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
});

describe("media address and URL policies", () => {
test("applies the host allowlist to each redirect without resolving twice", async () => {
process.env.MEDIA_SERVER_ALLOW_PRIVATE_URLS = "false";
process.env.MEDIA_SERVER_ALLOWED_HOSTS = "allowed.example";
const lookups: string[] = [];
const requests: string[] = [];
const url = "https://allowed.example/video.mp4";
const guarded = createGuardedMediaFetch(url, {
resolveHost: async (host) => {
lookups.push(host);
return ["93.184.216.34"];
},
fetchImpl: Object.assign(
async (
input: Parameters<typeof fetch>[0],
init?: Parameters<typeof fetch>[1],
) => {
requests.push(String(input));
expect(new Headers(init?.headers).get("host")).toBe(
"allowed.example",
);
expect(init).toHaveProperty("proxy", false);
return new Response(null, {
status: 302,
headers: { location: "https://unlisted.example/video.mp4" },
});
},
{ preconnect: fetch.preconnect },
),
});
await expect(guarded(url)).rejects.toBeInstanceOf(BlockedUrlError);
expect(requests).toEqual(["https://93.184.216.34/video.mp4"]);
expect(lookups).toEqual(["allowed.example", "unlisted.example"]);
});

test("reads private storage configured through either host setting", async () => {
process.env.MEDIA_SERVER_ALLOW_PRIVATE_URLS = "false";
const origin = serve(() => new Response("media"));
const port = new URL(origin.url).port;
const url = `http://storage.internal:${port}/video.mp4`;
const policy = { resolveHost: resolvesTo("127.0.0.1") };
process.env.MEDIA_SERVER_INTERNAL_MEDIA_HOSTS = "storage.internal";
let response = await createGuardedMediaFetch(url, policy)(url);
expect(await response.text()).toBe("media");
process.env.MEDIA_SERVER_INTERNAL_MEDIA_HOSTS = "";
process.env.S3_INTERNAL_ENDPOINT = `http://storage.internal:${port}`;
response = await createGuardedMediaFetch(url, policy)(url);
expect(await response.text()).toBe("media");
expect(origin.hits).toEqual(["/video.mp4", "/video.mp4"]);
});

test("does not grant a trusted storage host access to metadata", async () => {
process.env.MEDIA_SERVER_ALLOW_PRIVATE_URLS = "false";
process.env.S3_INTERNAL_ENDPOINT = "http://storage.internal";
await expect(
assertMediaUrlAllowed("http://storage.internal/latest/meta-data/", {
resolveHost: resolvesTo("169.254.169.254"),
}),
).rejects.toBeInstanceOf(MediaAddressError);
});
});

function serve(handler: (request: Request) => Response) {
Expand Down
78 changes: 67 additions & 11 deletions apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
Expand All @@ -24,8 +24,15 @@ const FIXTURES_DIR = join(import.meta.dir, "..", "fixtures");
const TEST_VIDEO_WITH_AUDIO = `file://${join(FIXTURES_DIR, "test-with-audio.mp4")}`;
const TEST_VIDEO_NO_AUDIO = `file://${join(FIXTURES_DIR, "test-no-audio.mp4")}`;

async function createHlsFixture(): Promise<string> {
async function createHlsFixture(encrypted = false): Promise<string> {
const dirPath = await mkdtemp(join(tmpdir(), "cap-audio-hls-"));
if (encrypted) {
await writeFile(join(dirPath, "key.bin"), Buffer.alloc(16, 7));
await writeFile(
join(dirPath, "key.info"),
`key.bin\n${join(dirPath, "key.bin")}\n`,
);
}
const proc = Bun.spawn({
cmd: [
"ffmpeg",
Expand All @@ -41,6 +48,7 @@ async function createHlsFixture(): Promise<string> {
"0.25",
"-hls_list_size",
"0",
...(encrypted ? ["-hls_key_info_file", join(dirPath, "key.info")] : []),
"-hls_segment_filename",
join(dirPath, "segment-%03d.ts"),
join(dirPath, "manifest.m3u8"),
Expand Down Expand Up @@ -210,7 +218,7 @@ describe("mediaAudio integration tests", () => {
try {
await expect(
checkHasAudioTrack(`${server.url}video.mp4`),
).rejects.toThrow("FFprobe exited");
).rejects.toThrow("Failed to fetch media resource: 503");
await waitForAudioOperations(beforeCount);
} finally {
await server.stop(true);
Expand All @@ -234,7 +242,9 @@ describe("mediaAudio integration tests", () => {
describe("extractAudioStream", () => {
test("preserves queued audio when the reader pauses before EOF", async () => {
const expected = await extractAudio(TEST_VIDEO_WITH_AUDIO);
const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
const { stream, cleanup } = await extractAudioStream(
TEST_VIDEO_WITH_AUDIO,
);
const reader = stream.getReader();
const chunks: Uint8Array[] = [];
try {
Expand All @@ -254,7 +264,9 @@ describe("mediaAudio integration tests", () => {
test("drains all encoded bytes when the consumer waits for FFmpeg to exit", async () => {
const expected = await extractAudio(TEST_VIDEO_WITH_AUDIO);
const registration = spyOn(subprocess, "registerSubprocess");
const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
const { stream, cleanup } = await extractAudioStream(
TEST_VIDEO_WITH_AUDIO,
);
try {
const proc = registration.mock.calls[0][0];
expect(await proc.exited).toBe(0);
Expand All @@ -269,7 +281,7 @@ describe("mediaAudio integration tests", () => {

test("errors the stream when FFmpeg cannot extract audio", async () => {
const beforeCount = getActiveProcessCount();
const { stream, cleanup } = extractAudioStream(TEST_VIDEO_NO_AUDIO);
const { stream, cleanup } = await extractAudioStream(TEST_VIDEO_NO_AUDIO);
try {
await expect(readStream(stream)).rejects.toThrow("FFmpeg exited");
await waitForAudioOperations(beforeCount);
Expand All @@ -281,9 +293,12 @@ describe("mediaAudio integration tests", () => {
test("errors the stream and terminates FFmpeg at the deadline", async () => {
const beforeCount = getActiveProcessCount();
const registration = spyOn(subprocess, "registerSubprocess");
const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO, {
timeoutMs: 1,
});
const { stream, cleanup } = await extractAudioStream(
TEST_VIDEO_WITH_AUDIO,
{
timeoutMs: 1,
},
);
try {
await expect(readStream(stream)).rejects.toThrow("timed out");
const proc = registration.mock.calls[0][0];
Expand All @@ -297,21 +312,62 @@ describe("mediaAudio integration tests", () => {

test("streams real mp3 data and cleans up its operation", async () => {
const beforeCount = getActiveProcessCount();
const { stream } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
const { stream } = await extractAudioStream(TEST_VIDEO_WITH_AUDIO);

const audioData = await readStream(stream);

expectMp3(audioData);
await waitForAudioOperations(beforeCount);
});

test("releases the operation when a remote download fails", async () => {
const beforeCount = getActiveProcessCount();
const server = Bun.serve({
port: 0,
fetch: () => new Response("Missing", { status: 404 }),
});

try {
await expect(
extractAudioStream(`${server.url}clip.mp4`),
).rejects.toThrow("Failed to fetch media resource: 404");
await waitForAudioOperations(beforeCount);
} finally {
await server.stop(true);
}
});

test("cleans up when a real stream is cancelled", async () => {
const beforeCount = getActiveProcessCount();
const { stream } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
const { stream } = await extractAudioStream(TEST_VIDEO_WITH_AUDIO);

await stream.cancel();

await waitForAudioOperations(beforeCount);
});
});
});

describe("encrypted remote HLS audio", () => {
test("extracts MP3 audio after staging AES-128 keys and segments locally", async () => {
const directory = await createHlsFixture(true);
const server = Bun.serve({
port: 0,
fetch(request) {
return new Response(
Bun.file(join(directory, new URL(request.url).pathname)),
);
},
});
try {
const url = new URL("manifest.m3u8", server.url).toString();
expect(await checkHasAudioTrack(url)).toBe(true);
expectMp3(await extractAudio(url));
const { stream } = await extractAudioStream(url);
expectMp3(await readStream(stream));
} finally {
server.stop(true);
await rm(directory, { recursive: true, force: true });
}
});
});
Loading
Loading