Use removeprefix to strip weak ETag indicator in is_not_modified#3193
Merged
Kludex merged 3 commits intoJun 10, 2026
Conversation
strip(" W/") removes individual characters from the set {' ', 'W', '/'}
from both ends of the string, which can mangle ETag values that happen
to end with those characters. Use strip() + removeprefix("W/") instead
to only strip whitespace and then remove the literal W/ prefix.
Signed-off-by: gnosyslambda <[email protected]>
themavik
reviewed
Mar 23, 2026
themavik
left a comment
There was a problem hiding this comment.
Good catch on strip(" W/") — it can remove unrelated characters at the ends of a strong etag. strip() plus removeprefix("W/") matches the weak-etag prefix case without over-stripping the quoted value.
themavik
reviewed
Mar 23, 2026
themavik
left a comment
There was a problem hiding this comment.
tag.strip().removeprefix("W/") fixes weak ETag handling compared to strip(" W/"), which could eat unrelated leading characters.
naarob
pushed a commit
to naarob/starlette
that referenced
this pull request
Mar 26, 2026
…D route precedence (Kludex#3182) fix Kludex#3193: staticfiles.is_not_modified() used tag.strip(' W/') which operates on a character set, incorrectly trimming ETags ending in 'W' or '/'. Replaced with a normalize_etag() helper that uses str.startswith('W/') for prefix removal. fix Kludex#3182: Route.__init__ adds HEAD implicitly when GET is defined, causing the GET route to steal HEAD requests before any explicit HEAD route can match. Tracked _explicit_methods and return Match.PARTIAL for implicit HEAD so explicit HEAD routes registered at the same path can take precedence.
removeprefix to strip weak ETag indicator in is_not_modified
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.
is_not_modified()usestag.strip(" W/")to remove the weak ETag prefix, butstrip()works on a character set — it strips any combination of' ','W', and'/'from both ends of the string. This means an ETag value ending inWor/would get incorrectly trimmed.Replaced with
tag.strip().removeprefix("W/")which strips whitespace first, then removes the literalW/prefix only.removeprefixis available since Python 3.9 and Starlette requires 3.10+.In practice this is unlikely to cause issues with the current MD5-based ETags from FileResponse, but the fix makes the code do what it actually intends to do.