diff --git a/tests/radio-source-plan.test.mjs b/tests/radio-source-plan.test.mjs index f599b03e..16b96798 100644 --- a/tests/radio-source-plan.test.mjs +++ b/tests/radio-source-plan.test.mjs @@ -203,3 +203,16 @@ test("never forwards OAuth-style stream credentials to a third-party proxy", () ]); assert.equal(result.filter(item => item.proxied).length, 1); }); + + +test("does not re-proxy an already proxied URL with canonical-equivalent host casing or port", () => { + const alreadyProxied = "https://CORS.EXAMPLE:443/https://radio.example/live.mp3"; + const result = buildPlaybackSources([{ src: alreadyProxied }], "https://cors.example/"); + assert.deepEqual(result.map(item => item.src), [alreadyProxied]); + + const otherStream = "https://radio.example/live.mp3"; + assert.deepEqual( + buildPlaybackSources([{ src: otherStream }], "https://cors.example/").map(item => item.src), + [otherStream, "https://cors.example/https://radio.example/live.mp3"], + ); +}); diff --git a/utils/radio-source-plan.mjs b/utils/radio-source-plan.mjs index da9c12dc..69493611 100644 --- a/utils/radio-source-plan.mjs +++ b/utils/radio-source-plan.mjs @@ -41,12 +41,15 @@ export const buildPlaybackSources = (sources, corsServer) => { // Never forward URL-embedded credentials to a third-party CORS proxy. // Preserve the direct candidate; only suppress its proxy fallback. let hasCredentials = false; + let alreadyProxied = false; try { const parsed = new URL(src); // Signed stream URLs (including AWS/GCS provider-specific query fields) must not reach third-party proxies. const sensitiveQuery = [...parsed.searchParams.keys()].some(key => /^(?:x-amz-.+|x-goog-.+|awsaccesskeyid|googleaccessid|token|(?:access|refresh|id|auth|session)[_-]?token|access[_-]?key|api[_-]?key|client[_-]?secret|session[_-]?key|auth(?:orization)?|signature|sig|secret|password|jwt|expires?|policy|key[_-]?pair[_-]?id)$/i.test(key)); hasCredentials = Boolean(parsed.username || parsed.password || sensitiveQuery); + // URL canonicalization normalizes host casing and default ports, unlike a raw string prefix check. + alreadyProxied = Boolean(proxyBase) && parsed.href.startsWith(new URL(proxyBase).href); } catch { /* Invalid URLs are not eligible for proxy fallback. */ hasCredentials = true; } @@ -57,7 +60,7 @@ export const buildPlaybackSources = (sources, corsServer) => { !isHls && !source.proxied && /^https?:\/\//i.test(src) && - !src.startsWith(proxyBase); + !alreadyProxied; if (canProxy) { push({