|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { once } = require('events'); |
| 9 | +const dc = require('diagnostics_channel'); |
| 10 | +const http2 = require('http2'); |
| 11 | + |
| 12 | +const kPublishCount = 8; |
| 13 | + |
| 14 | +let currentSession; |
| 15 | +let injection; |
| 16 | + |
| 17 | +dc.subscribe('http2.client.request.headers', common.mustCall(({ headers, session }) => { |
| 18 | + assert.strictEqual(session, currentSession); |
| 19 | + assert.strictEqual(session.constructor.name, 'ClientHttp2Session'); |
| 20 | + assert.ok(!!headers && typeof headers === 'object'); |
| 21 | + if (injection !== undefined) |
| 22 | + injection(headers); |
| 23 | +}, kPublishCount)); |
| 24 | + |
| 25 | +function doRequest(client, headers) { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + const stream = client.request(headers); |
| 28 | + stream.setEncoding('utf8'); |
| 29 | + let body = ''; |
| 30 | + stream.on('data', (chunk) => { body += chunk; }); |
| 31 | + stream.on('end', () => resolve(JSON.parse(body))); |
| 32 | + stream.on('error', reject); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +async function main() { |
| 37 | + const server = http2.createServer(); |
| 38 | + server.on('stream', (stream, headers) => { |
| 39 | + // Echo the received request headers back in the response body. |
| 40 | + stream.respond({ 'content-type': 'application/json' }); |
| 41 | + stream.end(JSON.stringify(headers)); |
| 42 | + }); |
| 43 | + server.listen(0); |
| 44 | + await once(server, 'listening'); |
| 45 | + const port = server.address().port; |
| 46 | + |
| 47 | + // Scenario 1: request() called while the session is still connecting, |
| 48 | + // with an object headers argument. |
| 49 | + { |
| 50 | + const client = http2.connect(`http://localhost:${port}`); |
| 51 | + currentSession = client; |
| 52 | + injection = (headers) => { |
| 53 | + headers.traceparent = '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'; |
| 54 | + }; |
| 55 | + // Also make sure the snapshot published to 'http2.client.stream.created' |
| 56 | + // reflects the injected header, since serialization happens after the |
| 57 | + // 'http2.client.request.headers' publish. |
| 58 | + const onCreated = common.mustCall(({ headers }) => { |
| 59 | + assert.strictEqual( |
| 60 | + headers.traceparent, |
| 61 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 62 | + }, 1); |
| 63 | + dc.subscribe('http2.client.stream.created', onCreated); |
| 64 | + const origHeaders = { ':path': '/connecting' }; |
| 65 | + const received = await doRequest(client, origHeaders); |
| 66 | + dc.unsubscribe('http2.client.stream.created', onCreated); |
| 67 | + assert.strictEqual( |
| 68 | + received.traceparent, |
| 69 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 70 | + assert.strictEqual(received[':path'], '/connecting'); |
| 71 | + // The subscriber mutates a copy, so the caller's object is unchanged. |
| 72 | + assert.deepStrictEqual(origHeaders, { ':path': '/connecting' }); |
| 73 | + client.close(); |
| 74 | + await once(client, 'close'); |
| 75 | + } |
| 76 | + |
| 77 | + // Scenario 2: request() called on an already-connected session. |
| 78 | + { |
| 79 | + const client = http2.connect(`http://localhost:${port}`); |
| 80 | + await once(client, 'connect'); |
| 81 | + currentSession = client; |
| 82 | + injection = (headers) => { |
| 83 | + headers.traceparent = '00-0af7651916cd43dd8f8ba5c1d1f77f0f-b7ad6b7169203331-01'; |
| 84 | + }; |
| 85 | + const received = await doRequest(client, { ':path': '/connected' }); |
| 86 | + assert.strictEqual( |
| 87 | + received.traceparent, |
| 88 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-b7ad6b7169203331-01'); |
| 89 | + assert.strictEqual(received[':path'], '/connected'); |
| 90 | + client.close(); |
| 91 | + await once(client, 'close'); |
| 92 | + } |
| 93 | + |
| 94 | + // Scenario 3: no headers argument at all. The subscriber must still be |
| 95 | + // able to inject headers into the fresh object that is published. |
| 96 | + { |
| 97 | + const client = http2.connect(`http://localhost:${port}`); |
| 98 | + currentSession = client; |
| 99 | + let sawFreshObject; |
| 100 | + injection = (headers) => { |
| 101 | + sawFreshObject = headers[':path'] === undefined; |
| 102 | + headers.traceparent = '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'; |
| 103 | + }; |
| 104 | + const received = await doRequest(client); |
| 105 | + assert.ok(sawFreshObject); |
| 106 | + assert.strictEqual( |
| 107 | + received.traceparent, |
| 108 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 109 | + assert.strictEqual(received[':path'], '/'); |
| 110 | + client.close(); |
| 111 | + await once(client, 'close'); |
| 112 | + } |
| 113 | + |
| 114 | + // Scenario 4: a subscriber that does not mutate the headers must have no |
| 115 | + // effect on the request. |
| 116 | + { |
| 117 | + const client = http2.connect(`http://localhost:${port}`); |
| 118 | + await once(client, 'connect'); |
| 119 | + currentSession = client; |
| 120 | + injection = undefined; |
| 121 | + const received = await doRequest(client, { |
| 122 | + ':path': '/no-mutation', |
| 123 | + 'x-test': 'value', |
| 124 | + }); |
| 125 | + assert.strictEqual(received.traceparent, undefined); |
| 126 | + assert.deepStrictEqual(received, { |
| 127 | + ':method': 'GET', |
| 128 | + ':scheme': 'http', |
| 129 | + ':authority': `localhost:${port}`, |
| 130 | + ':path': '/no-mutation', |
| 131 | + 'x-test': 'value', |
| 132 | + }); |
| 133 | + client.close(); |
| 134 | + await once(client, 'close'); |
| 135 | + } |
| 136 | + |
| 137 | + // Scenario 5: an array of raw headers can be mutated in place. |
| 138 | + { |
| 139 | + const client = http2.connect(`http://localhost:${port}`); |
| 140 | + currentSession = client; |
| 141 | + const origHeaders = [':path', '/array']; |
| 142 | + let sawArray; |
| 143 | + injection = (headers) => { |
| 144 | + sawArray = Array.isArray(headers); |
| 145 | + headers.push('traceparent', '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 146 | + }; |
| 147 | + const received = await doRequest(client, origHeaders); |
| 148 | + assert.ok(sawArray); |
| 149 | + assert.strictEqual( |
| 150 | + received.traceparent, |
| 151 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 152 | + assert.strictEqual(received[':path'], '/array'); |
| 153 | + // The subscriber mutates a copy, so the caller's array is unchanged. |
| 154 | + assert.deepStrictEqual(origHeaders, [':path', '/array']); |
| 155 | + client.close(); |
| 156 | + await once(client, 'close'); |
| 157 | + } |
| 158 | + |
| 159 | + // Scenario 6: injected headers go through the same validation as headers |
| 160 | + // passed directly to request(). Injecting an unknown pseudo-header throws |
| 161 | + // the same error that passing it directly would. |
| 162 | + { |
| 163 | + const client = http2.connect(`http://localhost:${port}`); |
| 164 | + currentSession = client; |
| 165 | + injection = (headers) => { |
| 166 | + headers[':foo'] = 'invalid'; |
| 167 | + }; |
| 168 | + assert.throws(() => client.request({ ':path': '/invalid' }), |
| 169 | + { code: 'ERR_HTTP2_INVALID_PSEUDOHEADER' }); |
| 170 | + injection = undefined; |
| 171 | + client.destroy(); |
| 172 | + } |
| 173 | + |
| 174 | + // Scenario 7: sensitiveHeaders is preserved on the published copy, in |
| 175 | + // object form, and mutating it does not touch the caller's list. |
| 176 | + { |
| 177 | + const client = http2.connect(`http://localhost:${port}`); |
| 178 | + currentSession = client; |
| 179 | + const origHeaders = { |
| 180 | + ':path': '/sensitive', |
| 181 | + 'authorization': 'secret', |
| 182 | + [http2.sensitiveHeaders]: ['authorization'], |
| 183 | + }; |
| 184 | + let sawSensitive; |
| 185 | + injection = (headers) => { |
| 186 | + sawSensitive = headers[http2.sensitiveHeaders]?.slice(); |
| 187 | + headers[http2.sensitiveHeaders].push('traceparent'); |
| 188 | + headers.traceparent = '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'; |
| 189 | + }; |
| 190 | + const received = await doRequest(client, origHeaders); |
| 191 | + assert.strictEqual( |
| 192 | + received.traceparent, |
| 193 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 194 | + assert.deepStrictEqual(sawSensitive, ['authorization']); |
| 195 | + assert.deepStrictEqual(origHeaders, { |
| 196 | + ':path': '/sensitive', |
| 197 | + 'authorization': 'secret', |
| 198 | + [http2.sensitiveHeaders]: ['authorization'], |
| 199 | + }); |
| 200 | + client.close(); |
| 201 | + await once(client, 'close'); |
| 202 | + } |
| 203 | + |
| 204 | + // Scenario 8: same as scenario 7, in raw array form. |
| 205 | + { |
| 206 | + const client = http2.connect(`http://localhost:${port}`); |
| 207 | + currentSession = client; |
| 208 | + const origHeaders = [':path', '/sensitive-array', 'authorization', 'secret']; |
| 209 | + origHeaders[http2.sensitiveHeaders] = ['authorization']; |
| 210 | + let sawArray; |
| 211 | + let sawSensitive; |
| 212 | + injection = (headers) => { |
| 213 | + sawArray = Array.isArray(headers); |
| 214 | + sawSensitive = headers[http2.sensitiveHeaders]?.slice(); |
| 215 | + headers[http2.sensitiveHeaders].push('traceparent'); |
| 216 | + headers.push('traceparent', '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 217 | + }; |
| 218 | + const received = await doRequest(client, origHeaders); |
| 219 | + assert.strictEqual( |
| 220 | + received.traceparent, |
| 221 | + '00-0af7651916cd43dd8f8ba5c1d1f77f0f-00f067aa0ba902b7-01'); |
| 222 | + assert.ok(sawArray); |
| 223 | + assert.deepStrictEqual(sawSensitive, ['authorization']); |
| 224 | + assert.strictEqual(origHeaders.length, 4); |
| 225 | + assert.deepStrictEqual(origHeaders[http2.sensitiveHeaders], ['authorization']); |
| 226 | + client.close(); |
| 227 | + await once(client, 'close'); |
| 228 | + } |
| 229 | + |
| 230 | + server.close(); |
| 231 | + await once(server, 'close'); |
| 232 | +} |
| 233 | + |
| 234 | +main().then(common.mustCall()); |
0 commit comments