Skip to content
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
5 changes: 5 additions & 0 deletions playground/Stress/Stress.ApiService/ConsoleStresser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public static void Stress()
Console.WriteLine("https://www.example.com/path/with/percent%25encoded");
Console.WriteLine("https://www.example.com/path/with/dollar$sign");
Console.WriteLine("https://www.example.com/path/with/exclamation!mark");
Console.WriteLine("https://www.example.com/;path/");
Console.WriteLine("https://www.example.com/path/?query;string");
Console.WriteLine("https://;www.example.com/");
Console.WriteLine("https://www;.example.com/");
Console.WriteLine("https://www.exa;mple.com/");

Console.Write("\x1b[0m"); // reset color

Expand Down
17 changes: 5 additions & 12 deletions src/Aspire.Dashboard/ConsoleLogs/UrlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -34,7 +35,7 @@ public static bool TryParse(string? text, Func<string, string>? nonMatchFragment
nextCharIndex = urlMatch.Index + urlMatch.Length;
var url = text[urlStart..nextCharIndex];

builder.Append(CultureInfo.InvariantCulture, $"<a target=\"_blank\" href=\"{url}\">{url}</a>");
builder.Append(CultureInfo.InvariantCulture, $"<a target=\"_blank\" href=\"{url}\">{WebUtility.HtmlEncode(url)}</a>");
urlMatch = urlMatch.NextMatch();
}

Expand Down Expand Up @@ -65,17 +66,9 @@ static void AppendNonMatchFragment(StringBuilder stringBuilder, Func<string, str
}

// Regular expression that detects http/https URLs in a log entry
// Based on the RegEx used in Windows Terminal for the same purpose. Some modifications:
// - Can start at a non word boundary. This behavior is similar to how GitHub matches URLs in pretty printed code.
// - Limited to only http/https URLs.
// - Ignore case. That means it matches URLs starting with http and HTTP.
//
// Explanation:
// https?:// - http:// or https://
// [-A-Za-z0-9+&@#/%?=~_|$!:,.;]* - Any character in the list, matched zero or more times.
// [A-Za-z0-9+&@#/%=~_|$] - Any character in the list, matched exactly once
// Based on the RegEx used by GitHub to detect links in content.
[GeneratedRegex(
"https?://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$]",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
@"((?<!\+)https?:\/\/(?:www\.)?(?:[-\p{L}.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w\p{L}.:%+~#*$!?&/=@]*(?:,(?!\s))*?)*)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture)]
public static partial Regex GenerateUrlRegEx();
}
24 changes: 23 additions & 1 deletion tests/Aspire.Dashboard.Tests/ConsoleLogsTests/UrlParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void TryParse_ReturnsCorrectResult(string input, bool expectedResult, str
[InlineData("http://bing.com/", "<a target=\"_blank\" href=\"http://bing.com/\">http://bing.com/</a>")]
[InlineData("http://bing.com/dir", "<a target=\"_blank\" href=\"http://bing.com/dir\">http://bing.com/dir</a>")]
[InlineData("http://bing.com/index.aspx", "<a target=\"_blank\" href=\"http://bing.com/index.aspx\">http://bing.com/index.aspx</a>")]
[InlineData("http://bing", "<a target=\"_blank\" href=\"http://bing\">http://bing</a>")]
[InlineData("http://localhost", "<a target=\"_blank\" href=\"http://localhost\">http://localhost</a>")]
public void TryParse_SupportedUrlFormats(string input, string? expectedOutput)
{
var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var modifiedText);
Expand Down Expand Up @@ -71,6 +71,15 @@ public void TryParse_ExcludeInvalidTrailingChars(string input, string? expectedO
Assert.Equal(expectedOutput, modifiedText);
}

[Fact]
public void TryParse_QueryString()
{
var result = UrlParser.TryParse("https://www.example.com?query=string&param=value", WebUtility.HtmlEncode, out var modifiedText);
Assert.True(result);

Assert.Equal("<a target=\"_blank\" href=\"https://www.example.com?query=string&param=value\">https://www.example.com?query=string&amp;param=value</a>", modifiedText);
}

[Theory]
[InlineData("http://www.localhost:8080")]
[InlineData("HTTP://WWW.LOCALHOST:8080")]
Expand All @@ -83,4 +92,17 @@ public void GenerateUrlRegEx_MatchUrlAfterContent(string content)
var match = regex.Match(content);
Assert.Equal("http://www.localhost:8080", match.Value.ToLowerInvariant());
}

[Theory]
[InlineData("http://www.localhost:8080!", "http://www.localhost:8080!")]
[InlineData("http://www.localhost:8080/path!", "http://www.localhost:8080/path!")]
[InlineData("http://www.localhost:8080/path;", "http://www.localhost:8080/path")]
[InlineData("http://www.localhost:8080;", "http://www.localhost:8080")]
[InlineData("http://www.local;host:8080;", "http://www.local")]
public void GenerateUrlRegEx_MatchUrlBeforeContent(string content, string expected)
{
var regex = UrlParser.GenerateUrlRegEx();
var match = regex.Match(content);
Assert.Equal(expected, match.Value.ToLowerInvariant());
}
}