Skip to content

Commit b3eeb3b

Browse files
styfleaduh95
authored andcommitted
doc: provide alternative to url.parse() using WHATWG URL
PR-URL: #59736 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 1ddaab1 commit b3eeb3b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

doc/api/url.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,15 @@ A `URIError` is thrown if the `auth` property is present but cannot be decoded.
17141714
strings. It is prone to security issues such as [host name spoofing][]
17151715
and incorrect handling of usernames and passwords. Do not use with untrusted
17161716
input. CVEs are not issued for `url.parse()` vulnerabilities. Use the
1717-
[WHATWG URL][] API instead.
1717+
[WHATWG URL][] API instead, for example:
1718+
1719+
```js
1720+
function getURL(req) {
1721+
const proto = req.headers['x-forwarded-proto'] || 'https';
1722+
const host = req.headers['x-forwarded-host'] || req.headers.host || 'example.com';
1723+
return new URL(req.url || '/', `${proto}://${host}`);
1724+
}
1725+
```
17181726
17191727
The example above assumes well-formed headers are forwarded from a reverse
17201728
proxy to your Node.js server. If you are not using a reverse proxy, you should

0 commit comments

Comments
 (0)