Skip to content

Commit eafe066

Browse files
committed
add NormalizePath to ensure IG paths are always full
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com> # Conflicts: # pkg/utils/datasource_event.go # pkg/utils/struct_event.go
1 parent 1a8e36e commit eafe066

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

pkg/utils/datasource_event.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ func (e *DatasourceEvent) GetExePath() string {
537537
logger.L().Warning("GetExePath - error reading field exepath", helpers.String("eventType", string(e.EventType)), helpers.Error(err))
538538
return ""
539539
}
540-
return exepath
540+
return NormalizePath(exepath)
541541
}
542542

543543
func (e *DatasourceEvent) GetExitCode() uint32 {
@@ -579,7 +579,7 @@ func (e *DatasourceEvent) GetFullPath() string {
579579
return ""
580580
}
581581
}
582-
return path
582+
return NormalizePath(path)
583583
}
584584

585585
func (e *DatasourceEvent) GetGid() *uint32 {
@@ -658,7 +658,7 @@ func (e *DatasourceEvent) GetNewPath() string {
658658
logger.L().Warning("GetNewPath - error reading field newpath", helpers.String("eventType", string(e.EventType)), helpers.Error(err))
659659
return ""
660660
}
661-
return newPath
661+
return NormalizePath(newPath)
662662
}
663663

664664
func (e *DatasourceEvent) GetNumAnswers() int {
@@ -676,7 +676,7 @@ func (e *DatasourceEvent) GetOldPath() string {
676676
logger.L().Warning("GetOldPath - error reading field oldpath", helpers.String("eventType", string(e.EventType)), helpers.Error(err))
677677
return ""
678678
}
679-
return oldPath
679+
return NormalizePath(oldPath)
680680
}
681681

682682
func (e *DatasourceEvent) GetOpcode() int {
@@ -710,7 +710,7 @@ func (e *DatasourceEvent) GetPath() string {
710710
logger.L().Warning("GetPath - error reading field fname", helpers.String("eventType", string(e.EventType)), helpers.Error(err))
711711
return ""
712712
}
713-
return path
713+
return NormalizePath(path)
714714
}
715715

716716
func (e *DatasourceEvent) GetPcomm() string {

pkg/utils/normalize_path_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNormalizePath(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input string
12+
expected string
13+
}{
14+
{
15+
name: "empty path",
16+
input: "",
17+
expected: "",
18+
},
19+
{
20+
name: "dot path",
21+
input: ".",
22+
expected: "/",
23+
},
24+
{
25+
name: "absolute path",
26+
input: "/etc/passwd",
27+
expected: "/etc/passwd",
28+
},
29+
{
30+
name: "headless proc path (task)",
31+
input: "/46/task/46/fd",
32+
expected: "/proc/46/task/46/fd",
33+
},
34+
{
35+
name: "headless proc path (fd)",
36+
input: "/46/fd/3",
37+
expected: "/proc/46/fd/3",
38+
},
39+
{
40+
name: "already absolute proc path",
41+
input: "/proc/46/fd/3",
42+
expected: "/proc/46/fd/3",
43+
},
44+
{
45+
name: "relative path (not dot)",
46+
input: "usr/bin/ls",
47+
expected: "/usr/bin/ls",
48+
},
49+
{
50+
name: "relative path with ./",
51+
input: "./config",
52+
expected: "/config",
53+
},
54+
{
55+
name: "path with redundant slashes",
56+
input: "/etc//passwd",
57+
expected: "/etc/passwd",
58+
},
59+
{
60+
name: "path with dot components",
61+
input: "/usr/./bin/../lib",
62+
expected: "/usr/lib",
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
got := NormalizePath(tt.input)
69+
assert.Equal(t, tt.expected, got)
70+
})
71+
}
72+
}

pkg/utils/path.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"path"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var headlessProcRegex = regexp.MustCompile(`^/\d+/(task|fd)/`)
10+
11+
// NormalizePath normalizes a path by:
12+
// 1. Prepending "/proc" to "headless" proc paths (e.g. /46/task/46/fd -> /proc/46/task/46/fd)
13+
// 2. Ensuring it starts with "/" if it's not empty
14+
// 3. Converting "." to "/"
15+
// 4. Cleaning the path (removing redundant slashes, dot-dots, etc.)
16+
func NormalizePath(p string) string {
17+
if p == "" {
18+
return ""
19+
}
20+
21+
if p == "." {
22+
return "/"
23+
}
24+
25+
if headlessProcRegex.MatchString(p) {
26+
p = "/proc" + p
27+
}
28+
29+
if !strings.HasPrefix(p, "/") {
30+
p = "/" + p
31+
}
32+
33+
return path.Clean(p)
34+
}

0 commit comments

Comments
 (0)