Skip to content

Commit a905eaa

Browse files
mlegenhausenmarco-ippolito
authored andcommittedMay 3, 2024
doc: correct unsafe URL example in http docs
The previous documentation example for converting `request.url` to an `URL` object was unsafe, as it could allow a server crash through malformed URL inputs and potentially enable host header attacks. This commit revises the example to use string concatenation over the usage of the `baseUrl` and removes the usage of the `req.headers.host` as the authority part of the url, mitigating both the crash and security risks by ensuring the host part of the URL remains controlled and predictable. Fixes #52494 Co-authored-by: @astlouisf Co-authored-by: @samhh PR-URL: #52555 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 69c21a6 commit a905eaa

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed
 

‎doc/api/http.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -2886,31 +2886,34 @@ Accept: text/plain
28862886
To parse the URL into its parts:
28872887

28882888
```js
2889-
new URL(request.url, `http://${request.headers.host}`);
2889+
new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
28902890
```
28912891
2892-
When `request.url` is `'/status?name=ryan'` and `request.headers.host` is
2893-
`'localhost:3000'`:
2892+
When `request.url` is `'/status?name=ryan'` and `process.env.HOST` is undefined:
28942893
28952894
```console
28962895
$ node
2897-
> new URL(request.url, `http://${request.headers.host}`)
2896+
> new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
28982897
URL {
2899-
href: 'http://localhost:3000/status?name=ryan',
2900-
origin: 'http://localhost:3000',
2898+
href: 'http://localhost/status?name=ryan',
2899+
origin: 'http://localhost',
29012900
protocol: 'http:',
29022901
username: '',
29032902
password: '',
2904-
host: 'localhost:3000',
2903+
host: 'localhost',
29052904
hostname: 'localhost',
2906-
port: '3000',
2905+
port: '',
29072906
pathname: '/status',
29082907
search: '?name=ryan',
29092908
searchParams: URLSearchParams { 'name' => 'ryan' },
29102909
hash: ''
29112910
}
29122911
```
29132912
2913+
Ensure that you set `process.env.HOST` to the server's host name, or consider
2914+
replacing this part entirely. If using `req.headers.host`, ensure proper
2915+
validation is used, as clients may specify a custom `Host` header.
2916+
29142917
## Class: `http.OutgoingMessage`
29152918
29162919
<!-- YAML

0 commit comments

Comments
 (0)
Please sign in to comment.