44package compile
55
66import (
7- "bytes"
87 "context"
98 "fmt"
109 "io/fs"
11- "os"
1210 "path/filepath"
1311 "regexp"
14- "runtime"
1512 "strings"
1613
1714 "github.com/chainguard-dev/clog"
@@ -20,11 +17,6 @@ import (
2017 yarax "github.com/VirusTotal/yara-x/go"
2118)
2219
23- const (
24- globalInclude = `include "rules/global/global.yara"`
25- globalPath = "rules/global/global.yara"
26- )
27-
2820var FS = rules .FS
2921
3022// badRules are noisy 3rd party rules to silently disable.
@@ -167,47 +159,6 @@ func removeRules(data []byte, rulesToRemove []string) []byte {
167159 return newlinePattern .ReplaceAll (modified , []byte ("\n \n " ))
168160}
169161
170- // findRoot locates the packages's root directory on the fly.
171- func findRoot () (string , error ) {
172- _ , here , _ , ok := runtime .Caller (0 )
173- if ! ok {
174- return "" , fmt .Errorf ("failed to get current file path" )
175- }
176-
177- dir := filepath .Dir (here )
178- current := dir
179- for {
180- rulesPath := filepath .Join (current , "rules" )
181- if fi , err := os .Stat (rulesPath ); err == nil && fi .IsDir () {
182- return current , nil
183- }
184-
185- parent := filepath .Dir (current )
186- if parent == current {
187- break
188- }
189- current = parent
190- }
191-
192- rulesPath := filepath .Join (filepath .Dir (dir ), "rules" )
193- if fi , err := os .Stat (rulesPath ); err == nil && fi .IsDir () {
194- return filepath .Dir (dir ), nil
195- }
196-
197- return "" , fmt .Errorf ("could not find rules directory from %s" , dir )
198- }
199-
200- // replaceGlobal updates the include string to reference the absolute path of rules/global/global.yara
201- // by default, the relative path is valid for local compilations and builds done from the root of the repository,
202- // but this is not valid for test files located in various directories.
203- func replaceGlobal (data []byte , path string ) []byte {
204- modified := data
205- if bytes .Contains (data , []byte (globalInclude )) {
206- modified = bytes .Replace (data , []byte (globalInclude ), fmt .Appendf (nil , `include "%s"` , path ), 1 )
207- }
208- return modified
209- }
210-
211162func Recursive (ctx context.Context , fss []fs.FS ) (* yarax.Rules , error ) {
212163 if ctx .Err () != nil {
213164 return nil , ctx .Err ()
@@ -218,11 +169,6 @@ func Recursive(ctx context.Context, fss []fs.FS) (*yarax.Rules, error) {
218169 return nil , fmt .Errorf ("yarax compiler: %w" , err )
219170 }
220171
221- rootPath , err := findRoot ()
222- if err != nil {
223- return nil , err
224- }
225-
226172 rulesToRemove := getRulesToRemove ()
227173
228174 for _ , root := range fss {
@@ -243,9 +189,6 @@ func Recursive(ctx context.Context, fss []fs.FS) (*yarax.Rules, error) {
243189
244190 bs = removeRules (bs , rulesToRemove )
245191
246- globalAbs := filepath .Join (rootPath , globalPath )
247- bs = replaceGlobal (bs , globalAbs )
248-
249192 yxc .NewNamespace (path )
250193 if err := yxc .AddSource (string (bs ), yarax .WithOrigin (path )); err != nil {
251194 return fmt .Errorf ("failed to parse %s: %v" , path , err )
0 commit comments