Skip to content

Commit 30f55fd

Browse files
isUrlLocal /3 (#35240)
* isUrlLocal /3 * isUrlLocal /3 * Update aspnetcore/security/preventing-open-redirects.md Co-authored-by: Martin Costello <[email protected]> --------- Co-authored-by: Martin Costello <[email protected]>
1 parent 88594a6 commit 30f55fd

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

Diff for: aspnetcore/security/preventing-open-redirects.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public IActionResult SomeAction(string redirectUrl)
5050

5151
`LocalRedirect` will throw an exception if a non-local URL is specified. Otherwise, it behaves just like the `Redirect` method.
5252

53-
### IsLocalUrl
53+
### IUrlHelper.IsLocalUrl
5454

5555
Use the <xref:Microsoft.AspNetCore.Mvc.IUrlHelper.IsLocalUrl%2A> method to test URLs before redirecting:
5656

@@ -70,4 +70,25 @@ private IActionResult RedirectToLocal(string returnUrl)
7070
}
7171
```
7272

73-
The `IsLocalUrl` method protects users from being inadvertently redirected to a malicious site. You can log the details of the URL that was provided when a non-local URL is supplied in a situation where you expected a local URL. Logging redirect URLs may help in diagnosing redirection attacks.
73+
The `IUrlHelper.IsLocalUrl` method protects users from being inadvertently redirected to a malicious site. You can log the details of the URL that was provided when a non-local URL is supplied in a situation where you expected a local URL. Logging redirect URLs may help in diagnosing redirection attacks.
74+
75+
:::moniker range=">= aspnetcore-10.0"
76+
77+
### Detect if URL is local using `RedirectHttpResult.IsLocalUrl`
78+
79+
The [`RedirectHttpResult.IsLocalUrl(url)`](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/RedirectHttpResult.cs,c0ece2e6266cb369) helper method detects if a URL is local. A URL is considered local if the following are true:
80+
81+
* It doesn't have the [host](https://developer.mozilla.org/docs/Web/API/URL/host) or [authority](https://developer.mozilla.org/docs/Web/URI/Authority) section.
82+
* It has an [absolute path](https://developer.mozilla.org/docs/Learn_web_development/Howto/Web_mechanics/What_is_a_URL#absolute_urls_vs._relative_urls).
83+
84+
URLs using [virtual paths](/previous-versions/aspnet/ms178116(v=vs.100)) `"~/"` are also local.
85+
86+
`IsLocalUrl` is useful for validating URLs before redirecting to them to prevent [open redirection attacks](https://brightsec.com/blog/open-redirect-vulnerabilities/).
87+
88+
```csharp
89+
if (RedirectHttpResult.IsLocalUrl(url))
90+
{
91+
return Results.LocalRedirect(url);
92+
}
93+
94+
:::moniker-end

0 commit comments

Comments
 (0)