Skip to content
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

Update default BlazorWebView host address #24884

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Microsoft.Maui-dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SharedSource", "SharedSource", "{4F2926C8-43AB-4328-A735-D9EAD699F81D}"
ProjectSection(SolutionItems) = preProject
src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs = src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs
src\BlazorWebView\src\SharedSource\HostAddressHelper.cs = src\BlazorWebView\src\SharedSource\HostAddressHelper.cs
src\BlazorWebView\src\SharedSource\QueryStringHelper.cs = src\BlazorWebView\src\SharedSource\QueryStringHelper.cs
src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs = src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs
src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs = src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs
Expand Down
39 changes: 1 addition & 38 deletions src/BlazorWebView/src/Maui/BlazorWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// </summary>
public partial class BlazorWebView : View, IBlazorWebView
{
internal static string AppHostAddress { get; } = GetAppHostAddress();

private const string AppHostAddressAlways0000Switch = "BlazorWebView.AppHostAddressAlways0000";

private static bool IsAppHostAddressAlways0000Enabled =>
AppContext.TryGetSwitch(AppHostAddressAlways0000Switch, out var enabled) && enabled;

private static string GetAppHostAddress()
{
if (IsAppHostAddressAlways0000Enabled)
{
return "0.0.0.0";
}
else
{
#if IOS || MACCATALYST
// On iOS/MacCatalyst 18 and higher the 0.0.0.0 address does not work, so we use localhost instead.
// This preserves behavior on older versions of those systems, while defaulting to new behavior on
// the new system.

// Note that pre-release versions of iOS/MacCatalyst have the expected Major/Minor values,
// but the Build, MajorRevision, MinorRevision, and Revision values are all -1, so we need
// to pass in int.MinValue for those values.

if (System.OperatingSystem.IsIOSVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue) ||
System.OperatingSystem.IsMacCatalystVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue))
{
return "localhost";
}
else
{
return "0.0.0.0";
}
#else
return "0.0.0.0";
#endif
}
}
internal static string AppHostAddress { get; } = HostAddressHelper.GetAppHostAddress();

private readonly JSComponentConfigurationStore _jSComponents = new();

Expand Down
19 changes: 19 additions & 0 deletions src/BlazorWebView/src/SharedSource/HostAddressHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNetCore.Components.WebView;

internal static class HostAddressHelper
{
private const string AppHostAddressAlways0000Switch = "BlazorWebView.AppHostAddressAlways0000";

private static bool IsAppHostAddressAlways0000Enabled =>
AppContext.TryGetSwitch(AppHostAddressAlways0000Switch, out var enabled) && enabled;

public static string GetAppHostAddress()
=> IsAppHostAddressAlways0000Enabled
? "0.0.0.0"
: "0.0.0.1";
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ internal class WebView2WebViewManager : WebViewManager
// Using an IP address means that WebView2 doesn't wait for any DNS resolution,
// making it substantially faster. Note that this isn't real HTTP traffic, since
// we intercept all the requests within this origin.
internal static readonly string AppHostAddress = "0.0.0.0";
internal static readonly string AppHostAddress = HostAddressHelper.GetAppHostAddress();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a Mapper that users can override

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent here is not to allow complete customization of the address at this point, so we don't need it to be a mapper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it was just a way in the future I think we can allow users to unblock themselves. and using the "recommended" approach for extensibility, I think we do it on WebView on android from some chrome stuff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable, but given the stage in the release cycle, I think we're trying to make this fix as targeted as possible. By adding a new API now, we'd be kind of stuck allowing the configuration of the host address, which adds a new opportunity for misconfiguration. In the chance that this becomes a recurring issue (e.g., if browsers decided to ban 0.0.0.1 next), we could add the mapper at that point. But I'm open to having my mind changed.

@Eilon, do you have any thoughts about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah definitely a nice feature request. I thin it's already tracked by another issue.


/// <summary>
/// Gets the application's base URI. Defaults to <c>https://0.0.0.0/</c>
/// Gets the application's base URI. Defaults to <c>https://0.0.0.1/</c>.
/// </summary>
protected static readonly string AppOrigin = $"https://{AppHostAddress}/";

Expand Down
Loading