diff --git a/Oras/Content/DigestUtility.cs b/Oras/Content/DigestUtility.cs index d5b4ea9..1f384ae 100644 --- a/Oras/Content/DigestUtility.cs +++ b/Oras/Content/DigestUtility.cs @@ -11,7 +11,8 @@ internal static class DigestUtility /// /// digestRegexp checks the digest. /// - private const string digestRegexp = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+"; + private const string digestRegexPattern = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+"; + static Regex digestRegex = new Regex(digestRegexPattern, RegexOptions.Compiled); /// /// ParseDigest verifies the digest header and throws an exception if it is invalid. @@ -19,7 +20,7 @@ internal static class DigestUtility /// internal static string ParseDigest(string digest) { - if (!Regex.IsMatch(digest, digestRegexp)) + if (!digestRegex.IsMatch(digest)) { throw new InvalidDigestException($"Invalid digest: {digest}"); } diff --git a/Oras/Remote/RemoteReference.cs b/Oras/Remote/RemoteReference.cs index be2b32a..2d4aef2 100644 --- a/Oras/Remote/RemoteReference.cs +++ b/Oras/Remote/RemoteReference.cs @@ -1,6 +1,7 @@ using Oras.Content; using Oras.Exceptions; using System; +using System.Net.NetworkInformation; using System.Text.RegularExpressions; namespace Oras.Remote @@ -36,14 +37,18 @@ public class RemoteReference /// - https://github.com/distribution/distribution/blob/v2.7.1/reference/regexp.go#L53 /// - https://github.com/opencontainers/distribution-spec/blob/v1.0.1/spec.md#pulling-manifests /// - private const string repositoryRegexp = @"^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*$"; + private const string repositoryRegexPattern = @"^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*$"; + + private static Regex repositoryRegex = new Regex(repositoryRegexPattern, RegexOptions.Compiled); /// /// tagRegexp checks the tag name. /// The docker and OCI spec have the same regular expression. /// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.0.1/spec.md#pulling-manifests /// - private const string tagRegexp = @"^[\w][\w.-]{0,127}$"; + private const string tagRegexPattern = @"^[\w][\w.-]{0,127}$"; + + private static Regex tagRegex = new Regex(tagRegexPattern, RegexOptions.Compiled); public static RemoteReference ParseReference(string artifact) { @@ -124,7 +129,7 @@ public void ValidateReferenceAsDigest() /// public void ValidateRepository() { - if (!Regex.IsMatch(Repository, repositoryRegexp)) + if (!repositoryRegex.IsMatch(Repository)) { throw new InvalidReferenceException("Invalid Respository"); } @@ -146,7 +151,7 @@ public void ValidateRegistry() public void ValidateReferenceAsTag() { - if (!Regex.IsMatch(Reference, tagRegexp)) + if (!tagRegex.IsMatch(Reference)) { throw new InvalidReferenceException("Invalid Tag"); }