Collapse scheme-relative leading slashes in Rewrite middleware redirect/rewrite targets#66961
Open
rokonec wants to merge 1 commit into
Open
Conversation
…ct/rewrite targets `AddRedirect`, IIS URL Rewrite, and Apache mod_rewrite rules let an attacker-controlled or misconfigured back-reference produce a target starting with `//`, `///`, or `/\` (and longer runs). When `PathBase` is empty, the resulting `Location` header is scheme-relative and resolves off-origin — an open redirect. This change adds a small internal helper `UrlNormalizer.CollapseLeadingSlashes` that mirrors the rejection predicate in `SharedUrlHelper.IsLocalUrl` but coerces instead of returning a boolean: any leading run of `/` and `\` is collapsed to a single `/`. The helper is applied at the three sinks that emit `Location` or mutate `Request.Path`: * `RedirectRule.ApplyRule` (`AddRedirect` surface) * `UrlActions.RedirectAction.ApplyAction` (IIS / Apache redirect import) * `UrlActions.RewriteAction.ApplyAction` (defense in depth for `Request.Path`) Adds regression theories on all three surfaces covering `//`, `///`, `//////`, `/\` shapes, both as literal replacements and as back-reference-synthesized captures. No public API surface change; `PublicAPI.Shipped.txt` / `PublicAPI.Unshipped.txt` unchanged.
fa44f13 to
f9ce7b0
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an open-redirect vulnerability (#66486) in the Rewrite middleware where scheme-relative URLs (//host, /\host) produced by AddRedirect, IIS URL Rewrite, or Apache mod_rewrite rules could be emitted as Location headers and resolved off-origin by browsers. It introduces a small UrlNormalizer.CollapseLeadingSlashes helper that mirrors the rejection logic in SharedUrlHelper.IsLocalUrl and applies it at the three sinks that emit Location headers or mutate Request.Path.
Changes:
- Add
internal static class UrlNormalizerwith a zero-allocation fast-pathCollapseLeadingSlashesmethod. - Invoke the helper in
RedirectRule.ApplyRule,RedirectAction.ApplyAction, andRewriteAction.ApplyAction(defense-in-depth); remove the stale// TODO check for false positivescomment. - Add
[Theory]coverage for//,///,//////, and/\shapes acrossAddRedirect, IIS URL Rewrite, and Apache mod_rewrite (both redirect and rewrite flows).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Middleware/Rewrite/src/UrlNormalizer.cs | New helper that collapses a leading run of //\ to a single /. |
| src/Middleware/Rewrite/src/RedirectRule.cs | Normalize replacement before host/path parsing in AddRedirect. |
| src/Middleware/Rewrite/src/UrlActions/RedirectAction.cs | Normalize pattern in IIS/Apache redirect imports; remove stale TODO. |
| src/Middleware/Rewrite/src/UrlActions/RewriteAction.cs | Defense-in-depth normalization before assigning to Request.Path. |
| src/Middleware/Rewrite/test/MiddlewareTests.cs | Theory covering AddRedirect scheme-relative collapse. |
| src/Middleware/Rewrite/test/IISUrlRewrite/MiddleWareTests.cs | Theory covering IIS URL Rewrite back-reference collapse. |
| src/Middleware/Rewrite/test/ApacheModRewrite/ModRewriteMiddlewareTest.cs | Theories covering Apache mod_rewrite redirect and rewrite collapse. |
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
AddRedirect, IIS URL Rewrite, and Apachemod_rewriterules let a back-reference (or a literal rule replacement) produce a redirect/rewrite target that starts with//,///, or/\(and longer runs). WhenPathBaseis empty, the resultingLocationheader is scheme-relative and resolves off-origin — i.e. an open redirect.Example today:
Fix
Introduce a small
internal static class UrlNormalizerwith one method:It mirrors the rejection predicate in
SharedUrlHelper.IsLocalUrlbut coerces instead of returning abool: any leading run of/and\is collapsed to a single/. The fast-path is zero-allocation and reference-returning when the input does not begin with//or/\.The helper is invoked at the three sinks that emit
Locationor mutateRequest.Path:RedirectRule.ApplyRuleAddRedirect(...)rulesUrlActions.RedirectAction.ApplyActionUrlActions.RewriteAction.ApplyActionRequest.PathAlso removes a stale
// TODO check for false positivescomment inRedirectActionthat was tracking exactly this gap.Tests
Four new
[Theory]methods covering all three surfaces, including back-reference-synthesized cases:CheckRedirect_CollapsesSchemeRelativeTarget—AddRedirect(7 rows)Invoke_RedirectCollapsesSchemeRelativeBackReference— IIS URL Rewrite (3 rows)Invoke_RedirectCollapsesSchemeRelativeBackReference— Apachemod_rewrite,[R=302,L](3 rows)Invoke_RewriteCollapsesSchemeRelativeBackReference— Apachemod_rewrite, no[R]flag, assertsRequest.Path(3 rows)Each surface is covered for
//,///,//////, and/\shapes, both as literal replacements and as$1/{R:1}captures.Compatibility
PublicAPI.Shipped.txtandPublicAPI.Unshipped.txtare byte-identical.Headers.Locationassertions in the Rewrite test suite still pass — full suite is 473 / 473 green.main.Validation
Fixes: #66486