Files
node/test/client-proxy/test-http-proxy-request-authority-construction.mjs
T
Joyee Cheung fd3419178e http: document and validate options.path when it's in absolute-form
When `options.path` passed to `http.request()` contains an absolute
URL, `http.request` has been sending it directly as the request target
in the HTTP 1.1 message. If the receiving server is a proxy,
the proxy server typically forwards the request to the destination
specified in the request target and ignores the Host header.
This means eventually the request can be forwarded to a destination
that is not consistent with `options.host`, depending on how the
receiving server behaves. Mimatched Host header and request target
also violates RFC 9112 Section 3.2, which we have been entirely
leaving to the users to verify.

This patch documents this behavior and warns that the user needs
to ensure the `path`, `option` and `headers` conform to the RFC.
If the receiving server is known to be a proxy server because
the request is routed by Node.js' built-in HTTP proxy support,
we now do a best-effort check to verify that the authority in
`options.path` (if absolute), Host headers and `options.host`
agree at request construction time. Node.js will give up on the
require target rewriting and throw an error when they don't match
at request construction.

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64108
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
2026-07-01 10:44:52 +00:00

83 lines
3.3 KiB
JavaScript

// Verify that the request path and Host header are constructed correctly
// for proxied requests across different option combinations.
import '../common/index.mjs';
import assert from 'node:assert';
import http from 'node:http';
function makeAgent() {
return new http.Agent({
proxyEnv: { HTTP_PROXY: 'http://localhost:1' },
});
}
function check(options, expectedPath, expectedHost) {
const agent = makeAgent();
const req = http.request({ agent, ...options });
req.on('error', () => {});
assert.strictEqual(req.path, expectedPath, `path for ${JSON.stringify(options)}`);
assert.strictEqual(req.getHeader('host'), expectedHost, `Host for ${JSON.stringify(options)}`);
req.destroy();
agent.destroy();
}
const cases = [
// [options, expectedPath, expectedHost]
// OPTIONS * and CONNECT bypass path rewriting.
[{ host: 'localhost', port: 3000, method: 'OPTIONS', path: '*' },
'*', 'localhost:3000'],
[{ host: 'example.com', port: 443, method: 'CONNECT', path: 'example.com:443' },
'example.com:443', 'example.com:443'],
// Basic cases: implicit Host, various port/defaultPort combos.
[{ host: 'localhost', port: 3000, path: '/a' },
'http://localhost:3000/a', 'localhost:3000'],
[{ host: 'localhost', port: 80, path: '/b' },
'http://localhost/b', 'localhost'],
[{ host: 'example.com', path: '/c' },
'http://example.com/c', 'example.com'],
[{ host: 'localhost', port: '3000', path: '/d' },
'http://localhost:3000/d', 'localhost:3000'],
// defaultPort suppresses port in auto-set Host, canonicalization corrects it.
[{ host: 'localhost', port: 80, defaultPort: 8080, path: '/e' },
'http://localhost/e', 'localhost'],
[{ host: 'localhost', port: 3000, defaultPort: 3000, path: '/f' },
'http://localhost:3000/f', 'localhost:3000'],
// User-explicit Host header: canonicalized to match request target.
[{ host: 'localhost', port: 80, defaultPort: 8080, path: '/g', headers: { Host: 'localhost:80' } },
'http://localhost/g', 'localhost'],
[{ host: 'localhost', port: 80, defaultPort: 8080, path: '/h', headers: { Host: 'localhost' } },
'http://localhost/h', 'localhost'],
[{ host: 'localhost', port: 80, path: '/i', headers: { Host: 'localhost:80' } },
'http://localhost/i', 'localhost'],
[{ host: 'localhost', port: 3000, defaultPort: 3000, path: '/j', headers: { Host: 'localhost:3000' } },
'http://localhost:3000/j', 'localhost:3000'],
[{ host: 'localhost', port: 3000, path: '/k', headers: { Host: 'LOCALHOST:3000' } },
'http://localhost:3000/k', 'localhost:3000'],
// setHost=false with user-provided Host.
[{ host: 'localhost', port: 3000, setHost: false, path: '/l', headers: { Host: 'localhost:3000' } },
'http://localhost:3000/l', 'localhost:3000'],
// IPv6.
[{ host: '::1', port: 3000, path: '/m' },
'http://[::1]:3000/m', '[::1]:3000'],
[{ host: '::1', port: 80, path: '/n' },
'http://[::1]/n', '[::1]'],
// Mixed-case host option: URL normalizes to lowercase.
[{ host: 'LocalHost', port: 3000, path: '/o' },
'http://localhost:3000/o', 'localhost:3000'],
// Absolute-form path matching authority.
[{ host: 'localhost', port: 3000, path: 'http://localhost:3000/p', headers: { Host: 'localhost:3000' } },
'http://localhost:3000/p', 'localhost:3000'],
];
for (const [options, expectedPath, expectedHost] of cases) {
check(options, expectedPath, expectedHost);
}