diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2017-11-03 22:50:32 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2017-11-06 17:25:34 +0800 |
commit | 11a9f36cae0d1e6e11c9f40ec82d9f887aa0a911 (patch) | |
tree | 92a86a0d986f92b76ff871c4f1d02709c5f227e9 | |
parent | 916e1cb72016333635e24fd49966e1295dfeb436 (diff) | |
download | node-new-11a9f36cae0d1e6e11c9f40ec82d9f887aa0a911.tar.gz |
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>
-rw-r--r-- | doc/api/errors.md | 2 | ||||
-rw-r--r-- | lib/internal/errors.js | 2 | ||||
-rw-r--r-- | lib/internal/http2/compat.js | 18 | ||||
-rw-r--r-- | test/parallel/test-http2-compat-serverresponse-headers.js | 4 | ||||
-rw-r--r-- | test/parallel/test-http2-compat-serverresponse-trailers.js | 4 |
5 files changed, 18 insertions, 12 deletions
diff --git a/doc/api/errors.md b/doc/api/errors.md index fa56c7a529..2f7f60983e 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -837,7 +837,7 @@ requests and responses. <a id="ERR_HTTP2_INVALID_HEADER_VALUE"></a> ### 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> ### ERR_HTTP2_INVALID_INFO_STATUS diff --git a/lib/internal/errors.js b/lib/internal/errors.js index d13a8e85de..da00ba2591 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -280,7 +280,7 @@ E('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', 'Informational status codes cannot be used'); E('ERR_HTTP2_INVALID_CONNECTION_HEADERS', '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', (code) => `Invalid informational status code: ${code}`); E('ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH', diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index c96fc93196..89a2227008 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -40,12 +40,18 @@ let statusMessageWarned = false; // close as possible to the current require('http') API function assertValidHeader(name, value) { - if (name === '' || typeof name !== 'string') - throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); - if (isPseudoHeader(name)) - throw new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'); - if (value === undefined || value === null) - throw new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE'); + let err; + if (name === '' || typeof name !== 'string') { + err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); + } else if (isPseudoHeader(name)) { + err = new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'); + } 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) { diff --git a/test/parallel/test-http2-compat-serverresponse-headers.js b/test/parallel/test-http2-compat-serverresponse-headers.js index aae1d96c55..ec1071bc34 100644 --- a/test/parallel/test-http2-compat-serverresponse-headers.js +++ b/test/parallel/test-http2-compat-serverresponse-headers.js @@ -85,14 +85,14 @@ server.listen(0, common.mustCall(function() { }, common.expectsError({ code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "null" for header "foo-bar"' })); assert.throws(function() { response.setHeader(real, undefined); }, common.expectsError({ code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "undefined" for header "foo-bar"' })); common.expectsError( () => response.setHeader(), // header name undefined diff --git a/test/parallel/test-http2-compat-serverresponse-trailers.js b/test/parallel/test-http2-compat-serverresponse-trailers.js index 4d93e14dbe..7332f9e8d0 100644 --- a/test/parallel/test-http2-compat-serverresponse-trailers.js +++ b/test/parallel/test-http2-compat-serverresponse-trailers.js @@ -28,7 +28,7 @@ server.listen(0, common.mustCall(() => { { code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "undefined" for header "test"' } ); common.expectsError( @@ -36,7 +36,7 @@ server.listen(0, common.mustCall(() => { { code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "null" for header "test"' } ); common.expectsError( |