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

SAA-2341 upgrade csrf dependency #1374

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 25 additions & 102 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"compression": "^1.7.5",
"connect-flash": "^0.1.1",
"connect-redis": "^8.0.1",
"csurf": "^1.11.0",
"csrf-sync": "^4.0.3",
"csv-parse": "^5.6.0",
"date-fns": "^4.1.0",
"dayjs": "^1.11.13",
Expand Down Expand Up @@ -137,7 +137,6 @@
"@types/bunyan-format": "^0.2.9",
"@types/compression": "^1.7.5",
"@types/connect-flash": "^0.0.40",
"@types/csurf": "^1.11.5",
"@types/express-session": "^1.18.1",
"@types/http-errors": "^2.0.4",
"@types/jest": "^29.5.14",
Expand Down Expand Up @@ -187,7 +186,6 @@
"typescript": "^5.7.3"
},
"overrides": {
"cookie": "~1.0.2",
"path-to-regexp": "~0.1.12",
"undici": "~6.21.1"
}
Expand Down
13 changes: 11 additions & 2 deletions server/middleware/setUpCsrf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express'
import csurf from 'csurf'
import { csrfSync } from 'csrf-sync'

const testMode = process.env.NODE_ENV === 'test'

Expand All @@ -8,7 +8,16 @@ export default function setUpCsrf(): Router {

// CSRF protection
if (!testMode) {
router.use(csurf())
const {
csrfSynchronisedProtection, // This is the default CSRF protection middleware.
} = csrfSync({
// By default, csrf-sync uses x-csrf-token header, but we use the token in forms and send it in the request body, so change getTokenFromRequest so it grabs from there
getTokenFromRequest: req => {
// eslint-disable-next-line no-underscore-dangle
return req.body._csrf || req.query._csrf
},
})
router.use(csrfSynchronisedProtection)
}

router.use((req, res, next) => {
Expand Down