Skip to content

Commit

Permalink
doc: correct unsafe URL example in http docs
Browse files Browse the repository at this point in the history
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, mitigating both the
crash and security risks by ensuring the host part of the URL remains
controlled and predictable.

Fixes: nodejs#52494
  • Loading branch information
thisalihassan committed Apr 15, 2024
1 parent f098b7a commit bf1ce59
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ const proxy = createServer((req, res) => {
});
proxy.on('connect', (req, clientSocket, head) => {
// Connect to an origin server
const { port, hostname } = new URL(`http://${req.url}`);
const { port, hostname } = new URL(`http://${req.headers.host}${req.url}`);
const serverSocket = connect(port || 80, hostname, () => {
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
Expand Down Expand Up @@ -543,7 +543,7 @@ const proxy = http.createServer((req, res) => {
});
proxy.on('connect', (req, clientSocket, head) => {
// Connect to an origin server
const { port, hostname } = new URL(`http://${req.url}`);
const { port, hostname } = new URL(`http://${req.headers.host}${req.url}`);
const serverSocket = net.connect(port || 80, hostname, () => {
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
Expand Down Expand Up @@ -2886,15 +2886,15 @@ Accept: text/plain
To parse the URL into its parts:

```js
new URL(request.url, `http://${request.headers.host}`);
new URL(`http://${req.headers.host}${req.url}`);
```

When `request.url` is `'/status?name=ryan'` and `request.headers.host` is
`'localhost:3000'`:

```console
$ node
> new URL(request.url, `http://${request.headers.host}`)
> new URL(`http://${req.headers.host}${req.url}`)
URL {
href: 'http://localhost:3000/status?name=ryan',
origin: 'http://localhost:3000',
Expand Down

0 comments on commit bf1ce59

Please sign in to comment.