Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webidl.brandcheck non strict should throw #2683

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
34 changes: 34 additions & 0 deletions test/cookie/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
setCookie(headers, {
name: 'key',
value: 'Cat',
httpOnly: true,
secure: true,
maxAge: 3
})
})