[1.x] Try to get CSRF token from cookie#242
Closed
m1guelpf wants to merge 3 commits intolaravel:masterfrom
m1guelpf:patch-1
Closed
[1.x] Try to get CSRF token from cookie#242m1guelpf wants to merge 3 commits intolaravel:masterfrom m1guelpf:patch-1
m1guelpf wants to merge 3 commits intolaravel:masterfrom
m1guelpf:patch-1
Conversation
Member
|
Does this work since content of that cookie is an encrypted version of the CSRF token, not the token itself? |
Author
|
As you can see, Laravel decrypts the header here: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php#L153 The only thing needed to make this work would be to change the header name from X-CSRF to X-XSRF when sending the encrypted versiom. Will PR that later today |
Member
|
Honestly I'm fine not messing with this. |
|
I was about to submit a PR with a slightly different approach to setting the I came up with this solution, which ensures the header is always kept up-to-date: const cookie = function (name) {
let match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
return (match ? decodeURIComponent(match[3]) : null);
}
const xsrf = (urlmatch) => {
let open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url) {
open.apply(this, arguments);
if ((new URL(url, window.location)).toString().includes(urlmatch)) {
this.setRequestHeader('X-XSRF-TOKEN', cookie('XSRF-TOKEN'))
}
}
};
xsrf('https://example.org/broadcasting/auth')
new Echo(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Laravel skeleton was recently updated to stop explicitly setting the CSRF token on the axios client, as it can automatically get it from the XSRF-TOKEN Laravel adds by default. This PR introduces the same behavior on Echo.