Skip to content

Commit

Permalink
Improve performance of csrf errors (github#16136)
Browse files Browse the repository at this point in the history
* Improve performance of csrf errors

* Fix handle csrf error middleware

Co-authored-by: Chiedo <chiedo@users.noreply.github.com>
  • Loading branch information
chiedo and chiedo authored Oct 20, 2020
1 parent c649023 commit 9f10fdd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 8 additions & 0 deletions middleware/handle-csrf-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = async function handleCSRFError (error, req, res, next) {
// If the CSRF token is bad
if (error.code === 'EBADCSRFTOKEN') {
return res.sendStatus(403)
}

return next(error)
}
5 changes: 0 additions & 5 deletions middleware/handle-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,5 @@ module.exports = async function handleError (error, req, res, next) {
}
}

// If the CSRF token is bad
if (error.code === 'EBADCSRFTOKEN') {
return res.sendStatus(403)
}

res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context))
}
1 change: 1 addition & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function (app) {
app.use(require('./robots'))
app.use(require('./cookie-parser'))
app.use(require('./csrf'))
app.use(require('./handle-csrf-errors'))
app.use(require('compression')())
app.use(require('connect-slashes')(false))
app.use('/dist', express.static('dist'))
Expand Down

0 comments on commit 9f10fdd

Please sign in to comment.