Skip to content

Commit

Permalink
Fix infinite redirect when a double slash appears in a URL (#2449)
Browse files Browse the repository at this point in the history
* Fix infinite redirect when a double slash appears in a URL
  • Loading branch information
blittle authored Aug 20, 2024
1 parent 768e505 commit 664a09d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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) || '/';
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: 301,
headers: {
location: url.pathname.replace(/\/+/g, '/'),
},
});
}

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

0 comments on commit 664a09d

Please sign in to comment.