Add callback to decide whether a session can be saved based on the response status code#220
Add callback to decide whether a session can be saved based on the response status code#2200rzech wants to merge 1 commit intomaxcountryman:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #220 +/- ##
==========================================
- Coverage 87.35% 86.68% -0.68%
==========================================
Files 5 5
Lines 356 368 +12
==========================================
+ Hits 311 319 +8
- Misses 45 49 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I think I'd like to understand better why Django doesn't do this. (Maybe there's some historical context or discussion about it.) In a similar vein, are there examples of other established frameworks also not saving on client errors? |
|
I did some archaeology and it looks like http status code exclusions were added fairly recently to, case-by-case and for different reasons - Django session middleware had been masking exceptions / status codes without these exclusions. At first no status codes were excluded, then only 500 (https://code.djangoproject.com/ticket/3881) and then status codes >= 500 (https://code.djangoproject.com/ticket/34173). I tried to find how other frameworks do it, but I'm not sure if they implement "always save" at all. RoR doesn't seem to have it: https://api.rubyonrails.org/v7.2.2/classes/ActionDispatch/Session/CookieStore.html . Phoenix Framework doesn't seem to have such option either: https://hexdocs.pm/plug/Plug.Session.html . Same with Spring: https://docs.spring.io/spring-session/reference/configuration/common.html . It looks like developers are supposed to implement it themselves there. |
|
Interesting! Thanks for looking into that. Do you think we shouldn't try to implement "always save" altogether? |
|
I think it's good to have this option, either built-in or through some Perhaps RFC will help us: https://datatracker.ietf.org/doc/html/rfc6265#section-3 ?
This means that setting cookies on both 4xx and 5xx is fine according to spec. Now, the question is whether we would like to skip refreshing the session when these errors occur and how to do it? I think there are several options:
|
|
I kind of like the first option, since that seems like the most flexible but maybe it would be annoying to implement? |
|
I like the first option too. I haven't tried to implement it yet, but |
|
If we can remove Clone and Debug or otherwise rework SessionConfig, that's fine. (There's another issue where some applications would like to mutate the config after it's been set up so it may be worthwhile to rethink that altogether.) |
|
Removing |
1d4adb9 to
92ad026
Compare
88948a5 to
f3987ad
Compare
f4a63b7 to
b6ef5d4
Compare
a11060f to
942c871
Compare
754b85f to
5495f2f
Compare
c9458ac to
f937bbf
Compare
|
@maxcountryman I left the
I think I might've used a bit too much of your GH Actions minutes. Please contact me if you run out of the minutes of your current billing cycle because of this. |
f937bbf to
dca926a
Compare
dca926a to
71e5634
Compare
This allows setting a callback to decide whether a session can be saved based on the response status code.
71e5634 to
f46246e
Compare
IMHO, session should not be saved on client error. For instance, with
always_saveoption it can lead to prolonging session lifetime on HTTP 401.Maybe I'm wrong, but in general it seems weird to save session even when client made an invalid request.
But perhaps it would be better not to save on client error, but only when the session was not modified anyway, like so:
?
Or maybe we should just stick to the Django way you showed in #220 (comment) and close this PR?