Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use removeprefix to strip weak ETag indicator in is_not_modified#3193

Merged
Kludex merged 3 commits into
Kludex:mainfrom
gnosyslambda:fix/etag-weak-indicator-parsing
Jun 10, 2026
Merged

Use removeprefix to strip weak ETag indicator in is_not_modified#3193
Kludex merged 3 commits into
Kludex:mainfrom
gnosyslambda:fix/etag-weak-indicator-parsing

Conversation

@gnosyslambda

Copy link
Copy Markdown
Contributor

is_not_modified() uses tag.strip(" W/") to remove the weak ETag prefix, but strip() works on a character set — it strips any combination of ' ', 'W', and '/' from both ends of the string. This means an ETag value ending in W or / would get incorrectly trimmed.

Replaced with tag.strip().removeprefix("W/") which strips whitespace first, then removes the literal W/ prefix only. removeprefix is 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.

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 themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@Kludex Kludex changed the title Fix incorrect ETag weak indicator stripping in is_not_modified Use removeprefix to strip weak ETag indicator in is_not_modified Jun 10, 2026

@Kludex Kludex left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Kludex Kludex merged commit 3730925 into Kludex:main Jun 10, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants