Fix rewrite map lookups nested in a pattern - #68721
Open
astralmaster wants to merge 1 commit into
Open
Conversation
RewriteMapSegment evaluated its key pattern with the shared RewriteContext
StringBuilder, so a rewrite map used inside another pattern (for example
{id-map:{C:1}} in an action URL) corrupted the builder the surrounding pattern
was still using. The key evaluation appended to and then cleared that shared
builder, so the map lookup missed and the rest of the URL was lost.
Give the key evaluation its own StringBuilder and restore the shared one
afterward, matching what ToLowerSegment, UrlDecodeSegment, and UrlEncodeSegment
already do.
Fixes dotnet#24618
Author
|
@dotnet-policy-service agree |
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 #24618. A rewrite map lookup used inside another pattern, such as
{id-map:{C:1}}in a rule's action URL, does not work: the redirect or rewrite drops the mapped value and the rest of the URL is lost.RewriteMapSegment.Evaluateevaluates its key pattern with theStringBuildershared onRewriteContext. Pattern evaluation is recursive in this case, because the map key is itself a pattern, so evaluating the key appends to and then clears the same builder the surrounding pattern is still using. The map lookup then misses, because the computed key is wrong, and the outer pattern's accumulated text is discarded.ToLowerSegment,UrlDecodeSegment, andUrlEncodeSegmentalready avoid this by swapping in a fresh builder while they evaluate their inner pattern and restoring it afterward.RewriteMapSegmentwas missing that swap.Change
Evaluate the rewrite map key with its own
StringBuilderand restore the shared one afterward, matching the sibling segments.Testing
Added
Invoke_RewriteMapNestedInActionUrlEvaluatesKeyto the IIS URL rewrite middleware tests, using a rewrite map nested in an action URL. It fails without the change (the response is/?id=1234-1234instead of/newpage.aspx?id=abcd-abcd) and passes with it. The full Rewrite test suite (478 tests) passes on Linux and Windows.