CVE-2023-27495 (JavaScript, published 2023-04-20T18:15Z)
Description: value: @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.
Severity: nan (CVSS v3 base score: 6.5)
Weakness: CWE-352 \u2014 Cross-Site Request Forgery (CSRF)
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.
Repository: https://github.com/fastify/csrf-protection
Files changed:
README.md
index.js
test/user-info.test.js
types/index.d.ts
types/index.test-d.ts
Commit message: 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>

Vulnerable code:
  
  export interface FastifyCsrfProtectionOptions {
    csrfOpts?: CSRFOptions;
    sessionPlugin?: '@fastify/cookie' | '@fastify/session' | '@fastify/secure-session';
fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1' } })
fastify.register(FastifyCsrfProtection, { getUserInfo(req) {
  return req.session.get('username')
}})

Fixed code:
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

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

    },
    csrfOpts: {
      hmacKey: 'foo'
    },
    csrfOpts: {
      hmacKey: 'foo'
    },
    csrfOpts: {
      hmacKey: 'foo'

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'
    }
  }))
})

  interface FastifyCsrfProtectionOptionsBase {
  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
  )

fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1', hmacKey: 'hmac' } })
fastify.register(FastifyCsrfProtection, {
  csrfOpts: {
    hmacKey: '123'
  },
  getUserInfo(req) {
    return req.session.get('username')
  }
})
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' })