CVE-2023-27495 (published 2023-04-20T18:15Z)

@fastify/csrf-protection is a plugin which helps protect Fastify servers against CSRF attacks. The CSRF protection enforced by the @fastify/csrf-protection library in combination with @fastify/cookie can be bypassed from network and same-site attackers under certain conditions. @fastify/csrf-protection supports an optional userInfo parameter that binds the CSRF token to the user. This parameter has been introduced to prevent cookie-tossing attacks as a fix for CVE-2021-29624. Whenever userInfo parameter is missing, or its value can be predicted for the target user account, network and same-site attackers can 1. fixate a _csrf cookie in the victim's browser, and 2. forge CSRF tokens that are valid for the victim's session. This allows attackers to bypass the CSRF protection mechanism. As a fix, @fastify/csrf-protection starting from version 6.3.0 (and v4.1.0) includes a server-defined secret hmacKey that cryptographically binds the CSRF token to the value of the _csrf cookie and the userInfo parameter, making tokens non-spoofable by attackers. This protection is effective as long as the userInfo parameter is unique for each user. This is patched in versions 6.3.0 and v4.1.0. Users are advised to upgrade. Users unable to upgrade may use a random, non-predictable userInfo parameter for each user as a mitigation.

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:H/A:N)

Weakness information:
cwe_id: CWE-352
cwe_name: Cross-Site Request Forgery (CSRF)
description: The web application does not, or can not, sufficiently verify whether a well-formed, valid, consistent request was intentionally provided by the user who submitted the request.
extended_description: When a web server is designed to receive a request from a client without any mechanism for verifying that it was intentionally sent, then it might be possible for an attacker to trick a client into making an unintentional request to the web server which will be treated as an authentic request. This can be done via a URL, image load, XMLHttpRequest, etc. and can result in exposure of data or unintended code execution.
         

Fix information:
repo_url: https://github.com/fastify/csrf-protection
commit_details: repo_url: https://github.com/fastify/csrf-protection
author: Marco Ippolito
author_date: 2023-04-20 09:39:58+02:00
msg: Merge pull request from GHSA-qrgf-9gpc-vrxw

* fix: enforce hmac key

* fix: check only if fastify/cookie is enabled

* fix: restored working testts

* fix: update README.md

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>

* Update index.d.ts

* Update index.test-d.ts

* Update index.d.ts

* Update index.test-d.ts

* fix unit tests

---------

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
repository: repo_url: https://github.com/fastify/csrf-protection
repo_name: fastify/csrf-protection
description: A fastify csrf plugin.
repo_language: JavaScript
owner: fastify
file_changes: filename: README.md
change_type: ModificationType.MODIFY
diff: @@ -161,6 +161,7 @@ You can also pass the [cookie serialization](https://github.com/fastify/fastify-
 The option `userInfo` is required if `getUserInfo` has been specified in the module option.
 The provided `userInfo` is hashed  inside the csrf token and it is not directly exposed.
 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
 
 ### `fastify.csrfProtection(request, reply, next)`
 

programming_language: Markdown
filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -42,6 +42,11 @@ async function fastifyCsrfProtection (fastify, opts) {
   if (opts.getUserInfo) {
     csrfOpts.userInfo = true
   }
+
+  if (sessionPlugin === '@fastify/cookie' && csrfOpts.userInfo) {
+    assert(csrfOpts.hmacKey, 'csrfOpts.hmacKey is required')
+  }
+
   const tokens = new CSRF(csrfOpts)
 
   const isCookieSigned = cookieOpts && cookieOpts.signed

programming_language: CoffeeScript
filename: user-info.test.js
change_type: ModificationType.MODIFY
diff: @@ -17,6 +17,9 @@ test('Cookies with User-Info', async t => {
   await fastify.register(fastifyCsrf, {
     getUserInfo (req) {
       return userInfoDB[req.body.username]
+    },
+    csrfOpts: {
+      hmacKey: 'foo'
     }
   })
 
@@ -73,6 +76,9 @@ test('Session with User-Info', async t => {
     sessionPlugin: '@fastify/session',
     getUserInfo (req) {
       return req.session.username
+    },
+    csrfOpts: {
+      hmacKey: 'foo'
     }
   })
 
@@ -122,6 +128,9 @@ test('SecureSession with User-Info', async t => {
     sessionPlugin: '@fastify/secure-session',
     getUserInfo (req) {
       return req.session.get('username')
+    },
+    csrfOpts: {
+      hmacKey: 'foo'
     }
   })
 
@@ -163,3 +172,82 @@ test('SecureSession with User-Info', async t => {
 
   t.equal(response2.statusCode, 200)
 })
+
+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'
+    }
+  }))
+})

