diff --git a/apps/radio/README.md b/apps/radio/README.md index 3432278d..502878c5 100644 --- a/apps/radio/README.md +++ b/apps/radio/README.md @@ -110,9 +110,15 @@ playlists are resolved directly in the browser. Bandcamp's fresh, validated `seep.eu.org`, `proxy.cors.sh`, then `cors.zme.ink`; the browser range-probes the pool under one deadline before selecting the highest-priority working relay. YouTube tries the -curated, release-tested providers `pipedapi.wireway.ch`, `yt.omada.cafe`, -`invidious.nikkosphere.com`, then `y.com.sb`, with per-provider and whole-pool -deadlines. See `BANDCAMP_RELAY_RESEARCH.md` and +curated providers `pipedapi.wireway.ch`, `yt.omada.cafe`, +`invidious.nikkosphere.com`, `yt.chocolatemoo53.com`, then `y.com.sb`, with a +five-second deadline per provider and a thirty-second deadline for the pool. +Chocolatemoo opts in to browser media redirects: the starting and final URLs +must pass public-URL/DNS validation and match its curated origins. Browser Fetch +controls the intermediate hops, which JavaScript cannot inspect through manual +redirects; this policy trusts the operator's routing. Successful verification +returns the final companion URL to the player. Other providers and all API +requests keep rejecting redirects. See `BANDCAMP_RELAY_RESEARCH.md` and `YOUTUBE_PROVIDER_RESEARCH.md` for the release probes and caveats. ## Analytics diff --git a/apps/radio/src/lib/youtube/client.test.ts b/apps/radio/src/lib/youtube/client.test.ts index 3e376256..0c7f21a5 100644 --- a/apps/radio/src/lib/youtube/client.test.ts +++ b/apps/radio/src/lib/youtube/client.test.ts @@ -36,6 +36,20 @@ describe("curated YouTube client", () => { id: "invidious-nikkosphere", kind: "invidious", }, + { + baseUrl: "https://yt.chocolatemoo53.com", + id: "invidious-chocolatemoo", + kind: "invidious", + mediaRedirectOrigins: [ + "https://yt.chocolatemoo53.com", + "https://yt-comp1.chocolatemoo53.com", + "https://yt-comp2.chocolatemoo53.com", + "https://yt-comp3.chocolatemoo53.com", + "https://yt-comp4.chocolatemoo53.com", + "https://yt-comp5.chocolatemoo53.com", + "https://yt-comp6.chocolatemoo53.com", + ], + }, { baseUrl: "https://y.com.sb", id: "invidious-y-com-sb", @@ -51,6 +65,7 @@ describe("curated YouTube client", () => { { id: "piped-wireway", kind: "piped" }, { id: "invidious-omada", kind: "invidious" }, { id: "invidious-nikkosphere", kind: "invidious" }, + { id: "invidious-chocolatemoo", kind: "invidious" }, { id: "invidious-y-com-sb", kind: "invidious" }, ]); }); @@ -61,6 +76,67 @@ describe("curated YouTube client", () => { ); }); + test("falls back to Chocolatemoo and returns its verified companion URL", async () => { + const hosts: string[] = []; + const companionUrl = "https://yt-comp4.chocolatemoo53.com/videoplayback"; + const fetchImpl = mock((input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(String(input)); + hosts.push(url.host); + if (url.host !== "yt.chocolatemoo53.com") { + return Promise.resolve(new Response("unavailable", { status: 503 })); + } + if (new Headers(init?.headers).has("Range")) { + if (init?.redirect !== "follow") { + return Promise.reject(new TypeError("Unexpected redirect")); + } + const response = new Response(new Uint8Array([1]), { + headers: { + "Content-Length": "65536", + "Content-Type": "audio/webm", + }, + status: 206, + }); + Object.defineProperty(response, "url", { value: companionUrl }); + return Promise.resolve(response); + } + expect(init?.redirect).toBe("error"); + return Promise.resolve( + Response.json({ + adaptiveFormats: [ + { + bitrate: "128000", + type: "audio/webm", + url: "/videoplayback", + }, + ], + author: "Artist", + lengthSeconds: 180, + liveNow: false, + title: "Track", + videoId: "abcdefghijk", + videoThumbnails: [], + }) + ); + }); + const client = createCuratedYouTubeClient(CURATED_YOUTUBE_PROVIDERS, { + fetchImpl: fetchImpl as unknown as typeof fetch, + }); + + await expect( + client.resolveItem("https://youtube.com/watch?v=abcdefghijk") + ).resolves.toMatchObject({ + metadata: { streamUrl: companionUrl }, + streamUrl: companionUrl, + }); + expect(hosts).toEqual([ + "pipedapi.wireway.ch", + "yt.omada.cafe", + "invidious.nikkosphere.com", + "yt.chocolatemoo53.com", + "yt.chocolatemoo53.com", + ]); + }); + test("bounds the complete failover operation", async () => { const fetchImpl = mock((_input: RequestInfo | URL, init?: RequestInit) => pendingUntilAborted(init) @@ -86,7 +162,7 @@ describe("curated YouTube client", () => { test("gives every provider a turn during two-stage resolution", async () => { const requests: string[] = []; - const providers = [1, 2, 3, 4].map((index) => ({ + const providers = [1, 2, 3, 4, 5].map((index) => ({ baseUrl: `https://provider-${index}.example`, id: `provider-${index}`, kind: "piped" as const, @@ -128,8 +204,8 @@ describe("curated YouTube client", () => { await expect(client.resolveStream("abcdefghijk")).rejects.toThrow( "Every configured YouTube provider failed" ); - expect(requests.filter((url) => url.includes("/streams/"))).toHaveLength(4); - expect(requests.filter((url) => url.includes("media-"))).toHaveLength(4); + expect(requests.filter((url) => url.includes("/streams/"))).toHaveLength(5); + expect(requests.filter((url) => url.includes("media-"))).toHaveLength(5); }); test("rejects an invalid pool timeout", () => { diff --git a/apps/radio/src/lib/youtube/client.ts b/apps/radio/src/lib/youtube/client.ts index 59269160..96ad68c2 100644 --- a/apps/radio/src/lib/youtube/client.ts +++ b/apps/radio/src/lib/youtube/client.ts @@ -13,6 +13,7 @@ export type CuratedYouTubeProvider = { baseUrl: string; id: string; kind: YouTubeProviderKind; + mediaRedirectOrigins?: readonly string[]; }; // See YOUTUBE_PROVIDER_RESEARCH.md. @@ -32,6 +33,20 @@ export const CURATED_YOUTUBE_PROVIDERS: readonly CuratedYouTubeProvider[] = [ id: "invidious-nikkosphere", kind: "invidious", }, + { + baseUrl: "https://yt.chocolatemoo53.com", + id: "invidious-chocolatemoo", + kind: "invidious", + mediaRedirectOrigins: [ + "https://yt.chocolatemoo53.com", + "https://yt-comp1.chocolatemoo53.com", + "https://yt-comp2.chocolatemoo53.com", + "https://yt-comp3.chocolatemoo53.com", + "https://yt-comp4.chocolatemoo53.com", + "https://yt-comp5.chocolatemoo53.com", + "https://yt-comp6.chocolatemoo53.com", + ], + }, { baseUrl: "https://y.com.sb", id: "invidious-y-com-sb", @@ -54,7 +69,8 @@ type YouTubeClientOptions = Pick< }; const DEFAULT_PROVIDER_TIMEOUT_MS = 5000; -const DEFAULT_OPERATION_TIMEOUT_MS = 25_000; +// Five providers get a full five-second attempt, plus time for pool overhead. +const DEFAULT_OPERATION_TIMEOUT_MS = 30_000; type DeadlineSignal = { cleanup: () => void; @@ -197,6 +213,7 @@ export function createCuratedYouTubeClient( ...providerOptions, baseUrl: provider.baseUrl, id: provider.id, + mediaRedirectOrigins: provider.mediaRedirectOrigins, timeoutMs, }; const adapter = diff --git a/packages/platforms/src/youtube/invidious-browser.ts b/packages/platforms/src/youtube/invidious-browser.ts index c79b095d..d6f02b33 100644 --- a/packages/platforms/src/youtube/invidious-browser.ts +++ b/packages/platforms/src/youtube/invidious-browser.ts @@ -172,8 +172,11 @@ async function resolveVideoStream( "No YouTube audio stream found" ); } - const streamUrl = await resolveProviderUrl(context, stream.url, signal); - await verifyProviderMedia(context, streamUrl, signal); + const streamUrl = await verifyProviderMedia( + context, + await resolveProviderUrl(context, stream.url, signal), + signal + ); return { streamUrl, video }; } diff --git a/packages/platforms/src/youtube/piped.ts b/packages/platforms/src/youtube/piped.ts index 3d318c7a..582b20c7 100644 --- a/packages/platforms/src/youtube/piped.ts +++ b/packages/platforms/src/youtube/piped.ts @@ -203,8 +203,11 @@ async function resolveVideoStream( "No YouTube audio stream found" ); } - const streamUrl = await resolveProviderUrl(context, stream.url, signal); - await verifyProviderMedia(context, streamUrl, signal); + const streamUrl = await verifyProviderMedia( + context, + await resolveProviderUrl(context, stream.url, signal), + signal + ); return { streamUrl, video }; } diff --git a/packages/platforms/src/youtube/provider.test.ts b/packages/platforms/src/youtube/provider.test.ts index 9af33c9d..8b754081 100644 --- a/packages/platforms/src/youtube/provider.test.ts +++ b/packages/platforms/src/youtube/provider.test.ts @@ -83,6 +83,272 @@ function pipedVideo() { }; } +function responseAt(response: Response, url: string): Response { + Object.defineProperty(response, "url", { value: url }); + return response; +} + +describe("opt-in YouTube media redirects", () => { + const origins = ["https://provider.test", "https://companion.test"]; + + test("keeps JSON redirects disabled when media redirects are enabled", async () => { + const fetchImpl = mockFetch( + (_input: RequestInfo | URL, init?: RequestInit) => { + expect(init?.redirect).toBe("error"); + return Promise.resolve( + Response.redirect("https://companion.test", 302) + ); + } + ); + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl, + mediaRedirectOrigins: origins, + }); + + await expect(adapter.probe()).rejects.toMatchObject({ + code: "http", + status: 302, + }); + expect(fetchImpl).toHaveBeenCalledTimes(1); + }); + + test("does not request media outside the configured starting origins", async () => { + const video = invidiousVideo(); + video.adaptiveFormats[0].url = "https://other-public.test/audio"; + const fetchImpl = mockFetch(() => Promise.resolve(json(video))); + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl, + mediaRedirectOrigins: origins, + resolveHostname: async () => [PUBLIC_ADDRESS], + }); + + await expect(adapter.resolveStream(VIDEO_ID)).rejects.toMatchObject({ + code: "invalid-schema", + }); + expect(fetchImpl).toHaveBeenCalledTimes(1); + }); + + test.each([ + "https://other-public.test/audio", + "https://companion.test.evil.test/audio", + "https://companion.test:8443/audio", + "https://user:password@companion.test/audio", + "http://companion.test/audio", + "", + ])( + "rejects and cancels an unapproved media response URL (%s)", + async (url) => { + const cancel = mock(() => undefined); + const fetchImpl = mockFetch( + (_input: RequestInfo | URL, init?: RequestInit) => + Promise.resolve( + new Headers(init?.headers).has("Range") + ? responseAt(media(new ReadableStream({ cancel })), url) + : json(invidiousVideo()) + ) + ); + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl, + mediaRedirectOrigins: origins, + resolveHostname: async () => [PUBLIC_ADDRESS], + }); + + await expect(adapter.resolveStream(VIDEO_ID)).rejects.toMatchObject({ + code: "invalid-schema", + }); + expect(cancel).toHaveBeenCalledTimes(1); + } + ); + + test("validates public DNS for the final companion and cancels rejected media", async () => { + const cancel = mock(() => undefined); + const fetchImpl = mockFetch( + (_input: RequestInfo | URL, init?: RequestInit) => + Promise.resolve( + new Headers(init?.headers).has("Range") + ? responseAt( + media(new ReadableStream({ cancel })), + "https://companion.test/audio" + ) + : json(invidiousVideo()) + ) + ); + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl, + mediaRedirectOrigins: origins, + resolveHostname: async (hostname) => + hostname === "companion.test" ? ["127.0.0.1"] : [PUBLIC_ADDRESS], + }); + + await expect(adapter.resolveStream(VIDEO_ID)).rejects.toMatchObject({ + code: "invalid-schema", + }); + expect(cancel).toHaveBeenCalledTimes(1); + }); + + test.each(["parent cancellation", "request timeout"] as const)( + "preserves %s during final companion DNS validation", + async (interruption) => { + const controller = new AbortController(); + const resolvingCompanion = Promise.withResolvers(); + const cancel = mock(() => undefined); + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl: mockFetch((_input: RequestInfo | URL, init?: RequestInit) => + Promise.resolve( + new Headers(init?.headers).has("Range") + ? responseAt( + media(new ReadableStream({ cancel })), + "https://companion.test/audio" + ) + : json(invidiousVideo()) + ) + ), + mediaRedirectOrigins: origins, + resolveHostname: async (hostname, { signal } = {}) => { + if (hostname !== "companion.test") { + return [PUBLIC_ADDRESS]; + } + resolvingCompanion.resolve(); + return await new Promise((_resolve, reject) => { + const onAbort = () => reject(signal?.reason); + if (signal?.aborted) { + onAbort(); + } else { + signal?.addEventListener("abort", onAbort, { once: true }); + } + }); + }, + timeoutMs: interruption === "parent cancellation" ? 1000 : 50, + }); + + const resolution = adapter + .resolveStream(VIDEO_ID, controller.signal) + .catch((error: unknown) => error); + await resolvingCompanion.promise; + if (interruption === "parent cancellation") { + controller.abort(); + } + expect(await resolution).toMatchObject( + interruption === "parent cancellation" + ? { code: "aborted", retryable: false } + : { code: "timeout", retryable: true } + ); + expect(cancel).toHaveBeenCalledTimes(1); + } + ); + + test.each([ + [ + "non-audio content", + () => media("HTML", { "Content-Type": "text/html" }), + { code: "unexpected-content-type" }, + ], + [ + "an invalid range", + () => media("audio", { "Content-Range": "bytes 1-2/3" }), + { code: "invalid-schema" }, + ], + ["an empty body", () => media(null), { code: "invalid-schema" }], + [ + "an ignored range", + () => new Response("audio", { status: 200 }), + { code: "invalid-schema" }, + ], + [ + "a media outage", + () => new Response("unavailable", { status: 503 }), + { code: "http", status: 503 }, + ], + ] as const)( + "still rejects %s after a handoff", + async (_name, response, error) => { + const adapter = createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + fetchImpl: mockFetch((_input: RequestInfo | URL, init?: RequestInit) => + Promise.resolve( + new Headers(init?.headers).has("Range") + ? responseAt(response(), "https://companion.test/audio") + : json(invidiousVideo()) + ) + ), + mediaRedirectOrigins: origins, + resolveHostname: async () => [PUBLIC_ADDRESS], + }); + + const resolution = adapter.resolveStream(VIDEO_ID); + await expect(resolution).rejects.toBeInstanceOf(YouTubeProviderError); + await expect(resolution).rejects.toMatchObject(error); + } + ); + + test.each([ + "http://companion.test", + "https://companion.test/path", + "https://companion.test?key=1", + "https://user:password@companion.test", + ])("rejects an invalid redirect-origin configuration (%s)", (origin) => { + expect(() => + createBrowserInvidiousAdapter({ + baseUrl: "https://provider.test", + mediaRedirectOrigins: [origin], + }) + ).toThrow(YouTubeProviderError); + }); + + test.each(["invidious", "piped"] as const)( + "%s verifies the media handoff and returns its final URL", + async (kind) => { + const finalUrl = "https://companion.test/videoplayback/audio"; + const fetchImpl = mockFetch( + (_input: RequestInfo | URL, init?: RequestInit) => { + if (!new Headers(init?.headers).has("Range")) { + expect(init?.redirect).toBe("error"); + return Promise.resolve( + json(kind === "invidious" ? invidiousVideo() : pipedVideo()) + ); + } + // Browser Fetch rejects a real 307 here when redirect is "error". + if (init?.redirect === "error") { + return Promise.reject(new TypeError("Unexpected redirect")); + } + expect(init?.redirect).toBe("follow"); + expect(init?.credentials).toBe("omit"); + expect(init?.referrerPolicy).toBe("no-referrer"); + expect(new Headers(init?.headers).get("Range")).toBe("bytes=0-65535"); + return Promise.resolve(responseAt(media(), finalUrl)); + } + ); + const createAdapter = + kind === "invidious" + ? createBrowserInvidiousAdapter + : createPipedAdapter; + const adapter = createAdapter({ + baseUrl: "https://provider.test", + fetchImpl, + mediaRedirectOrigins: [ + "https://provider.test", + "https://proxy.piped.test", + "https://companion.test", + ], + resolveHostname: async () => [PUBLIC_ADDRESS], + }); + + await expect( + adapter.resolveItem(`https://youtube.com/watch?v=${VIDEO_ID}`) + ).resolves.toMatchObject({ + metadata: { streamUrl: finalUrl }, + streamUrl: finalUrl, + }); + expect(fetchImpl).toHaveBeenCalledTimes(2); + } + ); +}); + describe("browser Invidious adapter", () => { test("does not follow provider JSON redirects to loopback", async () => { const fetchImpl = mockFetch( diff --git a/packages/platforms/src/youtube/provider.ts b/packages/platforms/src/youtube/provider.ts index c9f0a742..2ed19746 100644 --- a/packages/platforms/src/youtube/provider.ts +++ b/packages/platforms/src/youtube/provider.ts @@ -101,6 +101,12 @@ export type YouTubeProviderAdapterOptions = { fetchImpl?: typeof fetch; id?: string; maxResponseBytes?: number; + /** + * Opt in to browser media redirects between these starting/final origins. + * Intermediate hops are controlled by the trusted provider, not inspectable + * through browser manual redirects. JSON/text requests still reject redirects. + */ + mediaRedirectOrigins?: readonly string[]; resolveHostname?: PublicHostnameResolver | false; timeoutMs?: number; verifyMedia?: boolean; @@ -111,6 +117,7 @@ export type YouTubeProviderRequestContext = { fetchImpl: typeof fetch; kind: YouTubeProviderKind; maxResponseBytes: number; + mediaRedirectOrigins: readonly string[]; providerId: string; resolveHostname: PublicHostnameResolver | false; timeoutMs: number; @@ -165,6 +172,23 @@ function normalizeBaseUrl(value: string, kind: YouTubeProviderKind): string { return url.toString().replace(TRAILING_SLASH_PATTERN, ""); } +/** Accept only canonical HTTPS origins for a provider's media redirect policy. */ +function normalizeMediaRedirectOrigin( + value: string, + kind: YouTubeProviderKind +): string { + const normalized = normalizeBaseUrl(value, kind); + const url = new URL(normalized); + if (url.protocol !== "https:" || normalized !== url.origin) { + throw configurationError( + kind, + "YouTube media redirect origins must be HTTPS origins without a path" + ); + } + return url.origin; +} + +/** Normalize provider configuration; redirects require an explicit media opt-in. */ export function createYouTubeProviderRequestContext( kind: YouTubeProviderKind, options: YouTubeProviderAdapterOptions @@ -189,6 +213,9 @@ export function createYouTubeProviderRequestContext( fetchImpl, kind, maxResponseBytes, + mediaRedirectOrigins: (options.mediaRedirectOrigins ?? []).map((origin) => + normalizeMediaRedirectOrigin(origin, kind) + ), providerId: options.id?.trim() || `${kind}:${new URL(baseUrl).host}`, resolveHostname: options.resolveHostname ?? @@ -265,6 +292,33 @@ export async function resolveProviderUrl( return url.toString(); } +/** Check a starting or final media origin and its public URL/DNS resolution. */ +async function validateMediaRedirectUrl( + context: YouTubeProviderRequestContext, + value: string, + signal: AbortSignal +): Promise { + // Fetch always supplies an absolute response URL. An empty/opaque result + // cannot establish the destination and must not pass media verification. + let url: URL; + try { + url = new URL(value); + } catch (error) { + throw invalidProviderSchema( + context, + "YouTube media response URL is missing", + error + ); + } + if (!context.mediaRedirectOrigins.includes(url.origin)) { + throw invalidProviderSchema( + context, + "YouTube media URL is outside the configured redirect origins" + ); + } + await resolveProviderUrl(context, value, signal); +} + function responseTooLarge( context: YouTubeProviderRequestContext ): YouTubeProviderError { @@ -391,6 +445,7 @@ function requestError( ); } +/** Keep validation, fetching, and body reads within one classified request deadline. */ async function withProviderResponse( context: YouTubeProviderRequestContext, url: URL | string, @@ -401,18 +456,44 @@ async function withProviderResponse( ): Promise { const requestSignal = createRequestSignal(signal, context.timeoutMs); try { + const followMediaRedirects = + media && context.mediaRedirectOrigins.length > 0; + if (followMediaRedirects) { + await validateMediaRedirectUrl( + context, + String(url), + requestSignal.signal + ); + } const { fetchImpl } = context; const response = await fetchImpl(url, { cache: "no-store", credentials: "omit", referrerPolicy: "no-referrer", ...init, - redirect: "error", + redirect: followMediaRedirects ? "follow" : "error", signal: requestSignal.signal, }); + if (followMediaRedirects) { + try { + await validateMediaRedirectUrl( + context, + response.url, + requestSignal.signal + ); + } catch (error) { + await response.body?.cancel().catch(() => undefined); + throw error; + } + } return await read(response); } catch (error) { - if (error instanceof YouTubeProviderError) { + // DNS validation can wrap an interrupted resolver as an invalid schema. + // Cancellation/timeout still owns the request's recovery classification. + if ( + error instanceof YouTubeProviderError && + !requestSignal.signal.aborted + ) { throw error; } throw requestError(context, requestSignal, signal, error, media); @@ -539,16 +620,17 @@ export function providerOperationError( }); } +/** Probe a ranged audio body and return its verified URL for browser playback. */ export async function verifyProviderMedia( context: YouTubeProviderRequestContext, url: string, signal?: AbortSignal -): Promise { +): Promise { if (!context.verifyMedia) { - return; + return url; } - await withProviderResponse( + const responseUrl = await withProviderResponse( context, url, signal, @@ -616,6 +698,7 @@ export async function verifyProviderMedia( "YouTube media proxy returned an empty response" ); } + return response.url; } finally { if (reader) { await reader.cancel().catch(() => undefined); @@ -626,4 +709,5 @@ export async function verifyProviderMedia( } } ); + return context.mediaRedirectOrigins.length > 0 ? responseUrl : url; }