Unverified Commit cb882b7e authored by Damien Simonin Feugas's avatar Damien Simonin Feugas Committed by GitHub

fix: hardcoded protocol in request url (#37925)

* fix: hardcoded protocol in request url

* test: covers custom server using https

* Apply suggestions from code review

* fix test
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent f6ee4c06
...@@ -12,6 +12,7 @@ import type { ParsedUrlQuery } from 'querystring' ...@@ -12,6 +12,7 @@ import type { ParsedUrlQuery } from 'querystring'
import type { RenderOpts, RenderOptsPartial } from './render' import type { RenderOpts, RenderOptsPartial } from './render'
import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache' import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache'
import type { UrlWithParsedQuery } from 'url' import type { UrlWithParsedQuery } from 'url'
import type { TLSSocket } from 'tls'
import { import {
CacheFs, CacheFs,
NormalizeError, NormalizeError,
...@@ -21,6 +22,7 @@ import { ...@@ -21,6 +22,7 @@ import {
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http' import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { NodeNextRequest } from './base-http/node'
import type { PayloadOptions } from './send-payload' import type { PayloadOptions } from './send-payload'
import { join, resolve } from '../shared/lib/isomorphic/path' import { join, resolve } from '../shared/lib/isomorphic/path'
...@@ -407,14 +409,21 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -407,14 +409,21 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl.query = parseQs(parsedUrl.query) parsedUrl.query = parseQs(parsedUrl.query)
} }
const protocol = (
(req as NodeNextRequest).originalRequest?.socket as TLSSocket
)?.encrypted
? 'https'
: 'http'
// When there are hostname and port we build an absolute URL // When there are hostname and port we build an absolute URL
const initUrl = const initUrl =
this.hostname && this.port this.hostname && this.port
? `http://${this.hostname}:${this.port}${req.url}` ? `${protocol}://${this.hostname}:${this.port}${req.url}`
: req.url : req.url
addRequestMeta(req, '__NEXT_INIT_URL', initUrl) addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query }) addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })
addRequestMeta(req, '_protocol', protocol)
const domainLocale = detectDomainLocale( const domainLocale = detectDomainLocale(
this.nextConfig.i18n?.domains, this.nextConfig.i18n?.domains,
......
...@@ -1166,9 +1166,12 @@ export default class NextNodeServer extends BaseServer { ...@@ -1166,9 +1166,12 @@ export default class NextNodeServer extends BaseServer {
// For middleware to "fetch" we must always provide an absolute URL // For middleware to "fetch" we must always provide an absolute URL
const query = urlQueryToSearchParams(params.parsed.query).toString() const query = urlQueryToSearchParams(params.parsed.query).toString()
const locale = params.parsed.query.__nextLocale const locale = params.parsed.query.__nextLocale
const url = `http://${this.hostname}:${this.port}${
locale ? `/${locale}` : '' const url = `${getRequestMeta(params.request, '_protocol')}://${
}${params.parsed.pathname}${query ? `?${query}` : ''}` this.hostname
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
query ? `?${query}` : ''
}`
if (!url.startsWith('http')) { if (!url.startsWith('http')) {
throw new Error( throw new Error(
...@@ -1544,6 +1547,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1544,6 +1547,7 @@ export default class NextNodeServer extends BaseServer {
} }
const nodeReq = params.req as NodeNextRequest const nodeReq = params.req as NodeNextRequest
const result = await run({ const result = await run({
name: middlewareInfo.name, name: middlewareInfo.name,
paths: middlewareInfo.paths, paths: middlewareInfo.paths,
......
...@@ -19,6 +19,7 @@ export interface RequestMeta { ...@@ -19,6 +19,7 @@ export interface RequestMeta {
_nextDidRewrite?: boolean _nextDidRewrite?: boolean
_nextHadBasePath?: boolean _nextHadBasePath?: boolean
_nextRewroteUrl?: string _nextRewroteUrl?: string
_protocol?: string
} }
export function getRequestMeta( export function getRequestMeta(
......
import { NextResponse } from 'next/server'
export default function middleware(request) {
const response = NextResponse.next()
response.headers.append('x-original-url', request.url)
return response
}
export const config = {
matcher: '/middleware-augmented',
}
export default function Index() {
return <h1>You passed the middleware!</h1>
}
...@@ -2,18 +2,27 @@ if (process.env.POLYFILL_FETCH) { ...@@ -2,18 +2,27 @@ if (process.env.POLYFILL_FETCH) {
global.fetch = require('node-fetch').default global.fetch = require('node-fetch').default
} }
const http = require('http') const { readFileSync } = require('fs')
const next = require('next') const next = require('next')
const { join } = require('path')
const dev = process.env.NODE_ENV !== 'production' const dev = process.env.NODE_ENV !== 'production'
const dir = __dirname const dir = __dirname
const port = process.env.PORT || 3000 const port = process.env.PORT || 3000
const { createServer } = require(process.env.USE_HTTPS === 'true'
? 'https'
: 'http')
const app = next({ dev, dir }) const app = next({ dev, hostname: 'localhost', port, dir })
const handleNextRequests = app.getRequestHandler() const handleNextRequests = app.getRequestHandler()
const httpOptions = {
key: readFileSync(join(__dirname, 'ssh/privkey.pem')),
cert: readFileSync(join(__dirname, 'ssh/cert.pem')),
}
app.prepare().then(() => { app.prepare().then(() => {
const server = new http.Server(async (req, res) => { const server = createServer(httpOptions, async (req, res) => {
if (req.url === '/no-query') { if (req.url === '/no-query') {
return app.render(req, res, '/no-query') return app.render(req, res, '/no-query')
} }
......
-----BEGIN CERTIFICATE-----
MIIDOzCCAiMCCQDZakQbo5eJ3TANBgkqhkiG9w0BAQUFADBnMQswCQYDVQQGEwJV
UzENMAsGA1UECAwEVXRhaDEOMAwGA1UEBwwFUHJvdm8xIzAhBgNVBAoMGkFDTUUg
U2lnbmluZyBBdXRob3JpdHkgSW5jMRQwEgYDVQQDDAtleGFtcGxlLmNvbTAeFw0y
MjA2MjMwODEyMTRaFw0zNjAzMDEwODEyMTRaMFgxCzAJBgNVBAYTAlVTMQ0wCwYD
VQQIDARVdGFoMQ4wDAYDVQQHDAVQcm92bzEWMBQGA1UECgwNQUNNRSBUZWNoIElu
YzESMBAGA1UEAwwJbG9j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HR96ocgTMhrkK2ojO6CXOYYlrISlQfwnLflB3Ki
xyLqu2SMYL9It4Gqk2zmQgwx1gRVGZuMfX8gwfIWq1oGrbMmgl5z48j9dLD2+m4M
KeUCsV11ze6aOf07KAdjty33b/qeVuO+j9G7nO8kcd334yFhzF+bE9afAoGBANsQ
PaPXov5iVg8KKuJeAzoZJDmCt7kw1vAzjfZT1Mql4E8a8gf9z9q+pgJLoIKf5Kkd
8FjlSU1atC7AWZEJUPdJj5wFxan85fw37+RhEAaMvzGwwIZT5YK5pOdBMFQVhEnP
ZTvSXzBsefWdr4mlj5uFTB1LcyK1zRAg/WJKxKXzAoGBAO1tPfRC8SKJPQig+60X
26V20Nt+5MhkQ85P8m6VcoBT+/YTnV2URMgzuImjUaiHMaPehyiLMc9czyB1g0Qa
B/ZHL4wQozVMpoj4P8VlLR3DLdRd/0hatt7sJOsZdcTDf15Qp7JJmxGHIZE7vzaI
BCuH/FODoSjDVYuNHNWe81eNAoGAYr5WWpUntmzTB+J4ZCMmZzjP+FBwyjw2ln1W
n1M4VsQgcOffczS4TYykPeKjDl5ObuC85wUrXn/lOjGf7d16Lo1s+1IqwC3r6R25
t4L8+0bUI+zdS29jw+I0b70VsMEmKTMgGbABEk3DR7B1YKFNMzgJkCbjViEGtx93
t6emLnsCgYBQSpK+24/VmIDPqneilQar7bT1Nb+d+XG5Ef0sAEMTY8+LrAfiCDkJ
iBjl7gEZ9GbAkE3lUJoejMvOHX07ahq8viet1EpCpj9cw9RZ5o95cGJECQm6eaye
yzc5U154yq7k72RJYxHdEFH/NrwANjRs7RqTAhgW8grwBjY8gVpY3Q==
-----END RSA PRIVATE KEY-----
...@@ -4,6 +4,7 @@ import webdriver from 'next-webdriver' ...@@ -4,6 +4,7 @@ import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import getPort from 'get-port' import getPort from 'get-port'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import https from 'https'
import { import {
initNextServerScript, initNextServerScript,
killApp, killApp,
...@@ -22,53 +23,79 @@ let server ...@@ -22,53 +23,79 @@ let server
const context = {} const context = {}
const startServer = async (optEnv = {}, opts) => { describe.each([
const scriptPath = join(appDir, 'server.js') { title: 'using HTTP', useHttps: false },
context.appPort = appPort = await getPort() { title: 'using HTTPS', useHttps: true },
const env = Object.assign( ])('Custom Server $title', ({ useHttps }) => {
{ ...process.env }, let nextUrl
{ PORT: `${appPort}`, __NEXT_TEST_MODE: 'true' },
optEnv const agent = useHttps
) ? new https.Agent({
rejectUnauthorized: false,
server = await initNextServerScript( })
scriptPath, : undefined
/ready on/i,
env, const startServer = async (optEnv = {}, opts) => {
/ReferenceError: options is not defined/, const scriptPath = join(appDir, 'server.js')
opts context.appPort = appPort = await getPort()
) nextUrl = `http${useHttps ? 's' : ''}://127.0.0.1:${context.appPort}`
}
const env = Object.assign(
describe('Custom Server', () => { { ...process.env },
{ PORT: `${appPort}`, __NEXT_TEST_MODE: 'true', USE_HTTPS: useHttps },
optEnv
)
server = await initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/,
opts
)
}
describe('with dynamic assetPrefix', () => { describe('with dynamic assetPrefix', () => {
beforeAll(() => startServer()) beforeAll(() => startServer())
afterAll(() => killApp(server)) afterAll(() => killApp(server))
it('should serve internal file from render', async () => { it('should serve internal file from render', async () => {
const data = await renderViaHTTP(appPort, '/static/hello.txt') const data = await renderViaHTTP(
nextUrl,
'/static/hello.txt',
undefined,
{ agent }
)
expect(data).toMatch(/hello world/) expect(data).toMatch(/hello world/)
}) })
it('should handle render with undefined query', async () => { it('should handle render with undefined query', async () => {
expect(await renderViaHTTP(appPort, '/no-query')).toMatch(/"query":/) expect(
await renderViaHTTP(nextUrl, '/no-query', undefined, { agent })
).toMatch(/"query":/)
}) })
it('should set the assetPrefix dynamically', async () => { it('should set the assetPrefix dynamically', async () => {
const normalUsage = await renderViaHTTP(appPort, '/asset') const normalUsage = await renderViaHTTP(nextUrl, '/asset', undefined, {
agent,
})
expect(normalUsage).not.toMatch(/127\.0\.0\.1/) expect(normalUsage).not.toMatch(/127\.0\.0\.1/)
const dynamicUsage = await renderViaHTTP( const dynamicUsage = await renderViaHTTP(
appPort, nextUrl,
'/asset?setAssetPrefix=1' '/asset?setAssetPrefix=1',
undefined,
{ agent }
) )
expect(dynamicUsage).toMatch(/127\.0\.0\.1/) expect(dynamicUsage).toMatch(/127\.0\.0\.1/)
}) })
it('should handle null assetPrefix accordingly', async () => { it('should handle null assetPrefix accordingly', async () => {
const normalUsage = await renderViaHTTP( const normalUsage = await renderViaHTTP(
appPort, nextUrl,
'/asset?setEmptyAssetPrefix=1' '/asset?setEmptyAssetPrefix=1',
undefined,
{ agent }
) )
expect(normalUsage).toMatch(/"\/_next/) expect(normalUsage).toMatch(/"\/_next/)
}) })
...@@ -76,8 +103,10 @@ describe('Custom Server', () => { ...@@ -76,8 +103,10 @@ describe('Custom Server', () => {
it('should set the assetPrefix to a given request', async () => { it('should set the assetPrefix to a given request', async () => {
for (let lc = 0; lc < 1000; lc++) { for (let lc = 0; lc < 1000; lc++) {
const [normalUsage, dynamicUsage] = await Promise.all([ const [normalUsage, dynamicUsage] = await Promise.all([
await renderViaHTTP(appPort, '/asset'), await renderViaHTTP(nextUrl, '/asset', undefined, { agent }),
await renderViaHTTP(appPort, '/asset?setAssetPrefix=1'), await renderViaHTTP(nextUrl, '/asset?setAssetPrefix=1', undefined, {
agent,
}),
]) ])
expect(normalUsage).not.toMatch(/127\.0\.0\.1/) expect(normalUsage).not.toMatch(/127\.0\.0\.1/)
...@@ -86,12 +115,14 @@ describe('Custom Server', () => { ...@@ -86,12 +115,14 @@ describe('Custom Server', () => {
}) })
it('should render nested index', async () => { it('should render nested index', async () => {
const html = await renderViaHTTP(appPort, '/dashboard') const html = await renderViaHTTP(nextUrl, '/dashboard', undefined, {
agent,
})
expect(html).toMatch(/made it to dashboard/) expect(html).toMatch(/made it to dashboard/)
}) })
it('should contain customServer in NEXT_DATA', async () => { it('should contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(nextUrl, '/', undefined, { agent })
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true) expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true)
}) })
...@@ -105,7 +136,7 @@ describe('Custom Server', () => { ...@@ -105,7 +136,7 @@ describe('Custom Server', () => {
afterAll(() => killApp(server)) afterAll(() => killApp(server))
it('response includes etag header', async () => { it('response includes etag header', async () => {
const response = await fetchViaHTTP(appPort, '/') const response = await fetchViaHTTP(nextUrl, '/', undefined, { agent })
expect(response.headers.get('etag')).toBeTruthy() expect(response.headers.get('etag')).toBeTruthy()
}) })
}) })
...@@ -115,45 +146,48 @@ describe('Custom Server', () => { ...@@ -115,45 +146,48 @@ describe('Custom Server', () => {
afterAll(() => killApp(server)) afterAll(() => killApp(server))
it('response does not include etag header', async () => { it('response does not include etag header', async () => {
const response = await fetchViaHTTP(appPort, '/') const response = await fetchViaHTTP(nextUrl, '/', undefined, { agent })
expect(response.headers.get('etag')).toBeNull() expect(response.headers.get('etag')).toBeNull()
}) })
}) })
describe('HMR with custom server', () => { // playwright fails with SSL error due to self-signed cert
beforeAll(() => startServer()) if (!useHttps) {
afterAll(() => { describe('HMR with custom server', () => {
killApp(server) beforeAll(() => startServer())
indexPg.restore() afterAll(() => {
}) killApp(server)
indexPg.restore()
it('Should support HMR when rendering with /index pathname', async () => { })
let browser
try { it('Should support HMR when rendering with /index pathname', async () => {
browser = await webdriver(context.appPort, '/test-index-hmr') let browser
const text = await browser.elementByCss('#go-asset').text() try {
const logs = await browser.log() browser = await webdriver(nextUrl, '/test-index-hmr')
expect(text).toBe('Asset') const text = await browser.elementByCss('#go-asset').text()
const logs = await browser.log()
// Hydrates with react 18 is correct as expected expect(text).toBe('Asset')
expect(
logs.some((log) => // Hydrates with react 18 is correct as expected
log.message.includes( expect(
'ReactDOM.hydrate is no longer supported in React 18' logs.some((log) =>
log.message.includes(
'ReactDOM.hydrate is no longer supported in React 18'
)
) )
) ).toBe(false)
).toBe(false)
indexPg.replace('Asset', 'Asset!!') indexPg.replace('Asset', 'Asset!!')
await check(() => browser.elementByCss('#go-asset').text(), /Asset!!/) await check(() => browser.elementByCss('#go-asset').text(), /Asset!!/)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
}
} }
} })
}) })
}) }
describe('Error when rendering without starting slash', () => { describe('Error when rendering without starting slash', () => {
afterEach(() => killApp(server)) afterEach(() => killApp(server))
...@@ -168,7 +202,9 @@ describe('Custom Server', () => { ...@@ -168,7 +202,9 @@ describe('Custom Server', () => {
}, },
} }
) )
const html = await renderViaHTTP(appPort, '/no-slash') const html = await renderViaHTTP(nextUrl, '/no-slash', undefined, {
agent,
})
expect(html).toContain('made it to dashboard') expect(html).toContain('made it to dashboard')
expect(stderr).toContain('Cannot render page with path "dashboard"') expect(stderr).toContain('Cannot render page with path "dashboard"')
}) })
...@@ -188,7 +224,9 @@ describe('Custom Server', () => { ...@@ -188,7 +224,9 @@ describe('Custom Server', () => {
} }
) )
const html = await renderViaHTTP(appPort, '/no-slash') const html = await renderViaHTTP(nextUrl, '/no-slash', undefined, {
agent,
})
expect(html).toContain('made it to dashboard') expect(html).toContain('made it to dashboard')
expect(stderr).toContain('Cannot render page with path "dashboard"') expect(stderr).toContain('Cannot render page with path "dashboard"')
}) })
...@@ -201,7 +239,9 @@ describe('Custom Server', () => { ...@@ -201,7 +239,9 @@ describe('Custom Server', () => {
it.each(['/', '/no-query'])( it.each(['/', '/no-query'])(
'should handle compression for route %s', 'should handle compression for route %s',
async (route) => { async (route) => {
const response = await fetchViaHTTP(appPort, route) const response = await fetchViaHTTP(nextUrl, route, undefined, {
agent,
})
expect(response.headers.get('Content-Encoding')).toBe('gzip') expect(response.headers.get('Content-Encoding')).toBe('gzip')
} }
) )
...@@ -212,7 +252,12 @@ describe('Custom Server', () => { ...@@ -212,7 +252,12 @@ describe('Custom Server', () => {
afterAll(() => killApp(server)) afterAll(() => killApp(server))
it('should serve internal file from render', async () => { it('should serve internal file from render', async () => {
const data = await renderViaHTTP(appPort, '/static/hello.txt') const data = await renderViaHTTP(
nextUrl,
'/static/hello.txt',
undefined,
{ agent }
)
expect(data).toMatch(/hello world/) expect(data).toMatch(/hello world/)
}) })
}) })
...@@ -230,12 +275,25 @@ describe('Custom Server', () => { ...@@ -230,12 +275,25 @@ describe('Custom Server', () => {
}, },
} }
) )
await fetchViaHTTP(appPort, '/unhandled-rejection') await fetchViaHTTP(nextUrl, '/unhandled-rejection', undefined, { agent })
await check(() => stderr, /unhandledRejection/) await check(() => stderr, /unhandledRejection/)
expect(stderr).toContain( expect(stderr).toContain(
'error - unhandledRejection: Error: unhandled rejection' 'error - unhandledRejection: Error: unhandled rejection'
) )
expect(stderr).toContain('server.js:22:22') expect(stderr).toContain('server.js:31:22')
})
})
describe('with middleware $title', () => {
beforeAll(() => startServer(undefined, undefined, useHttps))
afterAll(() => killApp(server))
it('should read the expected url protocol in middleware', async () => {
const path = '/middleware-augmented'
const response = await fetchViaHTTP(nextUrl, path, undefined, { agent })
expect(response.headers.get('x-original-url')).toBe(
`${useHttps ? 'https' : 'http'}://localhost:${appPort}${path}`
)
}) })
}) })
}) })
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