Skip to content
Closed
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
118 changes: 59 additions & 59 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,65 +565,6 @@ export const composeHandler = ({
`
}

if (hasCookie) {
const get = (name: keyof CookieOptions, defaultValue?: unknown) => {
// @ts-ignore
const value = cookieMeta?.[name] ?? defaultValue
if (!value)
return typeof defaultValue === 'string'
? `${name}: "${defaultValue}",`
: `${name}: ${defaultValue},`

if (typeof value === 'string') return `${name}: '${value}',`
if (value instanceof Date)
return `${name}: new Date(${value.getTime()}),`

return `${name}: ${value},`
}

const options = cookieMeta
? `{
secrets: ${
cookieMeta.secrets !== undefined
? typeof cookieMeta.secrets === 'string'
? `'${cookieMeta.secrets}'`
: '[' +
cookieMeta.secrets.reduce(
(a, b) => a + `'${b}',`,
''
) +
']'
: 'undefined'
},
sign: ${
cookieMeta.sign === true
? true
: cookieMeta.sign !== undefined
? '[' +
cookieMeta.sign.reduce(
(a, b) => a + `'${b}',`,
''
) +
']'
: 'undefined'
},
${get('domain')}
${get('expires')}
${get('httpOnly')}
${get('maxAge')}
${get('path', '/')}
${get('priority')}
${get('sameSite')}
${get('secure')}
}`
: 'undefined'

if (hasHeaders)
fnLiteral += `\nc.cookie = await parseCookie(c.set, c.headers.cookie, ${options})\n`
else
fnLiteral += `\nc.cookie = await parseCookie(c.set, c.request.headers.get('cookie'), ${options})\n`
}

if (hasQuery) {
const destructured = <
{
Expand Down Expand Up @@ -889,6 +830,65 @@ export const composeHandler = ({

const requestMapper = `, c.request`

if (hasCookie) {
const get = (name: keyof CookieOptions, defaultValue?: unknown) => {
// @ts-ignore
const value = cookieMeta?.[name] ?? defaultValue
if (!value)
return typeof defaultValue === 'string'
? `${name}: "${defaultValue}",`
: `${name}: ${defaultValue},`

if (typeof value === 'string') return `${name}: '${value}',`
if (value instanceof Date)
return `${name}: new Date(${value.getTime()}),`

return `${name}: ${value},`
}

const options = cookieMeta
? `{
secrets: ${
cookieMeta.secrets !== undefined
? typeof cookieMeta.secrets === 'string'
? `'${cookieMeta.secrets}'`
: '[' +
cookieMeta.secrets.reduce(
(a, b) => a + `'${b}',`,
''
) +
']'
: 'undefined'
},
sign: ${
cookieMeta.sign === true
? true
: cookieMeta.sign !== undefined
? '[' +
cookieMeta.sign.reduce(
(a, b) => a + `'${b}',`,
''
) +
']'
: 'undefined'
},
${get('domain')}
${get('expires')}
${get('httpOnly')}
${get('maxAge')}
${get('path', '/')}
${get('priority')}
${get('sameSite')}
${get('secure')}
}`
: 'undefined'

if (hasHeaders)
fnLiteral += `\nc.cookie = await parseCookie(c.set, c.headers.cookie, ${options})\n`
else
fnLiteral += `\nc.cookie = await parseCookie(c.set, c.request.headers.get('cookie'), ${options})\n`
}

fnLiteral += `c.route = \`${path}\`\n`

const parseReporter = report('parse', {
Expand Down
22 changes: 22 additions & 0 deletions test/lifecycle/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,26 @@ describe('error', () => {
somePretty: 'json'
})
})

it('handle cookie signature error', async () => {
const app = new Elysia({
cookie: { secrets: 'secrets', sign: ['session'] }
})
.onError(({ code, error }) => {
if (code === 'INVALID_COOKIE_SIGNATURE')
return 'Where is the signature?'
})
.get('/', ({ cookie: { session } }) => '')

const root = await app.handle(
new Request('http://localhost/', {
headers: {
Cookie: 'session=1234'
}
})
)

expect(await root.text()).toBe('Where is the signature?')
expect(root.status).toBe(400)
})
})