Skip to content

Commit

Permalink
fix: webidl.brandcheck non strict should throw (nodejs#2683)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Uzlopak authored and crysmags committed Feb 21, 2024
1 parent f499bb8 commit af796c7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}

Expand Down
110 changes: 110 additions & 0 deletions test/cookie/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,113 @@ 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 undici owns Headers class', () => {
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 the global Headers class', () => {
const headers = new globalThis.Headers()
setCookie(headers, {
name: 'key',
value: 'Cat',
httpOnly: true,
secure: true,
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')
})

0 comments on commit af796c7

Please sign in to comment.