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