Skip to content

Commit 0ee0db8

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/release/10.8' into v10/dev
# Conflicts: # version.json
2 parents 119fde2 + c17d4e1 commit 0ee0db8

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

src/Umbraco.Core/Routing/WebPath.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,28 @@ public static string Combine(params string[]? paths)
5050

5151
return sb.ToString();
5252
}
53+
54+
55+
/// <summary>
56+
/// Determines whether the provided web path is well-formed according to the specified UriKind.
57+
/// </summary>
58+
/// <param name="webPath">The web path to check. This can be null.</param>
59+
/// <param name="uriKind">The kind of Uri (Absolute, Relative, or RelativeOrAbsolute).</param>
60+
/// <returns>
61+
/// true if <paramref name="webPath"/> is well-formed; otherwise, false.
62+
/// </returns>
63+
public static bool IsWellFormedWebPath(string? webPath, UriKind uriKind)
64+
{
65+
if (string.IsNullOrWhiteSpace(webPath))
66+
{
67+
return false;
68+
}
69+
70+
if (webPath.StartsWith("//"))
71+
{
72+
return uriKind is not UriKind.Relative;
73+
}
74+
75+
return Uri.IsWellFormedUriString(webPath, uriKind);
76+
}
5377
}

src/Umbraco.Web.BackOffice/Controllers/ImagesController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Umbraco.Cms.Core.IO;
88
using Umbraco.Cms.Core.Media;
99
using Umbraco.Cms.Core.Models;
10+
using Umbraco.Cms.Core.Routing;
1011
using Umbraco.Cms.Web.Common.Attributes;
1112
using Umbraco.Cms.Web.Common.DependencyInjection;
1213
using Umbraco.Extensions;
@@ -123,7 +124,7 @@ public IActionResult GetResized(string imagePath, int width)
123124

