apostrophe icon indicating copy to clipboard operation
apostrophe copied to clipboard

CSRF attack protection is needed

Open boutell opened this issue 11 years ago • 6 comments

Right now Apostrophe doesn't specifically protect against CSRF attacks on its APIs. So it is possible to create a form on a third-party site whose action is set to an Apostrophe API, and trick someone who has the right privileges into clicking a submit button on that third-party site to create a page or changing someone's permissions on an Apostrophe site.

This is currently unlikely because there are few opportunities to use these tactics against known Apostrophe admins. But it should still be addressed properly and as A2 becomes more common it will be a more significant issue. It also appears prominently in the OWASP Top 10 and other security guidelines.

One strategy is to generate a hidden form element that is specific to the current user and won't be accepted from any other user. Directly adding this to every form or JS API call is tedious for front-end developers.

Another strategy is to automatically enhance forms and jQuery API calls with such a CSRF code via javascript in the browser. This is an attractive solution because you can't forget to address it. We know it works because we're already enhancing $.get, $.post, $.ajax, etc. in this way for Apostrophe's prefix feature.

GET-method links should never be used to carry out actions, period. That requires putting the CSRF code in the address bar and the hover state of the link, which greatly increases the change it'll be accidentally shared.

I would like to arrive at an automatic solution if possible.

Symfony 1.x generated a CSRF token based on the user's session identifier and the name of the form class being submitted, to prevent a CSRF token for one action from being used to carry out another. My beef with this is that it is much more work (it can't be a transparent solution) and if someone has access to one of your tokens, they presumably have access to your session cookie too, and the game is already over.

My strong feeling about "replay attacks" is that if you don't want people reading your traffic (including your session key), you gotta use HTTPS, in which case replay attacks are not possible.

SERVER SIDE

apos.csrfSecret = options.csrfSecret;

In renderPage:

self.req.browserCall('apos.setCsrfToken(?)', hash(req.user.session.id + ':' + self.csrfSecret));

In global middleware:

app.use(function(req, res) {
  if (!req.body) {
    return next();
  }
  if (req.body.csrfToken !== hash(req.user.session.id + ':' + self.csrfSecret)) {
    // Maybe do a 404, why give them information? But technically it's Forbidden
    res.statusCode = 403;
    return res.send('Forbidden');
  }
  return next();
});

In browserland:

apos.setCsrfToken = function(token) {
  apos.csrfToken = token;
}

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
  if (options.data && (typeof(options.data) === 'object')) {
    options.data.csrfToken = apos.csrfToken;
  }
});

A disadvantage of this approach is that ALL POST requests must have a CSRF token. Also there must be a consistent session ID assigned for everyone, even an anonymous user. We can't just skip out of the middleware if there is no token, that would completely defeat the purpose and open everything up again.

We would also need a way to do this for all vanilla form submissions (non-AJAX). We could do this:

$('body').on('submit', function() {
  var $hidden = $('<input type="hidden" name="csrfToken" />');
  $hidden.val(apos.csrfToken);
  $(this).closest('form').append($hidden);
  return true;
});

boutell avatar Jul 16 '15 20:07 boutell

I assume that middleware would only be for non-GET requests?

Would adding CSRF tokens in schema forms help solve a lot of the issues? This would be similar to what Symfony offered, right?

benirose avatar Jul 16 '15 20:07 benirose

Also, thanks for writing this up!

benirose avatar Jul 16 '15 20:07 benirose

GET requests shouldn't be doing CSRF because they shouldn't be doing things. We should get out of the lazy habit of using anything but POST to actually alter the world.

Otherwise the CSRF token has to be in the URL, where it's a lot more likely to get accidentally shared sad trombone

The code I'm proposing here adds a CSRF token to every AJAX call and every form submission, full on, man...

Doing it with schemas is good, but we're unlikely to use schemas for every form, even though they are becoming more and more useful in more and more cases. So I went for something more global in its impact.

boutell avatar Jul 16 '15 20:07 boutell

"We should get out of the lazy habit of using anything but POST to actually alter the world." - Definitely. Let's plan an audit of any remaining GETs that are not idempotent and make that change. Lest we be left behind in 2007.

I wasn't aware of places where forms exist that aren't getting generated by schemas. I can get behind a global push with ajax and middleware though.

benirose avatar Jul 16 '15 21:07 benirose

We can also provide a macro to insert the CSRF token for those who need a form to work without JavaScript, even though that's pretty ridunkulous in 2015.

On Thu, Jul 16, 2015 at 5:48 PM, benirose [email protected] wrote:

"We should get out of the lazy habit of using anything but POST to actually alter the world." - Definitely. Let's plan an audit of any remaining GETs that are not idempotent and make that change. Lest we be left behind in 2007.

I wasn't aware of places where forms exist that aren't getting generated by schemas. I can get behind a global push with ajax and middleware though.

— Reply to this email directly or view it on GitHub https://github.com/punkave/apostrophe/issues/295#issuecomment-122113598.

*THOMAS BOUTELL, *DEV & OPS P'UNK AVENUE | (215) 755-1330 | punkave.com

boutell avatar Jul 17 '15 12:07 boutell

It is so!

jth- avatar Feb 01 '16 15:02 jth-