http2: improve errors thrown in header validation

PR-URL: https://github.com/nodejs/node/pull/16718
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Joyee Cheung 2017-11-03 22:50:32 +08:00
parent 916e1cb720
commit 11a9f36cae
5 changed files with 18 additions and 12 deletions

View File

@ -837,7 +837,7 @@ requests and responses.
<a id="ERR_HTTP2_INVALID_HEADER_VALUE"></a> <a id="ERR_HTTP2_INVALID_HEADER_VALUE"></a>
### ERR_HTTP2_INVALID_HEADER_VALUE ### ERR_HTTP2_INVALID_HEADER_VALUE
Used to indicate that an invalid HTTP/2 header value has been specified. Used to indicate that an invalid HTTP2 header value has been specified.
<a id="ERR_HTTP2_INVALID_INFO_STATUS"></a> <a id="ERR_HTTP2_INVALID_INFO_STATUS"></a>
### ERR_HTTP2_INVALID_INFO_STATUS ### ERR_HTTP2_INVALID_INFO_STATUS

View File

@ -280,7 +280,7 @@ E('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED',
'Informational status codes cannot be used'); 'Informational status codes cannot be used');
E('ERR_HTTP2_INVALID_CONNECTION_HEADERS', E('ERR_HTTP2_INVALID_CONNECTION_HEADERS',
'HTTP/1 Connection specific headers are forbidden: "%s"'); 'HTTP/1 Connection specific headers are forbidden: "%s"');
E('ERR_HTTP2_INVALID_HEADER_VALUE', 'Value must not be undefined or null'); E('ERR_HTTP2_INVALID_HEADER_VALUE', 'Invalid value "%s" for header "%s"');
E('ERR_HTTP2_INVALID_INFO_STATUS', E('ERR_HTTP2_INVALID_INFO_STATUS',
(code) => `Invalid informational status code: ${code}`); (code) => `Invalid informational status code: ${code}`);
E('ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH', E('ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH',

View File

@ -40,12 +40,18 @@ let statusMessageWarned = false;
// close as possible to the current require('http') API // close as possible to the current require('http') API
function assertValidHeader(name, value) { function assertValidHeader(name, value) {
if (name === '' || typeof name !== 'string') let err;
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); if (name === '' || typeof name !== 'string') {
if (isPseudoHeader(name)) err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name);
throw new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'); } else if (isPseudoHeader(name)) {
if (value === undefined || value === null) err = new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED');
throw new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE'); } else if (value === undefined || value === null) {
err = new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE', value, name);
}
if (err !== undefined) {
Error.captureStackTrace(err, assertValidHeader);
throw err;
}
} }
function isPseudoHeader(name) { function isPseudoHeader(name) {

View File

@ -85,14 +85,14 @@ server.listen(0, common.mustCall(function() {
}, common.expectsError({ }, common.expectsError({
code: 'ERR_HTTP2_INVALID_HEADER_VALUE', code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError, type: TypeError,
message: 'Value must not be undefined or null' message: 'Invalid value "null" for header "foo-bar"'
})); }));
assert.throws(function() { assert.throws(function() {
response.setHeader(real, undefined); response.setHeader(real, undefined);
}, common.expectsError({ }, common.expectsError({
code: 'ERR_HTTP2_INVALID_HEADER_VALUE', code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError, type: TypeError,
message: 'Value must not be undefined or null' message: 'Invalid value "undefined" for header "foo-bar"'
})); }));
common.expectsError( common.expectsError(
() => response.setHeader(), // header name undefined () => response.setHeader(), // header name undefined

View File

@ -28,7 +28,7 @@ server.listen(0, common.mustCall(() => {
{ {
code: 'ERR_HTTP2_INVALID_HEADER_VALUE', code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError, type: TypeError,
message: 'Value must not be undefined or null' message: 'Invalid value "undefined" for header "test"'
} }
); );
common.expectsError( common.expectsError(
@ -36,7 +36,7 @@ server.listen(0, common.mustCall(() => {
{ {
code: 'ERR_HTTP2_INVALID_HEADER_VALUE', code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError, type: TypeError,
message: 'Value must not be undefined or null' message: 'Invalid value "null" for header "test"'
} }
); );
common.expectsError( common.expectsError(