Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
189fd32
Initial plan
Copilot Jul 11, 2025
df9960b
Implement uninstrumented peer visualization for parameters, connectio…
Copilot Jul 11, 2025
d1fb28f
Support direct URL connection strings in peer resolution
Copilot Jul 11, 2025
138b2a7
Initial implementation of comprehensive connection string parser
Copilot Jul 11, 2025
5f58e3f
Add comprehensive connection string parser with extensive test coverage
Copilot Jul 11, 2025
40c8305
Update src/Aspire.Dashboard/Model/ConnectionStringParser.cs
davidfowl Jul 12, 2025
748f053
Fix failing ConnectionStringParser tests for comprehensive connection…
Copilot Jul 12, 2025
7d232fc
Refactor ConnectionStringParser with source-generated regexes and imp…
Copilot Jul 12, 2025
2c7129f
Use ConnectionStringParser for Parameter resources and remove TryPars…
Copilot Jul 12, 2025
01ae0cb
Implement robust hostname validation using RFC-compliant logic
Copilot Jul 12, 2025
40f8cb8
Simplify hostname validation using URI parsing as suggested
Copilot Jul 12, 2025
fe4c50f
Optimize ConnectionStringParser by using static readonly arrays and s…
Copilot Jul 12, 2025
7334208
Enhance GitHubModel resource initialization with connection string re…
davidfowl Jul 12, 2025
0792ea7
Change ConnectionStringParser class from public to internal
Copilot Jul 12, 2025
67540a7
Refactor to eliminate nested transformer loops and extend change dete…
Copilot Jul 14, 2025
d0c1afb
Cache resource addresses on ResourceOutgoingPeerResolver to avoid rec…
Copilot Jul 14, 2025
b7c2862
Move cache from ResourceOutgoingPeerResolver to ResourceViewModel
Copilot Jul 14, 2025
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
Prev Previous commit
Next Next commit
Simplify hostname validation using URI parsing as suggested
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
  • Loading branch information
Copilot and davidfowl committed Jul 12, 2025
commit 40f8cb8057f4e2315e070501e22fd06da14c703e
88 changes: 4 additions & 84 deletions src/Aspire.Dashboard/Model/ConnectionStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private static (string host, string port) SplitOnLast(string token)

/// <summary>
/// Determines if a string looks like a hostname rather than a file path or other non-hostname string.
/// Uses RFC-compliant validation with conservative heuristics to avoid false positives.
/// Uses URI validation with conservative heuristics to avoid false positives.
/// </summary>
/// <param name="connectionString">The string to evaluate. Examples: "localhost" (valid), "/path/to/file.db" (invalid), "api.example.com" (valid)</param>
/// <returns>True if the string appears to be a hostname; otherwise false.</returns>
Expand Down Expand Up @@ -497,88 +497,8 @@ private static bool LooksLikeHost(string connectionString)
return false;
}

// Try to validate as hostname using more robust logic
return IsValidHostname(trimmed) || IsValidIPAddress(trimmed);
}

/// <summary>
/// Validates if a string conforms to hostname rules based on RFC 1123.
/// </summary>
/// <param name="hostname">The hostname to validate</param>
/// <returns>True if the hostname is valid; otherwise false.</returns>
private static bool IsValidHostname(string hostname)
{
// RFC 1123 hostname rules:
// - Can contain letters, digits, dots, and hyphens
// - Cannot start or end with hyphen
// - Each label (between dots) must be 1-63 characters
// - Total length must be ≤ 253 characters

if (hostname.Length == 0 || hostname.Length > 253)
{
return false;
}

// Cannot start or end with dot or hyphen
if (hostname.StartsWith('.') || hostname.EndsWith('.') ||
hostname.StartsWith('-') || hostname.EndsWith('-'))
{
return false;
}

// Split into labels and validate each
var labels = hostname.Split('.');

foreach (var label in labels)
{
if (!IsValidHostnameLabel(label))
{
return false;
}
}

return true;
}

/// <summary>
/// Validates if a hostname label (part between dots) is valid.
/// </summary>
/// <param name="label">The label to validate</param>
/// <returns>True if the label is valid; otherwise false.</returns>
private static bool IsValidHostnameLabel(string label)
{
// Label must be 1-63 characters
if (label.Length == 0 || label.Length > 63)
{
return false;
}

// Cannot start or end with hyphen
if (label.StartsWith('-') || label.EndsWith('-'))
{
return false;
}

// Can only contain letters, digits, and hyphens
foreach (var c in label)
{
if (!char.IsLetterOrDigit(c) && c != '-')
{
return false;
}
}

return true;
}

/// <summary>
/// Checks if a string is a valid IP address (IPv4 or IPv6).
/// </summary>
/// <param name="address">The address to validate</param>
/// <returns>True if the address is a valid IP; otherwise false.</returns>
private static bool IsValidIPAddress(string address)
{
// Use .NET's IPAddress parsing for robust IP validation
return System.Net.IPAddress.TryParse(address, out _);
// Use Uri parsing to validate hostname - create a fake URI and see if it parses
var fakeUri = $"scheme://{trimmed}";
return Uri.TryCreate(fakeUri, UriKind.Absolute, out var uri) && !string.IsNullOrEmpty(uri.Host);
}
}
Loading