Skip to content
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

http2: Fix "ERR_HTTP2_INVALID_CONNECTION_HEADERS" error when adding "connection' into header #23908

Closed
wants to merge 22 commits into from
Closed
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
27 changes: 27 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const {
} = constants;

let statusMessageWarned = false;
let statusConnectionHeaderWarned = false;

// Defines and implements an API compatibility layer on top of the core
// HTTP/2 implementation, intended to provide an interface that is as
Expand All @@ -60,6 +61,8 @@ function assertValidHeader(name, value) {
err = new ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED();
} else if (value === undefined || value === null) {
err = new ERR_HTTP2_INVALID_HEADER_VALUE(value, name);
} else if (!isConnectionHeaderAllowed(name, value)) {
connectionHeaderMessageWarn();
sagitsofan marked this conversation as resolved.
Show resolved Hide resolved
}
if (err !== undefined) {
Error.captureStackTrace(err, assertValidHeader);
Expand Down Expand Up @@ -90,6 +93,25 @@ function statusMessageWarn() {
}
}

function isConnectionHeaderAllowed(name, value) {
if (name !== constants.HTTP2_HEADER_CONNECTION)
return true;
else
return value === 'trailers';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could be simplified to:

return name !== constants.HTTP2_HEADER_CONNECTION ||
       value === 'trailers';

}

function connectionHeaderMessageWarn() {
if (statusConnectionHeaderWarned === false) {
process.emitWarning(
'The provided connection header is not valid, ' +
'the value will be dropped from the header and ' +
'will never be in use.',
'UnsupportedWarning'
);
statusConnectionHeaderWarned = true;
}
}

function onStreamData(chunk) {
const request = this[kRequest];
if (request !== undefined && !request.push(chunk))
Expand Down Expand Up @@ -541,6 +563,11 @@ class Http2ServerResponse extends Stream {
[kSetHeader](name, value) {
name = name.trim().toLowerCase();
assertValidHeader(name, value);

if (!isConnectionHeaderAllowed(name, value)) {
return;
}

this[kHeaders][name] = value;
}

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-http2-server-set-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const body =
const server = http2.createServer((req, res) => {
res.setHeader('foobar', 'baz');
res.setHeader('X-POWERED-BY', 'node-test');
res.setHeader('connection', 'connection-test');
res.end(body);
});

Expand All @@ -34,4 +35,10 @@ server.listen(0, common.mustCall(() => {
req.end();
}));

const compatMsg = 'The provided connection header is not valid, ' +
'the value will be dropped from the header and ' +
'will never be in use.';

common.expectWarning('UnsupportedWarning', compatMsg);

server.on('error', common.mustNotCall());