From 6ed3d9ee6f9f95dd1e0149c03b3746c938bf4e87 Mon Sep 17 00:00:00 2001 From: egibs <20933572+egibs@users.noreply.github.com> Date: Wed, 21 May 2025 17:29:32 -0500 Subject: [PATCH] Support absolute paths when calling malcontent externally Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> --- pkg/compile/compile.go | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/compile/compile.go b/pkg/compile/compile.go index 07d44e3be..14ba0918a 100644 --- a/pkg/compile/compile.go +++ b/pkg/compile/compile.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "strings" "github.com/chainguard-dev/clog" @@ -166,22 +167,34 @@ func removeRules(data []byte, rulesToRemove []string) []byte { return newlinePattern.ReplaceAll(modified, []byte("\n\n")) } -// findRoot locates the repository root on the fly. -func findRoot(start string) string { - current := start +// findRoot locates the packages's root directory on the fly. +func findRoot() (string, error) { + _, here, _, ok := runtime.Caller(0) + if !ok { + return "", fmt.Errorf("failed to get current file path") + } + + dir := filepath.Dir(here) + current := dir for { - next := filepath.Join(current, "rules") - if _, err := os.Stat(next); err == nil { - return current + rulesPath := filepath.Join(current, "rules") + if fi, err := os.Stat(rulesPath); err == nil && fi.IsDir() { + return current, nil } parent := filepath.Dir(current) if parent == current { - return "" + break } - current = parent } + + rulesPath := filepath.Join(filepath.Dir(dir), "rules") + if fi, err := os.Stat(rulesPath); err == nil && fi.IsDir() { + return filepath.Dir(dir), nil + } + + return "", fmt.Errorf("could not find rules directory from %s", dir) } // replaceGlobal updates the include string to reference the absolute path of rules/global/global.yara @@ -205,17 +218,10 @@ func Recursive(ctx context.Context, fss []fs.FS) (*yarax.Rules, error) { return nil, fmt.Errorf("yarax compiler: %w", err) } - // use the current working directory to determine the root path - // this only needs to be done once - cwd, err := os.Getwd() - if err != nil { - return nil, err - } - abs, err := filepath.Abs(cwd) + rootPath, err := findRoot() if err != nil { return nil, err } - rootPath := findRoot(abs) rulesToRemove := getRulesToRemove()