@@ -7,7 +7,7 @@ import type { Context } from '../../context'
77import { HTTPException } from '../../http-exception'
88import type { MiddlewareHandler } from '../../types'
99
10- type IsAllowedOriginHandler = ( origin : string , context : Context ) => boolean
10+ type IsAllowedOriginHandler = ( origin : string , context : Context ) => boolean | Promise < boolean >
1111
1212const secFetchSiteValues = [ 'same-origin' , 'same-site' , 'none' , 'cross-site' ] as const
1313type SecFetchSite = ( typeof secFetchSiteValues ) [ number ]
@@ -100,12 +100,12 @@ export const csrf = (options?: CSRFOptions): MiddlewareHandler => {
100100 return ( origin ) => optsOrigin . includes ( origin )
101101 }
102102 } ) ( options ?. origin )
103- const isAllowedOrigin = ( origin : string | undefined , c : Context ) => {
103+ const isAllowedOrigin = async ( origin : string | undefined , c : Context ) => {
104104 if ( origin === undefined ) {
105105 // denied always when origin header is not present
106106 return false
107107 }
108- return originHandler ( origin , c )
108+ return await originHandler ( origin , c )
109109 }
110110
111111 const secFetchSiteHandler : IsAllowedSecFetchSiteHandler = ( ( optsSecFetchSite ) => {
@@ -137,7 +137,7 @@ export const csrf = (options?: CSRFOptions): MiddlewareHandler => {
137137 ! isSafeMethodRe . test ( c . req . method ) &&
138138 isRequestedByFormElementRe . test ( c . req . header ( 'content-type' ) || 'text/plain' ) &&
139139 ! isAllowedSecFetchSite ( c . req . header ( 'sec-fetch-site' ) , c ) &&
140- ! isAllowedOrigin ( c . req . header ( 'origin' ) , c )
140+ ! ( await isAllowedOrigin ( c . req . header ( 'origin' ) , c ) )
141141 ) {
142142 const res = new Response ( 'Forbidden' , { status : 403 } )
143143 throw new HTTPException ( 403 , { res } )
0 commit comments