Unverified Commit 39c4783f authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Ensure exotic rewrite param values are properly encoded (#96010)

https://github.com/vercel/next.js/security/advisories/GHSA-p9j2-gv94-2wf4
parent 0be95ebf
......@@ -1441,5 +1441,6 @@
"1440": "Route \"%s\": Next.js encountered uncached data during prerendering.\\n\\n\\`fetch(...)\\` or \\`connection()\\` accessed outside of \\`<Suspense>\\` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.\\n\\nWays to fix this:\\n - [stream] Provide a placeholder with \\`<Suspense fallback={...}>\\` around the data access\\n - [cache] Cache the data access with \\`\"use cache\"\\` (does not apply to \\`connection()\\`)\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic",
"1441": "DevValidationScheduler requires at least one active validation",
"1442": "The Server Reference ID did not match the expected format. Received %s.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"1443": "Unsupported body type: %s"
"1443": "Unsupported body type: %s",
"1444": "Invalid target hostname \"%s\" for proxy request, expected \"%s\""
}
......@@ -19,6 +19,10 @@ export async function proxyRequest(
parsedUrl.search = stringifyQuery(req as any, query)
const target = url.format(parsedUrl)
// Keep in mind that a WHATWG URL's hostname and the parsedUrl's hostname
// are not strictly equal due to lowercasing, IDN translation, IPv4 and IPv6 normalization, etc.
// We just make sure this is a valid URL since http-proxy doesn't validate.
new URL(target)
const HttpProxy =
require('next/dist/compiled/http-proxy') as typeof import('next/dist/compiled/http-proxy')
......
......@@ -264,7 +264,10 @@ export function prepareDestination(args: {
let destHostnameCompiler
if (destHostname) {
destHostnameCompiler = safeCompile(destHostname, { validate: false })
destHostnameCompiler = safeCompile(destHostname, {
validate: false,
encode: encodeURIComponent,
})
}
// update any params in query values
......
......@@ -1203,6 +1203,11 @@ export async function ncc_gzip_size(task, opts) {
.target('src/compiled/gzip-size')
}
externals['http-proxy'] = 'next/dist/compiled/http-proxy'
// The patched http-proxy (patches/http-proxy@1.18.1.patch) uses Next.js'
// parseUrl instead of the deprecated url.parse. Keep that import pointing at
// next's own dist instead of bundling a copy.
externals['next/dist/shared/lib/router/utils/parse-url'] =
'next/dist/shared/lib/router/utils/parse-url'
export async function ncc_http_proxy(task, opts) {
await task
.source(relative(__dirname, require.resolve('http-proxy')))
......
diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js
index 6513e81d80d5250ea249ea833f819ece67897c7e..09143dd1fe4e67885f40ea916a6ea1ef3e3afa19 100644
index 6513e81d80d5250ea249ea833f819ece67897c7e..b839d0a0cc69674f3d53c1e5eec07ea8cc7c4796 100644
--- a/lib/http-proxy/common.js
+++ b/lib/http-proxy/common.js
@@ -1,9 +1,9 @@
@@ -1,15 +1,35 @@
var common = exports,
url = require('url'),
- url = require('url'),
- extend = require('util')._extend,
+ parseUrl = require('next/dist/shared/lib/router/utils/parse-url').parseUrl,
required = require('requires-port');
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
......@@ -13,7 +14,33 @@ index 6513e81d80d5250ea249ea833f819ece67897c7e..09143dd1fe4e67885f40ea916a6ea1ef
isSSL = /^https|wss/;
/**
@@ -40,10 +40,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
* Simple Regex for testing if protocol is https
*/
common.isSSL = isSSL;
+
+/**
+ * Parses a url with Next.js' WHATWG-based `parseUrl` and adapts the result to
+ * the legacy `url.parse` shape http-proxy relies on (`host`, `path`, and a
+ * bracket-less `hostname` for IPv6).
+ */
+common.parseUrl = function(urlValue) {
+ var parsed = parseUrl(urlValue);
+ var bracketedHostname = parsed.hostname;
+ return Object.assign(parsed, {
+ host: bracketedHostname
+ ? bracketedHostname + (parsed.port ? ':' + parsed.port : '')
+ : bracketedHostname,
+ hostname:
+ bracketedHostname && bracketedHostname.startsWith('[')
+ ? bracketedHostname.slice(1, -1)
+ : bracketedHostname,
+ path: parsed.pathname + parsed.search,
+ });
+};
/**
* Copies the right headers from `options` and `req` to
* `outgoing` which is then used to fire the proxied
@@ -40,10 +60,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
);
outgoing.method = options.method || req.method;
......@@ -26,7 +53,7 @@ index 6513e81d80d5250ea249ea833f819ece67897c7e..09143dd1fe4e67885f40ea916a6ea1ef
}
if (options.auth) {
@@ -61,13 +61,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
@@ -61,13 +81,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.agent = options.agent || false;
outgoing.localAddress = options.localAddress;
......@@ -50,16 +77,31 @@ index 6513e81d80d5250ea249ea833f819ece67897c7e..09143dd1fe4e67885f40ea916a6ea1ef
if (typeof outgoing.headers.connection !== 'string'
|| !upgradeHeader.test(outgoing.headers.connection)
) { outgoing.headers.connection = 'close'; }
@@ -80,11 +109,8 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
? (target.path || '')
: '';
- //
- // Remark: Can we somehow not use url.parse as a perf optimization?
- //
var outgoingPath = !options.toProxy
- ? (url.parse(req.url).path || '')
+ ? (common.parseUrl(req.url).path || '')
: req.url;
//
diff --git a/lib/http-proxy/index.js b/lib/http-proxy/index.js
index 977a4b3622b9eaac27689f06347ea4c5173a96cd..88b2d0fcfa03c3aafa47c7e6d38e64412c45a7cc 100644
index 977a4b3622b9eaac27689f06347ea4c5173a96cd..9d94a5198155fbb517a243d6f0fd5e115c77daff 100644
--- a/lib/http-proxy/index.js
+++ b/lib/http-proxy/index.js
@@ -1,5 +1,4 @@
@@ -1,6 +1,5 @@
var httpProxy = module.exports,
- extend = require('util')._extend,
parse_url = require('url').parse,
- parse_url = require('url').parse,
+ parse_url = require('./common').parseUrl,
EE3 = require('eventemitter3'),
http = require('http'),
https = require('https'),
@@ -47,9 +46,9 @@ function createRightProxy(type) {
args[cntr] !== res
) {
......@@ -88,3 +130,27 @@ index 7ae735514190eea569c605fff7d27c045fe8d601..c7c25e7228b21c76b3c7115af82ddcbf
}
},
diff --git a/lib/http-proxy/passes/web-outgoing.js b/lib/http-proxy/passes/web-outgoing.js
index 46352f6e377affc701773328d528bd7e4352f242..cabd340c7d66c6a5d464262d88c698d2781697b3 100644
--- a/lib/http-proxy/passes/web-outgoing.js
+++ b/lib/http-proxy/passes/web-outgoing.js
@@ -51,8 +51,8 @@ module.exports = { // <--
if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
&& proxyRes.headers['location']
&& redirectRegex.test(proxyRes.statusCode)) {
- var target = url.parse(options.target);
- var u = url.parse(proxyRes.headers['location']);
+ var target = common.parseUrl(options.target);
+ var u = common.parseUrl(proxyRes.headers['location']);
// make sure the redirected host matches the target host before rewriting
if (target.host != u.host) {
@@ -68,7 +68,7 @@ module.exports = { // <--
u.protocol = options.protocolRewrite;
}
- proxyRes.headers['location'] = u.format();
+ proxyRes.headers['location'] = url.format(u);
}
},
/**
......@@ -38,7 +38,7 @@ patchedDependencies:
hash: cb53bfa5effb15b3a361e176003df667cadf757b27b7c9b458c2f0fce86ea893
path: patches/@vercel__blob.patch
http-proxy@1.18.1:
hash: 3aac1c597fabd03f9fb4e3fd6e689b87012758d4fed06f296541f9dc3b8d7b39
hash: 34b797595c63312eb8cb6e01f4b521bf5eea00315c5e48a31c3d89d866efd50f
path: patches/http-proxy@1.18.1.patch
minizlib@3.1.0:
hash: 587688821244aaee46680929cd869947377a284ac1b30c9ae8660a5ea7d2be67
......@@ -392,7 +392,7 @@ importers:
version: 5.1.18
http-proxy:
specifier: 1.18.1
version: 1.18.1(patch_hash=3aac1c597fabd03f9fb4e3fd6e689b87012758d4fed06f296541f9dc3b8d7b39)
version: 1.18.1(patch_hash=34b797595c63312eb8cb6e01f4b521bf5eea00315c5e48a31c3d89d866efd50f)
husky:
specifier: 9.0.11
version: 9.0.11
......@@ -1494,7 +1494,7 @@ importers:
version: 5.1.1
http-proxy:
specifier: 1.18.1
version: 1.18.1(patch_hash=3aac1c597fabd03f9fb4e3fd6e689b87012758d4fed06f296541f9dc3b8d7b39)
version: 1.18.1(patch_hash=34b797595c63312eb8cb6e01f4b521bf5eea00315c5e48a31c3d89d866efd50f)
http-proxy-agent:
specifier: 5.0.0
version: 5.0.0
......@@ -28862,7 +28862,7 @@ snapshots:
http-proxy-middleware@2.0.7(@types/express@4.17.21):
dependencies:
'@types/http-proxy': 1.17.17
http-proxy: 1.18.1(patch_hash=3aac1c597fabd03f9fb4e3fd6e689b87012758d4fed06f296541f9dc3b8d7b39)
http-proxy: 1.18.1(patch_hash=34b797595c63312eb8cb6e01f4b521bf5eea00315c5e48a31c3d89d866efd50f)
is-glob: 4.0.3
is-plain-obj: 3.0.0
micromatch: 4.0.8
......@@ -28871,7 +28871,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
http-proxy@1.18.1(patch_hash=3aac1c597fabd03f9fb4e3fd6e689b87012758d4fed06f296541f9dc3b8d7b39):
http-proxy@1.18.1(patch_hash=34b797595c63312eb8cb6e01f4b521bf5eea00315c5e48a31c3d89d866efd50f):
dependencies:
eventemitter3: 4.0.7
follow-redirects: 1.9.0
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<>
<pre>
<code>curl http://localhost:3000/rewrite-idn-case-unicode</code>
</pre>
</>
)
}
/** @type {import('next').NextConfig} */
const config = {
rewrites() {
return [
{
source: '/rewrite-idn-case-unicode',
destination: `http://你好.localhost:${process.env.TEST_TARGET_PORT}`,
},
]
},
}
module.exports = config
import { nextTestSetup } from 'e2e-utils'
import { findPort } from 'next-test-utils'
import createTargetServer from './target-server'
describe('rewrites hostname', () => {
const { skipped, next } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: true,
})
if (skipped) {
return
}
let targetPort: number | null = null
let closeTargetServer: (() => Promise<void>) | null = null
beforeAll(async () => {
targetPort = await findPort()
const closeServer = await createTargetServer(targetPort)
closeTargetServer = closeServer
await next.start({
env: { TEST_TARGET_PORT: String(targetPort) },
})
})
afterAll(async () => {
await closeTargetServer()
})
it('should navigate to a rewrite using unicode without error', async () => {
const response = await next.fetch(`/rewrite-idn-case-unicode`)
expect(await response.json()).toEqual({
forwardedHost: `localhost:${next.appPort}`,
host: `xn--6qq79v.localhost:${targetPort}`,
})
})
})
// @ts-check
const http = require('http')
const process = require('process')
const { promisify } = require('util')
/**
* @returns {Promise<() => Promise<void>>} A promise that resolves to a function that closes the server
*/
function createServer(targetPort) {
const server = http.createServer((req, res) => {
const headers = req.headers
res.setHeader('Content-Type', 'application/json')
res.end(
JSON.stringify({
forwardedHost: headers['x-forwarded-host'],
host: headers['host'],
})
)
})
return new Promise((resolve, reject) => {
server.listen(targetPort, () => {
console.log(`Server listening on port ${targetPort}`)
resolve(promisify(server.close.bind(server)))
})
})
}
module.exports = createServer
if (require.main === module) {
const targetPort = parseInt(process.env.TEST_TARGET_PORT, 10)
if (Number.isNaN(targetPort)) {
throw new Error('TEST_TARGET_PORT is not set or invalid')
}
createServer(targetPort).catch((err) => {
console.error(err)
process.exit(1)
})
}
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