Skip to content

Commit be195a4

Browse files
authored
Avoid regex for exact word matching in DscClassCache (PowerShell#26306)
1 parent ba02868 commit be195a4

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/System.Management.Automation/DscSupport/CimDSCParser.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,6 @@ public DscClassCacheEntry(DSCResourceRunAsCredential aDSCResourceRunAsCredential
525525
public static class DscClassCache
526526
{
527527
private const string InboxDscResourceModulePath = "WindowsPowershell\\v1.0\\Modules\\PsDesiredStateConfiguration";
528-
private const string reservedDynamicKeywords = "^(Synchronization|Certificate|IIS|SQL)$";
529-
530-
private const string reservedProperties = "^(Require|Trigger|Notify|Before|After|Subscribe)$";
531528

532529
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("DSC", "DSC Class Cache");
533530

@@ -1264,13 +1261,6 @@ public static void ValidateInstanceText(string instanceText)
12641261
parser.ValidateInstanceText(instanceText);
12651262
}
12661263

1267-
private static bool IsMagicProperty(string propertyName)
1268-
{
1269-
return System.Text.RegularExpressions.Regex.Match(propertyName,
1270-
"^(ResourceId|SourceInfo|ModuleName|ModuleVersion|ConfigurationName)$",
1271-
System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success;
1272-
}
1273-
12741264
private static string GetFriendlyName(CimClass cimClass)
12751265
{
12761266
try
@@ -1367,7 +1357,8 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
13671357
//
13681358
// Skip all of the base, meta, registration and other classes that are not intended to be used directly by a script author
13691359
//
1370-
if (System.Text.RegularExpressions.Regex.Match(keywordString, "^OMI_Base|^OMI_.*Registration", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
1360+
if (keywordString.StartsWith("OMI_Base", StringComparison.OrdinalIgnoreCase) ||
1361+
(keywordString.StartsWith("OMI_", StringComparison.OrdinalIgnoreCase) && keywordString.IndexOf("Registration", 4, StringComparison.OrdinalIgnoreCase) >= 0))
13711362
{
13721363
return null;
13731364
}
@@ -1383,7 +1374,7 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
13831374
};
13841375

13851376
// If it's one of reserved dynamic keyword, mark it
1386-
if (System.Text.RegularExpressions.Regex.Match(keywordString, reservedDynamicKeywords, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
1377+
if (IsReservedDynamicKeyword(keywordString))
13871378
{
13881379
keyword.IsReservedKeyword = true;
13891380
}
@@ -1441,7 +1432,7 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
14411432
}
14421433
}
14431434
// If it's one of our reserved properties, save it for error reporting
1444-
if (System.Text.RegularExpressions.Regex.Match(prop.Name, reservedProperties, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
1435+
if (IsReservedProperty(prop.Name))
14451436
{
14461437
keyword.HasReservedProperties = true;
14471438
continue;
@@ -1540,6 +1531,27 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
15401531
UpdateKnownRestriction(keyword);
15411532

15421533
return keyword;
1534+
1535+
static bool IsMagicProperty(string propertyName) =>
1536+
string.Equals(propertyName, "ResourceId", StringComparison.OrdinalIgnoreCase) ||
1537+
string.Equals(propertyName, "SourceInfo", StringComparison.OrdinalIgnoreCase) ||
1538+
string.Equals(propertyName, "ModuleName", StringComparison.OrdinalIgnoreCase) ||
1539+
string.Equals(propertyName, "ModuleVersion", StringComparison.OrdinalIgnoreCase) ||
1540+
string.Equals(propertyName, "ConfigurationName", StringComparison.OrdinalIgnoreCase);
1541+
1542+
static bool IsReservedDynamicKeyword(string keyword) =>
1543+
string.Equals(keyword, "Synchronization", StringComparison.OrdinalIgnoreCase) ||
1544+
string.Equals(keyword, "Certificate", StringComparison.OrdinalIgnoreCase) ||
1545+
string.Equals(keyword, "IIS", StringComparison.OrdinalIgnoreCase) ||
1546+
string.Equals(keyword, "SQL", StringComparison.OrdinalIgnoreCase);
1547+
1548+
static bool IsReservedProperty(string name) =>
1549+
string.Equals(name, "Require", StringComparison.OrdinalIgnoreCase) ||
1550+
string.Equals(name, "Trigger", StringComparison.OrdinalIgnoreCase) ||
1551+
string.Equals(name, "Notify", StringComparison.OrdinalIgnoreCase) ||
1552+
string.Equals(name, "Before", StringComparison.OrdinalIgnoreCase) ||
1553+
string.Equals(name, "After", StringComparison.OrdinalIgnoreCase) ||
1554+
string.Equals(name, "Subscribe", StringComparison.OrdinalIgnoreCase);
15431555
}
15441556

15451557
/// <summary>

0 commit comments

Comments
 (0)