From c564b2dc26ee0f9d687bc22364d2e6677c49eff8 Mon Sep 17 00:00:00 2001 From: Andrew Giel Date: Tue, 30 Jul 2024 10:19:28 -0400 Subject: [PATCH] fix: do not prepend PROXY_PREFIX if already prepends path (#237) --- src/utils/__tests__/matchAndRewriteRoute.test.ts | 14 ++++++++++++++ src/utils/patchUrlMappings.ts | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utils/__tests__/matchAndRewriteRoute.test.ts b/src/utils/__tests__/matchAndRewriteRoute.test.ts index 6c5444c..0d21ea6 100644 --- a/src/utils/__tests__/matchAndRewriteRoute.test.ts +++ b/src/utils/__tests__/matchAndRewriteRoute.test.ts @@ -216,6 +216,20 @@ describe('matchAndRewriteURL', () => { expect(base.toString()).toEqual('https://1234567890.discordsays.com/.proxy/'); }); + it("Doesn't apply /.proxy/ if it already prepends the path", () => { + const noPrepend = attemptRemap({ + url: new URL('https://1234567890.discordsays.com/.proxy/api/token'), + mappings: [], + }); + expect(noPrepend.toString()).toEqual('https://1234567890.discordsays.com/.proxy/api/token'); + + const prepend = attemptRemap({ + url: new URL('https://1234567890.discordsays.com/path/before/.proxy/api/token'), + mappings: [], + }); + expect(prepend.toString()).toEqual('https://1234567890.discordsays.com/.proxy/path/before/.proxy/api/token'); + }); + it("Doesn't apply trailing slash to complete filenames", () => { const prefixHost = '123456789012345678.discordsays.com'; const target = 'domain.com'; diff --git a/src/utils/patchUrlMappings.ts b/src/utils/patchUrlMappings.ts index 27bf135..8cd7978 100644 --- a/src/utils/patchUrlMappings.ts +++ b/src/utils/patchUrlMappings.ts @@ -16,6 +16,8 @@ interface PatchUrlMappingsConfig { patchSrcAttributes?: boolean; } +const PROXY_PREFIX = '/.proxy'; + export function patchUrlMappings( mappings: Mapping[], {patchFetch = true, patchWebSocket = true, patchXhr = true, patchSrcAttributes = false}: PatchUrlMappingsConfig = {}, @@ -132,8 +134,11 @@ function attemptSetNodeSrc(node: Node, mappings: Mapping[]) { export function attemptRemap({url, mappings}: RemapInput): URL { const newURL = new URL(url.toString()); - if (newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) { - newURL.pathname = '/.proxy' + newURL.pathname; + if ( + (newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) && + !newURL.pathname.startsWith(PROXY_PREFIX) + ) { + newURL.pathname = PROXY_PREFIX + newURL.pathname; } for (const mapping of mappings) { const mapped = matchAndRewriteURL({