Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[cookbook][cache] How to cache pages containing form with csrf token #1216

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

Closed
yosmanyga opened this issue Apr 3, 2012 · 8 comments
Closed

Comments

@yosmanyga
Copy link
Contributor

No description provided.

@wouterj
Copy link
Member

wouterj commented Jan 22, 2013

@weaverryan should get a doc request label.

@gunnarlium
Copy link
Contributor

As the CSRF token is private to each user, I don't think it makes sense to cache these pages.

I also believe CSRF tokens by necessity will always be stored in the session, and Varnish (and other similar proxies) must either ignore cookies or not cache pages with cookies.

If the whole page is expensive to generate, I suppose you might attempt to load the form (or even just the token field) with through ESI, but I'm not sure I would recommend that -- at least not highly enough to create a cookbook entry :).

@ricardclau
Copy link
Contributor

I know this is a very old issue but I would like to add my thoughts to it since it can be misleading and I also think that this should be closed

Looking at how the CSRF tokens are generated:

    public function generateCsrfToken($intention)
    {
        return sha1($this->secret.$intention.$this->getSessionId());
    }

Of course, you can add your own provider and return a plain hardcoded string but this is not my point and it would break the CSRF token purpose

Anyway, the tokens depend on the session_id which is unique per user / session. And caching such pages would effectively cause that the GET request to render the form gives you back a cached response (thus sending the same CSRF token for many users) and the POST request would actually fail because it would validate that token with the user session id.

This is an issue to many web applications when you put caching in place and this is why some high traffic web applications send a cached response for the static content but all the user interaction widgets are generated via uncached AJAX requests or similar techniques.

Hope this makes sense

Thoughts on this @wouterj @weaverryan @gunnarlium @yosmanyga ?

@weaverryan
Copy link
Member

@ricardclau What you're saying makes good sense. What do you propose? Do you think nothing needs to be said? Or should we have a small cookbook entry about this (that basically says don't cache them or load them with AJAX)?

@ricardclau
Copy link
Contributor

Well, this is one of those cases not related to Symfony2 itself but to how some technology + Symfony works. There are many opinions on adding cookbooks about Nginx, Varnish, logrotate and other bits.

I am clearly 👎 on writing a cookbook about this and it does not complement http://symfony.com/doc/current/cookbook/cache/varnish.html but maybe we could add a note on http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html

Anyway, not sure about it, but if you think it can be useful, I can work on it :)

@gunnarlium
Copy link
Contributor

I think it could make sense to mention it in the "Using CSRF Protection in the Login Form" entry. Perhaps more as a warning to not cache pages with CSRF tokens, and perhaps hint at the option of using AJAX or ESI instead.

@weaverryan
Copy link
Member

A note makes sense to me. It would also need to be added to the form chapter of the book where we talk about CSRF.

@ricardclau
Copy link
Contributor

Waiting for your comments @weaverryan @gunnarlium @wouterj

weaverryan added a commit that referenced this issue Jan 4, 2015
This PR was merged into the 2.3 branch.

Discussion
----------

Notes about caching pages with a CSRF Form

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | no
| Applies to    | all
| Fixed tickets | #1216

Commits
-------

1bc7ef2 cache_csrf_form
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

No branches or pull requests

5 participants