From a50dab683f0d902bcfab53ac5a351c661d816354 Mon Sep 17 00:00:00 2001 From: William Perron Date: Thu, 29 Apr 2021 13:56:59 -0400 Subject: fix(op_crate/fetch): infinite loop on fill headers (#10406) Fixes a pesky bug in the fetch implementation where if the init part is specified in `fetch` instead of the `Request` constructor, the fillHeaders function receives two references to the same object, causing it to append to the same list being iterated over. --- cli/tests/unit/fetch_test.ts | 15 +++++++++++++++ op_crates/fetch/23_request.js | 7 +++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index a46104ff8..e046e9c03 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -447,6 +447,21 @@ unitTest( }, ); +unitTest( + { perms: { net: true } }, + async function fetchSeparateInit(): Promise { + // related to: https://github.com/denoland/deno/issues/10396 + const req = new Request("http://localhost:4545/cli/tests/001_hello.js"); + const init = { + method: "GET", + }; + req.headers.set("foo", "bar"); + const res = await fetch(req, init); + assertEquals(res.status, 200); + await res.text(); + }, +); + unitTest( { perms: { net: true } }, async function fetchInitTypedArrayBody(): Promise { diff --git a/op_crates/fetch/23_request.js b/op_crates/fetch/23_request.js index 0e4bd2a3f..603a37a5f 100644 --- a/op_crates/fetch/23_request.js +++ b/op_crates/fetch/23_request.js @@ -247,11 +247,14 @@ // 31. if (Object.keys(init).length > 0) { - let headers = headerListFromHeaders(this[_headers]); + let headers = headerListFromHeaders(this[_headers]).slice( + 0, + headerListFromHeaders(this[_headers]).length, + ); if (init.headers !== undefined) { headers = init.headers; } - headerListFromHeaders(this[_headers]).slice( + headerListFromHeaders(this[_headers]).splice( 0, headerListFromHeaders(this[_headers]).length, ); -- cgit v1.2.3