Skip to content

[PERFORMANCE][SECURITY] Refactored Regex to cached (static readonly, Compiled, source generated, with timeout for security) #26095

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

Draft
wants to merge 67 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
8a57c86
refactored Regex (Compiled, source generated)
moljac Nov 25, 2024
9f1c9b0
fix for GeneratedRegexAttribute
moljac Nov 25, 2024
be5185e
typo missing bracket fix
moljac Nov 25, 2024
865ad9e
attribute with constant string expression
moljac Nov 26, 2024
1fb87b4
refactoring fixed to use internal classes
moljac Nov 26, 2024
23e8aef
refactoring to fix access for internal classes
moljac Nov 27, 2024
4f7cc29
internal access modifier added
moljac Nov 27, 2024
a92a09b
typo fixed
moljac Nov 27, 2024
89d1a16
copy paste error fixed
moljac Nov 27, 2024
1907248
refactored Regex (Compiled, source generated)
moljac Nov 25, 2024
0c827c4
fix for GeneratedRegexAttribute
moljac Nov 25, 2024
dee5aa9
typo missing bracket fix
moljac Nov 25, 2024
73e85fe
attribute with constant string expression
moljac Nov 26, 2024
4b852b0
refactoring fixed to use internal classes
moljac Nov 26, 2024
fcf7434
refactoring to fix access for internal classes
moljac Nov 27, 2024
fb35d99
internal access modifier added
moljac Nov 27, 2024
9b2846c
typo fixed
moljac Nov 27, 2024
520c155
copy paste error fixed
moljac Nov 27, 2024
07401cb
propert/field name fix
moljac Dec 5, 2024
2b319fe
Merge branch 'dev/moljac/20241115-net9.0-performance-regex-source-gen…
moljac Dec 5, 2024
bf3078b
accessibility fix
moljac Dec 5, 2024
eda2294
additional refactoring
moljac Jan 14, 2025
8c71735
Update maui.aar
moljac Jan 14, 2025
91bd03e
namespace fix
moljac Jan 24, 2025
76f381c
System.Text.RegularExpressions nuget added
moljac Jan 24, 2025
c97cf4d
namespace fix
moljac Jan 24, 2025
62dc6b9
namespace fix
moljac Jan 24, 2025
a172ac6
net9.0
moljac Jan 24, 2025
abf1856
Merge branch 'main' into dev/moljac/20241115-net9.0-performance-regex…
moljac Jan 24, 2025
b93adac
Merge branch 'main' into dev/moljac/20241115-net9.0-performance-regex…
moljac Jan 28, 2025
9bb4821
refactored Regex (Compiled, source generated)
moljac Nov 25, 2024
7c1149e
fix for GeneratedRegexAttribute
moljac Nov 25, 2024
440bb8f
typo missing bracket fix
moljac Nov 25, 2024
b74b8a7
attribute with constant string expression
moljac Nov 26, 2024
301273b
refactoring fixed to use internal classes
moljac Nov 26, 2024
eee8532
refactoring to fix access for internal classes
moljac Nov 27, 2024
743afac
internal access modifier added
moljac Nov 27, 2024
7fce2e9
typo fixed
moljac Nov 27, 2024
f24f8a7
copy paste error fixed
moljac Nov 27, 2024
7f7a676
propert/field name fix
moljac Dec 5, 2024
84812c1
refactored Regex (Compiled, source generated)
moljac Nov 25, 2024
652d6e2
typo missing bracket fix
moljac Nov 25, 2024
eec8426
attribute with constant string expression
moljac Nov 26, 2024
a0c0426
refactoring fixed to use internal classes
moljac Nov 26, 2024
eebbd13
refactoring to fix access for internal classes
moljac Nov 27, 2024
fda25ea
internal access modifier added
moljac Nov 27, 2024
3d0b682
typo fixed
moljac Nov 27, 2024
0a9bd3d
copy paste error fixed
moljac Nov 27, 2024
9551fa6
accessibility fix
moljac Dec 5, 2024
02992f1
additional refactoring
moljac Jan 14, 2025
182280d
Update maui.aar
moljac Jan 14, 2025
a1668d8
namespace fix
moljac Jan 24, 2025
19ac541
System.Text.RegularExpressions nuget added
moljac Jan 24, 2025
39b5d83
namespace fix
moljac Jan 24, 2025
b119127
namespace fix
moljac Jan 24, 2025
7bf09c5
net9.0
moljac Jan 24, 2025
4acd975
fix for GeneratedRegex
moljac Feb 21, 2025
6656095
collection expression init
moljac Feb 21, 2025
0973ca1
RegEX refactoring
moljac Feb 21, 2025
de94099
fix for GeneratedRegex
moljac Feb 21, 2025
4fcfee5
fix for GeneratedRegex
moljac Feb 21, 2025
4d7c8e8
fix for GeneratedRegex
moljac Feb 21, 2025
cfa8843
type fix
moljac Feb 21, 2025
6f897c5
merge typo fix
moljac Feb 21, 2025
4208b06
Merge branch 'dev/moljac/20241115-net9.0-performance-regex-source-gen…
moljac Mar 8, 2025
075705e
merge conflict fix
moljac Mar 13, 2025
dc6d986
merge conflict fix
moljac Mar 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

