From 84f802aeb88ac752c624badd60dc0325b32dc8f6 Mon Sep 17 00:00:00 2001 From: JEONG KITAE Date: Tue, 20 Oct 2026 09:50:46 +0900 Subject: [PATCH 1/2] fix(tracker): bound peer IDs and addresses on join A join's id and address were limited only by the 1 MB request body, and both are repeated in every peer list and in each inventory update about the peer. IDs are now at most 128 characters without whitespace or control characters, and addresses at most 2048 characters. Every bundled client uses short IDs (host names, generated browser IDs, loadtest-NNN, cap-N). --- API_REFERENCE.md | 4 ++++ CHANGELOG.md | 3 +++ tracker/src/server.ts | 27 +++++++++++++++++++++++++-- tracker/test/server.test.ts | 24 ++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/API_REFERENCE.md b/API_REFERENCE.md index 169ae13..5f16c40 100644 --- a/API_REFERENCE.md +++ b/API_REFERENCE.md @@ -154,6 +154,10 @@ Joins or refreshes a peer. } ``` +`id` is at most 128 characters without whitespace or control characters, and +`address` at most 2048 characters; both are sent to other peers in every peer +list. + Returns `201` for a new peer or `200` for an authenticated refresh: ```json diff --git a/CHANGELOG.md b/CHANGELOG.md index 47160c5..76cd1eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,9 @@ All notable changes to OpenStreamGrid are documented in this file. ### Changed +- Peer joins reject IDs over 128 characters or containing whitespace or control + characters, and addresses over 2048 characters. Both were bounded only by + the 1 MB body limit and are repeated in every peer list and inventory update. - The tracker keeps at most 128 KiB of segment-ID characters per peer, in addition to 2000 segments, dropping the oldest. Up to 2000 IDs of 512 characters let one peer carry about a megabyte of inventory into every peer diff --git a/tracker/src/server.ts b/tracker/src/server.ts index e9d4564..9982413 100644 --- a/tracker/src/server.ts +++ b/tracker/src/server.ts @@ -840,6 +840,29 @@ const readJson = async (request: IncomingMessage): Promise => { } }; +/** + * Peer IDs and addresses appear in every peer list and in each inventory + * update about the peer, so they are kept to identifier and URL sizes. + */ +const MAX_PEER_ID_LENGTH = 128; +const MAX_PEER_ADDRESS_LENGTH = 2_048; +const PEER_ID_PATTERN = /^[^\s\p{Cc}]+$/u; + +const boundedString = (value: string, key: string, maxLength: number): string => { + if (value.length > maxLength) { + throw new RequestError(`'${key}' must be at most ${maxLength} characters`); + } + return value; +}; + +const peerIdentifier = (value: string): string => { + boundedString(value, "id", MAX_PEER_ID_LENGTH); + if (!PEER_ID_PATTERN.test(value)) { + throw new RequestError("'id' must not contain whitespace or control characters"); + } + return value; +}; + const requiredString = (body: JsonObject, key: string): string => { const value = body[key]; if (typeof value !== "string" || value.trim() === "") { @@ -1251,8 +1274,8 @@ export const createTrackerHandler = ( ); const metadata = optionalMetadata(body); const join: PeerJoinRequest = { - id: requiredString(body, "id"), - address: requiredString(body, "address"), + id: peerIdentifier(requiredString(body, "id")), + address: boundedString(requiredString(body, "address"), "address", MAX_PEER_ADDRESS_LENGTH), ...(uploadBandwidthBps !== undefined ? { uploadBandwidthBps } : {}), ...(metadata ? { metadata } : {}), }; diff --git a/tracker/test/server.test.ts b/tracker/test/server.test.ts index b3310cf..c5e8a0c 100644 --- a/tracker/test/server.test.ts +++ b/tracker/test/server.test.ts @@ -2091,3 +2091,27 @@ test("keeps peer and broadcast metadata small", async () => { const broadcast = await register({ big: "v".repeat(1_025) }); assert.equal(broadcast.status, 400); }); + +test("bounds peer IDs and addresses on join", async () => { + const handler = createTrackerHandler(new TrackerStore()); + await invoke(handler, "POST", "/api/v1/broadcasts", { + id: "live", + playlistUrl: "http://origin/live.m3u8", + }); + const join = (id: string, address = `http://peer:9090`) => + invoke(handler, "POST", "/api/v1/broadcasts/live/peers", { id, address }); + + assert.equal((await join("peer-a.example_01:x")).status, 201); + assert.equal((await join("p".repeat(128), "http://peer-b:9090")).status, 201); + const cases: Array<[string, string, string]> = [ + ["p".repeat(129), "http://peer-c:9090", "'id' must be at most 128 characters"], + ["peer c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], + ["peer\u0000c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], + ["peer-c", `http://peer-c:9090/${"a".repeat(2_048)}`, "'address' must be at most 2048 characters"], + ]; + for (const [id, address, error] of cases) { + const response = await join(id, address); + assert.equal(response.status, 400, error); + assert.deepEqual(response.json, { error }); + } +}); From 2321162be7d2614e7face6cedf0538e7ab586248 Mon Sep 17 00:00:00 2001 From: JEONG KITAE Date: Tue, 20 Oct 2026 09:52:48 +0900 Subject: [PATCH 2/2] test(tracker): restore the merged server tests The previous merge combined two tests appended at the end of the file into one broken block. Rebuild the file from main and append this branch's test. --- tracker/test/server.test.ts | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/tracker/test/server.test.ts b/tracker/test/server.test.ts index 35ddc76..824a938 100644 --- a/tracker/test/server.test.ts +++ b/tracker/test/server.test.ts @@ -2093,8 +2093,6 @@ test("keeps peer and broadcast metadata small", async () => { assert.equal(broadcast.status, 400); }); -test("bounds peer IDs and addresses on join", async () => { - const handler = createTrackerHandler(new TrackerStore()); test("closes WebSockets that report inventory faster than it can be relayed", async () => { const server = new TrackerServer(() => new TrackerStore(), 30_000, { trackerApiKey: "test-secret", @@ -2157,22 +2155,6 @@ test("validates and rate-limits REST segment reports with the WebSocket budget", id: "live", playlistUrl: "http://origin/live.m3u8", }); - const join = (id: string, address = `http://peer:9090`) => - invoke(handler, "POST", "/api/v1/broadcasts/live/peers", { id, address }); - - assert.equal((await join("peer-a.example_01:x")).status, 201); - assert.equal((await join("p".repeat(128), "http://peer-b:9090")).status, 201); - const cases: Array<[string, string, string]> = [ - ["p".repeat(129), "http://peer-c:9090", "'id' must be at most 128 characters"], - ["peer c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], - ["peer\u0000c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], - ["peer-c", `http://peer-c:9090/${"a".repeat(2_048)}`, "'address' must be at most 2048 characters"], - ]; - for (const [id, address, error] of cases) { - const response = await join(id, address); - assert.equal(response.status, 400, error); - assert.deepEqual(response.json, { error }); - } const joined = await invoke(handler, "POST", "/api/v1/broadcasts/live/peers", { id: "peer-a", address: "http://peer-a:9090", @@ -2198,3 +2180,27 @@ test("validates and rate-limits REST segment reports with the WebSocket budget", now = 60_000; assert.equal((await report(["v".repeat(500)])).status, 200); }); + +test("bounds peer IDs and addresses on join", async () => { + const handler = createTrackerHandler(new TrackerStore()); + await invoke(handler, "POST", "/api/v1/broadcasts", { + id: "live", + playlistUrl: "http://origin/live.m3u8", + }); + const join = (id: string, address = `http://peer:9090`) => + invoke(handler, "POST", "/api/v1/broadcasts/live/peers", { id, address }); + + assert.equal((await join("peer-a.example_01:x")).status, 201); + assert.equal((await join("p".repeat(128), "http://peer-b:9090")).status, 201); + const cases: Array<[string, string, string]> = [ + ["p".repeat(129), "http://peer-c:9090", "'id' must be at most 128 characters"], + ["peer c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], + ["peer\u0000c", "http://peer-c:9090", "'id' must not contain whitespace or control characters"], + ["peer-c", `http://peer-c:9090/${"a".repeat(2_048)}`, "'address' must be at most 2048 characters"], + ]; + for (const [id, address, error] of cases) { + const response = await join(id, address); + assert.equal(response.status, 400, error); + assert.deepEqual(response.json, { error }); + } +});