Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ export const unsignCookie = async (input: string, secret: string | null) => {
}

const tentativeValue = input.slice(0, dot)

if (secret === null) return false

const expectedInput = await signCookie(tentativeValue, secret)

return constantTimeEqual(expectedInput, input) ? tentativeValue : false
Expand Down
16 changes: 16 additions & 0 deletions test/cookie/signature.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'bun:test'
import { parseCookie, Cookie } from '../../src/cookies'
import { signCookie } from '../../src/utils'
import { InvalidCookieSignature } from '../../src/error'

describe('Parse Cookie', () => {
it('handle empty cookie', async () => {
Expand Down Expand Up @@ -134,4 +135,19 @@ describe('Parse Cookie', () => {

expect(result.fischl.value).toEqual('fischl')
})

it('rejects invalid signature with null secret in rotation', async () => {
const set = {
headers: {},
cookie: {}
}

const cookieString = `wizard=1.invalid_sign`
await expect(
parseCookie(set, cookieString, {
secrets: ['valid-secret', null],
sign: ['wizard']
})
).rejects.toThrow(InvalidCookieSignature)
})
})