Skip to content

Conversation

@bradleybuda
Copy link
Contributor

This merges in the code from the breach-mitigation-rails gem that masks authenticity tokens on each request by XORing them with a random set of bytes. The masking is used to make it impossible for an attacker to steal a CSRF token from an SSL session by using techniques like the BREACH attack.

The patch is pretty simple - I've copied over the relevant code and updated the tests to pass, mostly by adjusting stubs and mocks. @NZKoz suggested that this would be a useful thing to have in Rails itself - we (and many others) have been using it in production for over a year with no issues.

The "length-hiding" code from the breach-mitigation-rails gem is not included here, as it is more complex, more brittle, and provides less protection against the attack - I don't think it's worth including.

This merges in the code from the breach-mitigation-rails gem that masks
authenticity tokens on each request by XORing them with a random set of
bytes. The masking is used to make it impossible for an attacker to
steal a CSRF token from an SSL session by using techniques like the
BREACH attack.

The patch is pretty simple - I've copied over the [relevant
code](https://github.com/meldium/breach-mitigation-rails/blob/master/lib/breach_mitigation/masking_secrets.rb)
and updated the tests to pass, mostly by adjusting stubs and mocks.
@jeremy
Copy link
Member

jeremy commented Aug 19, 2014

Very nice @bradleybuda 👍

@NZKoz
Copy link
Member

NZKoz commented Aug 19, 2014

Looks good to me!

@NZKoz
Copy link
Member

NZKoz commented Aug 19, 2014

Perhaps a CHANGELOG entry would be nice, indicate that we're masking the tokens now to mitigate BREACH and that if you were somehow manually generating your tokens before, you'll need to override valid_authenticity_token??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to reason through how these stub changes affect coverage. Difficult to reason through. Are we covering the old cases as well as demonstrating the new ones we need to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coverage remains on the old code, but the new stuff isn't fully tested. I'll add some additional cases and update the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradleybuda - OK! Aiming to include in 4.2 beta release, so we'll merge now and update tests + changelog for next beta ❤️

jeremy added a commit that referenced this pull request Aug 20, 2014
…f-token

CSRF token mask from breach-mitigation-rails gem
@jeremy jeremy merged commit 79d50ce into rails:master Aug 20, 2014
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use ActiveSupport::SecurityUtils.secure_compare(token, real_csrf_token(session)) now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 old PR, din't see.

@Alamoz
Copy link

Alamoz commented Mar 20, 2015

Are you going to revisit #11729 and rebase with this merge as @jeremy suggested?

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

Successfully merging this pull request may close these issues.

7 participants