fix(session): warn when cookie_same_site="none" lacks cookie_https_only - #217
Merged
Merged
Conversation
Browsers reject a SameSite=None cookie without the Secure flag, so this configuration silently never stored the session cookie. SessionConfig now emits a UserWarning at validation time. The combination is still accepted; rejecting it would break existing configurations. Closes #167
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #167 (part of #117)
Problem
Browsers reject a
SameSite=Nonecookie that is notSecure. Withcookie_same_site="none"and the defaultcookie_https_only=False,FastAPICacheXSessionMiddlewaresends a cookie that the client silently drops, so the session never sticks and nothing tells the developer why.Change
SessionConfighas amodel_validator(mode="after")that emits aUserWarningfor this combination.stacklevelpoints at the line that builds the config.SESSION.md): thecookie_same_sitecomment in the configuration example notes that"none"needscookie_https_only=True.Tests
In
tests/session/test_config.py:test_same_site_none_without_https_only_warnstest_other_cookie_settings_do_not_warn, fornone+True,lax+Falseandstrict+False, with warnings turned into errors.Mutation checks:
cookie_https_onlycondition fails only thenone-Truecase;Ruff,
mypy --strict, the full suite against live Redis and Memcached (897 passed), andzensical build --strictfor both languages.