vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.93k stars 26.98k forks source link

Url rewrites breaks url that contains multiple slashes #53438

Open VictorGlindasPaf opened 1 year ago

VictorGlindasPaf commented 1 year ago

Verify canary release

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/proud-haze-ylj2wz?file=%2Fpackage.json%3A21%2C4

To Reproduce

Go to the codesandbox, and pop open the network console.

Describe the Bug

When rewriting a url that contains multiple slashes, it gets stuck in an infinite loop.

For example /image/https://i.imgur.com/V7WhMQt.jpeg

For a real-life use case example for this, cloudflare image optimization requires us to pass the image source as part of the url.

Screenshot 2023-08-01 at 13 59 06

Expected Behavior

The image should not be redirected infinitely.

NEXT-1488

FelixStridsbergPaf commented 1 year ago

I did some basic debugging. The url above is redirected like this:

/image/https://i.imgur.com/V7WhMQt.jpeg -> /image/https:/i.imgur.com/V7WhMQt.jpeg

It removed the double slash (which looks like by design), but after that it continues redirecting to /image/https:/i.imgur.com/V7WhMQt.jpeg forever.

The redirect loop occurs here: https://github.com/vercel/next.js/blob/12e77ca/packages/next/src/server/base-server.ts#L650-L661

If I visit /http:/foo the req.url at that point has been expanded to /http://foo. So the regex check detects two slashes and trigger a redirect. However, the url did never contain two slashes in the first place, they have been added internally somewhere before that point. So the redirect redirects to the same url over and over.

abdelkrimdjerrah commented 3 months ago

any fixes ?