Skip to content

use GeneratedRegex property #58710

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

Merged
merged 2 commits into from
Jan 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public void EmbeddingServerAppInsideIframe_WithCompressionEnabled_Fails()

Assert.True(logs.Count > 0);

Assert.Matches(ParseErrorMessage(), logs[0].Message);
Assert.Matches(ParseErrorMessageRegex, logs[0].Message);
}

[GeneratedRegex(@"security - Refused to frame 'http://\d+\.\d+\.\d+\.\d+:\d+/' because an ancestor violates the following Content Security Policy directive: ""frame-ancestors 'none'"".")]
private static partial Regex ParseErrorMessage();
private static partial Regex ParseErrorMessageRegex { get; }
}

public partial class DefaultConfigurationWebSocketCompressionTests : AllowedWebSocketCompressionTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void StaticRendering_CanUseFingerprintedResources()
var url = $"{ServerPathBase}/resource-collection";
Navigate(url);

Browser.True(() => AppStylesRegex().IsMatch(Browser.Exists(By.Id("basic-app-styles")).Text));
Browser.True(() => AppStylesRegex.IsMatch(Browser.Exists(By.Id("basic-app-styles")).Text));

Browser.Exists(By.Id("import-module")).Click();

Browser.True(() => JsModuleRegex().IsMatch(Browser.Exists(By.Id("js-module")).Text));
Browser.True(() => JsModuleRegex.IsMatch(Browser.Exists(By.Id("js-module")).Text));
}

[Theory]
Expand All @@ -49,15 +49,15 @@ public void StaticRendering_CanUseFingerprintedResources_InteractiveModes(string

Browser.Equal(renderMode, () => Browser.Exists(By.Id("platform-name")).Text);

Browser.True(() => AppStylesRegex().IsMatch(Browser.Exists(By.Id("basic-app-styles")).Text));
Browser.True(() => AppStylesRegex.IsMatch(Browser.Exists(By.Id("basic-app-styles")).Text));

Browser.Exists(By.Id("import-module")).Click();

Browser.True(() => JsModuleRegex().IsMatch(Browser.Exists(By.Id("js-module")).Text));
Browser.True(() => JsModuleRegex.IsMatch(Browser.Exists(By.Id("js-module")).Text));
}

[GeneratedRegex("""BasicTestApp\.[a-zA-Z0-9]{10}\.styles\.css""")]
private static partial Regex AppStylesRegex();
private static partial Regex AppStylesRegex { get; }
[GeneratedRegex(""".*Index\.[a-zA-Z0-9]{10}\.mjs""")]
private static partial Regex JsModuleRegex();
private static partial Regex JsModuleRegex { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ public async Task IncrementallyUpdatesCache()
}

[GeneratedRegex("/Microsoft\\.AspNetCore\\.Components\\.\\w*\\.wasm")]
private static partial Regex GetFingerprintedComponentsEntryRegex();
private static partial Regex GetFingerprintedComponentsEntryRegex { get; }

[GeneratedRegex("/dotnet\\.native\\.\\w*\\.wasm")]
private static partial Regex GetFingerprintedDotNetWasmEntryRegex();
private static partial Regex GetFingerprintedDotNetWasmEntryRegex { get; }

private static bool IsFingerprintedComponentsEntry(string url)
=> GetFingerprintedComponentsEntryRegex().IsMatch(url);
=> GetFingerprintedComponentsEntryRegex.IsMatch(url);

private static bool IsFingerprintedDotNetWasmEntry(string url)
=> GetFingerprintedDotNetWasmEntryRegex().IsMatch(url);
=> GetFingerprintedDotNetWasmEntryRegex.IsMatch(url);

private IReadOnlyCollection<string> GetCacheEntryUrls()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ internal partial class AlphaRouteConstraint : RegexRouteConstraint
/// <summary>
/// Initializes a new instance of the <see cref="AlphaRouteConstraint" /> class.
/// </summary>
public AlphaRouteConstraint() : base(GetAlphaRouteRegex())
public AlphaRouteConstraint() : base(AlphaRouteRegex)
{
}

