From d5f81a31b3bc7ee5110402d3bf1ccde03a070919 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 25 Apr 2020 15:47:37 +0530 Subject: [PATCH 1/5] http2: use `Object.create(null)` for `getHeaders` refactor `getHeaders` to initialize headers using `Object.create(null)` Refs: https://github.com/nodejs/node/issues/29829 fixup: lint --- lib/internal/http2/compat.js | 2 +- test/parallel/test-http2-compat-serverresponse-headers.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 3abe9ba2ac6f82..6fd2fdb190b3fc 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -552,7 +552,7 @@ class Http2ServerResponse extends Stream { } getHeaders() { - return { ...this[kHeaders] }; + return this[kHeaders]; } hasHeader(name) { diff --git a/test/parallel/test-http2-compat-serverresponse-headers.js b/test/parallel/test-http2-compat-serverresponse-headers.js index 14e480f611857a..6a458bbb18d35e 100644 --- a/test/parallel/test-http2-compat-serverresponse-headers.js +++ b/test/parallel/test-http2-compat-serverresponse-headers.js @@ -102,11 +102,12 @@ server.listen(0, common.mustCall(function() { response.setHeader(real, expectedValue); const expectedHeaderNames = [real]; assert.deepStrictEqual(response.getHeaderNames(), expectedHeaderNames); - const expectedHeaders = { [real]: expectedValue }; + const expectedHeaders = Object.create(null); + expectedHeaders[real] = expectedValue; assert.deepStrictEqual(response.getHeaders(), expectedHeaders); response.getHeaders()[fake] = fake; - assert.strictEqual(response.hasHeader(fake), false); + assert.strictEqual(response.hasHeader(fake), true); assert.strictEqual(response.sendDate, true); response.sendDate = false; From 92d94811bc6fb8fd4b48e46556ae34cf2bd5bfc4 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 9 May 2020 19:08:58 +0530 Subject: [PATCH 2/5] fixup --- lib/internal/http2/compat.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 6fd2fdb190b3fc..ab8b747fcd14ea 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -3,6 +3,7 @@ const { ArrayIsArray, Boolean, + Object, ObjectAssign, ObjectCreate, ObjectKeys, @@ -552,7 +553,9 @@ class Http2ServerResponse extends Stream { } getHeaders() { - return this[kHeaders]; + const headers = Object.create(null); + Object.assign(headers, this[kHeaders]); + return headers; } hasHeader(name) { From cc0396d8982058f861586b1bb9132d08842da0ee Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 9 May 2020 19:15:40 +0530 Subject: [PATCH 3/5] fixup --- lib/internal/http2/compat.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index ab8b747fcd14ea..022b1913e295ab 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -3,7 +3,6 @@ const { ArrayIsArray, Boolean, - Object, ObjectAssign, ObjectCreate, ObjectKeys, @@ -553,8 +552,8 @@ class Http2ServerResponse extends Stream { } getHeaders() { - const headers = Object.create(null); - Object.assign(headers, this[kHeaders]); + const headers = ObjectCreate(null); + ObjectAssign(headers, this[kHeaders]); return headers; } From ab51769cbe254a1e18b1cdb3bbdde82a51bc1b22 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sun, 24 May 2020 06:15:42 +0530 Subject: [PATCH 4/5] fixup --- test/parallel/test-http2-compat-serverresponse-headers.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-http2-compat-serverresponse-headers.js b/test/parallel/test-http2-compat-serverresponse-headers.js index 6a458bbb18d35e..96875e1bce3400 100644 --- a/test/parallel/test-http2-compat-serverresponse-headers.js +++ b/test/parallel/test-http2-compat-serverresponse-headers.js @@ -107,7 +107,8 @@ server.listen(0, common.mustCall(function() { assert.deepStrictEqual(response.getHeaders(), expectedHeaders); response.getHeaders()[fake] = fake; - assert.strictEqual(response.hasHeader(fake), true); + assert.strictEqual(response.hasHeader(fake), false); + assert.strictEqual(Object.getPrototypeOf(response.getHeaders()), null); assert.strictEqual(response.sendDate, true); response.sendDate = false; From 9094028da8a4b853d2788843e6f391b5e2528b2c Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sun, 24 May 2020 06:20:37 +0530 Subject: [PATCH 5/5] fixup --- lib/internal/http2/compat.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 022b1913e295ab..73371de2dafb0b 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -553,8 +553,7 @@ class Http2ServerResponse extends Stream { getHeaders() { const headers = ObjectCreate(null); - ObjectAssign(headers, this[kHeaders]); - return headers; + return ObjectAssign(headers, this[kHeaders]); } hasHeader(name) {