-
Notifications
You must be signed in to change notification settings - Fork 856
Fix open redirect vulnerability #15952
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
+114
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7a33a2c
Fix open redirect vulnerability in ValidateTokenMiddleware
JamesNK 8010ef7
Use full ASP.NET Core IsLocalUrl implementation for returnUrl validation
JamesNK ad14a0e
Apply suggestion from @JamesNK
JamesNK 67b01ab
Move IsLocalUrl to shared UrlValidationHelper and fix Login page open…
JamesNK 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,83 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Aspire.Dashboard.Utils; | ||
|
|
||
| /// <summary> | ||
| /// Helpers for validating redirect URLs. | ||
| /// </summary> | ||
| internal static class UrlValidationHelper | ||
| { | ||
| /// <summary> | ||
| /// Checks whether a URL is safe to use as a redirect target. | ||
| /// The URL must be a valid URI and a local path (not absolute or protocol-relative). | ||
| /// </summary> | ||
| internal static bool IsSafeRedirectUrl([NotNullWhen(true)] string? url) | ||
| { | ||
| return Uri.TryCreate(url, UriKind.Relative, out _) && IsLocalUrl(url); | ||
| } | ||
|
|
||
| // Copied from ASP.NET Core's IsLocalUrl implementation: | ||
| // https://github.com/dotnet/aspnetcore/blob/7cbda0e023075490b4365a0754ca410ce6eff59a/src/Shared/ResultsHelpers/SharedUrlHelper.cs#L33 | ||
| internal static bool IsLocalUrl([NotNullWhen(true)] string? url) | ||
| { | ||
| if (string.IsNullOrEmpty(url)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Allows "/" or "/foo" but not "//" or "/\". | ||
| if (url[0] == '/') | ||
| { | ||
| // url is exactly "/" | ||
| if (url.Length == 1) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // url doesn't start with "//" or "/\" | ||
| if (url[1] != '/' && url[1] != '\\') | ||
| { | ||
| return !HasControlCharacter(url.AsSpan(1)); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // Allows "~/" or "~/foo" but not "~//" or "~/\". | ||
| if (url[0] == '~' && url.Length > 1 && url[1] == '/') | ||
| { | ||
| // url is exactly "~/" | ||
| if (url.Length == 2) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // url doesn't start with "~//" or "~/\" | ||
| if (url[2] != '/' && url[2] != '\\') | ||
| { | ||
| return !HasControlCharacter(url.AsSpan(2)); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| static bool HasControlCharacter(ReadOnlySpan<char> readOnlySpan) | ||
| { | ||
| // URLs may not contain ASCII control characters. | ||
| for (var i = 0; i < readOnlySpan.Length; i++) | ||
| { | ||
| if (char.IsControl(readOnlySpan[i])) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } |
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
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.