From 126ebcc183af2cc9f9427a0c7c37b1bc64b70d8e Mon Sep 17 00:00:00 2001 From: ladeak Date: Mon, 21 Jul 2025 14:05:00 +0200 Subject: [PATCH 1/2] Refactor IsLocalhost to static method Changed `IsLocalhost` from an instance method to a static method. Updated the parameter to use `host` directly instead of `parsedAddress.Host`. Modified the check for the `.localhost` TLD to ensure the host length is greater than 10, optimizing the condition to avoid unnecessary checks for shorter hostnames. Fixes #61155 --- src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs b/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs index 3acfeb5ebef9..f0fbbd5ac644 100644 --- a/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs +++ b/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs @@ -142,18 +142,17 @@ internal static ListenOptions ParseAddress(string address, out bool https) return options; - bool IsLocalhost(string host, out string? prefix) + static bool IsLocalhost(string host, out string? prefix) { // Check for "localhost" - if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(host, "localhost", StringComparison.OrdinalIgnoreCase)) { prefix = null; return true; } // Check for use of .localhost TLD (Top Level Domain) - if (host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase) - && !string.Equals(parsedAddress.Host, ".localhost", StringComparison.OrdinalIgnoreCase)) + if (host.Length > 10 && host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase)) { // Take all but the .localhost TLD as the prefix prefix = host[..^10]; // 10 is the length of ".localhost" From b21abedec143416e52d1b9305465da024c0b4a80 Mon Sep 17 00:00:00 2001 From: ladeak Date: Mon, 21 Jul 2025 17:31:52 +0200 Subject: [PATCH 2/2] Review comment --- src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs b/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs index f0fbbd5ac644..f892c9796c15 100644 --- a/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs +++ b/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs @@ -152,10 +152,11 @@ static bool IsLocalhost(string host, out string? prefix) } // Check for use of .localhost TLD (Top Level Domain) - if (host.Length > 10 && host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase)) + const string localhostTld = ".localhost"; + if (host.Length > localhostTld.Length && host.EndsWith(localhostTld, StringComparison.OrdinalIgnoreCase)) { // Take all but the .localhost TLD as the prefix - prefix = host[..^10]; // 10 is the length of ".localhost" + prefix = host[..^localhostTld.Length]; return true; }