Skip to content

Commit

Permalink
fix(session): fixed a bug regarding session middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinaldy Rafli committed Jun 25, 2021
1 parent 13cc5df commit c332b5b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ function defaultValue(req: CSRFRequest): string | string[] {
)
}

function verifyConfiguration(req: CSRFRequest, sessionKey: string, cookie: CookieOptions, middleware: MiddlewareOptions): boolean {
function verifyConfiguration(
req: CSRFRequest,
sessionKey: string,
cookie: CookieOptions,
middleware: MiddlewareOptions
): boolean {
if (!getSecretBag(req, sessionKey, cookie, middleware)) {
return false
}
Expand All @@ -144,7 +149,7 @@ function verifyConfiguration(req: CSRFRequest, sessionKey: string, cookie: Cooki

function getSecret(req: CSRFRequest, sessionKey: string, cookie: CookieOptions, middleware: MiddlewareOptions): string {
const bag = getSecretBag(req, sessionKey, cookie, middleware)
const key = (middleware === 'cookie') ? cookie.key : 'csrfSecret'
const key = middleware === 'cookie' ? cookie.key : 'csrfSecret'

if (!bag) {
throw new Error('misconfigured csrf')
Expand All @@ -153,7 +158,12 @@ function getSecret(req: CSRFRequest, sessionKey: string, cookie: CookieOptions,
return bag[key]
}

function getSecretBag(req: CSRFRequest, sessionKey: string, cookie: CookieOptions, middleware: MiddlewareOptions): string {
function getSecretBag(
req: CSRFRequest,
sessionKey: string,
cookie: CookieOptions,
middleware: MiddlewareOptions
): string {
if (middleware === 'cookie' && cookie) {
return cookie.signed ? req?.signedCookies : req?.cookies
}
Expand Down

0 comments on commit c332b5b

Please sign in to comment.