-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[BlazorWebView] Fixed Allow force reload for URLs with dots in query parameters #29560
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
Open
tw4
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
tw4:fix-25689
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
| ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BlazorWebView", "BlazorWebView", "{A703DA7E-5A1F-AF9B-CE7F-00B135C1C4F7}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{2DA3F4D8-35E4-39B8-EB7E-CFF59A88A25F}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiBlazorWebView.UnitTests", "src\BlazorWebView\tests\MauiBlazorWebView.UnitTests\MauiBlazorWebView.UnitTests.csproj", "{00E708AA-402D-4BB5-AD88-348274F8E1DF}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|Any CPU = Release|Any CPU | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|x64.Build.0 = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Debug|x86.Build.0 = Debug|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|x64.ActiveCfg = Release|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|x64.Build.0 = Release|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|x86.ActiveCfg = Release|Any CPU | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF}.Release|x86.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{A703DA7E-5A1F-AF9B-CE7F-00B135C1C4F7} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} | ||
{2DA3F4D8-35E4-39B8-EB7E-CFF59A88A25F} = {A703DA7E-5A1F-AF9B-CE7F-00B135C1C4F7} | ||
{00E708AA-402D-4BB5-AD88-348274F8E1DF} = {2DA3F4D8-35E4-39B8-EB7E-CFF59A88A25F} | ||
EndGlobalSection | ||
EndGlobal |
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 |
---|---|---|
@@ -1,20 +1,39 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Microsoft.AspNetCore.Components.WebView.Maui | ||
namespace Microsoft.AspNetCore.Components.WebView.Maui; | ||
|
||
internal static class UriExtensions | ||
{ | ||
internal static class UriExtensions | ||
internal static bool IsBaseOfPage(this Uri baseUri, string? uriString) | ||
{ | ||
internal static bool IsBaseOfPage(this Uri baseUri, string? uriString) | ||
if (string.IsNullOrWhiteSpace(uriString)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!Uri.TryCreate(uriString, UriKind.Absolute, out var uri)) | ||
{ | ||
if (Path.HasExtension(uriString)) | ||
{ | ||
// If the path ends in a file extension, it's not referring to a page. | ||
return false; | ||
} | ||
|
||
var uri = new Uri(uriString!); | ||
return baseUri.IsBaseOf(uri); | ||
return false; | ||
} | ||
|
||
if (uri.Scheme is not ("http" or "https")) | ||
{ | ||
|
||
return false; | ||
} | ||
|
||
if (Path.HasExtension(uri.AbsolutePath)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!string.Equals(baseUri.Host, uri.Host, StringComparison.OrdinalIgnoreCase) && | ||
!uri.Host.EndsWith("." + baseUri.Host, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return false; | ||
} | ||
|
||
return uri.AbsolutePath.StartsWith(baseUri.AbsolutePath, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.Controls.Internals; | ||
using System.Runtime.CompilerServices; | ||
|
||
|
||
[assembly: Preserve] | ||
|
||
[assembly: XmlnsDefinition("http://schemas.microsoft.com/dotnet/2021/maui", "Microsoft.AspNetCore.Components.WebView.Maui")] | ||
[assembly: InternalsVisibleTo("Microsoft.Maui.MauiBlazorWebView.UnitTests")] |
105 changes: 105 additions & 0 deletions
105
src/BlazorWebView/tests/MauiBlazorWebView.UnitTests/Extensions_Tests.cs
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,105 @@ | ||
using Microsoft.AspNetCore.Components.WebView.Maui; | ||
namespace MauiBlazorWebView.UnitTests; | ||
|
||
public class UriExtensions_Tests | ||
{ | ||
private readonly Uri _baseUri = new("https://example.com/"); | ||
|
||
[Theory] | ||
[InlineData("https://example.com/page", true)] | ||
[InlineData("page/subpage", false)] | ||
[InlineData("this is not a uri!", false)] | ||
[InlineData("https://example.com/", true)] | ||
[InlineData("https://example.com/page/", true)] | ||
[InlineData("https://example.com/page?weight=62.5", true)] | ||
[InlineData("https://example.com/page#section.1", true)] | ||
[InlineData("https://example.com/file.txt", false)] | ||
[InlineData("https://example.com/page.json?foo=bar", false)] | ||
[InlineData("ftp://example.com/page", false)] | ||
[InlineData("/relative/path", false)] | ||
[InlineData("", false)] | ||
[InlineData(null, false)] | ||
[InlineData("https://example.com/test", true)] | ||
[InlineData("https://example.com/folder/subfolder/", true)] | ||
[InlineData("https://example.com/folder/file.exe", false)] | ||
[InlineData("https://subdomain.example.com/page", true)] | ||
[InlineData("https://example.com/path/with/.dot/segment", true)] | ||
[InlineData("https://example.com/path/with space", true)] | ||
[InlineData("https://example.com/path/with%20encoded%20space", true)] | ||
[InlineData("https://example.com/page?param=value¶m2=value2", true)] | ||
[InlineData("https://example.com/page.html", false)] | ||
[InlineData("HTTPS://EXAMPLE.COM/PAGE", true)] | ||
[InlineData("https://subdomain.example.com/", true)] | ||
[InlineData("https://subdomain.example.com/page/", true)] | ||
[InlineData("https://subdomain.example.com/page?weight=62.5", true)] | ||
[InlineData("https://subdomain.example.com/page#section.1", true)] | ||
[InlineData("https://subdomain.example.com/file.txt", false)] | ||
[InlineData("https://subdomain.example.com/page.json?foo=bar", false)] | ||
[InlineData("ftp://subdomain.example.com/page", false)] | ||
[InlineData("https://subdomain.example.com/test", true)] | ||
[InlineData("https://subdomain.example.com/folder/subfolder/", true)] | ||
[InlineData("https://subdomain.example.com/folder/file.exe", false)] | ||
[InlineData("https://subdomain.example.com/path/with/.dot/segment", true)] | ||
[InlineData("https://subdomain.example.com/path/with space", true)] | ||
[InlineData("https://subdomain.example.com/path/with%20encoded%20space", true)] | ||
[InlineData("https://subdomain.example.com/page?param=value¶m2=value2", true)] | ||
[InlineData("https://subdomain.example.com/page.html", false)] | ||
[InlineData("HTTPS://SUBDOMAIN.EXAMPLE.COM/PAGE", true)] | ||
public void IsBaseOfPage_HandlesVariousUris(string? uriString, bool expected) | ||
{ | ||
var result = _baseUri.IsBaseOfPage(uriString); | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_ReturnsFalse_WhenPathHasFileExtension() | ||
{ | ||
var uriWithExtension = "https://example.com/assets/image.png"; | ||
Assert.False(_baseUri.IsBaseOfPage(uriWithExtension)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_ReturnsTrue_WhenUriIsBaseItself() | ||
{ | ||
var uri = "https://example.com/"; | ||
Assert.True(_baseUri.IsBaseOfPage(uri)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_IgnoresDotInQuery_AndReturnsFalse_WhenNotBase() | ||
{ | ||
var uri = "https://example.com/page?foo=1.2.3"; | ||
Assert.True(_baseUri.IsBaseOfPage(uri)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_ReturnsFalse_WhenSchemeIsNotHttpOrHttps() | ||
{ | ||
var uri = "ftp://example.com/page"; | ||
Assert.False(_baseUri.IsBaseOfPage(uri)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_DoesNotTreatDotInQueryAsExtension() | ||
{ | ||
var baseUri = new Uri("https://example.com"); | ||
var urlWithDotInQuery = "https://example.com/customer?weight=62.5"; | ||
Assert.True(baseUri.IsBaseOfPage(urlWithDotInQuery)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_ReturnsFalse_WhenPathHasDotExtension() | ||
{ | ||
var baseUri = new Uri("https://example.com"); | ||
var urlWithExtension = "https://example.com/customer.json"; | ||
Assert.False(baseUri.IsBaseOfPage(urlWithExtension)); | ||
} | ||
|
||
[Fact] | ||
public void IsBaseOfPage_TreatsFragmentWithDotAsNoExtension() | ||
{ | ||
var baseUri = new Uri("https://example.com"); | ||
var urlWithDotInFragment = "https://example.com/customer#section.1"; | ||
Assert.True(baseUri.IsBaseOfPage(urlWithDotInFragment)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/BlazorWebView/tests/MauiBlazorWebView.UnitTests/MauiBlazorWebView.UnitTests.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>Microsoft.Maui.MauiBlazorWebView.UnitTests</RootNamespace> | ||
<AssemblyName>Microsoft.Maui.MauiBlazorWebView.UnitTests</AssemblyName> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="xunit" Version="2.9.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Maui\Microsoft.AspNetCore.Components.WebView.Maui.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can include some additional cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestions! I have added the test cases you mentioned, and I noticed that my code was not handling subdomains correctly. After reviewing the code and extending the tests to include more subdomain scenarios, the implementation now also passes all subdomain-related test cases.