Skip to content

Commit

Permalink
Don't proxy middleware if host is the same (#31180)
Browse files Browse the repository at this point in the history
This changes the check for whether a rewrite is internal or if it should be proxied. Currently it checks if `protocol` is unset, which is only the case for relative URLs or localhost. This means that requests where there is a hostname set, or if localhost is specified in another way such as `127.0.0.1`, then it will be proxied, which potentially causes a proxy loop or ssl error. This PR changes the test so that it also checks if the hosts match, and only proxies if they are different.

Fixes #31179 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
ascorbic committed Nov 13, 2021
1 parent d1adf1d commit 9952cc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,12 @@ export default class Server {
query: parsedUrl.query,
})

if (parsedDestination.protocol) {
if (
parsedDestination.protocol &&
(parsedDestination.port
? `${parsedDestination.hostname}:${parsedDestination.port}`
: parsedDestination.hostname) !== req.headers.host
) {
return proxyRequest(req, res, parsedDestination)
}

Expand Down
10 changes: 10 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ function rewriteTests(locale = '') {
expect($('.title').text()).toBe('About Page')
})

it(`${locale} should rewrite when not using localhost`, async () => {
const res = await fetchViaHTTP(
`http://localtest.me:${context.appPort}`,
`${locale}/rewrites/rewrite-me-without-hard-navigation`
)
const html = await res.text()
const $ = cheerio.load(html)
expect($('.title').text()).toBe('About Page')
})

it(`${locale} should rewrite to Vercel`, async () => {
const res = await fetchViaHTTP(
context.appPort,
Expand Down

0 comments on commit 9952cc7

Please sign in to comment.