Files
7744cc91be fix: handle multiple x-forwarded-proto headers (#58824)
### What?
This PR changes how protocol is determined.
A change was recently made (in [PR
57815](https://github.com/vercel/next.js/commit/1caa58087a428666bb14d40dbc159b9a768b075d#diff-c49c4767e6ed8627e6e1b8f96b141ee13246153f5e9142e1da03450c8e81e96fR1744))
that did not take into account cases where there are multiple
`x-forwarded-proto` headers. In such cases, the protocol becomes e.g
"https, https".

### Why?
An error will occur in parseUrl on line 1616, since its not a valid url
(e.g. `https, https://localhost:3000`).

### How?
Reverted part of the changes in [PR
57815](https://github.com/vercel/next.js/pull/57815).

Fixes #58764 and fixes #59031

Closes NEXT-2437

---------

Co-authored-by: Balázs Orbán <[email protected]>
2024-02-14 14:47:26 +01:00

79 lines
2.8 KiB
TypeScript

import { createNextDescribe } from 'e2e-utils'
createNextDescribe('x-forwarded-headers', { files: __dirname }, ({ next }) => {
it('should include x-forwarded-* headers', async () => {
const res = await next.fetch('/')
const headers = await res.json()
const url = new URL(next.url)
expect(headers['x-forwarded-host']).toBe(url.host)
expect(headers['x-forwarded-port']).toBe(url.port)
expect(headers['x-forwarded-proto']).toBe(url.protocol.replace(':', ''))
expect(headers['middleware-x-forwarded-host']).toBe(url.host)
expect(headers['middleware-x-forwarded-port']).toBe(url.port)
expect(headers['middleware-x-forwarded-proto']).toBe(
url.protocol.replace(':', '')
)
})
describe('host header exists', () => {
it('should include x-forwarded-* headers relative to host', async () => {
const url = new URL(next.url)
const reqHeaders = {
host: `subdomain.localhost:${url.port}`,
}
const res = await next.fetch('/', {
headers: reqHeaders,
})
const headers = await res.json()
expect(headers['x-forwarded-host']).toBe(reqHeaders.host)
expect(headers['x-forwarded-port']).toBe(url.port)
expect(headers['x-forwarded-proto']).toBe(url.protocol.replace(':', ''))
expect(headers['middleware-x-forwarded-host']).toBe(reqHeaders.host)
expect(headers['middleware-x-forwarded-port']).toBe(url.port)
expect(headers['middleware-x-forwarded-proto']).toBe(
url.protocol.replace(':', '')
)
})
})
describe('already assigned', () => {
it('should not override existing x-forwarded-* headers', async () => {
const url = new URL(next.url)
const reqHeaders = {
host: `subdomain.localhost:${url.port}`,
port: '1234',
proto: 'https',
}
const res = await next.fetch('/', {
headers: {
host: 'override.localhost',
'x-forwarded-host': reqHeaders.host,
'x-forwarded-port': reqHeaders.port,
'x-forwarded-proto': reqHeaders.proto,
},
})
const headers = await res.json()
expect(headers['x-forwarded-host']).toBe(reqHeaders.host)
expect(headers['x-forwarded-port']).toBe(reqHeaders.port)
expect(headers['x-forwarded-proto']).toBe(reqHeaders.proto)
expect(headers['middleware-x-forwarded-host']).toBe(reqHeaders.host)
expect(headers['middleware-x-forwarded-port']).toBe(reqHeaders.port)
expect(headers['middleware-x-forwarded-proto']).toBe(reqHeaders.proto)
})
it('should work with multiple x-forwarded-* headers', async () => {
const res = await next.fetch('/', {
headers: { 'x-forwarded-proto': 'https, https' },
})
expect(res.status).toBe(200)
const headers = await res.json()
expect(headers['x-forwarded-proto']).toBe('https, https')
})
})
})