GeezerVison is showing github.com — http2: add strictSingleValueFields option to relax header validation · windwiny/node@0fccfa4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/http2.md +14Lines changed: 14 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2796,6 +2796,9 @@ Throws `ERR_INVALID_ARG_TYPE` for invalid `settings` argument. | 2796 | 2796 | <!-- YAML | 2797 | 2797 | added: v8.4.0 | 2798 | 2798 | changes: | | 2799 | + - version: REPLACEME | | 2800 | + pr-url: https://github.com/nodejs/node/pull/59917 | | 2801 | + description: Added the `strictSingleValueFields` option. | 2799 | 2802 | - version: REPLACEME | 2800 | 2803 | pr-url: https://github.com/nodejs/node/pull/61713 | 2801 | 2804 | description: Added `http1Options` option. The `Http1IncomingMessage` | @@ -2955,6 +2958,10 @@ changes: | 2955 | 2958 | and trailing whitespace validation for HTTP/2 header field names and values | 2956 | 2959 | as per [RFC-9113](https://www.rfc-editor.org/rfc/rfc9113.html#section-8.2.1). | 2957 | 2960 | **Default:** `true`. | | 2961 | + * `strictSingleValueFields` {boolean} If `true`, strict validation is used | | 2962 | + for headers and trailers defined as having only a single value, such that | | 2963 | + an error is thrown if multiple values are provided. | | 2964 | + **Default:** `true`. | 2958 | 2965 | * `...options` {Object} Any [`net.createServer()`][] option can be provided. | 2959 | 2966 | * `onRequestHandler` {Function} See [Compatibility API][] | 2960 | 2967 | * Returns: {Http2Server} | @@ -3012,6 +3019,9 @@ server.listen(8000); | 3012 | 3019 | <!-- YAML | 3013 | 3020 | added: v8.4.0 | 3014 | 3021 | changes: | | 3022 | + - version: REPLACEME | | 3023 | + pr-url: https://github.com/nodejs/node/pull/59917 | | 3024 | + description: Added the `strictSingleValueFields` option. | 3015 | 3025 | - version: REPLACEME | 3016 | 3026 | pr-url: https://github.com/nodejs/node/pull/61713 | 3017 | 3027 | description: Added `http1Options` option. | @@ -3133,6 +3143,10 @@ changes: | 3133 | 3143 | and trailing whitespace validation for HTTP/2 header field names and values | 3134 | 3144 | as per [RFC-9113](https://www.rfc-editor.org/rfc/rfc9113.html#section-8.2.1). | 3135 | 3145 | **Default:** `true`. | | 3146 | + * `strictSingleValueFields` {boolean} If `true`, strict validation is used | | 3147 | + for headers and trailers defined as having only a single value, such that | | 3148 | + an error is thrown if multiple values are provided. | | 3149 | + **Default:** `true`. | 3136 | 3150 | * `http1Options` {Object} An options object for configuring the HTTP/1 | 3137 | 3151 | fallback when `allowHTTP1` is `true`. These options are passed to the | 3138 | 3152 | underlying HTTP/1 server. See [`http.createServer()`][] for available | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +50-8Lines changed: 50 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,6 +136,7 @@ const { | 136 | 136 | const { | 137 | 137 | assertIsObject, | 138 | 138 | assertIsArray, | | 139 | + assertValidPseudoHeader, | 139 | 140 | assertValidPseudoHeaderResponse, | 140 | 141 | assertValidPseudoHeaderTrailer, | 141 | 142 | assertWithinRange, | @@ -148,6 +149,7 @@ const { | 148 | 149 | isPayloadMeaningless, | 149 | 150 | kAuthority, | 150 | 151 | kSensitiveHeaders, | | 152 | + kStrictSingleValueFields, | 151 | 153 | kSocket, | 152 | 154 | kRequest, | 153 | 155 | kProtocol, | @@ -1318,6 +1320,8 @@ class Http2Session extends EventEmitter { | 1318 | 1320 | this[kSocket] = socket; | 1319 | 1321 | this[kTimeout] = null; | 1320 | 1322 | this[kHandle] = undefined; | | 1323 | + this[kStrictSingleValueFields] = | | 1324 | + options.strictSingleValueFields; | 1321 | 1325 |
| 1322 | 1326 | // Do not use nagle's algorithm | 1323 | 1327 | if (typeof socket.setNoDelay === 'function') | @@ -2370,7 +2374,11 @@ class Http2Stream extends Duplex { | 2370 | 2374 |
| 2371 | 2375 | this[kUpdateTimer](); | 2372 | 2376 |
| 2373 | | - const headersList = buildNgHeaderString(headers, assertValidPseudoHeaderTrailer); | | 2377 | + const headersList = buildNgHeaderString( | | 2378 | + headers, | | 2379 | + assertValidPseudoHeaderTrailer, | | 2380 | + this.session[kStrictSingleValueFields], | | 2381 | + ); | 2374 | 2382 | this[kSentTrailers] = headers; | 2375 | 2383 |
| 2376 | 2384 | // Send the trailers in setImmediate so we don't do it on nghttp2 stack. | @@ -2579,7 +2587,11 @@ function prepareResponseHeaders(stream, headersParam, options) { | 2579 | 2587 | stream[kSentHeaders] = headers; | 2580 | 2588 | } | 2581 | 2589 |
| 2582 | | - const headersList = buildNgHeaderString(headers, assertValidPseudoHeaderResponse); | | 2590 | + const headersList = buildNgHeaderString( | | 2591 | + headers, | | 2592 | + assertValidPseudoHeaderResponse, | | 2593 | + stream.session[kStrictSingleValueFields], | | 2594 | + ); | 2583 | 2595 |
| 2584 | 2596 | return { headers, headersList, statusCode }; | 2585 | 2597 | } | @@ -2682,7 +2694,11 @@ function processRespondWithFD(self, fd, headers, offset = 0, length = -1, | 2682 | 2694 |
| 2683 | 2695 | let headersList; | 2684 | 2696 | try { | 2685 | | - headersList = buildNgHeaderString(headers, assertValidPseudoHeaderResponse); | | 2697 | + headersList = buildNgHeaderString( | | 2698 | + headers, | | 2699 | + assertValidPseudoHeaderResponse, | | 2700 | + self.session[kStrictSingleValueFields], | | 2701 | + ); | 2686 | 2702 | } catch (err) { | 2687 | 2703 | if (self.ownsFd) | 2688 | 2704 | tryClose(fd); | @@ -2909,7 +2925,11 @@ class ServerHttp2Stream extends Http2Stream { | 2909 | 2925 | if (headers[HTTP2_HEADER_METHOD] === HTTP2_METHOD_HEAD) | 2910 | 2926 | headRequest = options.endStream = true; | 2911 | 2927 |
| 2912 | | - const headersList = buildNgHeaderString(headers); | | 2928 | + const headersList = buildNgHeaderString( | | 2929 | + headers, | | 2930 | + assertValidPseudoHeader, | | 2931 | + this.session[kStrictSingleValueFields], | | 2932 | + ); | 2913 | 2933 |
| 2914 | 2934 | const streamOptions = options.endStream ? STREAM_OPTION_EMPTY_PAYLOAD : 0; | 2915 | 2935 |
| @@ -3173,7 +3193,11 @@ class ServerHttp2Stream extends Http2Stream { | 3173 | 3193 |
| 3174 | 3194 | this[kUpdateTimer](); | 3175 | 3195 |
| 3176 | | - const headersList = buildNgHeaderString(headers, assertValidPseudoHeaderResponse); | | 3196 | + const headersList = buildNgHeaderString( | | 3197 | + headers, | | 3198 | + assertValidPseudoHeaderResponse, | | 3199 | + this.session[kStrictSingleValueFields], | | 3200 | + ); | 3177 | 3201 | if (!this[kInfoHeaders]) | 3178 | 3202 | this[kInfoHeaders] = [headers]; | 3179 | 3203 | else | @@ -3326,21 +3350,30 @@ function initializeOptions(options) { | 3326 | 3350 | } | 3327 | 3351 |
| 3328 | 3352 | if (options.maxSessionInvalidFrames !== undefined) | 3329 | | - validateUint32(options.maxSessionInvalidFrames, 'maxSessionInvalidFrames'); | | 3353 | + validateUint32(options.maxSessionInvalidFrames, 'options.maxSessionInvalidFrames'); | 3330 | 3354 |
| 3331 | 3355 | if (options.maxSessionRejectedStreams !== undefined) { | 3332 | 3356 | validateUint32( | 3333 | 3357 | options.maxSessionRejectedStreams, | 3334 | | - 'maxSessionRejectedStreams', | | 3358 | + 'options.maxSessionRejectedStreams', | 3335 | 3359 | ); | 3336 | 3360 | } | 3337 | 3361 |
| 3338 | 3362 | if (options.unknownProtocolTimeout !== undefined) | 3339 | | - validateUint32(options.unknownProtocolTimeout, 'unknownProtocolTimeout'); | | 3363 | + validateUint32(options.unknownProtocolTimeout, 'options.unknownProtocolTimeout'); | 3340 | 3364 | else | 3341 | 3365 | // TODO(danbev): is this a good default value? | 3342 | 3366 | options.unknownProtocolTimeout = 10000; | 3343 | 3367 |
| | 3368 | + if (options.strictSingleValueFields !== undefined) { | | 3369 | + validateBoolean( | | 3370 | + options.strictSingleValueFields, | | 3371 | + 'options.strictSingleValueFields', | | 3372 | + ); | | 3373 | + } else { | | 3374 | + options.strictSingleValueFields = true; | | 3375 | + } | | 3376 | + | 3344 | 3377 |
| 3345 | 3378 | // Initialize http1Options bag for HTTP/1 fallback when allowHTTP1 is true. | 3346 | 3379 | // This bag is passed to storeHTTPOptions() to configure HTTP/1 server | @@ -3553,6 +3586,15 @@ function connect(authority, options, listener) { | 3553 | 3586 | throw new ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS(); | 3554 | 3587 | } | 3555 | 3588 |
| | 3589 | + if (options.strictSingleValueFields !== undefined) { | | 3590 | + validateBoolean( | | 3591 | + options.strictSingleValueFields, | | 3592 | + 'options.strictSingleValueFields', | | 3593 | + ); | | 3594 | + } else { | | 3595 | + options.strictSingleValueFields = true; | | 3596 | + } | | 3597 | + | 3556 | 3598 | if (typeof authority === 'string') | 3557 | 3599 | authority = new URL(authority); | 3558 | 3600 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/util.js +21-8Lines changed: 21 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ const { | 39 | 39 |
| 40 | 40 | const kAuthority = Symbol('authority'); | 41 | 41 | const kSensitiveHeaders = Symbol('sensitiveHeaders'); | | 42 | +const kStrictSingleValueFields = Symbol('strictSingleValueFields'); | 42 | 43 | const kSocket = Symbol('socket'); | 43 | 44 | const kProtocol = Symbol('protocol'); | 44 | 45 | const kProxySocket = Symbol('proxySocket'); | @@ -121,7 +122,7 @@ const kValidPseudoHeaders = new SafeSet([ | 121 | 122 |
| 122 | 123 | // This set contains headers that are permitted to have only a single | 123 | 124 | // value. Multiple instances must not be specified. | 124 | | -const kSingleValueHeaders = new SafeSet([ | | 125 | +const kSingleValueFields = new SafeSet([ | 125 | 126 | HTTP2_HEADER_STATUS, | 126 | 127 | HTTP2_HEADER_METHOD, | 127 | 128 | HTTP2_HEADER_AUTHORITY, | @@ -690,6 +691,7 @@ function prepareRequestHeadersArray(headers, session) { | 690 | 691 | const headersList = buildNgHeaderString( | 691 | 692 | rawHeaders, | 692 | 693 | assertValidPseudoHeader, | | 694 | + session[kStrictSingleValueFields], | 693 | 695 | ); | 694 | 696 |
| 695 | 697 | return { | @@ -731,7 +733,11 @@ function prepareRequestHeadersObject(headers, session) { | 731 | 733 | throw new ERR_HTTP2_CONNECT_PATH(); | 732 | 734 | } | 733 | 735 |
| 734 | | - const headersList = buildNgHeaderString(headersObject); | | 736 | + const headersList = buildNgHeaderString( | | 737 | + headersObject, | | 738 | + assertValidPseudoHeader, | | 739 | + session[kStrictSingleValueFields], | | 740 | + ); | 735 | 741 |
| 736 | 742 | return { | 737 | 743 | headersObject, | @@ -751,10 +757,15 @@ const kNoHeaderFlags = StringFromCharCode(NGHTTP2_NV_FLAG_NONE); | 751 | 757 | * format, rejecting illegal header configurations, and marking sensitive headers | 752 | 758 | * that should not be indexed en route. This takes either a flat map of | 753 | 759 | * raw headers ([k1, v1, k2, v2]) or a header object ({ k1: v1, k2: [v2, v3] }). | | 760 | + * | | 761 | + * Takes a validation function to check the pseudo-headers allowed for this | | 762 | + * message, and a boolean indicating whether to enforce strict single-value | | 763 | + * header validation. | 754 | 764 | * @returns {[string, number]} | 755 | 765 | */ | 756 | 766 | function buildNgHeaderString(arrayOrMap, | 757 | | - assertValuePseudoHeader = assertValidPseudoHeader) { | | 767 | + validatePseudoHeaderValue, | | 768 | + strictSingleValueFields) { | 758 | 769 | let headers = ''; | 759 | 770 | let pseudoHeaders = ''; | 760 | 771 | let count = 0; | @@ -765,7 +776,8 @@ function buildNgHeaderString(arrayOrMap, | 765 | 776 |
| 766 | 777 | function processHeader(key, value) { | 767 | 778 | key = key.toLowerCase(); | 768 | | - const isSingleValueHeader = kSingleValueHeaders.has(key); | | 779 | + const isStrictSingleValueField = strictSingleValueFields && | | 780 | + kSingleValueFields.has(key); | 769 | 781 | let isArray = ArrayIsArray(value); | 770 | 782 | if (isArray) { | 771 | 783 | switch (value.length) { | @@ -776,13 +788,13 @@ function buildNgHeaderString(arrayOrMap, | 776 | 788 | isArray = false; | 777 | 789 | break; | 778 | 790 | default: | 779 | | - if (isSingleValueHeader) | | 791 | + if (isStrictSingleValueField) | 780 | 792 | throw new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | 781 | 793 | } | 782 | 794 | } else { | 783 | 795 | value = String(value); | 784 | 796 | } | 785 | | - if (isSingleValueHeader) { | | 797 | + if (isStrictSingleValueField) { | 786 | 798 | if (singles.has(key)) | 787 | 799 | throw new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | 788 | 800 | singles.add(key); | @@ -791,7 +803,7 @@ function buildNgHeaderString(arrayOrMap, | 791 | 803 | kNeverIndexFlag : | 792 | 804 | kNoHeaderFlags; | 793 | 805 | if (key[0] === ':') { | 794 | | - const err = assertValuePseudoHeader(key); | | 806 | + const err = validatePseudoHeaderValue(key); | 795 | 807 | if (err !== undefined) | 796 | 808 | throw err; | 797 | 809 | pseudoHeaders += `${key}\0${value}\0${flags}`; | @@ -893,7 +905,7 @@ function toHeaderObject(headers, sensitiveHeaders) { | 893 | 905 | const existing = obj[name]; | 894 | 906 | if (existing === undefined) { | 895 | 907 | obj[name] = name === HTTP2_HEADER_SET_COOKIE ? [value] : value; | 896 | | - } else if (!kSingleValueHeaders.has(name)) { | | 908 | + } else if (!kSingleValueFields.has(name)) { | 897 | 909 | switch (name) { | 898 | 910 | case HTTP2_HEADER_COOKIE: | 899 | 911 | // https://tools.ietf.org/html/rfc7540#section-8.1.2.5 | @@ -970,6 +982,7 @@ module.exports = { | 970 | 982 | isPayloadMeaningless, | 971 | 983 | kAuthority, | 972 | 984 | kSensitiveHeaders, | | 985 | + kStrictSingleValueFields, | 973 | 986 | kSocket, | 974 | 987 | kProtocol, | 975 | 988 | kProxySocket, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/quic/quic.js +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -112,6 +112,7 @@ const { | 112 | 112 |
| 113 | 113 | const { | 114 | 114 | buildNgHeaderString, | | 115 | + assertValidPseudoHeader, | 115 | 116 | } = require('internal/http2/util'); | 116 | 117 |
| 117 | 118 | const kEmptyObject = { __proto__: null }; | @@ -887,8 +888,13 @@ class QuicStream { | 887 | 888 | } else { | 888 | 889 | debug(`stream ${this.id} sending headers`, headers); | 889 | 890 | } | | 891 | + const headerString = buildNgHeaderString( | | 892 | + headers, | | 893 | + assertValidPseudoHeader, | | 894 | + true, // This could become an option in future | | 895 | + ); | 890 | 896 | // TODO(@jasnell): Support differentiating between early headers, primary headers, etc | 891 | | - return this.#handle.sendHeaders(1, buildNgHeaderString(headers), 1); | | 897 | + return this.#handle.sendHeaders(1, headerString, 1); | 892 | 898 | } | 893 | 899 |
| 894 | 900 | [kFinishClose](error) { | |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | +const assert = require('assert'); | | 7 | +const http2 = require('http2'); | | 8 | + | | 9 | +const server = http2.createServer({ | | 10 | + strictSingleValueFields: false | | 11 | +}); | | 12 | + | | 13 | +server.on('stream', common.mustCall((stream, _headers, _flags, rawHeaders) => { | | 14 | + assert.deepStrictEqual(rawHeaders.slice(8), [ | | 15 | + 'user-agent', 'abc', | | 16 | + 'user-agent', 'xyz', | | 17 | + 'referer', 'qwe', | | 18 | + 'referer', 'asd', | | 19 | + ]); | | 20 | + | | 21 | + stream.respond({ | | 22 | + ':status': 200, | | 23 | + 'expires': 'Thu, 01 Jan 1970 00:00:00 GMT', | | 24 | + 'EXPIRES': 'Thu, 01 Jan 1970 00:00:00 GMT', | | 25 | + 'content-type': ['a', 'b'], | | 26 | + }); | | 27 | + | | 28 | + stream.end(); | | 29 | +})); | | 30 | + | | 31 | +server.listen(0, common.mustCall(() => { | | 32 | + const client = http2.connect(`http://localhost:${server.address().port}`, { | | 33 | + strictSingleValueFields: false | | 34 | + }); | | 35 | + | | 36 | + const res = client.request({ | | 37 | + 'user-agent': 'abc', | | 38 | + 'USER-AGENT': 'xyz', | | 39 | + 'referer': ['qwe', 'asd'], | | 40 | + }); | | 41 | + | | 42 | + res.on('response', common.mustCall((_headers, _flags, rawHeaders) => { | | 43 | + assert.deepStrictEqual(rawHeaders.slice(2, 10), [ | | 44 | + 'expires', 'Thu, 01 Jan 1970 00:00:00 GMT', | | 45 | + 'expires', 'Thu, 01 Jan 1970 00:00:00 GMT', | | 46 | + 'content-type', 'a', | | 47 | + 'content-type', 'b', | | 48 | + ]); | | 49 | + | | 50 | + server.close(); | | 51 | + client.close(); | | 52 | + })); | | 53 | +})); |
Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-util-assert-valid-pseudoheader.js +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | require('../common'); | 5 | 5 | const assert = require('assert'); | 6 | 6 |
| 7 | | -// Tests the assertValidPseudoHeader function that is used within the | 8 | | -// buildNgHeaderString function. The assert function is not exported so we | 9 | | -// have to test it through buildNgHeaderString | 10 | | - | 11 | | -const { buildNgHeaderString } = require('internal/http2/util'); | | 7 | +const { assertValidPseudoHeader } = require('internal/http2/util'); | 12 | 8 |
| 13 | 9 | // These should not throw | 14 | | -buildNgHeaderString({ ':status': 'a' }); | 15 | | -buildNgHeaderString({ ':path': 'a' }); | 16 | | -buildNgHeaderString({ ':authority': 'a' }); | 17 | | -buildNgHeaderString({ ':scheme': 'a' }); | 18 | | -buildNgHeaderString({ ':method': 'a' }); | | 10 | +assertValidPseudoHeader(':status'); | | 11 | +assertValidPseudoHeader(':path'); | | 12 | +assertValidPseudoHeader(':authority'); | | 13 | +assertValidPseudoHeader(':scheme'); | | 14 | +assertValidPseudoHeader(':method'); | 19 | 15 |
| 20 | | -assert.throws(() => buildNgHeaderString({ ':foo': 'a' }), { | | 16 | +assert.throws(() => assertValidPseudoHeader(':foo'), { | 21 | 17 | code: 'ERR_HTTP2_INVALID_PSEUDOHEADER', | 22 | 18 | name: 'TypeError', | 23 | 19 | message: '":foo" is an invalid pseudoheader or is used incorrectly' | |
|