Csrf.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const csurf = require('csurf')
  15. const csrf = csurf()
  16. const { promisify } = require('util')
  17. // Wrapper for `csurf` middleware that provides a list of routes that can be excluded from csrf checks.
  18. //
  19. // Include with `Csrf = require('./Csrf')`
  20. //
  21. // Add the middleware to the router with:
  22. // myRouter.csrf = new Csrf()
  23. // myRouter.use webRouter.csrf.middleware
  24. // When building routes, specify a route to exclude from csrf checks with:
  25. // myRouter.csrf.disableDefaultCsrfProtection "/path" "METHOD"
  26. //
  27. // To validate the csrf token in a request to ensure that it's valid, you can use `validateRequest`, which takes a
  28. // request object and calls a callback with an error if invalid.
  29. class Csrf {
  30. constructor() {
  31. this.middleware = this.middleware.bind(this)
  32. this.excluded_routes = {}
  33. }
  34. disableDefaultCsrfProtection(route, method) {
  35. if (!this.excluded_routes[route]) {
  36. this.excluded_routes[route] = {}
  37. }
  38. return (this.excluded_routes[route][method] = 1)
  39. }
  40. middleware(req, res, next) {
  41. // We want to call the middleware for all routes, even if excluded, because csurf sets up a csrfToken() method on
  42. // the request, to get a new csrf token for any rendered forms. For excluded routes we'll then ignore a 'bad csrf
  43. // token' error from csurf and continue on...
  44. // check whether the request method is excluded for the specified route
  45. if (
  46. (this.excluded_routes[req.path] != null
  47. ? this.excluded_routes[req.path][req.method]
  48. : undefined) === 1
  49. ) {
  50. // ignore the error if it's due to a bad csrf token, and continue
  51. return csrf(req, res, err => {
  52. if (err && err.code !== 'EBADCSRFTOKEN') {
  53. return next(err)
  54. } else {
  55. return next()
  56. }
  57. })
  58. } else {
  59. return csrf(req, res, next)
  60. }
  61. }
  62. static validateRequest(req, cb) {
  63. // run a dummy csrf check to see if it returns an error
  64. if (cb == null) {
  65. cb = function(valid) {}
  66. }
  67. return csrf(req, null, err => cb(err))
  68. }
  69. static validateToken(token, session, cb) {
  70. if (token == null) {
  71. return cb(new Error('missing token'))
  72. }
  73. // run a dummy csrf check to see if it returns an error
  74. // use this to simulate a csrf check regardless of req method, headers &c.
  75. const req = {
  76. body: {
  77. _csrf: token
  78. },
  79. headers: {},
  80. method: 'POST',
  81. session
  82. }
  83. return Csrf.validateRequest(req, cb)
  84. }
  85. }
  86. Csrf.promises = {
  87. validateRequest: promisify(Csrf.validateRequest)
  88. }
  89. module.exports = Csrf