|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const http = require('http'); |
| 5 | +const net = require('net'); |
| 6 | +const { duplexPair } = require('stream'); |
| 7 | + |
| 8 | +// Integration tests for relaxed header value validation. |
| 9 | +// When insecureHTTPParser is enabled, outgoing headers with control characters |
| 10 | +// (0x01-0x1f except HTAB, and DEL 0x7f) are allowed per Fetch spec. |
| 11 | +// NUL (0x00), CR (0x0d), and LF (0x0a) are always rejected. |
| 12 | + |
| 13 | +// Helper: create a request that won't actually connect (for setHeader tests) |
| 14 | +function dummyRequest(opts) { |
| 15 | + const req = http.request({ host: '127.0.0.1', port: 1, ...opts }); |
| 16 | + req.on('error', () => {}); // Suppress connection errors |
| 17 | + return req; |
| 18 | +} |
| 19 | + |
| 20 | +// ============================================================================ |
| 21 | +// Test 1: Client setHeader with control chars in strict mode (default) - throws |
| 22 | +// ============================================================================ |
| 23 | +{ |
| 24 | + const req = dummyRequest(); |
| 25 | + assert.throws(() => { |
| 26 | + req.setHeader('X-Test', 'value\x01here'); |
| 27 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 28 | + req.destroy(); |
| 29 | +} |
| 30 | + |
| 31 | +// ============================================================================ |
| 32 | +// Test 2: Client setHeader with control chars in lenient mode - allowed |
| 33 | +// ============================================================================ |
| 34 | +{ |
| 35 | + const req = dummyRequest({ insecureHTTPParser: true }); |
| 36 | + // Should not throw - control chars allowed in lenient mode |
| 37 | + req.setHeader('X-Test', 'value\x01here'); |
| 38 | + req.setHeader('X-Bel', 'ding\x07'); |
| 39 | + req.setHeader('X-Esc', 'esc\x1b'); |
| 40 | + req.setHeader('X-Del', 'del\x7f'); |
| 41 | + req.destroy(); |
| 42 | +} |
| 43 | + |
| 44 | +// ============================================================================ |
| 45 | +// Test 3: NUL, CR, LF always rejected even in lenient mode (client) |
| 46 | +// ============================================================================ |
| 47 | +{ |
| 48 | + const req = dummyRequest({ insecureHTTPParser: true }); |
| 49 | + assert.throws(() => { |
| 50 | + req.setHeader('X-Test', 'value\x00here'); |
| 51 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 52 | + assert.throws(() => { |
| 53 | + req.setHeader('X-Test', 'value\rhere'); |
| 54 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 55 | + assert.throws(() => { |
| 56 | + req.setHeader('X-Test', 'value\nhere'); |
| 57 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 58 | + req.destroy(); |
| 59 | +} |
| 60 | + |
| 61 | +// ============================================================================ |
| 62 | +// Test 4: Server response setHeader with control chars in lenient mode |
| 63 | +// ============================================================================ |
| 64 | +{ |
| 65 | + const server = http.createServer({ |
| 66 | + insecureHTTPParser: true, |
| 67 | + }, common.mustCall((req, res) => { |
| 68 | + // Should not throw - control chars allowed in lenient mode |
| 69 | + res.setHeader('X-Custom', 'value\x01here'); |
| 70 | + res.end('ok'); |
| 71 | + })); |
| 72 | + |
| 73 | + server.listen(0, common.mustCall(() => { |
| 74 | + const port = server.address().port; |
| 75 | + // Use a raw TCP connection to read the response headers directly, |
| 76 | + // since http.get would fail to parse the control char in the header. |
| 77 | + const client = net.connect(port, common.mustCall(() => { |
| 78 | + client.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n'); |
| 79 | + })); |
| 80 | + let data = ''; |
| 81 | + client.on('data', (chunk) => { data += chunk; }); |
| 82 | + client.on('end', common.mustCall(() => { |
| 83 | + // eslint-disable-next-line no-control-regex |
| 84 | + assert.match(data, /X-Custom: value\x01here/); |
| 85 | + server.close(); |
| 86 | + })); |
| 87 | + })); |
| 88 | +} |
| 89 | + |
| 90 | +// ============================================================================ |
| 91 | +// Test 5: Server response NUL/CR/LF always rejected in lenient mode |
| 92 | +// ============================================================================ |
| 93 | +{ |
| 94 | + const server = http.createServer({ |
| 95 | + insecureHTTPParser: true, |
| 96 | + }, common.mustCall((req, res) => { |
| 97 | + assert.throws(() => { |
| 98 | + res.setHeader('X-Test', 'value\x00here'); |
| 99 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 100 | + assert.throws(() => { |
| 101 | + res.setHeader('X-Test', 'value\rhere'); |
| 102 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 103 | + assert.throws(() => { |
| 104 | + res.setHeader('X-Test', 'value\nhere'); |
| 105 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 106 | + res.end('ok'); |
| 107 | + })); |
| 108 | + |
| 109 | + server.listen(0, common.mustCall(() => { |
| 110 | + http.get({ port: server.address().port }, common.mustCall((res) => { |
| 111 | + res.resume(); |
| 112 | + res.on('end', common.mustCall(() => { |
| 113 | + server.close(); |
| 114 | + })); |
| 115 | + })); |
| 116 | + })); |
| 117 | +} |
| 118 | + |
| 119 | +// ============================================================================ |
| 120 | +// Test 6: Server response strict mode (default) rejects control chars |
| 121 | +// ============================================================================ |
| 122 | +{ |
| 123 | + const server = http.createServer(common.mustCall((req, res) => { |
| 124 | + assert.throws(() => { |
| 125 | + res.setHeader('X-Test', 'value\x01here'); |
| 126 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 127 | + res.end('ok'); |
| 128 | + })); |
| 129 | + |
| 130 | + server.listen(0, common.mustCall(() => { |
| 131 | + http.get({ port: server.address().port }, common.mustCall((res) => { |
| 132 | + res.resume(); |
| 133 | + res.on('end', common.mustCall(() => { |
| 134 | + server.close(); |
| 135 | + })); |
| 136 | + })); |
| 137 | + })); |
| 138 | +} |
| 139 | + |
| 140 | +// ============================================================================ |
| 141 | +// Test 7: appendHeader also respects lenient mode |
| 142 | +// ============================================================================ |
| 143 | +{ |
| 144 | + const req = dummyRequest({ insecureHTTPParser: true }); |
| 145 | + // Should not throw in lenient mode |
| 146 | + req.appendHeader('X-Test', 'value\x01here'); |
| 147 | + req.destroy(); |
| 148 | +} |
| 149 | + |
| 150 | +// ============================================================================ |
| 151 | +// Test 8: appendHeader strict mode rejects control chars |
| 152 | +// ============================================================================ |
| 153 | +{ |
| 154 | + const req = dummyRequest(); |
| 155 | + assert.throws(() => { |
| 156 | + req.appendHeader('X-Test', 'value\x01here'); |
| 157 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 158 | + req.destroy(); |
| 159 | +} |
| 160 | + |
| 161 | +// ============================================================================ |
| 162 | +// Test 9: Explicit insecureHTTPParser: false overrides global flag |
| 163 | +// ============================================================================ |
| 164 | +{ |
| 165 | + const req = dummyRequest({ insecureHTTPParser: false }); |
| 166 | + assert.throws(() => { |
| 167 | + req.setHeader('X-Test', 'value\x01here'); |
| 168 | + }, { code: 'ERR_INVALID_CHAR' }); |
| 169 | + req.destroy(); |
| 170 | +} |
| 171 | + |
| 172 | +// ============================================================================ |
| 173 | +// Test 10: Inbound response header with control char accepted in lenient mode |
| 174 | +// (exercises the new llhttp_set_lenient_header_value_relaxed path) |
| 175 | +// ============================================================================ |
| 176 | +{ |
| 177 | + const [clientSide, serverSide] = duplexPair(); |
| 178 | + |
| 179 | + const req = http.request({ |
| 180 | + createConnection: common.mustCall(() => clientSide), |
| 181 | + insecureHTTPParser: true, |
| 182 | + }, common.mustCall((res) => { |
| 183 | + assert.strictEqual(res.headers['x-ctrl'], 'value\x01here'); |
| 184 | + res.resume(); |
| 185 | + res.on('end', common.mustCall()); |
| 186 | + })); |
| 187 | + req.end(); |
| 188 | + |
| 189 | + serverSide.resume(); |
| 190 | + serverSide.end( |
| 191 | + 'HTTP/1.1 200 OK\r\n' + |
| 192 | + 'X-Ctrl: value\x01here\r\n' + |
| 193 | + 'Content-Length: 0\r\n' + |
| 194 | + '\r\n', |
| 195 | + ); |
| 196 | +} |
| 197 | + |
| 198 | +// Test 10b: Same inbound header without insecureHTTPParser — parser must error |
| 199 | +{ |
| 200 | + const [clientSide, serverSide] = duplexPair(); |
| 201 | + |
| 202 | + const req = http.request({ |
| 203 | + createConnection: common.mustCall(() => clientSide), |
| 204 | + }, common.mustNotCall()); |
| 205 | + req.end(); |
| 206 | + req.on('error', common.mustCall()); |
| 207 | + |
| 208 | + serverSide.resume(); |
| 209 | + serverSide.end( |
| 210 | + 'HTTP/1.1 200 OK\r\n' + |
| 211 | + 'X-Ctrl: value\x01here\r\n' + |
| 212 | + 'Content-Length: 0\r\n' + |
| 213 | + '\r\n', |
| 214 | + ); |
| 215 | +} |
0 commit comments