From 500448e13327d1450dbc12cf08550171af533cfb Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 28 Nov 2023 16:09:53 +0900 Subject: [PATCH 1/3] fix(helper/cookie): make default `path` as `/` for `getCookie()`/`setSignedCookie()` --- src/helper/cookie/index.test.ts | 41 +++++++++++++++++++++++++++++---- src/helper/cookie/index.ts | 4 ++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/helper/cookie/index.test.ts b/src/helper/cookie/index.test.ts index 3517da54b..8cee5859a 100644 --- a/src/helper/cookie/index.test.ts +++ b/src/helper/cookie/index.test.ts @@ -148,7 +148,19 @@ describe('Cookie Middleware', () => { const res = await app.request('http://localhost/set-cookie') expect(res.status).toBe(200) const header = res.headers.get('Set-Cookie') - expect(header).toBe('delicious_cookie=macha') + expect(header).toBe('delicious_cookie=macha; Path=/') + }) + + app.get('/a/set-cookie-path', (c) => { + setCookie(c, 'delicious_cookie', 'macha', { path: '/a' }) + return c.text('Give cookie') + }) + + it('Set cookie with setCookie() and path option', async () => { + const res = await app.request('http://localhost/a/set-cookie-path') + expect(res.status).toBe(200) + const header = res.headers.get('Set-Cookie') + expect(header).toBe('delicious_cookie=macha; Path=/a') }) app.get('/set-signed-cookie', async (c) => { @@ -161,7 +173,24 @@ describe('Cookie Middleware', () => { const res = await app.request('http://localhost/set-signed-cookie') expect(res.status).toBe(200) const header = res.headers.get('Set-Cookie') - expect(header).toBe('delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D') + expect(header).toBe( + 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/' + ) + }) + + app.get('/a/set-signed-cookie-path', async (c) => { + const secret = 'secret chocolate chips' + await setSignedCookie(c, 'delicious_cookie', 'macha', secret, { path: '/a' }) + return c.text('Give signed cookie') + }) + + it('Set signed cookie with setSignedCookie() and path option', async () => { + const res = await app.request('http://localhost/a/set-signed-cookie-path') + expect(res.status).toBe(200) + const header = res.headers.get('Set-Cookie') + expect(header).toBe( + 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/a' + ) }) app.get('/set-cookie-complex', (c) => { @@ -219,7 +248,7 @@ describe('Cookie Middleware', () => { const res = await app.request('http://localhost/set-cookie-multiple') expect(res.status).toBe(200) const header = res.headers.get('Set-Cookie') - expect(header).toBe('delicious_cookie=macha, delicious_cookie=choco') + expect(header).toBe('delicious_cookie=macha; Path=/, delicious_cookie=choco; Path=/') }) }) @@ -235,7 +264,7 @@ describe('Cookie Middleware', () => { const res2 = await app.request('http://localhost/delete-cookie') expect(res2.status).toBe(200) const header2 = res2.headers.get('Set-Cookie') - expect(header2).toBe('delicious_cookie=; Max-Age=0') + expect(header2).toBe('delicious_cookie=; Max-Age=0; Path=/') }) app.get('/delete-cookie-multiple', (c) => { @@ -248,7 +277,9 @@ describe('Cookie Middleware', () => { const res2 = await app.request('http://localhost/delete-cookie-multiple') expect(res2.status).toBe(200) const header2 = res2.headers.get('Set-Cookie') - expect(header2).toBe('delicious_cookie=; Max-Age=0, delicious_cookie2=; Max-Age=0') + expect(header2).toBe( + 'delicious_cookie=; Max-Age=0; Path=/, delicious_cookie2=; Max-Age=0; Path=/' + ) }) app.get('/delete-cookie-with-options', (c) => { diff --git a/src/helper/cookie/index.ts b/src/helper/cookie/index.ts index aab040ca4..cd7f638e9 100644 --- a/src/helper/cookie/index.ts +++ b/src/helper/cookie/index.ts @@ -39,7 +39,7 @@ export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => { } export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => { - const cookie = serialize(name, value, opt) + const cookie = serialize(name, value, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true }) } @@ -50,7 +50,7 @@ export const setSignedCookie = async ( secret: string | BufferSource, opt?: CookieOptions ): Promise => { - const cookie = await serializeSigned(name, value, secret, opt) + const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true }) } From d9fd82ed0add155c3c703328f9708162417c9f0c Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 28 Nov 2023 16:15:23 +0900 Subject: [PATCH 2/3] denoify --- deno_dist/helper/cookie/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deno_dist/helper/cookie/index.ts b/deno_dist/helper/cookie/index.ts index 46596cae6..2d4ce36e2 100644 --- a/deno_dist/helper/cookie/index.ts +++ b/deno_dist/helper/cookie/index.ts @@ -39,7 +39,7 @@ export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => { } export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => { - const cookie = serialize(name, value, opt) + const cookie = serialize(name, value, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true }) } @@ -50,7 +50,7 @@ export const setSignedCookie = async ( secret: string | BufferSource, opt?: CookieOptions ): Promise => { - const cookie = await serializeSigned(name, value, secret, opt) + const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true }) } From f439000ad8a15908a4592fbd104461b156167f65 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 28 Nov 2023 16:19:33 +0900 Subject: [PATCH 3/3] fixed test --- runtime_tests/lambda/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime_tests/lambda/index.test.ts b/runtime_tests/lambda/index.test.ts index 74d074512..032991a32 100644 --- a/runtime_tests/lambda/index.test.ts +++ b/runtime_tests/lambda/index.test.ts @@ -95,14 +95,14 @@ describe('AWS Lambda Adapter for Hono', () => { key: 'id', value: crypto.randomUUID(), get serialized() { - return `${this.key}=${this.value}` + return `${this.key}=${this.value}; Path=/` }, } const testCookie2 = { key: 'secret', value: crypto.randomUUID(), get serialized() { - return `${this.key}=${this.value}` + return `${this.key}=${this.value}; Path=/` }, }