Skip to content

Commit 2cc38ad

Browse files
authored
fix: webidl.brandcheck non strict should throw (#2683)
* fix: webidl.brandcheck non strict should throw * add brandcheck test * add one more test case * one more test for globalThis.Headers * remove test * add brandcheck for all cases
1 parent d36b19e commit 2cc38ad

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

lib/fetch/webidl.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ webidl.errors.invalidArgument = function (context) {
3434

3535
// https://webidl.spec.whatwg.org/#implements
3636
webidl.brandCheck = function (V, I, opts = undefined) {
37-
if (opts?.strict !== false && !(V instanceof I)) {
38-
throw new TypeError('Illegal invocation')
37+
if (opts?.strict !== false) {
38+
if (!(V instanceof I)) {
39+
throw new TypeError('Illegal invocation')
40+
}
3941
} else {
40-
return V?.[Symbol.toStringTag] === I.prototype[Symbol.toStringTag]
42+
if (V?.[Symbol.toStringTag] !== I.prototype[Symbol.toStringTag]) {
43+
throw new TypeError('Illegal invocation')
44+
}
4145
}
4246
}
4347

test/cookie/cookies.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,113 @@ test('Set-Cookie parser', () => {
599599
headers = new Headers()
600600
assert.deepEqual(getSetCookies(headers), [])
601601
})
602+
603+
test('Cookie setCookie throws if headers is not of type Headers', () => {
604+
class Headers {
605+
[Symbol.toStringTag] = 'CustomHeaders'
606+
}
607+
const headers = new Headers()
608+
assert.throws(
609+
() => {
610+
setCookie(headers, {
611+
name: 'key',
612+
value: 'Cat',
613+
httpOnly: true,
614+
secure: true,
615+
maxAge: 3
616+
})
617+
},
618+
new TypeError('Illegal invocation')
619+
)
620+
})
621+
622+
test('Cookie setCookie does not throw if headers is an instance of undici owns Headers class', () => {
623+
const headers = new Headers()
624+
setCookie(headers, {
625+
name: 'key',
626+
value: 'Cat',
627+
httpOnly: true,
628+
secure: true,
629+
maxAge: 3
630+
})
631+
})
632+
633+
test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => {
634+
const headers = new globalThis.Headers()
635+
setCookie(headers, {
636+
name: 'key',
637+
value: 'Cat',
638+
httpOnly: true,
639+
secure: true,
640+
maxAge: 3
641+
})
642+
})
643+
644+
test('Cookie getCookies throws if headers is not of type Headers', () => {
645+
class Headers {
646+
[Symbol.toStringTag] = 'CustomHeaders'
647+
}
648+
const headers = new Headers()
649+
assert.throws(
650+
() => {
651+
getCookies(headers)
652+
},
653+
new TypeError('Illegal invocation')
654+
)
655+
})
656+
657+
test('Cookie getCookies does not throw if headers is an instance of undici owns Headers class', () => {
658+
const headers = new Headers()
659+
getCookies(headers)
660+
})
661+
662+
test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => {
663+
const headers = new globalThis.Headers()
664+
getCookies(headers)
665+
})
666+
667+
test('Cookie getSetCookies throws if headers is not of type Headers', () => {
668+
class Headers {
669+
[Symbol.toStringTag] = 'CustomHeaders'
670+
}
671+
const headers = new Headers({ 'set-cookie': 'Space=Cat' })
672+
assert.throws(
673+
() => {
674+
getSetCookies(headers)
675+
},
676+
new TypeError('Illegal invocation')
677+
)
678+
})
679+
680+
test('Cookie getSetCookies does not throw if headers is an instance of undici owns Headers class', () => {
681+
const headers = new Headers({ 'set-cookie': 'Space=Cat' })
682+
getSetCookies(headers)
683+
})
684+
685+
test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => {
686+
const headers = new globalThis.Headers({ 'set-cookie': 'Space=Cat' })
687+
getSetCookies(headers)
688+
})
689+
690+
test('Cookie deleteCookie throws if headers is not of type Headers', () => {
691+
class Headers {
692+
[Symbol.toStringTag] = 'CustomHeaders'
693+
}
694+
const headers = new Headers()
695+
assert.throws(
696+
() => {
697+
deleteCookie(headers, 'deno')
698+
},
699+
new TypeError('Illegal invocation')
700+
)
701+
})
702+
703+
test('Cookie deleteCookie does not throw if headers is an instance of undici owns Headers class', () => {
704+
const headers = new Headers()
705+
deleteCookie(headers, 'deno')
706+
})
707+
708+
test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => {
709+
const headers = new globalThis.Headers()
710+
deleteCookie(headers, 'deno')
711+
})

0 commit comments

Comments
 (0)