From 678f0f485a6062770e924dbe0f554ef03b7af27d Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 1 Feb 2024 09:35:13 +0100 Subject: [PATCH 1/6] fix: webidl.brandcheck non strict should throw --- lib/fetch/webidl.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/fetch/webidl.js b/lib/fetch/webidl.js index cb6bcc37db9..d639b8b7668 100644 --- a/lib/fetch/webidl.js +++ b/lib/fetch/webidl.js @@ -34,10 +34,14 @@ webidl.errors.invalidArgument = function (context) { // https://webidl.spec.whatwg.org/#implements webidl.brandCheck = function (V, I, opts = undefined) { - if (opts?.strict !== false && !(V instanceof I)) { - throw new TypeError('Illegal invocation') + if (opts?.strict !== false) { + if (!(V instanceof I)) { + throw new TypeError('Illegal invocation') + } } else { - return V?.[Symbol.toStringTag] === I.prototype[Symbol.toStringTag] + if (V?.[Symbol.toStringTag] !== I.prototype[Symbol.toStringTag]) { + throw new TypeError('Illegal invocation') + } } } From 025c3aa23d6328a52c5300ce16b13ead290ebc36 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 3 Feb 2024 18:25:02 +0100 Subject: [PATCH 2/6] add brandcheck test --- test/cookie/cookies.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/cookie/cookies.js b/test/cookie/cookies.js index 7859c6b2618..7c0f67afe96 100644 --- a/test/cookie/cookies.js +++ b/test/cookie/cookies.js @@ -599,3 +599,37 @@ test('Set-Cookie parser', () => { headers = new Headers() assert.deepEqual(getSetCookies(headers), []) }) + +test('Cookie setCookie throws if headers is not of type Headers', () => { + class Headers { + [Symbol.toStringTag] = 'CustomHeaders' + } + const headers = new Headers() + assert.throws( + () => { + setCookie(headers, { + name: 'key', + value: 'Cat', + httpOnly: true, + secure: true, + maxAge: 3 + }) + }, + new TypeError('Illegal invocation') + ) +}) + +test('Cookie setCookie does not throw if headers is an instance of a custom Headers class', () => { + class Headers { + [Symbol.toStringTag] = 'Headers' + append () { } + } + const headers = new Headers() + setCookie(headers, { + name: 'key', + value: 'Cat', + httpOnly: true, + secure: true, + maxAge: 3 + }) +}) From 5ad739e2596bb0b869a165ebcb8d9d10757fe650 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sat, 3 Feb 2024 20:26:17 +0100 Subject: [PATCH 3/6] add one more test case --- test/cookie/cookies.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/cookie/cookies.js b/test/cookie/cookies.js index 7c0f67afe96..eea766583ca 100644 --- a/test/cookie/cookies.js +++ b/test/cookie/cookies.js @@ -633,3 +633,14 @@ test('Cookie setCookie does not throw if headers is an instance of a custom Head maxAge: 3 }) }) + +test('Cookie setCookie does not throw if headers is an instance of undici owns Headers class', () => { + const headers = new Headers() + setCookie(headers, { + name: 'key', + value: 'Cat', + httpOnly: true, + secure: true, + maxAge: 3 + }) +}) From 83cbe4020cc4c237edd2c20df5ebc87e13bb8c38 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sat, 3 Feb 2024 20:27:26 +0100 Subject: [PATCH 4/6] one more test for globalThis.Headers --- test/cookie/cookies.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/cookie/cookies.js b/test/cookie/cookies.js index eea766583ca..eca0505bf02 100644 --- a/test/cookie/cookies.js +++ b/test/cookie/cookies.js @@ -644,3 +644,14 @@ test('Cookie setCookie does not throw if headers is an instance of undici owns H maxAge: 3 }) }) + +test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => { + const headers = new globalThis.Headers() + setCookie(headers, { + name: 'key', + value: 'Cat', + httpOnly: true, + secure: true, + maxAge: 3 + }) +}) From 46b8762b82fc21252d2a740f2296b0300917140d Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sat, 3 Feb 2024 21:37:50 +0100 Subject: [PATCH 5/6] remove test --- test/cookie/cookies.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/cookie/cookies.js b/test/cookie/cookies.js index eca0505bf02..8f50188d0d4 100644 --- a/test/cookie/cookies.js +++ b/test/cookie/cookies.js @@ -619,21 +619,6 @@ test('Cookie setCookie throws if headers is not of type Headers', () => { ) }) -test('Cookie setCookie does not throw if headers is an instance of a custom Headers class', () => { - class Headers { - [Symbol.toStringTag] = 'Headers' - append () { } - } - const headers = new Headers() - setCookie(headers, { - name: 'key', - value: 'Cat', - httpOnly: true, - secure: true, - maxAge: 3 - }) -}) - test('Cookie setCookie does not throw if headers is an instance of undici owns Headers class', () => { const headers = new Headers() setCookie(headers, { From 2fe3a7da367f52dcf812ae8362d8300777185526 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 3 Feb 2024 22:14:57 +0100 Subject: [PATCH 6/6] add brandcheck for all cases --- test/cookie/cookies.js | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test/cookie/cookies.js b/test/cookie/cookies.js index 8f50188d0d4..d6fed72be16 100644 --- a/test/cookie/cookies.js +++ b/test/cookie/cookies.js @@ -640,3 +640,72 @@ test('Cookie setCookie does not throw if headers is an instance of the global He maxAge: 3 }) }) + +test('Cookie getCookies throws if headers is not of type Headers', () => { + class Headers { + [Symbol.toStringTag] = 'CustomHeaders' + } + const headers = new Headers() + assert.throws( + () => { + getCookies(headers) + }, + new TypeError('Illegal invocation') + ) +}) + +test('Cookie getCookies does not throw if headers is an instance of undici owns Headers class', () => { + const headers = new Headers() + getCookies(headers) +}) + +test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => { + const headers = new globalThis.Headers() + getCookies(headers) +}) + +test('Cookie getSetCookies throws if headers is not of type Headers', () => { + class Headers { + [Symbol.toStringTag] = 'CustomHeaders' + } + const headers = new Headers({ 'set-cookie': 'Space=Cat' }) + assert.throws( + () => { + getSetCookies(headers) + }, + new TypeError('Illegal invocation') + ) +}) + +test('Cookie getSetCookies does not throw if headers is an instance of undici owns Headers class', () => { + const headers = new Headers({ 'set-cookie': 'Space=Cat' }) + getSetCookies(headers) +}) + +test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => { + const headers = new globalThis.Headers({ 'set-cookie': 'Space=Cat' }) + getSetCookies(headers) +}) + +test('Cookie deleteCookie throws if headers is not of type Headers', () => { + class Headers { + [Symbol.toStringTag] = 'CustomHeaders' + } + const headers = new Headers() + assert.throws( + () => { + deleteCookie(headers, 'deno') + }, + new TypeError('Illegal invocation') + ) +}) + +test('Cookie deleteCookie does not throw if headers is an instance of undici owns Headers class', () => { + const headers = new Headers() + deleteCookie(headers, 'deno') +}) + +test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => { + const headers = new globalThis.Headers() + deleteCookie(headers, 'deno') +})