Skip to content

Commit

Permalink
fix: prevent multiple remappings from applying the .proxy path twice
Browse files Browse the repository at this point in the history
  • Loading branch information
matthova committed Sep 8, 2024
1 parent ec0e0b6 commit 02b54b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/utils/__tests__/matchAndRewriteRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ describe('matchAndRewriteURL', () => {
}
});

it('matches base even if remapped twice', () => {
const TEST_CASES: Array<MatchAndRewriteURLInputs & {result: string}> = [
{
originalURL: new URL('https://discord.com'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/.proxy/',
},
{
originalURL: new URL('wss://discord.com'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'wss://123456789012345678.discordsays.com/.proxy/',
},
];
for (const {result, ...rest} of TEST_CASES) {
const resultURL = matchAndRewriteURL(rest);
if (!(resultURL instanceof URL)) {
throw new Error('URL expected');
}
const result2URL = matchAndRewriteURL({
...rest,
originalURL: resultURL,
});
expect(result2URL?.toString()).toEqual(result);
}
});

it('matches non-base paths and doesnt mangle rest of path', () => {
const TEST_CASES: Array<MatchAndRewriteURLInputs & {result: string}> = [
{
Expand Down
5 changes: 2 additions & 3 deletions src/utils/patchUrlMappings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {absoluteURL, matchAndRewriteURL} from './url';
import {absoluteURL, matchAndRewriteURL, PROXY_PREFIX} from './url';

export interface Mapping {
prefix: string;
Expand All @@ -16,8 +16,6 @@ interface PatchUrlMappingsConfig {
patchSrcAttributes?: boolean;
}

const PROXY_PREFIX = '/.proxy';

export function patchUrlMappings(
mappings: Mapping[],
{patchFetch = true, patchWebSocket = true, patchXhr = true, patchSrcAttributes = false}: PatchUrlMappingsConfig = {},
Expand Down Expand Up @@ -162,6 +160,7 @@ export function attemptRemap({url, mappings}: RemapInput): URL {
const newURL = new URL(url.toString());
if (
(newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) &&
// Only apply proxy prefix once
!newURL.pathname.startsWith(PROXY_PREFIX)
) {
newURL.pathname = PROXY_PREFIX + newURL.pathname;
Expand Down
9 changes: 7 additions & 2 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const PROXY_PREFIX = '/.proxy';

/**
* Creates a regular expression from a target string. The target string
* may contain `{name}` tokens which will end up being translated to
Expand Down Expand Up @@ -48,8 +50,11 @@ export function matchAndRewriteURL({originalURL, prefix, prefixHost, target}: Ma
// Append the original path
newURL.pathname += newURL.pathname === '/' ? originalURL.pathname.slice(1) : originalURL.pathname;
// prepend /.proxy/ to path if using discord activities proxy
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;
}
// Remove the target's path from the new url path
newURL.pathname = newURL.pathname.replace(targetURL.pathname, '');
Expand Down

0 comments on commit 02b54b7

Please sign in to comment.