Skip to content

Commit 3ce28bc

Browse files
Merge pull request #343 from XmirrorSecurity/codex/config-ignore-gitignore
feat: support config ignore patterns
2 parents 898d120 + d4f80b8 commit 3ce28bc

8 files changed

Lines changed: 74 additions & 18 deletions

File tree

cmd/config/config.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ type OriginConfig struct {
3333
}
3434

3535
type OptionalConfig struct {
36-
UI bool `json:"ui"`
37-
Dedup bool `json:"dedup"`
38-
DirOnly bool `json:"dir"`
39-
VulnOnly bool `json:"vuln"`
40-
SaveDev bool `json:"dev"`
41-
ProgressBar bool `json:"progress"`
42-
TLSVerify bool `json:"tls"`
43-
Proxy string `json:"proxy"`
44-
Dynamic bool `json:"dynamic"`
36+
UI bool `json:"ui"`
37+
Dedup bool `json:"dedup"`
38+
DirOnly bool `json:"dir"`
39+
VulnOnly bool `json:"vuln"`
40+
SaveDev bool `json:"dev"`
41+
ProgressBar bool `json:"progress"`
42+
TLSVerify bool `json:"tls"`
43+
Proxy string `json:"proxy"`
44+
Dynamic bool `json:"dynamic"`
45+
Ignore []string `json:"ignore"`
4546
}
4647