[GeneratedRegex(@"^[A-Za-z]*$")]
private static partial Regex GetAlphaRouteRegex();
private static partial Regex AlphaRouteRegex { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Microsoft.AspNetCore.Authentication.Negotiate;
internal static partial class LdapAdapter
{
[GeneratedRegex(@"(?<![^\\]\\),")]
internal static partial Regex DistinguishedNameSeparator();
internal static partial Regex DistinguishedNameSeparatorRegex { get; }

public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdentity identity, ILogger logger)
{
var user = identity.Name!;
Expand Down Expand Up @@ -66,7 +66,7 @@ public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdenti
{
// Example distinguished name: CN=TestGroup,DC=KERB,DC=local
var groupDN = $"{Encoding.UTF8.GetString((byte[])group)}";
var groupCN = DistinguishedNameSeparator().Split(groupDN)[0].Substring("CN=".Length);
var groupCN = DistinguishedNameSeparatorRegex.Split(groupDN)[0].Substring("CN=".Length);

if (!settings.IgnoreNestedGroups)
{
Expand Down Expand Up @@ -124,7 +124,7 @@ private static void GetNestedGroups(LdapConnection connection, ClaimsIdentity pr
foreach (var member in memberof)
{
var nestedGroupDN = $"{Encoding.UTF8.GetString((byte[])member)}";
var nestedGroupCN = DistinguishedNameSeparator().Split(nestedGroupDN)[0].Substring("CN=".Length);
var nestedGroupCN = DistinguishedNameSeparatorRegex.Split(nestedGroupDN)[0].Substring("CN=".Length);

if (processedGroups.Contains(nestedGroupDN))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ public class LdapAdapterTests
[Fact]
public void DistinguishedNameWithoutCommasSuccess()
{
var parts = LdapAdapter.DistinguishedNameSeparator().Split("Testing group - City");
var parts = LdapAdapter.DistinguishedNameSeparatorRegex.Split("Testing group - City");

Assert.Equal(new[] { "Testing group - City" }, parts);
}

[Fact]
public void DistinguishedNameWithEscapedCommaSuccess()
{
var parts = LdapAdapter.DistinguishedNameSeparator().Split(@"Testing group\,City");
var parts = LdapAdapter.DistinguishedNameSeparatorRegex.Split(@"Testing group\,City");

Assert.Equal(new[] { @"Testing group\,City" }, parts);
}

[Fact]
public void DistinguishedNameWithNotEscapedCommaSuccess()
{
var parts = LdapAdapter.DistinguishedNameSeparator().Split("Testing group,City");
var parts = LdapAdapter.DistinguishedNameSeparatorRegex.Split("Testing group,City");

Assert.Equal(new[] { "Testing group", "City" }, parts);
}

[Fact]
public void DistinguishedNameWithEscapedBackslashAndNotEscapedCommaSuccess()
{
var parts = LdapAdapter.DistinguishedNameSeparator().Split(@"Testing group\\,City");
var parts = LdapAdapter.DistinguishedNameSeparatorRegex.Split(@"Testing group\\,City");

Assert.Equal(new[] { @"Testing group\\", "City" }, parts);
}
Expand Down
16 changes: 6 additions & 10 deletions src/Shared/CertificateGeneration/UnixCertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ private static List<NssDb> GetNssDbs(string homeDirectory)
}

[GeneratedRegex("OPENSSLDIR:\\s*\"([^\"]+)\"")]
private static partial Regex OpenSslVersionRegex();
private static partial Regex OpenSslVersionRegex { get; }

/// <remarks>
/// It is the caller's responsibility to ensure that <see cref="OpenSslCommand"/> is available.
Expand All @@ -803,7 +803,7 @@ private static bool TryGetOpenSslDirectory([NotNullWhen(true)] out string? openS
return false;
}

var match = OpenSslVersionRegex().Match(stdout);
var match = OpenSslVersionRegex.Match(stdout);
if (!match.Success)
{
Log.UnixOpenSslVersionParsingFailed();
Expand Down Expand Up @@ -858,14 +858,14 @@ private static bool TryGetOpenSslHash(string certificatePath, [NotNullWhen(true)
}

[GeneratedRegex("^[0-9a-f]+\\.[0-9]+$")]
private static partial Regex OpenSslHashFilenameRegex();
private static partial Regex OpenSslHashFilenameRegex { get; }

/// <remarks>
/// We only ever use .pem, but someone will eventually put their own cert in this directory,
/// so we should handle the same extensions as c_rehash (other than .crl).
/// </remarks>
[GeneratedRegex("\\.(pem|crt|cer)$")]
private static partial Regex OpenSslCertificateExtensionRegex();
private static partial Regex OpenSslCertificateExtensionRegex { get; }

/// <remarks>
/// This is a simplified version of c_rehash from OpenSSL. Using the real one would require
Expand All @@ -876,21 +876,17 @@ private static bool TryRehashOpenSslCertificates(string certificateDirectory)
try
{
// First, delete all the existing symlinks, so we don't have to worry about fragmentation or leaks.

var hashRegex = OpenSslHashFilenameRegex();
var extensionRegex = OpenSslCertificateExtensionRegex();

var certs = new List<FileInfo>();

var dirInfo = new DirectoryInfo(certificateDirectory);
foreach (var file in dirInfo.EnumerateFiles())
{
var isSymlink = (file.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;
if (isSymlink && hashRegex.IsMatch(file.Name))
if (isSymlink && OpenSslHashFilenameRegex.IsMatch(file.Name))
{
file.Delete();
}
else if (extensionRegex.IsMatch(file.Name))
else if (OpenSslCertificateExtensionRegex.IsMatch(file.Name))
{
certs.Add(file);
}
Expand Down
Loading