Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CSRF protection for public endpoints #18674

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zomars
Copy link
Member

@zomars zomars commented Jan 15, 2025

What does this PR do?

This pull request introduces a CSRF protection mechanism across various parts of the application, including the addition of a new CSRF module and its integration into API routes. The most important changes include the creation of the CSRF module, updates to API routes to include CSRF verification, and modifications to the environment configuration.

image image

CSRF Protection Implementation:

  • New CSRF Module:

  • Integration into API Routes:

    • Updated various API route handlers to include CSRF verification using CSRF.init().verify(req, res). (apps/web/pages/api/book/event.ts - [1] apps/web/pages/api/book/instant-event.ts - [2] apps/web/pages/api/book/recurring-event.ts - [3]
    • Added CSRF setup in server-side props function to ensure CSRF tokens are set. (apps/web/server/lib/[user]/[type]/getServerSideProps.ts - apps/web/server/lib/[user]/[type]/getServerSideProps.tsR299)

Environment Configuration:

  • Environment Variables:
    • Added CSRF_SECRET to .env.example to guide users on generating a CSRF secret. (.env.example - .env.exampleR84-R86)
    • Included CSRF_SECRET in turbo.json for environment variable management. (turbo.json - turbo.jsonR278)

Dependency Updates:

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

Checklist

  • I haven't read the contributing guide
  • My code doesn't follow the style guidelines of this project
  • I haven't commented my code, particularly in hard-to-understand areas
  • I haven't checked if my changes generate no new warnings

@github-actions github-actions bot added the ❗️ .env changes contains changes to env variables label Jan 15, 2025
@keithwillcode keithwillcode added core area: core, team members only foundation labels Jan 15, 2025
Copy link

linear bot commented Jan 15, 2025

Copy link

socket-security bot commented Jan 15, 2025

New, updated, and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@bcoe/v8-coverage@0.2.3 None 0 277 kB bcoe
npm/@cspotcode/source-map-support@0.8.1 filesystem +1 194 kB cspotcode
npm/@humanwhocodes/module-importer@1.0.1 unsafe 0 21.2 kB nzakas
npm/@humanwhocodes/object-schema@1.2.1 None 0 49.4 kB nzakas
npm/@isaacs/cliui@8.0.2 None +3 62.9 kB isaacs
npm/@istanbuljs/schema@0.1.3 None 0 17.2 kB coreyfarrell
npm/@nodelib/fs.stat@2.0.5 filesystem 0 11.8 kB mrmlnc
npm/@nodelib/fs.walk@1.2.8 None 0 26.4 kB mrmlnc
npm/@pkgjs/parseargs@0.11.0 None 0 74.2 kB oss-bot
npm/@tootallnate/once@2.0.0 None 0 16.3 kB tootallnate
npm/@types/istanbul-lib-coverage@2.0.4 None 0 5.76 kB types
npm/@types/mime-types@2.1.4 None 0 3.38 kB types
npm/@types/node@16.9.1 🔁 npm/@types/node@20.17.13, npm/@types/node@22.10.6 None 0 1.63 MB types
npm/abbrev@1.1.1 None 0 4.78 kB isaacs
npm/acorn-jsx@5.3.2 None 0 24.4 kB rreverser
npm/agent-base@6.0.2 None 0 34.6 kB tootallnate
npm/aggregate-error@3.1.0 None 0 6.69 kB sindresorhus
npm/ansi-colors@4.1.3 environment 0 26.1 kB jonschlinkert
npm/ansi-regex@5.0.1 None 0 5.61 kB qix
npm/are-we-there-yet@3.0.1 None 0 14.3 kB lukekarrys
npm/arg@4.1.3 None 0 12.9 kB qix
npm/argparse@2.0.1 environment, filesystem 0 172 kB vitaly
npm/astral-regex@2.0.0 None 0 3.4 kB kevva
npm/balanced-match@1.0.2 None 0 6.94 kB juliangruber
npm/base64-js@1.5.1 None 0 9.62 kB feross
npm/bl@4.1.0 None 0 64.4 kB matteo.collina
npm/buffer-from@1.1.2 None 0 5.05 kB linusu
npm/buffer@5.7.1 None 0 82.5 kB feross
npm/callsites@3.1.0 None 0 6.33 kB sindresorhus
npm/camelcase@5.3.1 🔁 npm/camelcase@6.3.0 None 0 7.45 kB sindresorhus
npm/chownr@2.0.0 filesystem 0 5.75 kB isaacs
npm/clean-stack@2.2.0 None 0 5.51 kB sindresorhus
npm/cli-cursor@3.1.0 None 0 4.37 kB sindresorhus
npm/clone@1.0.4 None 0 11.1 kB pvorb
npm/color-convert@2.0.1 🔁 npm/color-convert@1.9.3 None 0 27.2 kB qix
npm/color-name@1.1.4 None 0 6.69 kB dfcreative
npm/color-support@1.1.3 None 0 9.23 kB isaacs
npm/concat-map@0.0.1 None 0 4.86 kB substack
npm/console-control-strings@1.1.0 None 0 12.7 kB iarna
npm/convert-source-map@2.0.0 None 0 15.9 kB phated
npm/create-require@1.1.1 filesystem, unsafe 0 6.25 kB pi0
npm/decamelize@1.2.0 None 0 2.94 kB sindresorhus
npm/deep-is@0.1.4 None 0 8.11 kB thlorenz
npm/delegates@1.0.0 None 0 7.46 kB tjholowaychuk
npm/diff@4.0.2 None 0 335 kB kpdecker
npm/doctrine@3.0.0 None 0 106 kB eslint
npm/eastasianwidth@0.2.0 None 0 13.6 kB komagata
npm/emoji-regex@8.0.0 None 0 48.3 kB mathias
npm/env-paths@2.2.1 None 0 10.2 kB sindresorhus
npm/err-code@2.0.3 None 0 12.3 kB achingbrain
npm/escape-string-regexp@4.0.0 None 0 3.79 kB sindresorhus
npm/esprima@4.0.1 None 0 314 kB ariya
npm/esrecurse@4.3.0 None 0 13.5 kB michaelficarra
npm/estraverse@5.3.0 None 0 37.1 kB michaelficarra
npm/esutils@2.0.3 None 0 50.6 kB michaelficarra
npm/exponential-backoff@3.1.1 None 0 37.3 kB sssayegh
npm/fast-deep-equal@3.1.3 None 0 13 kB esp
npm/fast-json-stable-stringify@2.1.0 None 0 17 kB esp
npm/fast-levenshtein@2.0.6 None 0 9.44 kB hiddentao
npm/find-up@5.0.0 None 0 11.8 kB sindresorhus
npm/fs-minipass@2.1.0 filesystem 0 14.1 kB isaacs
npm/fs.realpath@1.0.0 environment, filesystem 0 13.4 kB isaacs
npm/get-caller-file@2.0.5 None 0 4.72 kB stefanpenner
npm/glob-parent@6.0.2 None 0 7.72 kB phated
npm/has-flag@4.0.0 None 0 4.42 kB sindresorhus
npm/has-unicode@2.0.1 environment 0 3.44 kB iarna
npm/html-escaper@2.0.2 None 0 13.1 kB webreflection
npm/http-cache-semantics@4.1.1 None 0 35.9 kB kornel
npm/http-proxy-agent@5.0.0 network 0 17.1 kB tootallnate
npm/humanize-ms@1.2.1 None 0 3.66 kB dead_horse
npm/ieee754@1.2.1 None 0 6.8 kB feross
npm/import-fresh@3.3.0 None 0 4.87 kB sindresorhus
npm/imurmurhash@0.1.4 None 0 11.9 kB jensyt
npm/indent-string@4.0.0 None 0 4.4 kB sindresorhus
npm/inflight@1.0.6 None 0 3.76 kB isaacs
npm/inherits@2.0.4 None 0 3.96 kB isaacs
npm/is-extglob@2.1.1 None 0 6.22 kB jonschlinkert
npm/is-fullwidth-code-point@3.0.0 None 0 4.99 kB sindresorhus
npm/is-glob@4.0.3 None 0 13.6 kB phated
npm/is-interactive@1.0.0 None 0 4.62 kB sindresorhus
npm/is-lambda@1.0.1 None 0 2.94 kB watson
npm/is-number@7.0.0 None 0 9.62 kB jonschlinkert
npm/is-unicode-supported@0.1.0 None 0 3.54 kB sindresorhus
npm/is-windows@1.0.2 None 0 7.96 kB jonschlinkert
npm/isexe@2.0.0 environment, filesystem 0 11 kB isaacs
npm/istanbul-lib-coverage@3.2.0 None 0 29.3 kB oss-bot
npm/js-tokens@4.0.0 None 0 15.1 kB lydell
npm/js-yaml@4.1.0 None 0 405 kB vitaly
npm/json-stable-stringify-without-jsonify@1.0.1 None 0 14.2 kB samn
npm/kind-of@6.0.3 None 0 22.8 kB doowb
npm/locate-path@6.0.0 🔁 npm/locate-path@5.0.0 None 0 7.02 kB sindresorhus
npm/lodash.merge@4.6.2 None 0 54.1 kB jdalton
npm/log-symbols@4.1.0 None 0 4.58 kB sindresorhus
npm/make-error@1.3.6 None 0 12.4 kB julien-f
npm/merge-stream@2.0.0 None 0 4.31 kB stevemao
npm/merge2@1.4.1 None 0 8.9 kB zensh
npm/minipass-collect@1.0.2 None 0 4.87 kB isaacs
npm/minipass-pipeline@1.2.4 None 0 7 kB isaacs
npm/minipass-sized@1.0.3 None 0 124 kB isaacs
npm/minipass@3.3.6 🔁 npm/minipass@4.2.8 None 0 48.1 kB isaacs
npm/mkdirp@1.0.4 environment, filesystem 0 19.1 kB isaacs
npm/natural-compare@1.4.0 None 0 5.65 kB megawac
npm/negotiator@0.6.3 None 0 27.4 kB dougwilson
npm/nopt@6.0.0 environment 0 23.9 kB gar
npm/normalize-path@3.0.0 None 0 9.22 kB jonschlinkert
npm/npmlog@6.0.2 None 0 17.1 kB lukekarrys
npm/once@1.4.0 None 0 4.05 kB isaacs
npm/ora@5.4.1 None 0 23.2 kB sindresorhus
npm/p-limit@3.1.0 None 0 7.75 kB sindresorhus
npm/p-locate@5.0.0 🔁 npm/p-locate@4.1.0 None 0 7.24 kB sindresorhus
npm/p-try@2.2.0 None 0 4.37 kB sindresorhus
npm/parent-module@1.0.1 None 0 3.92 kB sindresorhus
npm/path-exists@4.0.0 filesystem 0 3.92 kB sindresorhus
npm/path-is-absolute@1.0.1 None 0 3.62 kB sindresorhus
npm/path-key@3.1.1 None 0 4.55 kB sindresorhus
npm/path-parse@1.0.7 None 0 4.51 kB jbgutierrez
npm/pify@4.0.1 None 0 7.23 kB sindresorhus
npm/prelude-ls@1.2.1 None 0 36.7 kB gkz
npm/queue-microtask@1.2.3 None 0 8.37 kB feross
npm/regexpp@3.2.0 None 0 302 kB mysticatea
npm/require-directory@2.1.1 filesystem 0 12.1 kB troygoode
npm/require-from-string@2.0.2 unsafe 0 3.42 kB floatdrop
npm/require-main-filename@2.0.0 None 0 3.93 kB bcoe
npm/resolve-from@4.0.0 filesystem, unsafe 0 4.64 kB sindresorhus
npm/retry@0.12.0 None 0 32.2 kB tim-kos
npm/reusify@1.0.4 None 0 9.44 kB matteo.collina
npm/rimraf@3.0.2 filesystem 0 17.3 kB isaacs
npm/run-parallel@1.2.0 None 0 6.56 kB feross
npm/safe-buffer@5.2.1 None 0 32.1 kB feross
npm/safer-buffer@2.1.2 None 0 42.3 kB chalker
npm/set-blocking@2.0.0 None 0 4.22 kB bcoe
npm/shebang-command@2.0.0 None 0 2.56 kB kevva
npm/shebang-regex@3.0.0 None 0 2.83 kB sindresorhus
npm/signal-exit@3.0.7 None 0 9.96 kB isaacs
npm/sisteransi@1.0.5 None 0 6.79 kB terkelg
npm/smart-buffer@4.2.0 None 0 138 kB joshglazebrook
npm/socks@2.7.1 network 0 152 kB joshglazebrook
npm/sprintf-js@1.0.3 None 0 34.8 kB alexei
npm/string_decoder@1.3.0 🔁 npm/string_decoder@1.1.1 None 0 14.4 kB matteo.collina
npm/string-width-cjs@4.2.3 None 0 0 B
npm/string-width@4.2.3 None 0 5.16 kB sindresorhus
npm/strip-ansi-cjs@6.0.1 None 0 0 B
npm/strip-ansi@6.0.1 None 0 4.03 kB sindresorhus
npm/strip-json-comments@3.1.1 None 0 6.96 kB sindresorhus
npm/supports-preserve-symlinks-flag@1.0.0 None 0 9.18 kB ljharb
npm/test-exclude@6.0.0 None 0 23.6 kB coreyfarrell
npm/text-table@0.2.0 None 0 11 kB substack
npm/to-regex-range@5.0.1 None 0 22.9 kB jonschlinkert
npm/type-check@0.4.0 None 0 21.2 kB gkz
npm/type-fest@0.20.2 🔁 npm/type-fest@0.7.1 None 0 111 kB sindresorhus
npm/uri-js@4.4.1 None 0 470 kB garycourt
npm/util-deprecate@1.0.2 None 0 5.48 kB tootallnate
npm/v8-compile-cache-lib@3.0.1 environment, filesystem, unsafe 0 18.2 kB cspotcode
npm/v8-to-istanbul@9.2.0 filesystem, unsafe 0 46.9 kB oss-bot
npm/wcwidth@1.0.1 None 0 14.2 kB timoxley
npm/which-module@2.0.1 None 0 4.04 kB nexdrew
npm/which@2.0.2 environment 0 9.97 kB isaacs
npm/wide-align@1.1.5 None 0 4.47 kB iarna
npm/wrap-ansi-cjs@7.0.0 None 0 0 B
npm/wrap-ansi@7.0.0 None 0 10.6 kB sindresorhus
npm/wrappy@1.0.2 None 0 2.96 kB zkat
npm/y18n@5.0.8 filesystem 0 23.4 kB oss-bot
npm/yn@3.1.1 None 0 6.31 kB sindresorhus
npm/yocto-queue@0.1.0 None 0 6.03 kB sindresorhus

🚮 Removed packages: npm/@axiomhq/winston@1.3.1, npm/@calcom/platform-libraries@0.0.2, npm/@calcom/platform-libraries@0.0.81, npm/@golevelup/ts-jest@0.4.0, npm/@microsoft/microsoft-graph-types-beta@0.42.0-preview, npm/@nest-lab/throttler-storage-redis@1.0.0, npm/@nestjs/bull@10.2.3, npm/@nestjs/cli@10.4.9, npm/@nestjs/common@10.4.15, npm/@nestjs/config@3.3.0, npm/@nestjs/core@10.4.15, npm/@nestjs/jwt@10.2.0, npm/@nestjs/passport@10.0.3, npm/@nestjs/platform-express@10.4.15, npm/@nestjs/schematics@10.2.3, npm/@nestjs/swagger@7.4.2, npm/@nestjs/testing@10.4.15, npm/@nestjs/throttler@6.2.1, npm/@sentry/nestjs@8.48.0, npm/@sentry/nextjs@8.48.0, npm/@sentry/profiling-node@8.48.0, npm/@types/cookie-parser@1.4.8, npm/@types/express@4.17.21, npm/@types/fs-extra@11.0.4, npm/@types/luxon@3.4.2, npm/@types/passport-jwt@3.0.13, npm/@types/supertest@2.0.16, npm/bcryptjs@2.4.3, npm/body-parser@1.20.3, npm/bull@4.16.5, npm/class-transformer@0.5.1, npm/class-validator@0.14.1, npm/connect@3.7.0, npm/cookie-parser@1.4.7, npm/googleapis@84.0.0, npm/helmet@7.2.0, npm/http-proxy-middleware@2.0.7, npm/http@0.0.1-security, npm/ioredis@5.4.2, npm/jest-date-mock@1.0.10, npm/jest@29.7.0, npm/jsforce@1.11.1, npm/memory-cache@0.2.0, npm/nest-winston@1.10.1, npm/next-api-middleware@1.0.1, npm/next-auth@4.24.11, npm/next-axiom@0.17.0, npm/next-swagger-doc@0.3.6, npm/next-validations@0.2.1, npm/next@13.5.8, npm/node-mocks-http@1.16.2, npm/passport-jwt@4.0.1, npm/passport@0.7.0, npm/qs-stringify@1.2.1, npm/querystring@0.2.1, npm/stripe@15.12.0, npm/supertest@6.3.4, npm/ts-jest@29.2.5, npm/ts-loader@9.5.2, npm/tsconfig-paths@4.2.0, npm/tzdata@1.0.42

View full report↗︎

Copy link

socket-security bot commented Jan 15, 2025

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteCI
Critical CVE npm/dompurify@=2.3.3 ⚠︎

View full report↗︎

Next steps

What is a critical CVE?

Contains a Critical Common Vulnerability and Exposure (CVE).

Remove or replace dependencies that include known critical CVEs. Consumers can use dependency overrides or npm audit fix --force to remove vulnerable dependencies.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/foo@1.0.0 or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore npm/dompurify@=2.3.3

Copy link
Member Author

@zomars zomars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for review

@@ -81,6 +81,9 @@ NEXTAUTH_SECRET=
# Used for cross-domain cookie authentication
NEXTAUTH_COOKIE_DOMAIN=

# You can use: `openssl rand -base64 32` to generate one
CSRF_SECRET=
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is opt-in. If not set the class will no-op.

@@ -0,0 +1,10 @@
import type { ICSRF } from "./csrf.interface";

export class MockCSRF implements ICSRF {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback when env var is not set

Copy link

vercel bot commented Jan 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Jan 15, 2025 4:07am
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Jan 15, 2025 4:07am

@zomars zomars marked this pull request as ready for review January 15, 2025 04:08
@graphite-app graphite-app bot requested a review from a team January 15, 2025 04:08
@dosubot dosubot bot added api area: API, enterprise API, access token, OAuth ✨ feature New feature or request labels Jan 15, 2025
Copy link

graphite-app bot commented Jan 15, 2025

Graphite Automations

"Add foundation team as reviewer" took an action on this PR • (01/15/25)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Contributor

E2E results are ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api area: API, enterprise API, access token, OAuth core area: core, team members only ❗️ .env changes contains changes to env variables ✨ feature New feature or request foundation ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants