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

CSRF Hooks #63

Open
uptownhr opened this issue Aug 13, 2015 · 1 comment
Open

CSRF Hooks #63

uptownhr opened this issue Aug 13, 2015 · 1 comment

Comments

@uptownhr
Copy link

Is there a way to hook into to when a CSRF returns 404? I'd like to check in on the IP of the originating server to see if I can just block them.

@jasisk
Copy link
Contributor

jasisk commented Aug 13, 2015

When csrf fails, we put an error into the typical express error handling pipeline. Feel free to read up on it in the express guide about error handling.

You could register a new error-handling middleware that does what you want. Something like this:

// ./lib/logCsrf.js
module.exports = function logCsrfFailuresGenerator() {
  return function logCsrfFailures(err, req, res, next) {
    if (res.statusCode === 403 && /csrf/i.test(err.message)) {
      // ... csrf failure -- do your logging here ...
    }
    next(err); // don't forget to proceed with the continuation!
  };
}

Then, in your config:

{
  // ...
  "middleware": {
    // ...
    "logCsrfFailures": {
      "enabled": true,
      "priority": 139, // just make sure it's before any error handlers that render!
      "module": "path:./lib/logCsrf"
    }
  }
}

This was referenced Dec 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants