From 2c8bdfe849f84bfc1025026b2df4552e59fd9e17 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Wed, 27 May 2026 15:17:24 +0200 Subject: [PATCH 1/2] check_files: improve path normalization path normalization function cleans up the paths, and is then used for depth calculation. for windows, convert forward slashes to backward slashes using filepath.FromSlash for all systems, replace groups of seperators into a single seperator. this prevents a////b////c from having higher depth than a/b/c --- pkg/snclient/check_files.go | 12 ++++++++++ pkg/snclient/check_files_windows_test.go | 28 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/pkg/snclient/check_files.go b/pkg/snclient/check_files.go index 2c791b7a..0de71870 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 seperator + 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..f55b9735 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 seperator 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) +} From 5fba244884d0b551dc309afea45f0339a6878444 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Wed, 27 May 2026 16:16:05 +0200 Subject: [PATCH 2/2] fix typo for citest --- pkg/snclient/check_files.go | 2 +- pkg/snclient/check_files_windows_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/snclient/check_files.go b/pkg/snclient/check_files.go index 0de71870..9cf05948 100644 --- a/pkg/snclient/check_files.go +++ b/pkg/snclient/check_files.go @@ -598,7 +598,7 @@ func (l *CheckFiles) normalizePath(path string) string { path = fromSlash } - // Remove groups of seperators next to each other , and replace them with a single seperator + // 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) diff --git a/pkg/snclient/check_files_windows_test.go b/pkg/snclient/check_files_windows_test.go index f55b9735..775c4904 100644 --- a/pkg/snclient/check_files_windows_test.go +++ b/pkg/snclient/check_files_windows_test.go @@ -94,7 +94,7 @@ func TestCheckPathSpecifications(t *testing.T) { var res *CheckResult // =============== C:\Windows\Fonts\arial.ttf - // Intended seperator is backward slashes like this + // 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")