-
Notifications
You must be signed in to change notification settings - Fork 268
Add sanitazing of redirect uri in authorization scenarios #3825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cpp11nullptr
merged 5 commits into
master
from
iepoly/add-sanitazing-of-redirect-uri-in-authorization-scenarios
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e92a4b
Add sanitazing of redirect uri in authorization scenarios
5147e15
Add redirect uri helper
0f6bd0e
Merge branch 'master' into iepoly/add-sanitazing-of-redirect-uri-in-a…
cpp11nullptr c73cb7e
Fix build errors
ad5adda
Merge branch 'iepoly/add-sanitazing-of-redirect-uri-in-authorization-…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Identity.Web; | ||
|
|
||
| /// <summary> | ||
| /// Shared redirect-URI sanitization helpers for consistent local-URL validation | ||
| /// across login/logout endpoints and authorization attributes. | ||
| /// </summary> | ||
| internal static class RedirectUriHelper | ||
| { | ||
| /// <summary> | ||
| /// Returns <c>true</c> when <paramref name="url"/> is a strictly local path | ||
| /// (starts with a single "/" that is not followed by another "/" or "\") | ||
| /// and does not begin with a percent-encoded slash or backslash sequence. | ||
| /// </summary> | ||
| internal static bool IsLocalUrl(string? url) | ||
| { | ||
| if (string.IsNullOrEmpty(url)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (HasPercentEncodedSlashPrefix(url!)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // "/foo" is local, but not "//foo" (protocol-relative) and not "/\foo" (slash-backslash). | ||
| if (url![0] == '/') | ||
| { | ||
| return url.Length == 1 || (url[1] != '/' && url[1] != '\\'); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>true</c> when <paramref name="path"/> starts with a percent-encoded | ||
| /// forward slash (<c>%2f</c>) or backslash (<c>%5c</c>). | ||
| /// </summary> | ||
| internal static bool HasPercentEncodedSlashPrefix(string path) => | ||
| path.StartsWith("/%2f", StringComparison.OrdinalIgnoreCase) | ||
| || path.StartsWith("/%5c", StringComparison.OrdinalIgnoreCase); | ||
| } |
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
59 changes: 59 additions & 0 deletions
59
tests/Microsoft.Identity.Web.Test/RedirectUriHelperTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Identity.Web.Test | ||
| { | ||
| public class RedirectUriHelperTests | ||
| { | ||
| [Theory] | ||
| // Local paths — accepted. | ||
| [InlineData("/", true)] | ||
| [InlineData("/home", true)] | ||
| [InlineData("/home?query=1", true)] | ||
| [InlineData("/a/b/c", true)] | ||
| // Null/empty — rejected. | ||
| [InlineData(null, false)] | ||
| [InlineData("", false)] | ||
| // Protocol-relative — rejected. | ||
| [InlineData("//some.example", false)] | ||
| [InlineData("//some.example/path", false)] | ||
| // Slash-backslash — rejected. | ||
| [InlineData("/\\some.example", false)] | ||
| [InlineData("/\\\\some.example", false)] | ||
| // Absolute URLs — rejected. | ||
| [InlineData("https://some.example/", false)] | ||
| [InlineData("http://some.example/", false)] | ||
| [InlineData("javascript:alert(1)", false)] | ||
| // Bare hostnames / non-slash-prefixed — rejected. | ||
| [InlineData("some.example", false)] | ||
| [InlineData("home", false)] | ||
| // Percent-encoded slash/backslash — rejected (reverse proxies may decode these). | ||
| [InlineData("/%2Fsome.example", false)] | ||
| [InlineData("/%2fsome.example", false)] | ||
| [InlineData("/%5Csome.example", false)] | ||
| [InlineData("/%5csome.example", false)] | ||
| [InlineData("/%2f%2fsome.example/x", false)] | ||
| [InlineData("/%2F%5Csome.example", false)] | ||
| public void IsLocalUrl_ValidatesCorrectly(string? input, bool expected) | ||
| { | ||
| Assert.Equal(expected, RedirectUriHelper.IsLocalUrl(input)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("/%2Fsome.example", true)] | ||
| [InlineData("/%2fsome.example", true)] | ||
| [InlineData("/%5Csome.example", true)] | ||
| [InlineData("/%5csome.example", true)] | ||
| [InlineData("/%2f%2fsome.example/x", true)] | ||
| [InlineData("/%2F%5Csome.example", true)] | ||
| [InlineData("/home", false)] | ||
| [InlineData("/a/b/c", false)] | ||
| [InlineData("/", false)] | ||
| public void HasPercentEncodedSlashPrefix_DetectsEncodedSlashes(string input, bool expected) | ||
| { | ||
| Assert.Equal(expected, RedirectUriHelper.HasPercentEncodedSlashPrefix(input)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.