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 infinite redirect when a double slash appears in a URL #2449

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .changeset/serious-islands-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/remix-oxygen': patch
'@shopify/mini-oxygen': patch
---

Prevent infinite redirects when a double slash exists in the URL
7 changes: 3 additions & 4 deletions packages/mini-oxygen/src/vite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import type {ViteDevServer} from 'vite';
*/
export function toURL(req: string | IncomingMessage = '/', origin?: string) {
const isRequest = typeof req !== 'string';
const pathname = (isRequest ? req.url : req) || '/';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the second argument to new URL, just manually concatenate the origin with the pathname.

let pathname = (isRequest ? req.url : req) || '/';

return new URL(
pathname,
origin ||
(origin ||
(isRequest && req.headers.host && `http://${req.headers.host}`) ||
'http://example.com',
'http://example.com') + pathname,
);
}

Expand Down
11 changes: 11 additions & 0 deletions packages/remix-oxygen/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export function createRequestHandler<Context = unknown>({
});
}

const url = new URL(request.url);

if (url.pathname.includes('//')) {
return new Response(null, {
status: 302,
blittle marked this conversation as resolved.
Show resolved Hide resolved
headers: {
location: url.pathname.replace(/\/+/g, '/'),
},
});
}

const context = getLoadContext
? ((await getLoadContext(request)) as AppLoadContext)
: undefined;
Expand Down
Loading