Fix URL parameter loss when middleware rewrites request path - #5
Open
wasim-builds wants to merge 1 commit into
Open
Fix URL parameter loss when middleware rewrites request path#5wasim-builds wants to merge 1 commit into
wasim-builds wants to merge 1 commit into
Conversation
When middleware rewrites r.URL.Path (e.g., stripping prefixes), chi's cached RoutePath becomes stale and URL parameters get lost. Changes: - Add URLPath/RawPath fields to chi.Context for tracking path mutations - Add SyncPath(r) method to detect and resync RoutePath after middleware - Call SyncPath in ServeHTTP (after pool fetch) and routeHTTP (before matching) - Add test verifying URL params survive a middleware path rewrite Fixes madalynerlge2#1
Author
|
Hi, this PR fixes the URL parameter loss bug described in #1. The fix resyncs RoutePath in chi.go when middleware rewrites the request path. Happy to make any changes or add tests if needed! |
Author
|
/claim #1 |
|
😅 Unfortunately there are no rewards left to claim in this issue! |
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
Fixes #1
When middleware rewrites
r.URL.Path(e.g., stripping prefixes or normalizing paths), chi's cachedRoutePathbecomes stale and URL parameters get lost.chi.URLParam(r, "key")returns empty strings because route matching happens against the old path.Root Cause
chi caches the request path in
rctx.RoutePathat the start ofServeHTTP. When middleware modifiesr.URL.Pathbetween that point and whenrouteHTTPdoes pattern matching, the stale cache causes wrong route matches and lost parameters.Fix
Added
SyncPath(r)in two key locations:ServeHTTP— resyncs after getting the routing context (both for fresh requests and parent-router contexts)routeHTTP— resyncs right before pattern matching, catching any middleware mutationsAdded
URLPathandRawPathfields tochi.Contextto track the last-seenr.URL.Pathand detect when it's been mutated.Test
Backward Compatibility
r.URL.Pathis not modified (string comparison short-circuits)chi.URLParam(r, "key")require zero changes