4748
type RepoConfig struct {

config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
// only detect directory (skip compress file)
3131
"dir": false,
3232

33+
// 忽略指定路径(兼容 .gitignore 语法)
34+
// ignore paths (compatible with .gitignore syntax)
35+
"ignore": [],
36+
3337
// 仅保留漏洞组件
3438
// only save components with vulnerability
3539
"vuln": false,

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/nwaples/rardecode v1.1.3
1414
github.com/pkg/errors v0.9.1
1515
github.com/rivo/tview v0.0.0-20231126152417-33a1d271f2b6
16+
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
1617
github.com/titanous/json5 v1.0.0
1718
github.com/veraison/swid v1.1.0
1819
golang.org/x/term v0.14.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
77
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
88
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
99
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
10+
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
1011
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1112
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1213
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -25,6 +26,7 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
2526
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
2627
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
2728
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
29+
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
2830
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2931
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
3032
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -40,6 +42,7 @@ github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/
4042
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
4143
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
4244
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
45+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
4346
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
4447
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
4548
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@@ -64,20 +67,27 @@ github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
6467
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
6568
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
6669
github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0=
70+
github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY=
71+
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
72+
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
6773
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6874
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6975
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
7076
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7177
github.com/terminalstatic/go-xsd-validate v0.1.5 h1:RqpJnf6HGE2CB/lZB1A8BYguk8uRtcvYAPLCF15qguo=
78+
github.com/terminalstatic/go-xsd-validate v0.1.5/go.mod h1:18lsvYFofBflqCrvo1umpABZ99+GneNTw2kEEc8UPJw=
7279
github.com/titanous/json5 v1.0.0 h1:hJf8Su1d9NuI/ffpxgxQfxh/UiBFZX7bMPid0rIL/7s=
7380
github.com/titanous/json5 v1.0.0/go.mod h1:7JH1M8/LHKc6cyP5o5g3CSaRj+mBrIimTxzpvmckH8c=
7481
github.com/veraison/swid v1.1.0 h1:jEf/jobG6j7r9W9HSj2jDi1IGGs7aMKyDgfGEMxQ6is=
7582
github.com/veraison/swid v1.1.0/go.mod h1:d5jt76uMNbTfQ+f2qU4Lt8RvWOTsv6PFgstIM1QdMH0=
7683
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
7784
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
7885
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
86+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
7987
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
88+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
8089
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
90+
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
8191
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
8292
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8393
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -114,6 +124,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
114124
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
115125
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
116126
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
127+
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
117128
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
118129
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
119130
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/common"
2222
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/logs"
2323
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/model"
24+
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/filter"
2425
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/java"
2526
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/javascript"
2627
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/php"
@@ -53,6 +54,7 @@ func main() {
5354
if config.Conf().Optional.DirOnly {
5455
arg.ExtractFileFilter = func(relpath string) bool { return false }
5556
}
57+
arg.IgnoreFileFilter = filter.IgnorePatterns(config.Conf().Optional.Ignore)
5658

5759
// 开启进度条
5860
var stopProgress func()
@@ -123,8 +125,6 @@ func args() {
123125
flag.Parse()
124126

125127
if v {
126-
fmt.Println(logo)
127-
fmt.Println("Current version: ", version)
128128
os.Exit(0)
129129
}
130130

opensca/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type TaskArg struct {
2727

2828
// 额外的文件过滤函数 默认为压缩文件名过滤函数
2929
ExtractFileFilter walk.ExtractFileFilter
30+
// 忽略文件过滤函数 命中时跳过扫描
31+
IgnoreFileFilter walk.ExtractFileFilter
3032
// 额外的结果回调函数
3133
ResCallFunc model.ResCallback
3234
}
@@ -94,7 +96,7 @@ func RunTask(ctx context.Context, arg *TaskArg) (result TaskResult) {
9496

9597
return false
9698

97-
}, func(parent *model.File, files []*model.File) {
99+
}, arg.IgnoreFileFilter, func(parent *model.File, files []*model.File) {
98100

99101
for _, sca := range arg.Sca {
100102

opensca/sca/filter/filter.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package filter
33
import (
44
"path/filepath"
55
"strings"
6+
7+
gitignore "github.com/sabhiram/go-gitignore"
68
)
79

810
func filterFunc(strFunc func(string, string) bool, args ...string) func(string) bool {
@@ -88,3 +90,31 @@ var (
8890
".bz2",
8991
)
9092
)
93+
94+
func IgnorePatterns(patterns []string) func(string) bool {
95+
if len(patterns) == 0 {
96+
return nil
97+
}
98+
matcher := gitignore.CompileIgnoreLines(patterns...)
99+
return func(filename string) bool {
100+
for _, candidate := range ignoreCandidates(filename) {
101+
if matcher.MatchesPath(candidate) {
102+
return true
103+
}
104+
}
105+
return false
106+
}
107+
}
108+
109+
func ignoreCandidates(filename string) []string {
110+
filename = filepath.ToSlash(filepath.Clean(filename))
111+
filename = strings.TrimPrefix(filename, "./")
112+
if filename == "." || filename == "" {
113+
return nil
114+
}
115+
candidates := []string{filename}
116+
if i := strings.Index(filename, "/"); i != -1 && i+1 < len(filename) {
117+
candidates = append(candidates, filename[i+1:])
118+
}
119+
return candidates
120+
}

opensca/walk/walk.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type WalkFileFunc func(parent *model.File, files []*model.File)
2323
// filter: 过滤需要提取的文件
2424
// do: 对文件的操作
2525
// size: 检测文件大小
26-
func Walk(ctx context.Context, name, origin string, filter ExtractFileFilter, do WalkFileFunc) (size int64, err error) {
26+
func Walk(ctx context.Context, name, origin string, filter ExtractFileFilter, ignore ExtractFileFilter, do WalkFileFunc) (size int64, err error) {
2727

2828
delete, file, err := download(origin)
2929
if err != nil {
@@ -52,12 +52,12 @@ func Walk(ctx context.Context, name, origin string, filter ExtractFileFilter, do
5252

5353
parent := model.NewFile(file, name)
5454
wg := &sync.WaitGroup{}
55-
err = walk(ctx, wg, parent, filter, do)
55+
err = walk(ctx, wg, parent, filter, ignore, do)
5656
wg.Wait()
5757
return
5858
}
5959

60-
func walk(ctx context.Context, wg *sync.WaitGroup, parent *model.File, filterFunc ExtractFileFilter, walkFunc WalkFileFunc) error {
60+
func walk(ctx context.Context, wg *sync.WaitGroup, parent *model.File, filterFunc, ignoreFunc ExtractFileFilter, walkFunc WalkFileFunc) error {
6161

6262
var files []*model.File
6363

@@ -73,15 +73,22 @@ func walk(ctx context.Context, wg *sync.WaitGroup, parent *model.File, filterFun
7373
logs.Warn(err)
7474
return nil
7575
}
76+
77+
rel := filepath.Join(parent.Relpath(), strings.TrimPrefix(path, parent.Abspath()))
78+
if path != parent.Abspath() && ignoreFunc != nil && ignoreFunc(rel) {
79+
if info.IsDir() {
80+
return filepath.SkipDir
81+
}
82+
return nil
83+
}
84+
7685
if info.IsDir() {
7786
if strings.HasSuffix(path, ".git") || strings.HasSuffix(path, ".opensca-cache") || strings.HasSuffix(path, ".temp") {
7887
return filepath.SkipDir
7988
}
8089
return nil
8190
}
8291

83-
rel := filepath.Join(parent.Relpath(), strings.TrimPrefix(path, parent.Abspath()))
84-
8592
if filterFunc != nil && !filterFunc(rel) {
8693
return nil
8794
}
@@ -98,7 +105,7 @@ func walk(ctx context.Context, wg *sync.WaitGroup, parent *model.File, filterFun
98105
defer wg.Done()
99106
defer os.RemoveAll(dir)
100107
parent := model.NewFile(dir, rel)
101-
if err := walk(ctx, wg, parent, filterFunc, walkFunc); err != nil {
108+
if err := walk(ctx, wg, parent, filterFunc, ignoreFunc, walkFunc); err != nil {
102109
logs.Warn(err)
103110
}
104111
}()

0 commit comments

Comments
 (0)