diff --git a/src/Essentials/src/Types/Shared/WebUtils.shared.cs b/src/Essentials/src/Types/Shared/WebUtils.shared.cs index 7638d0379a3a..5fa28ece6daf 100644 --- a/src/Essentials/src/Types/Shared/WebUtils.shared.cs +++ b/src/Essentials/src/Types/Shared/WebUtils.shared.cs @@ -8,6 +8,8 @@ namespace Microsoft.Maui static class WebUtils { #if !NETSTANDARD + static readonly char[] s_queryOrFragmentDelimiters = new[] { '?', '#' }; + internal static string RemovePossibleQueryString(string? url) { if (string.IsNullOrEmpty(url)) @@ -15,7 +17,7 @@ internal static string RemovePossibleQueryString(string? url) return string.Empty; } - var indexOfQueryString = url.IndexOf('?', StringComparison.Ordinal); + var indexOfQueryString = url.IndexOfAny(s_queryOrFragmentDelimiters); return (indexOfQueryString == -1) ? url : url.Substring(0, indexOfQueryString); diff --git a/src/Essentials/test/UnitTests/WebUtils_Tests.cs b/src/Essentials/test/UnitTests/WebUtils_Tests.cs index 6a4ef2653ce9..c6241387ca13 100644 --- a/src/Essentials/test/UnitTests/WebUtils_Tests.cs +++ b/src/Essentials/test/UnitTests/WebUtils_Tests.cs @@ -110,6 +110,10 @@ public void ResolveRelativePath_EncodedDotDot_HandledCorrectly() [InlineData("https://example.com", "https://example.com")] [InlineData("https://example.com?foo=bar", "https://example.com")] [InlineData("https://example.com/path?query=1&other=2", "https://example.com/path")] + [InlineData("https://example.com/index.html#code=abc", "https://example.com/index.html")] + [InlineData("https://example.com/path?query=1#frag", "https://example.com/path")] + [InlineData("https://example.com/path#frag?notquery", "https://example.com/path")] + [InlineData("https://0.0.0.1/index.html#code=1234asdf", "https://0.0.0.1/index.html")] public void RemovePossibleQueryString_ReturnsExpected(string? input, string expected) { var result = Microsoft.Maui.WebUtils.RemovePossibleQueryString(input);