Skip to content

Commit

Permalink
lint: more lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso committed Nov 17, 2021
1 parent c279824 commit bd5e185
Show file tree
Hide file tree
Showing 76 changed files with 727 additions and 930 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: lint

on:
pull_request:
branches:
- '*'
- 'v2/*'
push:
branches: [master, v2/*]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]
20 changes: 12 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ repos:
hooks:
- id: go-fmt
- id: go-vet
- id: go-lint
# - id: go-lint
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: no-go-testing
- id: golangci-lint
- id: go-critic
- id: go-unit-tests
- id: go-mod-tidy
# - id: go-cyclo
# args: [-over=37] # we must drop it down to 15
# - id: golangci-lint
# - id: go-critic
# - id: go-unit-tests
- id: go-mod-tidy
#- repo: https://github.com/adrienverge/yamllint.git
# rev: v1.26.3
# hooks:
# - id: yamllint
# args: [-c=.yamllint]
7 changes: 7 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends: default

rules:
# 80 chars should be enough, but don't fail if a line is longer
line-length:
max: 80
level: warning
21 changes: 11 additions & 10 deletions actions/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ import (
"github.com/jptosso/coraza-waf/v2/types"
)

//0 nothing, 1 phase, 2 request
// 0 nothing, 1 phase, 2 request
type allowFn struct {
allow int
}

func (a *allowFn) Init(r *coraza.Rule, b1 string) error {
if b1 == "phase" {
a.allow = 2 //skip current phase
} else if b1 == "request" {
a.allow = 3 //skip phases until RESPONSE_HEADERS
} else if b1 == "" {
switch b1 {
case "phase":
a.allow = 2 // skip current phase
case "request":
a.allow = 3 // skip phases until RESPONSE_HEADERS
case "":
a.allow = 1 // skip all phases
} else {
return fmt.Errorf("invalid value for action allow")
default:
return fmt.Errorf("invalid argument %s for allow", b1)
}
return nil
}

func (a *allowFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
//TODO implement this:
// TODO implement this:
/*
if a.allow == 1 {
tx.RuleEngine = coraza.RULE_ENGINE_OFF
Expand All @@ -61,6 +62,6 @@ func allow() coraza.RuleAction {
}

var (
_ coraza.RuleAction = &allowFn{}
_ coraza.RuleAction = (*allowFn)(nil)
_ ruleActionWrapper = allow
)
2 changes: 1 addition & 1 deletion actions/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func auditlog() coraza.RuleAction {
}

var (
_ coraza.RuleAction = &auditlogFn{}
_ coraza.RuleAction = (*auditlogFn)(nil)
_ ruleActionWrapper = auditlog
)
20 changes: 10 additions & 10 deletions actions/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type ctlFunctionType int

const (
ctlRemoveTargetById ctlFunctionType = 0
ctlRemoveTargetByID ctlFunctionType = 0
ctlRemoveTargetByTag ctlFunctionType = 1
ctlRemoveTargetByMsg ctlFunctionType = 2
ctlAuditEngine ctlFunctionType = 3
Expand All @@ -38,7 +38,7 @@ const (
ctlRequestBodyAccess ctlFunctionType = 6
ctlRequestBodyLimit ctlFunctionType = 7
ctlRuleEngine ctlFunctionType = 8
ctlRuleRemoveById ctlFunctionType = 9
ctlRuleRemoveByID ctlFunctionType = 9
ctlRuleRemoveByMsg ctlFunctionType = 10
ctlRuleRemoveByTag ctlFunctionType = 11
ctlHashEngine ctlFunctionType = 12
Expand All @@ -64,7 +64,7 @@ func (a *ctlFn) Init(r *coraza.Rule, data string) error {

func (a *ctlFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
switch a.action {
case ctlRemoveTargetById:
case ctlRemoveTargetByID:
id, _ := strconv.Atoi(a.value)
tx.RemoveRuleTargetById(id, a.collection, a.colKey)
case ctlRemoveTargetByTag:
Expand All @@ -89,7 +89,7 @@ func (a *ctlFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
}
tx.AuditEngine = ae
case ctlAuditLogParts:
//TODO lets switch it to a string
// TODO lets switch it to a string
tx.AuditLogParts = []rune(a.value)
case ctlForceRequestBodyVar:
val := strings.ToLower(a.value)
Expand All @@ -110,7 +110,7 @@ func (a *ctlFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
tx.Waf.Logger.Error(err.Error())
}
tx.RuleEngine = re
case ctlRuleRemoveById:
case ctlRuleRemoveByID:
id, _ := strconv.Atoi(a.value)
tx.RemoveRuleById(id)
case ctlRuleRemoveByMsg:
Expand All @@ -134,10 +134,10 @@ func (a *ctlFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
case ctlHashEnforcement:
// Not supported yet
case ctlDebugLogLevel:
//lvl, _ := strconv.Atoi(a.Value)
// lvl, _ := strconv.Atoi(a.Value)
// TODO
// We cannot update the log level, it would affect the whole waf instance...
//tx.Waf.SetLogLevel(lvl)
// tx.Waf.SetLogLevel(lvl)
}

}
Expand All @@ -149,7 +149,7 @@ func (a *ctlFn) Type() types.RuleActionType {
func parseCtl(data string) (ctlFunctionType, string, variables.RuleVariable, string, error) {
spl1 := strings.SplitN(data, "=", 2)
if len(spl1) != 2 {
return ctlRemoveTargetById, "", 0, "", fmt.Errorf("invalid syntax")
return ctlRemoveTargetByID, "", 0, "", fmt.Errorf("invalid syntax")
}
spl2 := strings.SplitN(spl1[1], ";", 2)
action := spl1[0]
Expand Down Expand Up @@ -188,13 +188,13 @@ func parseCtl(data string) (ctlFunctionType, string, variables.RuleVariable, str
case "ruleEngine":
act = ctlRuleEngine
case "ruleRemoveById":
act = ctlRuleRemoveById
act = ctlRuleRemoveByID
case "ruleRemoveByMsg":
act = ctlRuleRemoveByMsg
case "ruleRemoveByTag":
act = ctlRuleRemoveByTag
case "ruleRemoveTargetById":
act = ctlRemoveTargetById
act = ctlRemoveTargetByID
case "ruleRemoveTargetByMsg":
act = ctlRemoveTargetByMsg
case "ruleRemoveTargetByTag":
Expand Down
2 changes: 1 addition & 1 deletion actions/initcol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a *initcolFn) Init(r *coraza.Rule, data string) error {
}

func (a *initcolFn) Evaluate(r *coraza.Rule, tx *coraza.Transaction) {
//tx.Waf.Logger.Error("initcol was used but it's not supported", zap.Int("rule", r.Id))
// tx.Waf.Logger.Error("initcol was used but it's not supported", zap.Int("rule", r.Id))
/*
key := tx.MacroExpansion(a.key)
data := tx.Waf.Persistence.Get(a.variable, key)
Expand Down
11 changes: 6 additions & 5 deletions actions/setvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (a *setvarFn) Type() types.RuleActionType {
func (a *setvarFn) evaluateTxCollection(r *coraza.Rule, tx *coraza.Transaction, key string, value string) {
collection := tx.GetCollection(a.collection)
if collection == nil {
//fmt.Println("Invalid Collection " + a.Collection) LOG error?
// fmt.Println("Invalid Collection " + a.Collection) LOG error?
return
}

Expand All @@ -84,23 +84,24 @@ func (a *setvarFn) evaluateTxCollection(r *coraza.Rule, tx *coraza.Transaction,
collection.Set(tx.MacroExpansion(a.key), []string{"0"})
res = []string{"0"}
}
if len(a.value) == 0 {
switch {
case len(a.value) == 0:
collection.Set(tx.MacroExpansion(a.key), []string{""})
} else if a.value[0] == '+' {
case a.value[0] == '+':
me, _ := strconv.Atoi(tx.MacroExpansion(a.value[1:]))
txv, err := strconv.Atoi(res[0])
if err != nil {
return
}
collection.Set(tx.MacroExpansion(a.key), []string{strconv.Itoa(me + txv)})
} else if a.value[0] == '-' {
case a.value[0] == '-':
me, _ := strconv.Atoi(tx.MacroExpansion(a.value[1:]))
txv, err := strconv.Atoi(res[0])
if err != nil {
return
}
collection.Set(tx.MacroExpansion(a.key), []string{strconv.Itoa(txv - me)})
} else {
default:
collection.Set(tx.MacroExpansion(a.key), []string{tx.MacroExpansion(a.value)})
}
}
Expand Down
2 changes: 1 addition & 1 deletion actions/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSeverity(t *testing.T) {
{"NOTICE", 5},
{"INFO", 6},
{"DEBUG", 7},
//numeric input
// numeric input
{"0", 0},
{"1", 1},
{"2", 2},
Expand Down
4 changes: 2 additions & 2 deletions actions/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/jptosso/coraza-waf/v2/types"
)

var HTTP_STATUSES = []int{100, 101, 102, 103, 200,
var htpStatuses = []int{100, 101, 102, 103, 200,
201, 202, 203, 200, 204, 205, 206, 207, 208,
226, 300, 301, 302, 303, 304, 305, 306, 307,
302, 308, 301, 400, 401, 402, 403, 404, 405,
Expand All @@ -40,7 +40,7 @@ func (a *statusFn) Init(r *coraza.Rule, b1 string) error {
if err != nil {
return err
}
for _, s := range HTTP_STATUSES {
for _, s := range htpStatuses {
if status == s {
a.status = status
return nil
Expand Down
2 changes: 1 addition & 1 deletion actions/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (a *tFn) Init(r *coraza.Rule, input string) error {
// TODO there is a chance that it won't work, it requires tests
// none is a special hardcoded transformation, it must remove previous transformations
if input == "none" {
//remove elements
// remove elements
r.ClearTransformations()
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions body_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// BodyReader is used to read RequestBody and ResponseBody objects
// It will handle memory usage for buffering and processing
type bodyBuffer struct {
io.Writer //OK?
io.Writer // OK?
tmpDir string
buffer *bytes.Buffer
writer *os.File
Expand All @@ -42,7 +42,9 @@ func (br *bodyBuffer) Write(data []byte) (n int, err error) {
return 0, err
}
// we dump the previous buffer
br.writer.Write(br.buffer.Bytes())
if _, err := br.writer.Write(br.buffer.Bytes()); err != nil {
return 0, err
}
defer br.buffer.Reset()
}
br.length = l
Expand Down
26 changes: 19 additions & 7 deletions body_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import (

func TestBodyReaderMemory(t *testing.T) {
br := NewBodyReader("/tmp", 500)
br.Write([]byte("test"))
if _, err := br.Write([]byte("test")); err != nil {
t.Error(err)
}
buf := new(strings.Builder)
io.Copy(buf, br.Reader())
if _, err := io.Copy(buf, br.Reader()); err != nil {
t.Error(err)
}
if buf.String() != "test" {
t.Error("Failed to get BodyReader from memory")
}
Expand All @@ -35,14 +39,18 @@ func TestBodyReaderMemory(t *testing.T) {
func TestBodyReaderFile(t *testing.T) {
// body reader memory limit is 1 byte
br := NewBodyReader("/tmp", 1)
br.Write([]byte("test"))
if _, err := br.Write([]byte("test")); err != nil {
t.Error(err)
}
buf := new(strings.Builder)
io.Copy(buf, br.Reader())
if _, err := io.Copy(buf, br.Reader()); err != nil {
t.Error(err)
}
if buf.String() != "test" {
t.Error("Failed to get BodyReader from file")
}
// Let's check if files are being deleted
f := br.Reader().(*os.File)
f := (br.Reader()).(*os.File)
if _, err := os.Stat(f.Name()); os.IsNotExist(err) {
t.Error("BodyReader's Tmp file does not exist")
}
Expand All @@ -55,9 +63,13 @@ func TestBodyReaderFile(t *testing.T) {
func TestBodyReaderWriteFromReader(t *testing.T) {
br := NewBodyReader("/tmp", 5)
b := strings.NewReader("test")
io.Copy(br, b)
if _, err := io.Copy(br, b); err != nil {
t.Error(err)
}
buf := new(strings.Builder)
io.Copy(buf, br.Reader())
if _, err := io.Copy(buf, br.Reader()); err != nil {
t.Error(err)
}
if buf.String() != "test" {
t.Error("Failed to write bodyreader from io.Reader")
}
Expand Down
12 changes: 6 additions & 6 deletions bodyprocessors/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (js *jsonBodyProcessor) Find(expr string) (map[string][]string, error) {
}

func (js *jsonBodyProcessor) VariableHook() variables.RuleVariable {
return variables.Json
return variables.JSON
}

// Transform JSON to a map[string]string
Expand Down Expand Up @@ -108,12 +108,12 @@ func interfaceToMap(data map[string]interface{}) (map[string]string, error) {
}
// we set the parent key to count the number of items
result[key] = strconv.Itoa(len(m))
if m2, err := interfaceToMap(m); err != nil {
m2, err := interfaceToMap(m)
if err != nil {
return nil, err
} else {
for key2, value2 := range m2 {
result[key+"."+key2] = value2
}
}
for key2, value2 := range m2 {
result[key+"."+key2] = value2
}
case string:
result[key] = value.(string)
Expand Down
2 changes: 1 addition & 1 deletion bodyprocessors/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestJSONToMap(t *testing.T) {
if err != nil {
t.Error(err)
}
//fmt.Println(jsonMap)
// fmt.Println(jsonMap)
for k, v := range asserts {
if jsonMap[k] != v {
t.Errorf("Expected %s=%s", k, v)
Expand Down
Loading

0 comments on commit bd5e185

Please sign in to comment.