fix(cache): percent-encode | and % in cache-key host and path - #263
Merged
Merged
Conversation
The default key builder joined the raw Host header and decoded path with |||, so a Host of 'example.com|||/p' on GET /x stored the response under the key of GET /p%7C%7C%7C/x. Encode | and % in both components with escape_key_component; clear_path encodes its argument the same way and the monitoring routes decode for display. Recommend TrustedHostMiddleware in the HTTP caching guide. Closes #230
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.
Summary
Closes #230.
default_key_builderjoined the rawHostheader and the decoded URL path with|||. Both come from the client, so either could carry the separator and shift the components. Reproduced on master:Changes
fastapi_cachex/types.py: newescape_key_component(%→%25,|→%7C) andunescape_key_component. Encoding%as well keeps the mapping injective, so a path holding a literal%7Ccannot collide with one holding|.default_key_builderencodes the host and path. The query string is URL-encoded bystr(request.query_params)already and is left alone.clear_path()on the memory and Redis backends encodes its argument before matching. It still takes the path as the app sees it. Memcached is unchanged, since it only does an exact-key delete._parse_cache_key) decode host and path for display.clear_pattern()interacts with it, and the one-off miss after upgrading. RecommendsTrustedHostMiddleware. The per-user key builder example now usesescape_key_component.Compatibility
Keys only change when the host or path contains
|or%. Everything else hits the existing entries after the upgrade.Tests
tests/test_cache_key.py::TestCacheKeySeparatorInComponents:%encoding, built from a raw scope, because TestClient decodes the path twice;tests/backends/test_clear_pattern_contract.py::test_clear_path_finds_paths_with_encoded_charactersruns on memory and Redis.%escaping, memoryclear_pathraw, Redisclear_pathraw, routes without unescape. Each fails only the new tests aimed at it.CHANGELOG
Added under Unreleased → Security.