Skip to content

Commit 4560bc5

Browse files
authored
doc: improve code snippet alternative of url.parse() using WHATWG URL
The previous code snippet was not safe since a url may start with double slashes which would cause the hostname to be replaced.
1 parent bab752d commit 4560bc5

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

doc/api/url.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ input. CVEs are not issued for `url.parse()` vulnerabilities. Use the
18531853
function getURL(req) {
18541854
const proto = req.headers['x-forwarded-proto'] || 'https';
18551855
const host = req.headers['x-forwarded-host'] || req.headers.host || 'example.com';
1856-
return new URL(req.url || '/', `${proto}://${host}`);
1856+
return new URL(`${proto}://${host}${req.url || '/'}`);
18571857
}
18581858
```
18591859
@@ -1863,7 +1863,7 @@ use the example below:
18631863
18641864
```js
18651865
function getURL(req) {
1866-
return new URL(req.url || '/', 'https://example.com');
1866+
return new URL(`https://example.com${req.url || '/'}`);
18671867
}
18681868
```
18691869

0 commit comments

Comments
 (0)