Skip to content

Fix rewrite map lookups nested in a pattern - #68721

Open
astralmaster wants to merge 1 commit into
dotnet:mainfrom
astralmaster:rewrite-map-nested-pattern
Open

Fix rewrite map lookups nested in a pattern#68721
astralmaster wants to merge 1 commit into
dotnet:mainfrom
astralmaster:rewrite-map-nested-pattern

Conversation

@astralmaster

Copy link
Copy Markdown

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.Evaluate evaluates its key pattern with the StringBuilder shared on RewriteContext. 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, and UrlEncodeSegment already avoid this by swapping in a fresh builder while they evaluate their inner pattern and restoring it afterward. RewriteMapSegment was missing that swap.

Change

Evaluate the rewrite map key with its own StringBuilder and restore the shared one afterward, matching the sibling segments.

Testing

Added Invoke_RewriteMapNestedInActionUrlEvaluatesKey to 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-1234 instead of /newpage.aspx?id=abcd-abcd) and passes with it. The full Rewrite test suite (478 tests) passes on Linux and Windows.

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
@astralmaster

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

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.

Rewrite module - Rule action not working with rewrite maps

1 participant