Skip to content

Commit

Permalink
fix: share correct app key with csrf middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 6, 2020
1 parent f90edb9 commit a2bc5eb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion providers/ShieldProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default class ShieldProvider {
this.container.singleton('Adonis/Addons/ShieldMiddleware', () => {
const Config = this.container.use('Adonis/Core/Config')
const shieldConfig = Config.get('shield', {})
return new (require('../src/ShieldMiddleware').ShieldMiddleware)(shieldConfig)
const appKey = Config.get('app.appKey')
return new (require('../src/ShieldMiddleware').ShieldMiddleware)(shieldConfig, appKey)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/ShieldMiddleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ShieldMiddleware {
* Actions to be performed
*/
private actions = [
shield.csrfFactory(this.config.csrf || {}, ''),
shield.csrfFactory(this.config.csrf || {}, this.appKey),
shield.cspFactory(this.config.csp || {}),
shield.dnsPrefetchFactory(this.config.dnsPrefetch || {}),
shield.frameGuardFactory(this.config.xFrame || {}),
Expand All @@ -31,7 +31,7 @@ export class ShieldMiddleware {
shield.xssFactory(this.config.xss || {}),
]

constructor (private config: ShieldConfig) {
constructor (private config: ShieldConfig, private appKey: string) {
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/csrf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Csrf {
*/
private secretSessionKey = 'csrf-secret'

constructor (private options: CsrfOptions, private applicationKey: string) {
constructor (private options: CsrfOptions, private appKey: string) {
}

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Csrf {
}

const encryptedToken = request.header('x-xsrf-token')
const unpackedToken = encryptedToken ? unpack(encryptedToken, this.applicationKey) : null
const unpackedToken = encryptedToken ? unpack(decodeURIComponent(encryptedToken), this.appKey) : null
return unpackedToken && unpackedToken.signed ? unpackedToken.value : null
}

Expand Down Expand Up @@ -183,11 +183,11 @@ export class Csrf {
* A factory function that returns a new function to enforce CSRF
* protection
*/
export function csrfFactory (options: CsrfOptions, applicationKey: string) {
export function csrfFactory (options: CsrfOptions, appKey: string) {
if (!options.enabled) {
return noop
}

const csrfMiddleware = new Csrf(options, applicationKey)
const csrfMiddleware = new Csrf(options, appKey)
return csrfMiddleware.handle.bind(csrfMiddleware)
}

0 comments on commit a2bc5eb

Please sign in to comment.