Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/csrf-protection

Package Overview
Dependencies
Maintainers
19
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/csrf-protection - npm Package Compare versions

Comparing version 4.0.1 to 4.1.0

5

index.d.ts

@@ -41,6 +41,11 @@ /// <reference types="node" />

sessionPlugin?: '@fastify/cookie' | '@fastify/session' | '@fastify/secure-session';
csrfOpts: CSRFOptions;
}
export interface CSRFOptions {
hmacKey: string;
}
declare const fastifyCsrf: FastifyPlugin<FastifyCsrfOptions>;
export default fastifyCsrf;

@@ -42,2 +42,7 @@ 'use strict'

}
if (sessionPlugin === '@fastify/cookie' && csrfOpts.userInfo) {
assert(csrfOpts.hmacKey, 'csrfOpts.hmacKey is required')
}
const tokens = new CSRF(csrfOpts)

@@ -44,0 +49,0 @@

2

package.json
{
"name": "@fastify/csrf-protection",
"version": "4.0.1",
"version": "4.1.0",
"description": "A plugin for adding CSRF protection to Fastify.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -165,2 +165,3 @@ # @fastify/csrf-protection

This option is needed to protect against cookie tossing.
The option `csrfOpts.hmacKey` is required if `getUserInfo` has been specified in the module option in combination with using [@fastify/cookie](https://github.com/fastify/fastify-cookie) as sessionPlugin

@@ -167,0 +168,0 @@ ### `fastify.csrfProtection(request, reply, next)`

@@ -9,2 +9,3 @@ import Fastify from 'fastify'

await fastify.register(FastifyCsrf)
fastify.register(FastifyCsrf, { csrfOpts: { hmacKey: 'hmac' } })

@@ -11,0 +12,0 @@ fastify.route({

@@ -20,2 +20,5 @@ 'use strict'

return userInfoDB[req.body.username]
},
csrfOpts: {
hmacKey: 'foo'
}

@@ -77,2 +80,5 @@ })

return req.session.username
},
csrfOpts: {
hmacKey: 'foo'
}

@@ -127,2 +133,5 @@ })

return req.session.get('username')
},
csrfOpts: {
hmacKey: 'foo'
}

@@ -169,1 +178,80 @@ })

})
test('Validate presence of hmac key with User-Info /1', async (t) => {
const fastify = Fastify()
await fastify.register(fastifyCookie)
await t.rejects(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
}
}), Error('csrfOpts.hmacKey is required'))
})
test('Validate presence of hmac key with User-Info /2', async (t) => {
const fastify = Fastify()
await fastify.register(fastifyCookie)
await t.rejects(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
},
sessionPlugin: '@fastify/cookie'
}), Error('csrfOpts.hmacKey is required'))
})
test('Validate presence of hmac key with User-Info /3', async (t) => {
const fastify = Fastify()
await fastify.register(fastifyCookie)
await t.rejects(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
},
csrfOpts: {
hmacKey: undefined
}
}), Error('csrfOpts.hmacKey is required'))
})
test('Validate presence of hmac key with User-Info /4', async (t) => {
const fastify = Fastify()
await fastify.register(fastifyCookie)
await t.rejects(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
},
sessionPlugin: '@fastify/cookie',
csrfOpts: {
hmacKey: undefined
}
}), Error('csrfOpts.hmacKey is required'))
})
test('Validate presence of hmac key with User-Info /5', async (t) => {
const fastify = Fastify()
await fastify.register(fastifySecureSession, { key, cookie: { path: '/', secure: false } })
await t.resolves(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
},
sessionPlugin: '@fastify/secure-session'
}))
})
test('Validate presence of hmac key with User-Info /6', async (t) => {
const fastify = Fastify()
await fastify.register(fastifySecureSession, { key, cookie: { path: '/', secure: false } })
await t.resolves(fastify.register(fastifyCsrf, {
getUserInfo (req) {
return req.session.get('username')
},
sessionPlugin: '@fastify/secure-session',
csrfOpts: {
hmacKey: 'foo'
}
}))
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc