Skip to content

Commit d7f04c3

Browse files
Copilotpragnya17
andcommitted
Complete RootPathPatterns implementation with working pattern matching
Co-authored-by: pragnya17 <59893188+pragnya17@users.noreply.github.com>
1 parent bbca699 commit d7f04c3

4 files changed

Lines changed: 28 additions & 107 deletions

File tree

debug_pattern.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

debug_test.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Microsoft.Sbom.Api/Filters/DownloadedRootPathFilter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public void Init()
124124
// Check for new pattern-based configuration first (takes precedence)
125125
if (configuration.RootPathPatterns != null && !string.IsNullOrWhiteSpace(configuration.RootPathPatterns.Value))
126126
{
127-
skipValidation = false;
128127
patterns = new List<string>();
129128
var patternStrings = configuration.RootPathPatterns.Value.Split(';');
130129

@@ -137,6 +136,12 @@ public void Init()
137136
logger.Verbose($"Added pattern {trimmedPattern}");
138137
}
139138
}
139+
140+
// Only set skipValidation to false if we actually have patterns
141+
if (patterns.Count > 0)
142+
{
143+
skipValidation = false;
144+
}
140145
}
141146

142147
// Fall back to legacy path prefix configuration

src/Microsoft.Sbom.Api/Utils/PathPatternMatcher.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using System.IO;
56
using System.Text.RegularExpressions;
67

@@ -29,20 +30,33 @@ public static bool IsMatch(string filePath, string pattern, string basePath = nu
2930
// Normalize the file path
3031
var normalizedFilePath = Path.GetFullPath(filePath);
3132

32-
// If basePath is provided and pattern is relative, create the full pattern
33-
string normalizedPattern;
33+
// If basePath is provided and pattern is relative, we need to check if the file
34+
// is within the base path and then match against the relative portion
35+
string patternToMatch;
3436
if (!string.IsNullOrEmpty(basePath) && !Path.IsPathRooted(pattern))
3537
{
36-
var combinedPath = Path.Combine(basePath, pattern);
37-
normalizedPattern = Path.GetFullPath(combinedPath);
38+
var normalizedBasePath = Path.GetFullPath(basePath);
39+
40+
// Check if the file is within the base path
41+
if (!normalizedFilePath.StartsWith(normalizedBasePath, StringComparison.OrdinalIgnoreCase))
42+
{
43+
return false;
44+
}
45+
46+
// Get the relative path from the base path
47+
var relativePath = Path.GetRelativePath(normalizedBasePath, normalizedFilePath);
48+
49+
// Match the relative path against the pattern
50+
patternToMatch = pattern;
51+
normalizedFilePath = relativePath;
3852
}
3953
else
4054
{
41-
normalizedPattern = pattern;
55+
patternToMatch = pattern;
4256
}
4357

4458
// Convert glob pattern to regex
45-
var regexPattern = ConvertGlobToRegex(normalizedPattern);
59+
var regexPattern = ConvertGlobToRegex(patternToMatch);
4660

4761
// Perform case-insensitive matching for cross-platform compatibility
4862
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase);
@@ -101,7 +115,7 @@ private static string ConvertGlobToRegex(string globPattern)
101115
}
102116
}
103117

104-
// Anchor the pattern to match from the beginning
105-
return "^" + regexPattern.ToString() + ".*$";
118+
// Don't anchor to end with .* since we want exact path matching
119+
return "^" + regexPattern.ToString() + "$";
106120
}
107121
}

0 commit comments

Comments
 (0)