namespace Microsoft.AspNetCore.Components.WebView
{
internal static class StaticContentHotReloadManager
internal static partial class StaticContentHotReloadManager
{
private delegate void ContentUpdatedHandler(string assemblyName, string relativePath);

private readonly static Regex ContentUrlRegex = new Regex("^_content/(?<AssemblyName>[^/]+)/(?<RelativePath>.*)");
private readonly static Regex ContentUrlRegex = RegexHelper.ContentUrlRegex;
private static event ContentUpdatedHandler? OnContentUpdated;

// If the current platform can't tell us the application entry assembly name, we can use a placeholder name
Expand Down Expand Up @@ -105,7 +105,7 @@ private static (string AssemblyName, string RelativePath) GetAssemblyNameAndRela
// we can use the existing IJSRuntime. In turn we can get an instance of this
// that's always attached to the currently-loaded page (if it's a Blazor page)
// by injecting this headless root component.
private sealed class StaticContentChangeNotifier : IComponent, IDisposable
private sealed partial class StaticContentChangeNotifier : IComponent, IDisposable
{
private ILogger _logger = default!;

Expand Down Expand Up @@ -154,6 +154,26 @@ private async Task NotifyContentUpdatedAsync(string assemblyName, string relativ

public Task SetParametersAsync(ParameterView parameters)
=> Task.CompletedTask;

}

internal static partial class RegexHelper
{
static readonly ReadOnlySpan<char> pattern = "^_content/(?<AssemblyName>[^/]+)/(?<RelativePath>.*)";

#if NET7_0_OR_GREATER
[GeneratedRegex (pattern, RegexOptions.None, matchTimeoutMilliseconds: 1000)]
static partial Regex ContentUrlRegex
{
get;
}
#else
static readonly Regex ContentUrlRegex =
new (
pattern,
RegexOptions.Compiled,
TimeSpan.FromMilliseconds(1000) // against malicious input
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.Maui.Devices;

namespace Microsoft.Maui.Controls.ControlGallery.GalleryPages
{
public static class GalleryBuilder
{
public static Button NavButton(string galleryName, Func<Page> gallery, INavigation nav)
{
var automationId = RegexHelper.AutomationIdRegex.Replace(galleryName, string.Empty);
var button = new Button { Text = $"{galleryName}", AutomationId = automationId, FontSize = 10, HeightRequest = DeviceInfo.Platform == DevicePlatform.Android ? 40 : 30 };
button.Clicked += (sender, args) => { nav.PushAsync(gallery()); };
return button;
}
}

internal static partial class RegexHelper
{
static readonly string AutomationIdRegexPattern = " |\\(|\\)";
#if NET7_0_OR_GREATER
[GeneratedRegex (AutomationIdRegexPattern, RegexOptions.None, matchTimeoutMilliseconds: 1000)]
internal static partial Regex AutomationIdRegex
{
get;
}
#else
internal static readonly Regex AutomationIdRegex =
new (
AutomationIdRegexPattern,
RegexOptions.Compiled,
TimeSpan.FromMilliseconds(1000) // against malicious input
);
#endif
}
}
Loading
Loading