Skip to content

Commit 883ba10

Browse files
author
José Carlos Chávez
committed
fix: warn instead of returning error on empty glob result (#1280)
1 parent 779742f commit 883ba10

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

Diff for: internal/seclang/parser.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ func (p *Parser) FromFile(profilePath string) error {
4646
if err != nil {
4747
return fmt.Errorf("failed to glob: %s", err.Error())
4848
}
49+
4950
if len(files) == 0 {
50-
return fmt.Errorf("empty glob: %s does not match any file", profilePath)
51+
p.options.WAF.Logger.Warn().Int("line", p.currentLine).Msg("empty glob result")
5152
}
5253
} else {
5354
files = append(files, profilePath)
5455
}
56+
5557
for _, profilePath := range files {
5658
profilePath = strings.TrimSpace(profilePath)
5759
if !strings.HasPrefix(profilePath, "/") {
@@ -157,20 +159,22 @@ func (p *Parser) evaluateLine(l string) error {
157159
if len(opts) >= 3 && opts[0] == '"' && opts[len(opts)-1] == '"' {
158160
opts = strings.Trim(opts, `"`)
159161
}
162+
160163
if directive == "include" {
161164
// this is a special hardcoded case
162165
// we cannot add it as a directive type because there are recursion issues
163166
// note a user might still include another file that includes the original file
164167
// generating a DDOS attack
165168
if p.includeCount >= maxIncludeRecursion {
166-
return p.log(fmt.Sprintf("cannot include more than %d files", maxIncludeRecursion))
169+
return p.logAndReturnErr(fmt.Sprintf("cannot include more than %d files", maxIncludeRecursion))
167170
}
168171
p.includeCount++
169172
return p.FromFile(opts)
170173
}
174+
171175
d, ok := directivesMap[directive]
172176
if !ok || d == nil {
173-
return p.log(fmt.Sprintf("unknown directive %q", directive))
177+
return p.logAndReturnErr(fmt.Sprintf("unknown directive %q", directive))
174178
}
175179

176180
p.options.Raw = l
@@ -194,7 +198,7 @@ func (p *Parser) evaluateLine(l string) error {
194198
return nil
195199
}
196200

197-
func (p *Parser) log(msg string) error {
201+
func (p *Parser) logAndReturnErr(msg string) error {
198202
p.options.WAF.Logger.Error().Int("line", p.currentLine).Msg(msg)
199203
return errors.New(msg)
200204
}

Diff for: internal/seclang/parser_test.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,34 @@ func TestErrorWithBackticks(t *testing.T) {
8989
func TestLoadConfigurationFile(t *testing.T) {
9090
waf := coraza.NewWAF()
9191
p := NewParser(waf)
92-
err := p.FromFile("../../coraza.conf-recommended")
93-
if err != nil {
94-
t.Errorf("unexpected error: %s", err.Error())
95-
}
9692

97-
err = p.FromFile("../doesnotexist.conf")
98-
if err == nil {
99-
t.Error("expected not found error")
100-
}
93+
t.Run("existing file", func(t *testing.T) {
94+
err := p.FromFile("../../coraza.conf-recommended")
95+
if err != nil {
96+
t.Errorf("unexpected error: %s", err.Error())
97+
}
98+
})
10199

102-
err = p.FromFile("./testdata/glob/*.conf")
103-
if err != nil {
104-
t.Errorf("unexpected error: %s", err.Error())
105-
}
100+
t.Run("unexisting file", func(t *testing.T) {
101+
err := p.FromFile("../doesnotexist.conf")
102+
if err == nil {
103+
t.Error("expected not found error")
104+
}
105+
})
106106

107-
err = p.FromFile("./testdata/glob/*.comf")
108-
if err == nil {
109-
t.Errorf("expected an error as glob does not match any file")
110-
}
107+
t.Run("successful glob", func(t *testing.T) {
108+
err := p.FromFile("./testdata/glob/*.conf")
109+
if err != nil {
110+
t.Errorf("unexpected error: %s", err.Error())
111+
}
112+
})
113+
114+
t.Run("empty glob result", func(t *testing.T) {
115+
err := p.FromFile("./testdata/glob/*.comf")
116+
if err != nil {
117+
t.Errorf("unexpected error despite glob not matching any file")
118+
}
119+
})
111120
}
112121

113122
// Connectors are supporting embedding github.com/corazawaf/coraza-coreruleset to ease CRS integration

0 commit comments

Comments
 (0)