Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds owasp rules to vacuum #285

Merged
merged 198 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
198 commits
Select commit Hold shift + click to select a range
1e093ed
save
rica-graca Jun 1, 2023
241a840
asd
rica-graca Jun 1, 2023
28855e7
Revert "save"
rica-graca Jun 1, 2023
f54fe26
save
rica-graca Jun 1, 2023
c9be289
save
rica-graca Jun 1, 2023
d44aad5
Adds first rule oo owasp
rica-graca Jun 1, 2023
1cd6134
remove
rica-graca Jun 1, 2023
031c62f
remove launch
rica-graca Jun 1, 2023
01f2701
adds seconds rule
rica-graca Jun 1, 2023
3684783
rename
rica-graca Jun 1, 2023
a87be23
rename description
rica-graca Jun 1, 2023
0d44b75
resets files
rica-graca Jun 1, 2023
4c1743a
adds new owasp rule
rica-graca Jun 1, 2023
4a7335c
adds another rule
rica-graca Jun 1, 2023
9e4e6d0
Adds: Authentication scheme is considered outdated or insecure:
rica-graca Jun 1, 2023
337211b
remove
rica-graca Jun 1, 2023
1e4a49c
save
rica-graca Jun 1, 2023
0a7ea5c
able to read an array of strings from given
rica-graca Jun 1, 2023
aa5d8a3
move tests
rica-graca Jun 1, 2023
f838438
revert
rica-graca Jun 1, 2023
8517842
revert
rica-graca Jun 1, 2023
278e489
added a new rule, but it is not working as expected
rica-graca Jun 2, 2023
c947428
finishes GetOWASPRuleDefineErrorValidation
rica-graca Jun 4, 2023
216e8d5
revert
rica-graca Jun 4, 2023
6578b90
Revert bin
rica-graca Jun 4, 2023
0a27d7b
small fix
rica-graca Jun 4, 2023
25d2e98
small fix
rica-graca Jun 4, 2023
9e52ff7
added GetOWASPRuleRateLimitRetryAfter
rica-graca Jun 4, 2023
89cc6a8
save
rica-graca Jun 5, 2023
fa2d8c5
adds new rule GetOWASPRuleDefineErrorResponses401
rica-graca Jun 5, 2023
9147ad2
Adds another rule GetOWASPRuleDefineErrorResponses500
rica-graca Jun 5, 2023
5b8d5ac
Adds function for GetOWASPRuleRateLimit rule
rica-graca Jun 5, 2023
f64392a
done
rica-graca Jun 5, 2023
0917919
add new rule
rica-graca Jun 5, 2023
70561af
added GetOWASPRuleNoAdditionalProperties rule
rica-graca Jun 6, 2023
b9c1e62
fixed author
Ricagraca Jun 7, 2023
44989be
Adds README document to know what rules are implemented and what are …
rica-graca Jun 7, 2023
b4d1fce
Adds README document to know what rules are implemented and what are …
Ricagraca Jun 7, 2023
f8277de
Merge branch 'rg/owasp' of https://github.com/Ricagraca/vacuum into r…
Ricagraca Jun 7, 2023
080f228
Revert "fixed author"
Ricagraca Jun 7, 2023
18ec699
Merge branch 'main' into rg/owasp
Ricagraca Jun 7, 2023
af39cc0
merge
Ricagraca Jun 7, 2023
87b3435
Adds a table with the current OWASP rules
Ricagraca Jun 7, 2023
36843cb
Remove changes to falsy
Ricagraca Jun 8, 2023
eb69fea
change name of file
Ricagraca Jun 8, 2023
271da03
fixes rule define error validation
Ricagraca Jun 8, 2023
8749383
updates table
Ricagraca Jun 8, 2023
7920d4d
updates table
Ricagraca Jun 8, 2023
105f3be
fix table
Ricagraca Jun 8, 2023
36a7ed0
fix table
Ricagraca Jun 8, 2023
8e7e9ba
adds constrained additional properties rule
Ricagraca Jun 10, 2023
7ad24ca
fix readme
Ricagraca Jun 10, 2023
86c2806
implements new OWASPRuleStringLimit rule
Ricagraca Jun 10, 2023
c24a365
implements new rule
Ricagraca Jun 10, 2023
2c6c8df
Update table
Ricagraca Jun 10, 2023
6bc26ef
adds rule string restricted
Ricagraca Jun 10, 2023
5482a28
fix table
Ricagraca Jun 10, 2023
2578566
puts each test in its module
Ricagraca Jun 11, 2023
689a4df
fixes all tests and adds string restricted
Ricagraca Jun 11, 2023
fc3390e
add integer legacy rule
Ricagraca Jun 11, 2023
c017203
add integer format rule
Ricagraca Jun 11, 2023
33f00d8
fix
Ricagraca Jun 11, 2023
e915bb0
added hosts https oas rules
Ricagraca Jun 11, 2023
c00fed1
save
Ricagraca Jun 11, 2023
3a4c2e4
adds new rule, next implement function
Ricagraca Jun 11, 2023
5393720
adds checkSecurity function
Ricagraca Jun 12, 2023
6de22c4
Fixes tests
rica-graca Jun 12, 2023
454e1c5
finishes all rules
rica-graca Jun 12, 2023
20018e1
small fixes
rica-graca Jun 12, 2023
636ff33
Adds owasp as a rule set
rica-graca Jun 14, 2023
c950590
save
Ricagraca Jun 14, 2023
115994c
fixing ratelimit function
Ricagraca Jun 16, 2023
68ad4d7
update the test naming to follow conventions
rica-graca Jun 17, 2023
0a46145
Merge branch 'rg/owasp' of https://github.com/Ricagraca/vacuum into r…
rica-graca Jun 17, 2023
ca8bac6
Adds tests and removes TODOS
rica-graca Jun 17, 2023
e97ffe5
Add unused field
rica-graca Jun 17, 2023
445c794
Fix conflicts
rica-graca Jun 26, 2023
b9eb0f5
Fixes conflicts
rica-graca Jun 26, 2023
7a2f2c5
Adds owasp rule to extends
rica-graca Jun 27, 2023
bf3f021
Adds test case
rica-graca Jun 27, 2023
b1bdd8e
Small fixes
rica-graca Jun 27, 2023
c94e7e8
Fix last small issues
rica-graca Jun 27, 2023
04434ee
fixed comment
rica-graca Jun 27, 2023
8eb8a5a
Fixes name of function
rica-graca Jun 27, 2023
1409bd6
Generalizes owaspRateLimitDefinition to account for any header set as…
rica-graca Jun 30, 2023
80677b1
Fixes test names
rica-graca Jun 30, 2023
b412c8f
Adds owasp to vacuum namespace
rica-graca Jun 30, 2023
ec7d563
Adds an how to fix for each of the owasp rules and also fixes the fal…
rica-graca Jul 2, 2023
013d47d
Adds a comment to each function that uses the if/then/else to know th…
rica-graca Jul 2, 2023
7354a9e
Extends falsy test to evaluate a non empty object to an error if found
rica-graca Jul 2, 2023
f8b584c
Merge branch 'main' into rg/owasp
Ricagraca Jul 3, 2023
bedd6d0
asd
rica-graca Jun 1, 2023
eb4d592
save
rica-graca Jun 1, 2023
34871d6
save
rica-graca Jun 1, 2023
41861f5
Adds first rule oo owasp
rica-graca Jun 1, 2023
47ce730
remove
rica-graca Jun 1, 2023
d277768
remove launch
rica-graca Jun 1, 2023
182653f
adds seconds rule
rica-graca Jun 1, 2023
75b9c09
rename
rica-graca Jun 1, 2023
97ed8e2
rename description
rica-graca Jun 1, 2023
b0edcb5
resets files
rica-graca Jun 1, 2023
84a990d
adds new owasp rule
rica-graca Jun 1, 2023
6a42d69
adds another rule
rica-graca Jun 1, 2023
a5682c7
Adds: Authentication scheme is considered outdated or insecure:
rica-graca Jun 1, 2023
38b6a7d
remove
rica-graca Jun 1, 2023
e4dd8d8
save
rica-graca Jun 1, 2023
51aaf4e
able to read an array of strings from given
rica-graca Jun 1, 2023
3bb6d53
move tests
rica-graca Jun 1, 2023
d4870f9
revert
rica-graca Jun 1, 2023
2a3d511
revert
rica-graca Jun 1, 2023
43d31d4
added a new rule, but it is not working as expected
rica-graca Jun 2, 2023
bddf0f3
finishes GetOWASPRuleDefineErrorValidation
rica-graca Jun 4, 2023
425cc4b
revert
rica-graca Jun 4, 2023
9d475a8
Revert bin
rica-graca Jun 4, 2023
a787858
small fix
rica-graca Jun 4, 2023
9eb2dce
small fix
rica-graca Jun 4, 2023
b141688
added GetOWASPRuleRateLimitRetryAfter
rica-graca Jun 4, 2023
44f463a
save
rica-graca Jun 5, 2023
99cefa6
adds new rule GetOWASPRuleDefineErrorResponses401
rica-graca Jun 5, 2023
dd48ef1
Adds another rule GetOWASPRuleDefineErrorResponses500
rica-graca Jun 5, 2023
4e27577
Adds function for GetOWASPRuleRateLimit rule
rica-graca Jun 5, 2023
ea225c2
done
rica-graca Jun 5, 2023
35b9090
add new rule
rica-graca Jun 5, 2023
4a4b259
added GetOWASPRuleNoAdditionalProperties rule
rica-graca Jun 6, 2023
5fb3da2
fixed author
Ricagraca Jun 7, 2023
ac69602
Adds README document to know what rules are implemented and what are …
Ricagraca Jun 7, 2023
a3694cb
Revert "fixed author"
Ricagraca Jun 7, 2023
e7b9b90
save
rica-graca Jun 1, 2023
baa7381
Revert "save"
rica-graca Jun 1, 2023
0ef707f
Fixes falsy
rica-graca Jun 5, 2023
fcc9de3
fix indent issues
rica-graca Jun 5, 2023
6b38056
fix
rica-graca Jun 5, 2023
2afb73e
fix
rica-graca Jun 5, 2023
9538eb1
fix
rica-graca Jun 5, 2023
2fa6968
fix
rica-graca Jun 5, 2023
dd25e88
fix
rica-graca Jun 5, 2023
444464d
fix
rica-graca Jun 5, 2023
97e06f7
fix description
rica-graca Jun 5, 2023
4307ddb
last fix
rica-graca Jun 5, 2023
7133058
quick fix
rica-graca Jun 6, 2023
a58bfa1
quick fix
rica-graca Jun 6, 2023
57cb9f6
Adds a table with the current OWASP rules
Ricagraca Jun 7, 2023
6331402
change name of file
Ricagraca Jun 8, 2023
d8cd7ae
fixes rule define error validation
Ricagraca Jun 8, 2023
a46a5e0
updates table
Ricagraca Jun 8, 2023
5e79aa1
updates table
Ricagraca Jun 8, 2023
df69b9d
fix table
Ricagraca Jun 8, 2023
3b686aa
fix table
Ricagraca Jun 8, 2023
c6da6ae
adds constrained additional properties rule
Ricagraca Jun 10, 2023
08799f9
fix readme
Ricagraca Jun 10, 2023
6c0d7e6
implements new OWASPRuleStringLimit rule
Ricagraca Jun 10, 2023
619e59e
implements new rule
Ricagraca Jun 10, 2023
466a1d2
Update table
Ricagraca Jun 10, 2023
0eace6c
adds rule string restricted
Ricagraca Jun 10, 2023
a40c8d5
fix table
Ricagraca Jun 10, 2023
cf4af21
puts each test in its module
Ricagraca Jun 11, 2023
50735b1
fixes all tests and adds string restricted
Ricagraca Jun 11, 2023
e541479
add integer legacy rule
Ricagraca Jun 11, 2023
dc8be19
add integer format rule
Ricagraca Jun 11, 2023
2d68295
fix
Ricagraca Jun 11, 2023
b8f4cfd
added hosts https oas rules
Ricagraca Jun 11, 2023
3c35b28
save
Ricagraca Jun 11, 2023
aba0614
adds new rule, next implement function
Ricagraca Jun 11, 2023
6ea75ff
adds checkSecurity function
Ricagraca Jun 12, 2023
f098f00
Fixes tests
rica-graca Jun 12, 2023
7bdc831
finishes all rules
rica-graca Jun 12, 2023
71974b5
small fixes
rica-graca Jun 12, 2023
3448978
Adds owasp as a rule set
rica-graca Jun 14, 2023
2dfbf40
save
Ricagraca Jun 14, 2023
4d38517
update the test naming to follow conventions
rica-graca Jun 17, 2023
8c92c50
fixing ratelimit function
Ricagraca Jun 16, 2023
1c94211
Adds tests and removes TODOS
rica-graca Jun 17, 2023
b35d053
Add unused field
rica-graca Jun 17, 2023
53909fe
Updated license and install script to retry on failure
daveshanley Jun 17, 2023
aa4c616
Updated pattern function rendering to be more useful #276
daveshanley Jun 17, 2023
74a8495
Fixed dashboard bug with empty vilolations scrolling #292
daveshanley Jun 17, 2023
d1c8cf1
Period in path templates now supported #282
daveshanley Jun 17, 2023
95967ad
Ran `go fmt` across all files to reset whitespace.
daveshanley Jun 17, 2023
ceaef8f
Allow for indexing post resolving #294
daveshanley Jun 17, 2023
27a92a6
Fixed a small path issue
daveshanley Jun 17, 2023
1e1dcb4
Updated dependencies to latest.
daveshanley Jun 17, 2023
f3bbf2b
Schema was not being constructed properly #281
daveshanley Jun 19, 2023
2c40121
Adds owasp rule to extends
rica-graca Jun 27, 2023
16d532c
Adds test case
rica-graca Jun 27, 2023
6a4900d
Small fixes
rica-graca Jun 27, 2023
548949a
Fix last small issues
rica-graca Jun 27, 2023
e7729da
fixed comment
rica-graca Jun 27, 2023
aa9d0f9
Fixes name of function
rica-graca Jun 27, 2023
0c6cfd9
Generalizes owaspRateLimitDefinition to account for any header set as…
rica-graca Jun 30, 2023
1552883
Fixes test names
rica-graca Jun 30, 2023
9440851
Adds owasp to vacuum namespace
rica-graca Jun 30, 2023
9c81d59
Adds an how to fix for each of the owasp rules and also fixes the fal…
rica-graca Jul 2, 2023
97054b0
Adds a comment to each function that uses the if/then/else to know th…
rica-graca Jul 2, 2023
22dda2d
Extends falsy test to evaluate a non empty object to an error if found
rica-graca Jul 2, 2023
aad430a
use RuleSetExecution.Document in ApplyRulesToRuleSet
FedeBev Jun 23, 2023
9605b26
use RuleSetExecution.Document in ApplyRulesToRuleSet
FedeBev Jun 23, 2023
e2cebcd
Fixes conflicts
rica-graca Jul 5, 2023
66cb47e
Fixes some issues
rica-graca Jul 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion functions/core/falsy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core

import (
"fmt"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (f Falsy) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []

fieldNode, fieldNodeValue := utils.FindKeyNode(context.RuleAction.Field, node.Content)
if (fieldNode != nil && fieldNodeValue != nil) &&
(fieldNodeValue.Value != "" && fieldNodeValue.Value != "false" && fieldNodeValue.Value != "0") {
(fieldNodeValue.Value != "" && fieldNodeValue.Value != "false" && fieldNodeValue.Value != "0" || (fieldNodeValue.Value == "" && fieldNodeValue.Content != nil)) {
results = append(results, model.RuleFunctionResult{
Message: fmt.Sprintf("%s: '%s' must be falsy", context.Rule.Description, context.RuleAction.Field),
StartNode: node,
Expand Down
18 changes: 12 additions & 6 deletions functions/core/falsy_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package core

import (
"testing"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestFalsy_RunRule_Fail(t *testing.T) {
Expand All @@ -16,12 +17,15 @@ tags:
- name: "non-falsy tag 2"
description: 1
- name: "non-falsy tag 3"
description: "hello"`
description: "hello"
- name: "non-falsy tag 4"
description:
hello: goodbye`

path := "$.tags[*]"

nodes, _ := utils.FindNodes([]byte(sampleYaml), path)
assert.Len(t, nodes, 3)
assert.Len(t, nodes, 4)

rule := buildCoreTestRule(path, model.SeverityError, "falsy", "description", nil)
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), nil)
Expand All @@ -31,7 +35,7 @@ tags:
tru := Falsy{}
res := tru.RunRule(nodes, ctx)

assert.Len(t, res, 3)
assert.Len(t, res, 4)
}

func TestFalsy_RunRule_Fail_NoNodes(t *testing.T) {
Expand Down Expand Up @@ -72,12 +76,14 @@ tags:
- name: "falsy tag 3"
description: ""
- name: "falsy Tag 4"
description: "0"`
description: "0"
- name: "falsy Tag 5"
description:`

path := "$.tags[*]"

nodes, _ := utils.FindNodes([]byte(sampleYaml), path)
assert.Len(t, nodes, 4)
assert.Len(t, nodes, 5)

rule := buildCoreTestRule(path, model.SeverityError, "Falsy", "description", nil)
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), nil)
Expand Down
8 changes: 8 additions & 0 deletions functions/core/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core

import (
"fmt"

"github.com/daveshanley/vacuum/model"
"github.com/daveshanley/vacuum/parser"
validationErrors "github.com/pb33f/libopenapi-validator/errors"
Expand Down Expand Up @@ -81,6 +82,13 @@ func (sch Schema) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
schema = highBase.NewSchema(&lowSchema)
}

// use the current node to validate (field not needed)
forceValidationOnCurrentNode := utils.ExtractValueFromInterfaceMap("forceValidationOnCurrentNode", context.Options)
if _, ok := forceValidationOnCurrentNode.(bool); ok && len(nodes) > 0 {
results = append(results, validateNodeAgainstSchema(schema, nodes[0], context, 0)...)
return results
}

for x, node := range nodes {
if x%2 == 0 && len(nodes) > 1 {
continue
Expand Down
9 changes: 8 additions & 1 deletion functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package functions

import (
"sync"

"github.com/daveshanley/vacuum/functions/core"
openapi_functions "github.com/daveshanley/vacuum/functions/openapi"
"github.com/daveshanley/vacuum/functions/owasp"
"github.com/daveshanley/vacuum/model"
"github.com/daveshanley/vacuum/plugin"
"sync"
)

type customFunction struct {
Expand Down Expand Up @@ -92,6 +94,11 @@ func MapBuiltinFunctions() Functions {
funcs["pathsKebabCase"] = openapi_functions.PathsKebabCase{}
funcs["oasOpErrorResponse"] = openapi_functions.Operation4xResponse{}

// add owasp functions used by the owasp rules
funcs["owaspHeaderDefinition"] = owasp.HeaderDefinition{}
funcs["owaspDefineErrorDefinition"] = owasp.DefineErrorDefinition{}
funcs["owaspCheckSecurity"] = owasp.CheckSecurity{}

})

return functionsSingleton
Expand Down
5 changes: 3 additions & 2 deletions functions/functions_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package functions

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMapBuiltinFunctions(t *testing.T) {
funcs := MapBuiltinFunctions()
assert.Len(t, funcs.GetAllFunctions(), 42)
assert.Len(t, funcs.GetAllFunctions(), 45)
}
115 changes: 115 additions & 0 deletions functions/owasp/check_security.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package owasp

import (
"fmt"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)

type CheckSecurity struct {
}

// GetSchema returns a model.RuleFunctionSchema defining the schema of the CheckSecurity rule.
func (cd CheckSecurity) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{Name: "check_security"}
}

// RunRule will execute the CheckSecurity rule, based on supplied context and a supplied []*yaml.Node slice.
func (cd CheckSecurity) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
if len(nodes) <= 0 {
return nil
}

var nullable bool
nullableMap := utils.ExtractValueFromInterfaceMap("nullable", context.Options)
if castedNullable, ok := nullableMap.(bool); ok {
nullable = castedNullable
}

var methods []string
methodsMap := utils.ExtractValueFromInterfaceMap("methods", context.Options)
if castedMethods, ok := methodsMap.([]string); ok {
methods = castedMethods
}

// security at the global level replaces if not defined at the operation level
_, valueOfSecurityGlobalNode := utils.FindFirstKeyNode("security", nodes, 0)

var results []model.RuleFunctionResult
_, valueOfPathNode := utils.FindFirstKeyNode("paths", nodes, 0)
if valueOfPathNode == nil {
return nil
}

for i := 1; i < len(valueOfPathNode.Content); i += 2 {
for j := 0; j < len(valueOfPathNode.Content[i].Content); j += 2 {
if slices.Contains([]string{
"get",
"head",
"post",
"put",
"patch",
"delete",
"options",
"trace",
}, valueOfPathNode.Content[i].Content[j].Value) && slices.Contains(methods, valueOfPathNode.Content[i].Content[j].Value) && len(valueOfPathNode.Content[i].Content) > j+1 {
operation := valueOfPathNode.Content[i].Content[j+1]
results = append(results, checkSecurityRule(operation, valueOfSecurityGlobalNode, nullable, valueOfPathNode.Content[i-1].Value, valueOfPathNode.Content[i].Content[j].Value, context)...)
}
}
}

return results
}

func checkSecurityRule(operation *yaml.Node, valueOfSecurityGlobalNode *yaml.Node, nullable bool, pathPrefix, method string, context model.RuleFunctionContext) []model.RuleFunctionResult {
_, valueOfSecurityNode := utils.FindFirstKeyNode("security", operation.Content, 0)
if valueOfSecurityNode == nil { // if not defined at the operation level, use global
valueOfSecurityNode = valueOfSecurityGlobalNode
}
if valueOfSecurityNode == nil {
return []model.RuleFunctionResult{
{
Message: fmt.Sprintf("security' was not defined: for path %q in method %q.", pathPrefix, method),
StartNode: operation,
EndNode: operation,
Path: fmt.Sprintf("$.paths.%s.%s", pathPrefix, method),
Rule: context.Rule,
},
}
}
if len(valueOfSecurityNode.Content) == 0 {
return []model.RuleFunctionResult{
{
Message: fmt.Sprintf("'security' is empty: for path %q in method %q.", pathPrefix, method),
StartNode: valueOfSecurityNode,
EndNode: valueOfSecurityNode,
Path: fmt.Sprintf("$.paths.%s.%s.security", pathPrefix, method),
Rule: context.Rule,
},
}
}
if valueOfSecurityNode.Kind == yaml.SequenceNode {
var results []model.RuleFunctionResult
for k := 0; k < len(valueOfSecurityNode.Content); k++ {
if valueOfSecurityNode.Content[k].Kind != yaml.MappingNode {
continue
}
if len(valueOfSecurityNode.Content[k].Content) == 0 && !nullable {
results = append(results, model.RuleFunctionResult{
Message: fmt.Sprintf("'security' has null elements: for path %q in method %q with element.", pathPrefix, method),
StartNode: valueOfSecurityNode.Content[k],
EndNode: utils.FindLastChildNodeWithLevel(valueOfSecurityNode.Content[k], 0),
Path: fmt.Sprintf("$.paths.%s.%s.security", pathPrefix, method),
Rule: context.Rule,
})
}
}
return results
}

return nil
}
55 changes: 55 additions & 0 deletions functions/owasp/check_security_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package owasp

import (
"testing"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
)

func TestCheckSecurity_GetSchema(t *testing.T) {
def := CheckSecurity{}
assert.Equal(t, "check_security", def.GetSchema().Name)
}

func TestCheckSecurity_RunRule(t *testing.T) {
def := CheckSecurity{}
res := def.RunRule(nil, model.RuleFunctionContext{})
assert.Len(t, res, 0)
}

func TestCheckSecurity_SecurityMissing(t *testing.T) {

yml := `openapi: 3.0.1
info:
version: "1.2.3"
title: "securitySchemes"
paths:
/security-gloabl-ok-put:
put:
responses: {}
/security-ok-put:
put:
responses: {}
components:
securitySchemes:
BasicAuth:
type: http
scheme: basic`

path := "$"

nodes, _ := utils.FindNodes([]byte(yml), path)

rule := buildOpenApiTestRuleAction(path, "check_security", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), map[string]interface{}{
"methods": []string{"put"},
})

def := CheckSecurity{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 2)

}
45 changes: 45 additions & 0 deletions functions/owasp/define_error_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package owasp

import (
"fmt"
"strings"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)

type DefineErrorDefinition struct {
}

// GetSchema returns a model.RuleFunctionSchema defining the schema of the DefineError rule.
func (cd DefineErrorDefinition) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{Name: "define_error_definition"}
}

// RunRule will execute the DefineError rule, based on supplied context and a supplied []*yaml.Node slice.
func (cd DefineErrorDefinition) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {

if len(nodes) <= 0 {
return nil
}

var responseCode string
for i, node := range nodes[0].Content {
if i%2 == 0 {
responseCode = node.Value
} else if responseCode == "400" || responseCode == "422" || strings.ToUpper(responseCode) == "4XX" {
return []model.RuleFunctionResult{}
}
}

return []model.RuleFunctionResult{
{
Message: "Error '400', '422' or '4XX' was not defined",
StartNode: nodes[0],
EndNode: utils.FindLastChildNodeWithLevel(nodes[0], 0),
Path: fmt.Sprintf("%s", context.Given),
Rule: context.Rule,
},
}
}
Loading