Skip to content
Myr

feat(csrf): Support async `IsAllowedOriginHandler` (#4558)

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

* support async IsAllowedOriginHandler * remove return type

authored by Yuichiro Yamashita · committed by GitHub · parent f8c5a79 · Browse files · 2 files+49 added−4 removed
This commit

State

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

Diff stats

2 files+49 added−4 removed

  • tests:+45 added−0 removed
  • other:+4 added−4 removed

2 files+49 added−4 removed

modified:src/middleware/csrf/index.test.ts+45 added
322 unmodified lines
323 expect(res.status).toBe(403)324 expect(simplePostHandler).not.toHaveBeenCalled()325 })326 })327328 describe('async IsAllowedOriginHandler', () => {329 const app = new Hono()330331 app.use(332 '*',333 csrf({334 origin: async (origin) => {335 await new Promise((r) => setTimeout(r, 10))336 return /https:\/\/(\w+\.)?example\.com$/.test(origin)337 },338 })339 )340 app.post('/form', simplePostHandler)341342 it('should be 200 for allowed origin with async handler', async () => {343 let res = await app.request(344 'https://hono.example.com/form',345 buildSimplePostRequestData({ origin: 'https://hono.example.com' })346 )347 expect(res.status).toBe(200)348349 res = await app.request(350 'https://example.com/form',351 buildSimplePostRequestData({ origin: 'https://example.com' })352 )353 expect(res.status).toBe(200)354 })355356 it('should be 403 for not allowed origin with async handler', async () => {357 let res = await app.request(358 'http://honojs.hono.example.jp/form',359 buildSimplePostRequestData({ origin: 'http://example.jp' })360 )361 expect(res.status).toBe(403)362 expect(simplePostHandler).not.toHaveBeenCalled()363364 res = await app.request(365 'http://example.jp/form',366 buildSimplePostRequestData({ origin: 'http://example.jp' })367 )368 expect(res.status).toBe(403)369 expect(simplePostHandler).not.toHaveBeenCalled()370 })371 })372 })373374 describe('with secFetchSite option', () => {375 describe('string', () => {
100 unmodified lines
modified:src/middleware/csrf/index.ts+4 added-4 removed