Skip to content
Myr

feat(csrf): Support async `IsAllowedSecFetchSiteHandler` (#4559)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: support async IsAllowedSecFetchSiteHandler * remove return type * ci: apply automated fixes ---------

authored by Yuichiro Yamashita · committed by GitHub · parent 489afe6 · Browse files · 2 files+48 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+48 added−4 removed

  • tests:+41 added−0 removed
  • other:+7 added−4 removed

2 files+48 added−4 removed

modified:src/middleware/csrf/index.test.ts+41 added
469 unmodified lines
470 )471 expect(res.status).toBe(403)472 })473 })474475 describe('async IsAllowedSecFetchSiteHandler', () => {476 const app = new Hono()477 app.use(478 '*',479 csrf({480 secFetchSite: async (secFetchSite, c) => {481 await new Promise((r) => setTimeout(r, 10))482 if (secFetchSite === 'same-origin') return true483 if (c.req.path.startsWith('/webhook/')) return true484 return false485 },486 })487 )488 app.post('/form', simplePostHandler)489 app.post('/webhook/test', simplePostHandler)490491 it('should use async custom logic for allowed values', async () => {492 const res = await app.request(493 'http://localhost/form',494 buildSimplePostRequestData({ secFetchSite: 'same-origin' })495 )496 expect(res.status).toBe(200)497 })498499 it('should use async custom logic for path-based bypass', async () => {500 const res = await app.request(501 'http://localhost/webhook/test',502 buildSimplePostRequestData({ secFetchSite: 'cross-site' })503 )504 expect(res.status).toBe(200)505 })506507 it('should block when async custom logic returns false', async () => {508 const res = await app.request(509 'http://localhost/form',510 buildSimplePostRequestData({ secFetchSite: 'cross-site' })511 )512 expect(res.status).toBe(403)513 })514 })515 })516})
modified:src/middleware/csrf/index.ts+7 added-4 removed