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
Provenance
- Commit
489afe6- Tree
d2abebd- Author
Yuichiro Yamashita <xydybaseball@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
f8c5a79
2 files changed+49 added−4 removed
modified:src/middleware/csrf/index.test.ts+45 added
322 unmodified lines323 expect(res.status).toBe(403)324 expect(simplePostHandler).not.toHaveBeenCalled()325 })326 })327+328+ describe('async IsAllowedOriginHandler', () => {329+ const app = new Hono()330+331+ 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)341+342+ 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)348+349+ res = await app.request(350+ 'https://example.com/form',351+ buildSimplePostRequestData({ origin: 'https://example.com' })352+ )353+ expect(res.status).toBe(200)354+ })355+356+ 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()363+364+ 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 })373 374 describe('with secFetchSite option', () => {375 describe('string', () => {100 unmodified linesmodified:src/middleware/csrf/index.ts+4 added-4 removed
5 unmodified lines678910101112131484 unmodified lines9910010110210310310410510610710810810911011111223 unmodified lines1361371381391401401411421431444 unmodified lines5 unmodified linesimport type { Context } from '../../context'import { HTTPException } from '../../http-exception'import type { MiddlewareHandler } from '../../types'
type IsAllowedOriginHandler = (origin: string, context: Context) => booleantype IsAllowedOriginHandler = (origin: string, context: Context) => boolean | Promise<boolean>
const secFetchSiteValues = ['same-origin', 'same-site', 'none', 'cross-site'] as consttype SecFetchSite = (typeof secFetchSiteValues)[number]
84 unmodified lines } else { return (origin) => optsOrigin.includes(origin) } })(options?.origin) const isAllowedOrigin = (origin: string | undefined, c: Context) => { const isAllowedOrigin = async (origin: string | undefined, c: Context) => { if (origin === undefined) { // denied always when origin header is not present return false } return originHandler(origin, c) return await originHandler(origin, c) }
const secFetchSiteHandler: IsAllowedSecFetchSiteHandler = ((optsSecFetchSite) => { if (!optsSecFetchSite) {23 unmodified lines if ( !isSafeMethodRe.test(c.req.method) && isRequestedByFormElementRe.test(c.req.header('content-type') || 'text/plain') && !isAllowedSecFetchSite(c.req.header('sec-fetch-site'), c) && !isAllowedOrigin(c.req.header('origin'), c) !(await isAllowedOrigin(c.req.header('origin'), c)) ) { const res = new Response('Forbidden', { status: 403 }) throw new HTTPException(403, { res }) }4 unmodified lines