Skip to content

http2: set Http2Stream#sentHeaders for raw headers #59244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const kProceed = Symbol('proceed');
const kRemoteSettings = Symbol('remote-settings');
const kRequestAsyncResource = Symbol('requestAsyncResource');
const kSentHeaders = Symbol('sent-headers');
const kRawHeaders = Symbol('raw-headers');
const kSentTrailers = Symbol('sent-trailers');
const kServer = Symbol('server');
const kState = Symbol('state');
Expand Down Expand Up @@ -1807,12 +1808,14 @@ class ClientHttp2Session extends Http2Session {

let headersList;
let headersObject;
let rawHeaders;
let scheme;
let authority;
let method;

if (ArrayIsArray(headersParam)) {
({
rawHeaders,
headersList,
scheme,
authority,
Expand Down Expand Up @@ -1859,6 +1862,7 @@ class ClientHttp2Session extends Http2Session {
// eslint-disable-next-line no-use-before-define
const stream = new ClientHttp2Stream(this, undefined, undefined, {});
stream[kSentHeaders] = headersObject; // N.b. Only set for object headers, not raw headers
stream[kRawHeaders] = rawHeaders; // N.b. Only set for raw headers, not object headers
stream[kOrigin] = `${scheme}://${authority}`;
const reqAsync = new AsyncResource('PendingRequest');
stream[kRequestAsyncResource] = reqAsync;
Expand Down Expand Up @@ -2128,6 +2132,33 @@ class Http2Stream extends Duplex {
}

get sentHeaders() {
if (this[kSentHeaders] || !this[kRawHeaders]) {
return this[kSentHeaders];
}

const rawHeaders = this[kRawHeaders];
const headersObject = { __proto__: null };

for (let i = 0; i < rawHeaders.length; i += 2) {
const key = rawHeaders[i];
const value = rawHeaders[i + 1];

const existing = headersObject[key];
if (existing === undefined) {
headersObject[key] = value;
} else if (ArrayIsArray(existing)) {
existing.push(value);
} else {
headersObject[key] = [existing, value];
}
}

if (rawHeaders[kSensitiveHeaders] !== undefined) {
headersObject[kSensitiveHeaders] = rawHeaders[kSensitiveHeaders];
}

this[kSentHeaders] = headersObject;

return this[kSentHeaders];
}

Expand Down
12 changes: 10 additions & 2 deletions lib/internal/http2/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,23 @@ function prepareRequestHeadersArray(headers, session) {
throw new ERR_HTTP2_CONNECT_PATH();
}

const headersList = buildNgHeaderString(
const rawHeaders =
additionalPsuedoHeaders.length ?
additionalPsuedoHeaders.concat(headers) :
headers,
headers;

if (headers[kSensitiveHeaders] !== undefined) {
rawHeaders[kSensitiveHeaders] = headers[kSensitiveHeaders];
}

const headersList = buildNgHeaderString(
rawHeaders,
assertValidPseudoHeader,
headers[kSensitiveHeaders],
);

return {
rawHeaders,
headersList,
scheme,
authority: authority ?? headers[HTTP2_HEADER_HOST],
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-http2-raw-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const http2 = require('http2');
'a', 'c',
]).end();

assert.deepStrictEqual(req.sentHeaders, {
'__proto__': null,
':path': '/foobar',
':scheme': 'http',
':authority': `localhost:${server.address().port}`,
':method': 'GET',
'a': [ 'b', 'c' ],
'x-FOO': 'bar',
});

req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
client.close();
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-http2-sensitive-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ const { duplexPair } = require('stream');

const req = client.request(rawHeaders);

assert.deepStrictEqual(req.sentHeaders, {
'__proto__': null,
':method': 'GET',
':authority': 'localhost:80',
':scheme': 'http',
':path': '/',
'secret': 'secret-value',
[http2.sensitiveHeaders]: [ 'secret' ],
});

req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
}));
Expand Down
Loading