Skip to content

Commit

Permalink
Tweak cleanPath function and tests (chainguard-dev#308)
Browse files Browse the repository at this point in the history
* Remove /private prefixes from paths

Signed-off-by: egibs <[email protected]>

* Use filepath.EvalSymlinks; update tests

Signed-off-by: egibs <[email protected]>

* Run make fix

Signed-off-by: egibs <[email protected]>

---------

Signed-off-by: egibs <[email protected]>
  • Loading branch information
egibs authored Jul 2, 2024
1 parent ae3a08b commit c056273
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
18 changes: 15 additions & 3 deletions pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ func findFilesRecursively(ctx context.Context, root string, c Config) ([]string,
}

// cleanPath removes the temporary directory prefix from the path.
func cleanPath(path string, prefix string) string {
return strings.TrimPrefix(path, prefix)
func cleanPath(path string, prefix string) (string, error) {
pathEval, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
prefixEval, err := filepath.EvalSymlinks(prefix)
if err != nil {
return "", err
}
return strings.TrimPrefix(pathEval, prefixEval), nil
}

// formatPath formats the path for display.
Expand Down Expand Up @@ -96,7 +104,11 @@ func scanSinglePath(ctx context.Context, c Config, yrs *yara.Rules, path string,
// If absPath is provided, use it instead of the path if they are different.
// This is useful when scanning images and archives.
if absPath != "" && absPath != path && archiveRoot != "" {
fr.Path = fmt.Sprintf("%s ∴ %s", absPath, formatPath(cleanPath(path, archiveRoot)))
cleanPath, err := cleanPath(path, archiveRoot)
if err != nil {
return nil, err
}
fr.Path = fmt.Sprintf("%s ∴ %s", absPath, formatPath(cleanPath))
}

if len(fr.Behaviors) == 0 && c.OmitEmpty {
Expand Down
99 changes: 80 additions & 19 deletions pkg/action/scan_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,98 @@
package action

import "testing"
import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestCleanPath(t *testing.T) {
// create a temporary directory
tempDir, err := os.MkdirTemp("", "TestCleanPath")
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// create and symlink a nested directory
// create a file within the nested directory
nestedDir := filepath.Join(tempDir, "nested")
if err := os.Mkdir(nestedDir, 0o755); err != nil {
t.Fatalf("failed to create nested directory: %v", err)
}
symlinkPath := filepath.Join(tempDir, "symlink")
if err := os.Symlink(nestedDir, symlinkPath); err != nil {
t.Fatalf("failed to create symlink: %v", err)
}

filePath := filepath.Join(nestedDir, "test.txt")
file, err := os.Create(filePath)
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
defer file.Close()

tests := []struct {
name string
path string
prefix string
want string
name string
path string
prefix string
want string
wantErr bool
}{
{
name: "linux",
path: "/tmp/static3980366648/usr/share/zoneinfo/zone1970",
prefix: "/tmp/static3980366648/",
want: "usr/share/zoneinfo/zone1970",
name: "expected behavior",
path: filepath.Join(nestedDir, "test.txt"),
prefix: nestedDir,
want: "/test.txt",
},
{
name: "symlink in path",
path: filepath.Join(symlinkPath, "test.txt"),
prefix: nestedDir,
want: "/test.txt",
},
{
name: "macOS",
path: "/var/folders/3g/88131l9j11x995ppjbxsvhbh0000gn/T/apko_0.13.2_linux_arm64.tar.gz2526862474/apko_0.13.2_linux_arm64/apko",
prefix: "/var/folders/3g/88131l9j11x995ppjbxsvhbh0000gn/T/apko_0.13.2_linux_arm64.tar.gz2526862474/",
want: "apko_0.13.2_linux_arm64/apko",
name: "symlink in prefix",
path: filepath.Join(nestedDir, "test.txt"),
prefix: symlinkPath,
want: "/test.txt",
},
{
name: "windows",
path: "C:\\Users\\abc\\AppData\\Local\\Temp\\static3980366648\\usr\\share\\zoneinfo\\zone1970",
prefix: "C:\\Users\\abc\\AppData\\Local\\Temp\\static3980366648\\",
want: "usr\\share\\zoneinfo\\zone1970",
name: "non-existent path",
path: filepath.Join(tempDir, "does_not_exist", "test.txt"),
prefix: tempDir,
wantErr: true,
},
{
name: "path prefix mismatch",
path: filepath.Join(nestedDir, "test.txt"),
prefix: "",
want: filepath.Join(nestedDir, "test.txt"),
},
{
name: "empty paths",
path: "",
prefix: "",
want: "",
},
{
name: "identical path and prefix",
path: nestedDir,
prefix: nestedDir,
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cleanPath(tt.path, tt.prefix); got != tt.want {
t.Errorf("CleanPath() = %v, want %v", got, tt.want)
got, err := cleanPath(tt.path, tt.prefix)
if (err != nil) != tt.wantErr {
t.Errorf("cleanPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !strings.HasSuffix(got, tt.want) {
t.Errorf("cleanPath() = %v, want suffix %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit c056273

Please sign in to comment.