Skip to content

Commit 17dab26

Browse files
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 #61597 This change corrects the docs and adds some tests to assert the expected behavior Refs: #61597 Refs: #63571 Assisted-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Ethan Arrowood <ethan@arrowood.dev> PR-URL: #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>
1 parent 77fdeb6 commit 17dab26

2 files changed

Lines changed: 64 additions & 18 deletions

File tree

‎doc/api/http.md‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,15 +3767,18 @@ changes:
37673767
`readableHighWaterMark` and `writableHighWaterMark`. This affects
37683768
`highWaterMark` property of both `IncomingMessage` and `ServerResponse`.
37693769
**Default:** See [`stream.getDefaultHighWaterMark()`][].
3770-
* `httpValidation` {string} Controls HTTP header value validation strictness
3771-
for incoming requests. Accepted values are:
3772-
* `'strict'`: Strictest validation; rejects any non-ASCII or control
3773-
characters in header values.
3774-
* `'relaxed'`: Allows a limited set of non-ASCII characters in header
3775-
values, aligning with the
3776-
[Fetch specification](https://fetch.spec.whatwg.org/).
3777-
* `'insecure'`: Disables all header value validation (equivalent to
3778-
`insecureHTTPParser: true`).
3770+
* `httpValidation` {string} Controls how strictly HTTP header values are
3771+
validated, both in incoming requests and in outgoing responses. Accepted
3772+
values are:
3773+
* `'strict'`: Rejects control characters other than horizontal tab, and
3774+
characters outside the `latin1` range.
3775+
* `'relaxed'`: Also allows control characters other than NUL, CR, and LF,
3776+
aligning with the [Fetch specification](https://fetch.spec.whatwg.org/).
3777+
* `'insecure'`: Equivalent to `insecureHTTPParser: true`. Incoming
3778+
requests are parsed with all [`--insecure-http-parser`][] leniency
3779+
flags, which disables their header value validation. Outgoing header
3780+
values are validated as in `'relaxed'` mode, so NUL, CR, and LF are
3781+
still rejected.
37793782
Cannot be used together with `insecureHTTPParser`. **Default:** `'strict'`.
37803783
* `insecureHTTPParser` {boolean} If set to `true`, it will use an HTTP parser
37813784
with leniency flags enabled. Using the insecure parser should be avoided.
@@ -4097,15 +4100,18 @@ changes:
40974100
request to. **Default:** `'localhost'`.
40984101
* `hostname` {string} Alias for `host`. To support [`url.parse()`][],
40994102
`hostname` will be used if both `host` and `hostname` are specified.
4100-
* `httpValidation` {string} Controls HTTP header value validation strictness
4101-
for outgoing requests. Accepted values are:
4102-
* `'strict'`: Strictest validation; rejects any non-ASCII or control
4103-
characters in header values.
4104-
* `'relaxed'`: Allows a limited set of non-ASCII characters in header
4105-
values, aligning with the
4106-
[Fetch specification](https://fetch.spec.whatwg.org/).
4107-
* `'insecure'`: Disables all header value validation (equivalent to
4108-
`insecureHTTPParser: true`).
4103+
* `httpValidation` {string} Controls how strictly HTTP header values are
4104+
validated, both in outgoing requests and in incoming responses. Accepted
4105+
values are:
4106+
* `'strict'`: Rejects control characters other than horizontal tab, and
4107+
characters outside the `latin1` range.
4108+
* `'relaxed'`: Also allows control characters other than NUL, CR, and LF,
4109+
aligning with the [Fetch specification](https://fetch.spec.whatwg.org/).
4110+
* `'insecure'`: Equivalent to `insecureHTTPParser: true`. Incoming
4111+
responses are parsed with all [`--insecure-http-parser`][] leniency
4112+
flags, which disables their header value validation. Outgoing header
4113+
values are validated as in `'relaxed'` mode, so NUL, CR, and LF are
4114+
still rejected.
41094115
Cannot be used together with `insecureHTTPParser`. **Default:** `'strict'`.
41104116
* `insecureHTTPParser` {boolean} If set to `true`, it will use an HTTP parser
41114117
with leniency flags enabled. Using the insecure parser should be avoided.

‎test/parallel/test-http-header-value-relaxed.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,49 @@ if (kRelaxedInboundSupported) {
234234
// Should not throw - control chars allowed in insecure mode
235235
req.setHeader('X-Test', 'value\x01here');
236236
req.setHeader('X-Bel', 'ding\x07');
237+
// Outgoing values are validated as in relaxed mode: NUL, CR, and LF are
238+
// still rejected, as they would allow header injection.
239+
for (const value of ['value\x00here', 'value\rhere', 'value\nhere']) {
240+
assert.throws(() => req.setHeader('X-Test', value),
241+
{ code: 'ERR_INVALID_CHAR' });
242+
assert.throws(() => req.appendHeader('X-Test', value),
243+
{ code: 'ERR_INVALID_CHAR' });
244+
}
237245
req.destroy();
238246
}
239247

248+
// ============================================================================
249+
// Test 11b: httpValidation: 'insecure' server response still rejects
250+
// NUL, CR, and LF in outgoing header values
251+
// ============================================================================
252+
{
253+
const server = http.createServer({
254+
httpValidation: 'insecure',
255+
}, common.mustCall((req, res) => {
256+
for (const value of ['value\x00here', 'value\rhere', 'value\nhere']) {
257+
assert.throws(() => res.setHeader('X-Test', value),
258+
{ code: 'ERR_INVALID_CHAR' });
259+
assert.throws(() => res.writeHead(200, { 'X-Test': value }),
260+
{ code: 'ERR_INVALID_CHAR' });
261+
}
262+
res.setHeader('X-Custom', 'value\x01here');
263+
res.end('ok');
264+
}));
265+
266+
server.listen(0, common.mustCall(() => {
267+
http.get({
268+
port: server.address().port,
269+
httpValidation: 'insecure',
270+
}, common.mustCall((res) => {
271+
assert.strictEqual(res.headers['x-custom'], 'value\x01here');
272+
res.resume();
273+
res.on('end', common.mustCall(() => {
274+
server.close();
275+
}));
276+
}));
277+
}));
278+
}
279+
240280
// ============================================================================
241281
// Test 12: Mutual exclusion - client throws when both options are set
242282
// ============================================================================

0 commit comments

Comments
 (0)