pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
501 stars 239 forks source link

Do not set cookie if not required #70

Closed zebpalmer closed 1 year ago

zebpalmer commented 7 years ago

As currently implemented, flask session will add the set-cookie header to every request including static files, etc. this prevents most standard caching setups. We should call self.should_set_cookie() to determine if the set-cookie header should be included.

With flask default config, this doesn't actually modify the set-cookie header behavior. But, with this change, and by setting SESSION_REFRESH_EACH_REQUEST to False in the flask app config, the set-cookie header will only be included when session.modified is True. This attribute is set anytime one modifies the session, but can also be set manually in specific flask views if needed to update session timeouts.

knivre commented 7 years ago

Hi,

The motivation behind that pull request makes a lot of sense and I concur that this issue should be addressed.

That being said, it raises a significant number of questions and remarks.

Last but not least: I know this is flask-session's Git repository, but the behaviour implemented by flask itself raises questions too: does anyone know why SESSION_REFRESH_EACH_REQUEST applies to permanent sessions only? The original commit ( https://github.com/pallets/flask/commit/d1d835c02302884b2db1cab099b3ea6a84f41d32 ) says nothing about this discrimination.

bakert commented 4 years ago

Did anything like this get implemented? We are having some very strange effects when Cloudflare caches a particular SVG static resource that has a Set-Cookie for the session cookie in the response headers. Is there a way to prevent this header being sent with static resources?

funoverip commented 3 years ago

Uncommenting the block allowed me to send cookies only when needed. To anyone wondering why Cloudflare does not cache static files (js, css, ...), just activate this check in version 0.3.2.

if not self.should_set_cookie(app, session): 
     return