Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not prepend PROXY_PREFIX if already prepends path #237

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/utils/__tests__/matchAndRewriteRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 7 additions & 2 deletions src/utils/patchUrlMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down Expand Up @@ -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({
Expand Down