`undici.request` vulnerable to SSRF using absolute URL on `pathname`
Description
undici is an HTTP/1.1 client, written from scratch for Node.js.undici is vulnerable to SSRF (Server-side Request Forgery) when an application takes in user input into the path/pathname option of undici.request. If a user specifies a URL such as http://127.0.0.1 or //127.0.0.1 ``js const undici = require("undici") undici.request({origin: "http://example.com", pathname: "//127.0.0.1"}) ` Instead of processing the request as http://example.org//127.0.0.1 (or http://example.org/http://127.0.0.1 when http://127.0.0.1 is used), it actually processes the request as http://127.0.0.1/ and sends it to http://127.0.0.1. If a developer passes in user input into path parameter of undici.request, it can result in an _SSRF_ as they will assume that the hostname cannot change, when in actual fact it can change because the specified path parameter is combined with the base URL. This issue was fixed in undici@5.8.1. The best workaround is to validate user input before passing it to the undici.request` call.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
undicinpm | < 5.8.2 | 5.8.2 |
Affected products
1Patches
1124f7ebf7053Merge pull request from GHSA-8qr4-xgw6-wmr3
3 files changed · +171 −4
index.js+6 −1 modified@@ -53,7 +53,12 @@ function makeDispatcher (fn) { throw new InvalidArgumentError('invalid opts.path') } - url = new URL(opts.path, util.parseOrigin(url)) + let path = opts.path + if (!opts.path.startsWith('/')) { + path = `/${path}` + } + + url = new URL(util.parseOrigin(url).origin + path) } else { if (!opts) { opts = typeof url === 'object' ? url : {}
lib/core/util.js+14 −3 modified@@ -108,14 +108,25 @@ function parseURL (url) { const port = url.port != null ? url.port : (url.protocol === 'https:' ? 443 : 80) - const origin = url.origin != null + let origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}` - const path = url.path != null + let path = url.path != null ? url.path : `${url.pathname || ''}${url.search || ''}` - url = new URL(path, origin) + if (origin.endsWith('/')) { + origin = origin.substring(0, origin.length - 1) + } + + if (path && !path.startsWith('/')) { + path = `/${path}` + } + // new URL(path, origin) is unsafe when `path` contains an absolute URL + // From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL: + // If first parameter is a relative URL, second param is required, and will be used as the base URL. + // If first parameter is an absolute URL, a given second param will be ignored. + url = new URL(origin + path) } return url
test/request.js+151 −0 added@@ -0,0 +1,151 @@ +'use strict' + +const { createServer } = require('http') +const { test } = require('tap') +const { request } = require('..') + +test('no-slash/one-slash pathname should be included in req.path', async (t) => { + const pathServer = createServer((req, res) => { + t.fail('it shouldn\'t be called') + res.statusCode = 200 + res.end('hello') + }) + + const requestedServer = createServer((req, res) => { + t.equal(`/localhost:${pathServer.address().port}`, req.url) + t.equal('GET', req.method) + t.equal(`localhost:${requestedServer.address().port}`, req.headers.host) + res.statusCode = 200 + res.end('hello') + }) + + t.teardown(requestedServer.close.bind(requestedServer)) + t.teardown(pathServer.close.bind(pathServer)) + + await Promise.all([ + requestedServer.listen(0), + pathServer.listen(0) + ]) + + const noSlashPathname = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + pathname: `localhost:${pathServer.address().port}` + }) + t.equal(noSlashPathname.statusCode, 200) + const noSlashPath = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + path: `localhost:${pathServer.address().port}` + }) + t.equal(noSlashPath.statusCode, 200) + const noSlashPath2Arg = await request( + `http://localhost:${requestedServer.address().port}`, + { path: `localhost:${pathServer.address().port}` } + ) + t.equal(noSlashPath2Arg.statusCode, 200) + const oneSlashPathname = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + pathname: `/localhost:${pathServer.address().port}` + }) + t.equal(oneSlashPathname.statusCode, 200) + const oneSlashPath = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + path: `/localhost:${pathServer.address().port}` + }) + t.equal(oneSlashPath.statusCode, 200) + const oneSlashPath2Arg = await request( + `http://localhost:${requestedServer.address().port}`, + { path: `/localhost:${pathServer.address().port}` } + ) + t.equal(oneSlashPath2Arg.statusCode, 200) + t.end() +}) + +test('protocol-relative URL as pathname should be included in req.path', async (t) => { + const pathServer = createServer((req, res) => { + t.fail('it shouldn\'t be called') + res.statusCode = 200 + res.end('hello') + }) + + const requestedServer = createServer((req, res) => { + t.equal(`//localhost:${pathServer.address().port}`, req.url) + t.equal('GET', req.method) + t.equal(`localhost:${requestedServer.address().port}`, req.headers.host) + res.statusCode = 200 + res.end('hello') + }) + + t.teardown(requestedServer.close.bind(requestedServer)) + t.teardown(pathServer.close.bind(pathServer)) + + await Promise.all([ + requestedServer.listen(0), + pathServer.listen(0) + ]) + + const noSlashPathname = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + pathname: `//localhost:${pathServer.address().port}` + }) + t.equal(noSlashPathname.statusCode, 200) + const noSlashPath = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + path: `//localhost:${pathServer.address().port}` + }) + t.equal(noSlashPath.statusCode, 200) + const noSlashPath2Arg = await request( + `http://localhost:${requestedServer.address().port}`, + { path: `//localhost:${pathServer.address().port}` } + ) + t.equal(noSlashPath2Arg.statusCode, 200) + t.end() +}) + +test('Absolute URL as pathname should be included in req.path', async (t) => { + const pathServer = createServer((req, res) => { + t.fail('it shouldn\'t be called') + res.statusCode = 200 + res.end('hello') + }) + + const requestedServer = createServer((req, res) => { + t.equal(`/http://localhost:${pathServer.address().port}`, req.url) + t.equal('GET', req.method) + t.equal(`localhost:${requestedServer.address().port}`, req.headers.host) + res.statusCode = 200 + res.end('hello') + }) + + t.teardown(requestedServer.close.bind(requestedServer)) + t.teardown(pathServer.close.bind(pathServer)) + + await Promise.all([ + requestedServer.listen(0), + pathServer.listen(0) + ]) + + const noSlashPathname = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + pathname: `http://localhost:${pathServer.address().port}` + }) + t.equal(noSlashPathname.statusCode, 200) + const noSlashPath = await request({ + method: 'GET', + origin: `http://localhost:${requestedServer.address().port}`, + path: `http://localhost:${pathServer.address().port}` + }) + t.equal(noSlashPath.statusCode, 200) + const noSlashPath2Arg = await request( + `http://localhost:${requestedServer.address().port}`, + { path: `http://localhost:${pathServer.address().port}` } + ) + t.equal(noSlashPath2Arg.statusCode, 200) + t.end() +})
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
5- github.com/advisories/GHSA-8qr4-xgw6-wmr3ghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2022-35949ghsaADVISORY
- github.com/nodejs/undici/commit/124f7ebf705366b2e1844dff721928d270f87895ghsaWEB
- github.com/nodejs/undici/releases/tag/v5.8.2ghsaWEB
- github.com/nodejs/undici/security/advisories/GHSA-8qr4-xgw6-wmr3ghsaWEB
News mentions
0No linked articles in our index yet.