feat(session): expose the proxy-aware client IP used for IP binding - #97
Merged
Merged
Conversation
get_client_ip() is now public and exported from fastapi_cachex.session, and ClientIPDep resolves it with the registered SessionManager's config. Passing it to create_session() stores the same address the middleware checks, so sessions created behind a trusted proxy are no longer rejected on their next request. Closes #87
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 #87.
Problem
With
ip_bindingon, the middleware checks each request against the address from the private_get_client_ip(). Behind one oftrusted_proxies, that address comes fromX-Forwarded-For/X-Real-IP. Applications passedrequest.client.hosttocreate_session(), but behind the proxy that is the proxy's address. The bound IP never matched, so every session was rejected on its second request.Changes
get_client_ip(connection, config)is now public insession/middleware.pyand exported fromfastapi_cachex.session. Its logic is unchanged, and the middleware's_get_client_ipmethod is kept.get_session_client_ipis a new dependency, exported fromfastapi_cachex.session, with theClientIPDepalias insession.dependenciesnext to the other*Depaliases. It resolves the address using the registeredSessionManager's config.docs/SESSION.md: the login example usesClientIPDep. The reverse-proxy section now shows both options instead of only warning.[Unreleased]Added entry.Tests
tests/session/test_client_ip.py:get_client_ipbehind a trusted and an untrusted peer, and with no peer.ClientIPDepis honoured on the next request.request.client.hostthere is rejected.The existing
test_starlette_middleware.pytests now import the public name.Local: ruff check/format and mypy (package strict, tests, scripts) are clean.
zensical build --strictpasses. Against live Redis and Memcached: 697 passed and 1 skipped (the live-server gate), with 100% coverage.