-
Notifications
You must be signed in to change notification settings - Fork 639
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
Ensure response is not cached when generating a CSRF token #15281
Conversation
Just to make sure I understand this correctly, does this mean that when the code snippet below is added to your <script>
window.csrfTokenName = "{{ craft.app.config.general.csrfTokenName|e('js') }}";
window.csrfTokenValue = "{{ craft.app.request.csrfToken|e('js') }}";
</script> If so, that might be problematic for a lot of websites. |
@mikesnoeren Basically yes. (Actually, your comment made us realize this PR didn’t go quite far enough, and we should be doing the same thing from There are two reasons we made this change:
Craft 4.9 and 5.2 added the asyncCsrfInputs config setting, which makes it easy to include CSRF inputs in forms without sacrificing cachability. When that’s enabled, You could take the same Ajax approach with your JS code: <script>
(function() {
fetch("{{ actionUrl('users/session-info') }}", {
headers: {
'Accept': 'application/json',
}
})
.then(response => response.json())
.then(data => {
window.csrfTokenName = data.csrfTokenName;
window.csrfTokenValue = data.csrfTokenValue;
});
})();
</script> |
That makes total sense, and it seems like a good change, especialy the asyncCsrfInputs config setting is a very welcome change. However, this update could have significant performance implications for sites that rely heavily on craft native caching. Would it be possible to include a notice in the release notes or update documentation highlighting this change? This would allow developers to assess the potential impact on their sites and make any necessary adjustments before updating. Perhaps something like: |
To be clear, this only impacts the cache response headers, e.g. |
Ah yes, I apologize for the confusion in my last message. However, the core intention remains the same. We have a couple of sites cached behind Cloudflare to significantly reduce the load on their servers. Updating to this release without thoroughly reviewing all the update logs could potentially lead to issues or even server crashes. |
Fair enough, added a note to the release notes for the next release (be75da6). |
We're applying this to a multi-site setup and running into a CORS issue, with the error "No 'Access-Control-Allow-Origin' header is present on the requested resource". I assume we need to allow the origin URL as below, though we've not established how to do that yet! Access-Control-Allow-Origin: www.example.com |
@paulwaddington the upcoming Craft 4.11/5.3 release will provide a easy way to do this via |
Description
Prevents accidental caching when generating CSRF. Technically we could now remove this explicit call to
setNoCacheHeaders
, but I left it for now.Related issues