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 all 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
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')
})