diff options
author | Ahab <ahabhgk@gmail.com> | 2021-09-30 00:42:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-29 18:42:06 +0200 |
commit | 923d9c77865730232094f3788e6b1b2a62243e11 (patch) | |
tree | 49b1821a8b1f53a3faad9da5b180e22abbf6c415 /cli/tests/unit/fetch_test.ts | |
parent | c896ba2e1949047ce9a46f1aca27f30632911eb4 (diff) |
fix(ext/fetch): avoid panic when header is invalid (#12244)
Diffstat (limited to 'cli/tests/unit/fetch_test.ts')
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index eca62f8eb..5bce2af43 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -1299,3 +1299,51 @@ unitTest( } }, ); + +unitTest( + { permissions: { net: true } }, + async function fetchHeaderValueShouldNotPanic() { + for (let i = 0; i < 0x21; i++) { + if (i === 0x09 || i === 0x0A || i === 0x0D || i === 0x20) { + continue; // these header value will be normalized, will not cause an error. + } + // ensure there will be an error instead of panic. + await assertRejects(() => + fetch("http://localhost:4545/echo_server", { + method: "HEAD", + headers: { "val": String.fromCharCode(i) }, + }), TypeError); + } + await assertRejects(() => + fetch("http://localhost:4545/echo_server", { + method: "HEAD", + headers: { "val": String.fromCharCode(127) }, + }), TypeError); + }, +); + +unitTest( + { permissions: { net: true } }, + async function fetchHeaderNameShouldNotPanic() { + const validTokens = + "!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUWVXYZ^_`abcdefghijklmnopqrstuvwxyz|~" + .split(""); + for (let i = 0; i <= 255; i++) { + const token = String.fromCharCode(i); + if (validTokens.includes(token)) { + continue; + } + // ensure there will be an error instead of panic. + await assertRejects(() => + fetch("http://localhost:4545/echo_server", { + method: "HEAD", + headers: { [token]: "value" }, + }), TypeError); + } + await assertRejects(() => + fetch("http://localhost:4545/echo_server", { + method: "HEAD", + headers: { "": "value" }, + }), TypeError); + }, +); |