doc,test: clarify http1 insecure validation

The httpValidation docs said 'insecure' disables all header value
validation. That wasn't entirely true. It only did so for incomming
messages (which are parsed with all --insecure-http-parser leniency
flags). Outgoing header values are still validaed as in 'relaxed'
mode. This comes from the design in https://github.com/nodejs/node/pull/61597

This change corrects the docs and adds some tests to assert the
expected behavior

Refs: https://github.com/nodejs/node/pull/61597
Refs: https://github.com/nodejs/node/pull/63571
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
PR-URL: https://github.com/nodejs/node/pull/67560
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Ethan Arrowood authored and Antoine du Hamel committed 2026-11-23 12:34:20 +01:00
1 parent 46872fa7be
commit e834c1bafa
2 files changed
+64 -18

No files matched your search

+24 -18
View File
@@ -3767,15 +3767,18 @@ changes:
`readableHighWaterMark` and `writableHighWaterMark`. This affects
`highWaterMark` property of both `IncomingMessage` and `ServerResponse`.
**Default:** See [`stream.getDefaultHighWaterMark()`][].
* `httpValidation` {string} Controls HTTP header value validation strictness
for incoming requests. Accepted values are:
* `'strict'`: Strictest validation; rejects any non-ASCII or control
characters in header values.
* `'relaxed'`: Allows a limited set of non-ASCII characters in header
values, aligning with the
[Fetch specification](https://fetch.spec.whatwg.org/).
* `'insecure'`: Disables all header value validation (equivalent to
`insecureHTTPParser: true`).
* `httpValidation` {string} Controls how strictly HTTP header values are
validated, both in incoming requests and in outgoing responses. Accepted
values are:
* `'strict'`: Rejects control characters other than horizontal tab, and
characters outside the `latin1` range.
* `'relaxed'`: Also allows control characters other than NUL, CR, and LF,
aligning with the [Fetch specification](https://fetch.spec.whatwg.org/).
* `'insecure'`: Equivalent to `insecureHTTPParser: true`. Incoming
requests are parsed with all [`--insecure-http-parser`][] leniency
flags, which disables their header value validation. Outgoing header
values are validated as in `'relaxed'` mode, so NUL, CR, and LF are
still rejected.
Cannot be used together with `insecureHTTPParser`. **Default:** `'strict'`.
* `insecureHTTPParser` {boolean} If set to `true`, it will use an HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
@@ -4097,15 +4100,18 @@ changes:
request to. **Default:** `'localhost'`.
* `hostname` {string} Alias for `host`. To support [`url.parse()`][],
`hostname` will be used if both `host` and `hostname` are specified.
* `httpValidation` {string} Controls HTTP header value validation strictness
for outgoing requests. Accepted values are:
* `'strict'`: Strictest validation; rejects any non-ASCII or control
characters in header values.
* `'relaxed'`: Allows a limited set of non-ASCII characters in header
values, aligning with the
[Fetch specification](https://fetch.spec.whatwg.org/).
* `'insecure'`: Disables all header value validation (equivalent to
`insecureHTTPParser: true`).
* `httpValidation` {string} Controls how strictly HTTP header values are
validated, both in outgoing requests and in incoming responses. Accepted
values are:
* `'strict'`: Rejects control characters other than horizontal tab, and
characters outside the `latin1` range.
* `'relaxed'`: Also allows control characters other than NUL, CR, and LF,
aligning with the [Fetch specification](https://fetch.spec.whatwg.org/).
* `'insecure'`: Equivalent to `insecureHTTPParser: true`. Incoming
responses are parsed with all [`--insecure-http-parser`][] leniency
flags, which disables their header value validation. Outgoing header
values are validated as in `'relaxed'` mode, so NUL, CR, and LF are
still rejected.
Cannot be used together with `insecureHTTPParser`. **Default:** `'strict'`.
* `insecureHTTPParser` {boolean} If set to `true`, it will use an HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
@@ -234,9 +234,49 @@ if (kRelaxedInboundSupported) {
// Should not throw - control chars allowed in insecure mode
req.setHeader('X-Test', 'value\x01here');
req.setHeader('X-Bel', 'ding\x07');
// Outgoing values are validated as in relaxed mode: NUL, CR, and LF are
// still rejected, as they would allow header injection.
for (const value of ['value\x00here', 'value\rhere', 'value\nhere']) {
assert.throws(() => req.setHeader('X-Test', value),
{ code: 'ERR_INVALID_CHAR' });
assert.throws(() => req.appendHeader('X-Test', value),
{ code: 'ERR_INVALID_CHAR' });
}
req.destroy();
}
// ============================================================================
// Test 11b: httpValidation: 'insecure' server response still rejects
// NUL, CR, and LF in outgoing header values
// ============================================================================
{
const server = http.createServer({
httpValidation: 'insecure',
}, common.mustCall((req, res) => {
for (const value of ['value\x00here', 'value\rhere', 'value\nhere']) {
assert.throws(() => res.setHeader('X-Test', value),
{ code: 'ERR_INVALID_CHAR' });
assert.throws(() => res.writeHead(200, { 'X-Test': value }),
{ code: 'ERR_INVALID_CHAR' });
}
res.setHeader('X-Custom', 'value\x01here');
res.end('ok');
}));
server.listen(0, common.mustCall(() => {
http.get({
port: server.address().port,
httpValidation: 'insecure',
}, common.mustCall((res) => {
assert.strictEqual(res.headers['x-custom'], 'value\x01here');
res.resume();
res.on('end', common.mustCall(() => {
server.close();
}));
}));
}));
}
// ============================================================================
// Test 12: Mutual exclusion - client throws when both options are set
// ============================================================================