124125
private bool IsAllowed(string encodedImagePath)
125126
{
126-
if(Uri.IsWellFormedUriString(encodedImagePath, UriKind.Relative))
127+
if(WebPath.IsWellFormedWebPath(encodedImagePath, UriKind.Relative))
127128
{
128129
return true;
129130
}

src/Umbraco.Web.BackOffice/Controllers/PreviewController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Umbraco.Cms.Core.Models.Membership;
1212
using Umbraco.Cms.Core.Models.PublishedContent;
1313
using Umbraco.Cms.Core.PublishedCache;
14+
using Umbraco.Cms.Core.Routing;
1415
using Umbraco.Cms.Core.Security;
1516
using Umbraco.Cms.Core.Services;
1617
using Umbraco.Cms.Core.Web;
@@ -152,8 +153,7 @@ public ActionResult End(string? redir = null)
152153
// Expire Client-side cookie that determines whether the user has accepted to be in Preview Mode when visiting the website.
153154
_cookieManager.ExpireCookie(Constants.Web.AcceptPreviewCookieName);
154155

155-
if (Uri.IsWellFormedUriString(redir, UriKind.Relative)
156-
&& redir.StartsWith("//") == false
156+
if (WebPath.IsWellFormedWebPath(redir, UriKind.Relative)
157157
&& Uri.TryCreate(redir, UriKind.Relative, out Uri? url))
158158
{
159159
return Redirect(url.ToString());

tests/Umbraco.Tests.UnitTests/Umbraco.Core/Routing/WebPathTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,87 @@ public void Combine_must_handle_empty_array() =>
3131

3232
[Test]
3333
public void Combine_must_handle_null() => Assert.Throws<ArgumentNullException>(() => WebPath.Combine(null));
34+
35+
36+
[Test]
37+
[TestCase("ftp://hello.com/", UriKind.Absolute, ExpectedResult = true)]
38+
[TestCase("file:///hello.com/", UriKind.Absolute, ExpectedResult = true)]
39+
[TestCase("ws://hello.com/", UriKind.Absolute, ExpectedResult = true)]
40+
[TestCase("wss://hello.com/", UriKind.Absolute, ExpectedResult = true)]
41+
[TestCase("https://hello.com:8080/", UriKind.Absolute, ExpectedResult = true)]
42+
[TestCase("http://hello.com:8080/", UriKind.Absolute, ExpectedResult = true)]
43+
[TestCase("https://hello.com/path", UriKind.Absolute, ExpectedResult = true)]
44+
[TestCase("http://hello.com/path", UriKind.Absolute, ExpectedResult = true)]
45+
[TestCase("https://hello.com/path?query=param", UriKind.Absolute, ExpectedResult = true)]
46+
[TestCase("http://hello.com/path?query=param", UriKind.Absolute, ExpectedResult = true)]
47+
[TestCase("https://hello.com/path#fragment", UriKind.Absolute, ExpectedResult = true)]
48+
[TestCase("http://hello.com/path#fragment", UriKind.Absolute, ExpectedResult = true)]
49+
[TestCase("https://hello.com/path?query=param#fragment", UriKind.Absolute, ExpectedResult = true)]
50+
[TestCase("http://hello.com/path?query=param#fragment", UriKind.Absolute, ExpectedResult = true)]
51+
[TestCase("https://hello.com:8080/path?query=param#fragment", UriKind.Absolute, ExpectedResult = true)]
52+
[TestCase("http://hello.com:8080/path?query=param#fragment", UriKind.Absolute, ExpectedResult = true)]
53+
[TestCase("//hello.com:8080/path?query=param#fragment", UriKind.Absolute, ExpectedResult = true)]
54+
[TestCase("//hello.com:8080/path", UriKind.Absolute, ExpectedResult = true)]
55+
[TestCase("//hello.com:8080", UriKind.Absolute, ExpectedResult = true)]
56+
[TestCase("//hello.com", UriKind.Absolute, ExpectedResult = true)]
57+
[TestCase("/test/test.jpg", UriKind.Absolute, ExpectedResult = false)]
58+
[TestCase("/test", UriKind.Absolute, ExpectedResult = false)]
59+
[TestCase("test", UriKind.Absolute, ExpectedResult = false)]
60+
[TestCase("", UriKind.Absolute, ExpectedResult = false)]
61+
[TestCase(null, UriKind.Absolute, ExpectedResult = false)]
62+
[TestCase("this is not welformed", UriKind.Absolute, ExpectedResult = false)]
63+
[TestCase("ftp://hello.com/", UriKind.Relative, ExpectedResult = false)]
64+
[TestCase("file:///hello.com/", UriKind.Relative, ExpectedResult = false)]
65+
[TestCase("ws://hello.com/", UriKind.Relative, ExpectedResult = false)]
66+
[TestCase("wss://hello.com/", UriKind.Relative, ExpectedResult = false)]
67+
[TestCase("https://hello.com:8080/", UriKind.Relative, ExpectedResult = false)]
68+
[TestCase("http://hello.com:8080/", UriKind.Relative, ExpectedResult = false)]
69+
[TestCase("https://hello.com/path", UriKind.Relative, ExpectedResult = false)]
70+
[TestCase("http://hello.com/path", UriKind.Relative, ExpectedResult = false)]
71+
[TestCase("https://hello.com/path?query=param", UriKind.Relative, ExpectedResult = false)]
72+
[TestCase("http://hello.com/path?query=param", UriKind.Relative, ExpectedResult = false)]
73+
[TestCase("https://hello.com/path#fragment", UriKind.Relative, ExpectedResult = false)]
74+
[TestCase("http://hello.com/path#fragment", UriKind.Relative, ExpectedResult = false)]
75+
[TestCase("https://hello.com/path?query=param#fragment", UriKind.Relative, ExpectedResult = false)]
76+
[TestCase("http://hello.com/path?query=param#fragment", UriKind.Relative, ExpectedResult = false)]
77+
[TestCase("https://hello.com:8080/path?query=param#fragment", UriKind.Relative, ExpectedResult = false)]
78+
[TestCase("http://hello.com:8080/path?query=param#fragment", UriKind.Relative, ExpectedResult = false)]
79+
[TestCase("//hello.com:8080/path?query=param#fragment", UriKind.Relative, ExpectedResult = false)]
80+
[TestCase("//hello.com:8080/path", UriKind.Relative, ExpectedResult = false)]
81+
[TestCase("//hello.com:8080", UriKind.Relative, ExpectedResult = false)]
82+
[TestCase("//hello.com", UriKind.Relative, ExpectedResult = false)]
83+
[TestCase("/test/test.jpg", UriKind.Relative, ExpectedResult = true)]
84+
[TestCase("/test", UriKind.Relative, ExpectedResult = true)]
85+
[TestCase("test", UriKind.Relative, ExpectedResult = true)]
86+
[TestCase("", UriKind.Relative, ExpectedResult = false)]
87+
[TestCase(null, UriKind.Relative, ExpectedResult = false)]
88+
[TestCase("this is not welformed", UriKind.Relative, ExpectedResult = false)]
89+
[TestCase("ftp://hello.com/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
90+
[TestCase("file:///hello.com/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
91+
[TestCase("ws://hello.com/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
92+
[TestCase("wss://hello.com/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
93+
[TestCase("https://hello.com:8080/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
94+
[TestCase("http://hello.com:8080/", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
95+
[TestCase("https://hello.com/path", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
96+
[TestCase("http://hello.com/path", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
97+
[TestCase("https://hello.com/path?query=param", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
98+
[TestCase("http://hello.com/path?query=param", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
99+
[TestCase("https://hello.com/path#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
100+
[TestCase("http://hello.com/path#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
101+
[TestCase("https://hello.com/path?query=param#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
102+
[TestCase("http://hello.com/path?query=param#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
103+
[TestCase("https://hello.com:8080/path?query=param#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
104+
[TestCase("http://hello.com:8080/path?query=param#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
105+
[TestCase("//hello.com:8080/path?query=param#fragment", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
106+
[TestCase("//hello.com:8080/path", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
107+
[TestCase("//hello.com:8080", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
108+
[TestCase("//hello.com", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
109+
[TestCase("/test/test.jpg", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
110+
[TestCase("/test", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
111+
[TestCase("test", UriKind.RelativeOrAbsolute, ExpectedResult = true)]
112+
[TestCase("", UriKind.RelativeOrAbsolute, ExpectedResult = false)]
113+
[TestCase(null, UriKind.RelativeOrAbsolute, ExpectedResult = false)]
114+
[TestCase("this is not welformed", UriKind.RelativeOrAbsolute, ExpectedResult = false)]
115+
public bool IsWellFormedWebPath(string? webPath, UriKind uriKind) => WebPath.IsWellFormedWebPath(webPath, uriKind);
116+
34117
}

0 commit comments

Comments
 (0)