-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Do not save session on client error #220
base: main
Are you sure you want to change the base?
Conversation
@@ -250,6 +250,7 @@ where | |||
|
|||
_ if (modified || session_config.always_save) | |||
&& !empty | |||
&& !res.status().is_client_error() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you go into more detail regarding the logic here? Django only filters out server errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated PR description. Sorry for the mess.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #220 +/- ##
==========================================
+ Coverage 83.80% 83.85% +0.04%
==========================================
Files 5 5
Lines 352 353 +1
==========================================
+ Hits 295 296 +1
Misses 57 57 ☔ View full report in Codecov by Sentry. |
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 |
IMHO, session should not be saved on client error. For instance, with
always_save
option 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?