Skip to content

Allow both encrypted + unencrypted CSRF header token - #7528

Merged
taylorotwell merged 1 commit into
laravel:5.0from
barryvdh:patch-9
Feb 20, 2015
Merged

Allow both encrypted + unencrypted CSRF header token#7528
taylorotwell merged 1 commit into
laravel:5.0from
barryvdh:patch-9

Conversation

@barryvdh

Copy link
Copy Markdown
Contributor

I know something similar has been submitted before, but currently CSRF works for these situations:

  • CSRF works for hidden _token input
  • CSRF works automatically for Angular, which uses the encrypted value from the cookie (+ X-XSRF-TOKEN header)

But it doesn't for other common use-cases:

  • Plain text token header
  • Scripts that use X-CSRF convention

Proposal: Also check the X-CSRF-TOKEN header for plain-text token. This make it easier to add a meta-tag to the page which javascript checks.
<meta name="csrf-token" content="<?= csrf_token ?>" />

Some frameworks/scripts already use this convention. like Jquery UJS

CSRFProtection: function(xhr) {
      var token = $('meta[name="csrf-token"]').attr('content');
      if (token) xhr.setRequestHeader('X-CSRF-Token', token);
    },

We see a lot of issues by people not understaning the decryption or need to remove it. This will allow all cases:

  1. Check the _token input, plain text
  2. Check the X-CSRF-TOKEN, plain text
  3. Check the X-XSRF-TOKEN, encrypted

And a section would needed to be added to the docs, but I'm willing to write that, with a few simple examples.

Fixes #7418 #7437 #7436 #7373 #7435 #7288 #7287

@JoostK

JoostK commented Feb 20, 2015

Copy link
Copy Markdown
Contributor

Now this seems like a proper universal fix. Clears up the apparently unknown convention of XSRF vs. CSRF.

@barryvdh

Copy link
Copy Markdown
Contributor Author

Well I don't know if that is really the convention. I think the token being encrypted is more of a side-effect of laravel's cookies always being encrypted, which just doesn't matter for Angular bot does for other ways to pass the token.

taylorotwell added a commit that referenced this pull request Feb 20, 2015
Allow both encrypted + unencrypted CSRF header token
@taylorotwell
taylorotwell merged commit 8687d42 into laravel:5.0 Feb 20, 2015
@taylorotwell

Copy link
Copy Markdown
Member

Can you submit a documentation PR?

@barryvdh

Copy link
Copy Markdown
Contributor Author

Docs PR in laravel/docs#1179

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.

4 participants