Skip to content

Commit 5f24de3

Browse files
authored
Merge pull request from GHSA-j74q-mv2c-rxmp
1 parent 94cef50 commit 5f24de3

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
@@ -8,6 +8,7 @@
88
using Umbraco.Cms.Core.IO;
99
using Umbraco.Cms.Core.Media;
1010
using Umbraco.Cms.Core.Models;
11+
using Umbraco.Cms.Core.Routing;
1112
using Umbraco.Cms.Web.Common.Attributes;
1213
using Umbraco.Cms.Web.Common.DependencyInjection;
1314
using Umbraco.Extensions;
@@ -122,7 +123,7 @@ public IActionResult GetResized(string imagePath, int width)
122123

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

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
@@ -30,4 +30,87 @@ public void Combine_must_handle_empty_array() =>
3030

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

0 commit comments

Comments
 (0)