programming_language: TypeScript
filename: index.d.ts
change_type: ModificationType.MODIFY
diff: @@ -26,17 +26,36 @@ declare namespace fastifyCsrfProtection {
   export type CookieSerializeOptions = FastifyCookieSerializeOptions
 
   export type GetTokenFn = (req: FastifyRequest) => string | void;
-  
-  export interface FastifyCsrfProtectionOptions {
-    csrfOpts?: CSRFOptions;
+
+  interface FastifyCsrfProtectionOptionsBase {
     cookieKey?: string;
     cookieOpts?: CookieSerializeOptions;
     sessionKey?: string;
     getUserInfo?: (req: FastifyRequest) => string;
     getToken?: GetTokenFn;
-    sessionPlugin?: '@fastify/cookie' | '@fastify/session' | '@fastify/secure-session';
   }
 
+  interface FastifyCsrfProtectionOptionsFastifyCookie {
+    sessionPlugin?: '@fastify/cookie';
+    csrfOpts: Omit<CSRFOptions, 'hmacKey'> & Required<Pick<CSRFOptions, 'hmacKey'>>;
+  }
+
+  interface FastifyCsrfProtectionOptionsFastifySession {
+    sessionPlugin: '@fastify/session';
+    csrfOpts?: CSRFOptions;
+  }
+
+  interface FastifyCsrfProtectionOptionsFastifySecureSession {
+    sessionPlugin: '@fastify/secure-session';
+    csrfOpts?: CSRFOptions;
+  }
+
+  export type FastifyCsrfProtectionOptions = FastifyCsrfProtectionOptionsBase & (
+    FastifyCsrfProtectionOptionsFastifyCookie |
+    FastifyCsrfProtectionOptionsFastifySession |
+    FastifyCsrfProtectionOptionsFastifySecureSession
+  )
+
   /**
    * @deprecated Use FastifyCsrfProtectionOptions instead
    */

programming_language: TypeScript
filename: index.test-d.ts
change_type: ModificationType.MODIFY
diff: @@ -31,13 +31,28 @@ async function run() {
 }
 
 
-fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1' } })
+fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1', hmacKey: 'hmac' } })
 expectError(fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 1 } }))
 
 fastify.register(FastifySession)
-fastify.register(FastifyCsrfProtection, { getUserInfo(req) {
-  return req.session.get('username')
-}})
+fastify.register(FastifyCsrfProtection, {
+  csrfOpts: {
+    hmacKey: '123'
+  },
+  getUserInfo(req) {
+    return req.session.get('username')
+  }
+})
 expectError(fastify.register(FastifyCsrfProtection, { getUserInfo: 'invalid' }))
 
+fastify.register(FastifyCsrfProtection, { csrfOpts: { hmacKey: 'hmac' }, sessionPlugin: '@fastify/cookie' })
+fastify.register(FastifyCsrfProtection, { csrfOpts: { hmacKey: 'hmac' } })
+expectError(fastify.register(FastifyCsrfProtection, { }))
+expectError(fastify.register(FastifyCsrfProtection, { csrfOpts: { }}))
+expectError(fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie', csrfOpts: { }}))
+fastify.register(FastifyCsrfProtection, { csrfOpts: { }, sessionPlugin: '@fastify/session' })
+fastify.register(FastifyCsrfProtection, { csrfOpts: { }, sessionPlugin: '@fastify/secure-session' })
+fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/session' })
+fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/secure-session' })
+
 expectDeprecated({} as FastifyCsrfOptions)

programming_language: TypeScript