Unverified Commit 9952cc79 authored by Matt Kane's avatar Matt Kane Committed by GitHub

Don't proxy middleware if host is the same (#31180)

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`
parent d1adf1d5
...@@ -1241,7 +1241,12 @@ export default class Server { ...@@ -1241,7 +1241,12 @@ export default class Server {
query: parsedUrl.query, 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) return proxyRequest(req, res, parsedDestination)
} }
......
...@@ -156,6 +156,16 @@ function rewriteTests(locale = '') { ...@@ -156,6 +156,16 @@ function rewriteTests(locale = '') {
expect($('.title').text()).toBe('About Page') 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 () => { it(`${locale} should rewrite to Vercel`, async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
context.appPort, context.appPort,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment