diff --git a/pkg/snclient/check_files.go b/pkg/snclient/check_files.go index 2c791b7a..9cf05948 100644 --- a/pkg/snclient/check_files.go +++ b/pkg/snclient/check_files.go @@ -592,6 +592,18 @@ func (l *CheckFiles) normalizePath(path string) string { // Special handling for Windows drive letters, // Files directly under the drive do not have separators, e.g: C:pagefile.sys // This confuses the depth calculation + if runtime.GOOS == "windows" { + // Convert forward slashes to backward slashes + fromSlash := filepath.FromSlash(path) + path = fromSlash + } + + // Remove groups of seperators next to each other , and replace them with a single separator + sep := string(os.PathSeparator) + for strings.Contains(path, sep+sep) { + path = strings.ReplaceAll(path, sep+sep, sep) + } + if runtime.GOOS == "windows" { // C: -> C:\ if len(path) == 2 && unicode.IsUpper(rune(path[0])) && path[1] == ':' { diff --git a/pkg/snclient/check_files_windows_test.go b/pkg/snclient/check_files_windows_test.go index 5a0c0c4a..775c4904 100644 --- a/pkg/snclient/check_files_windows_test.go +++ b/pkg/snclient/check_files_windows_test.go @@ -88,3 +88,31 @@ func TestCheckDriveLetterPaths(t *testing.T) { StopTestAgent(t, snc) } + +func TestCheckPathSpecifications(t *testing.T) { + snc := StartTestAgent(t, "") + var res *CheckResult + + // =============== C:\Windows\Fonts\arial.ttf + // Intended separator is backward slashes like this + res = snc.RunCheck("check_files", []string{"path=C:\\Windows\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "}) + assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches") + + // Forward slashes should be converted to backward slashes + res = snc.RunCheck("check_files", []string{"path=C:/Windows/fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "}) + assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches") + + // Multiple backward slashes that do not actually go into subfolders and add depth should be ignored + res = snc.RunCheck("check_files", []string{"path=C:\\\\\\Windows\\\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "}) + assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches") + + // Multiple backward slashes that do not actually go into subfolders and add depth should be ignored + res = snc.RunCheck("check_files", []string{"path=C:\\\\Windows\\\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "}) + assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches") + + // Multiple forward slashes that do not actually go into subfolders and add depth should be ignored + res = snc.RunCheck("check_files", []string{"path=C://Windows////fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "}) + assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches") + + StopTestAgent(t, snc) +}