2018-07-19 18:42:25 +02:00
|
|
|
package gosec_test
|
2017-07-19 23:17:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
|
2022-01-03 19:11:35 +02:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2017-07-19 23:17:00 +02:00
|
|
|
. "github.com/onsi/gomega"
|
2020-04-01 22:18:39 +02:00
|
|
|
"github.com/securego/gosec/v2"
|
2017-07-19 23:17:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockrule struct {
|
2018-07-19 18:42:25 +02:00
|
|
|
issue *gosec.Issue
|
2017-07-19 23:17:00 +02:00
|
|
|
err error
|
2018-07-19 18:42:25 +02:00
|
|
|
callback func(n ast.Node, ctx *gosec.Context) bool
|
2017-07-19 23:17:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:32:03 +02:00
|
|
|
func (m *mockrule) ID() string {
|
|
|
|
return "MOCK"
|
|
|
|
}
|
|
|
|
|
2018-07-19 18:42:25 +02:00
|
|
|
func (m *mockrule) Match(n ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
|
2017-07-19 23:17:00 +02:00
|
|
|
if m.callback(n, ctx) {
|
|
|
|
return m.issue, nil
|
|
|
|
}
|
|
|
|
return nil, m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("Rule", func() {
|
|
|
|
Context("when using a ruleset", func() {
|
|
|
|
var (
|
2018-07-19 18:42:25 +02:00
|
|
|
ruleset gosec.RuleSet
|
|
|
|
dummyErrorRule gosec.Rule
|
|
|
|
dummyIssueRule gosec.Rule
|
2017-07-19 23:17:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
JustBeforeEach(func() {
|
2018-07-19 18:42:25 +02:00
|
|
|
ruleset = gosec.NewRuleSet()
|
2017-07-19 23:17:00 +02:00
|
|
|
dummyErrorRule = &mockrule{
|
|
|
|
issue: nil,
|
|
|
|
err: fmt.Errorf("An unexpected error occurred"),
|
2018-07-19 18:42:25 +02:00
|
|
|
callback: func(n ast.Node, ctx *gosec.Context) bool { return false },
|
2017-07-19 23:17:00 +02:00
|
|
|
}
|
|
|
|
dummyIssueRule = &mockrule{
|
2018-07-19 18:42:25 +02:00
|
|
|
issue: &gosec.Issue{
|
|
|
|
Severity: gosec.High,
|
|
|
|
Confidence: gosec.High,
|
2017-07-19 23:17:00 +02:00
|
|
|
What: `Some explanation of the thing`,
|
|
|
|
File: "main.go",
|
|
|
|
Code: `#include <stdio.h> int main(){ puts("hello world"); }`,
|
2017-09-16 02:12:27 +02:00
|
|
|
Line: "42",
|
2017-07-19 23:17:00 +02:00
|
|
|
},
|
|
|
|
err: nil,
|
2018-07-19 18:42:25 +02:00
|
|
|
callback: func(n ast.Node, ctx *gosec.Context) bool { return true },
|
2017-07-19 23:17:00 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
It("should be possible to register a rule for multiple ast.Node", func() {
|
|
|
|
registeredNodeA := (*ast.CallExpr)(nil)
|
|
|
|
registeredNodeB := (*ast.AssignStmt)(nil)
|
|
|
|
unregisteredNode := (*ast.BinaryExpr)(nil)
|
|
|
|
|
2021-12-09 12:53:36 +02:00
|
|
|
ruleset.Register(dummyIssueRule, false, registeredNodeA, registeredNodeB)
|
2017-07-19 23:17:00 +02:00
|
|
|
Expect(ruleset.RegisteredFor(unregisteredNode)).Should(BeEmpty())
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNodeA)).Should(ContainElement(dummyIssueRule))
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNodeB)).Should(ContainElement(dummyIssueRule))
|
2021-12-09 12:53:36 +02:00
|
|
|
Expect(ruleset.IsRuleSuppressed(dummyIssueRule.ID())).Should(BeFalse())
|
2017-07-19 23:17:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should not register a rule when no ast.Nodes are specified", func() {
|
2021-12-09 12:53:36 +02:00
|
|
|
ruleset.Register(dummyErrorRule, false)
|
|
|
|
Expect(ruleset.Rules).Should(BeEmpty())
|
2017-07-19 23:17:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should be possible to retrieve a list of rules for a given node type", func() {
|
|
|
|
registeredNode := (*ast.CallExpr)(nil)
|
|
|
|
unregisteredNode := (*ast.AssignStmt)(nil)
|
2021-12-09 12:53:36 +02:00
|
|
|
ruleset.Register(dummyErrorRule, false, registeredNode)
|
|
|
|
ruleset.Register(dummyIssueRule, false, registeredNode)
|
2017-07-19 23:17:00 +02:00
|
|
|
Expect(ruleset.RegisteredFor(unregisteredNode)).Should(BeEmpty())
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNode)).Should(HaveLen(2))
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNode)).Should(ContainElement(dummyErrorRule))
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNode)).Should(ContainElement(dummyIssueRule))
|
|
|
|
})
|
2021-12-09 12:53:36 +02:00
|
|
|
|
|
|
|
It("should register a suppressed rule", func() {
|
|
|
|
registeredNode := (*ast.CallExpr)(nil)
|
|
|
|
unregisteredNode := (*ast.AssignStmt)(nil)
|
|
|
|
ruleset.Register(dummyIssueRule, true, registeredNode)
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNode)).Should(ContainElement(dummyIssueRule))
|
|
|
|
Expect(ruleset.RegisteredFor(unregisteredNode)).Should(BeEmpty())
|
|
|
|
Expect(ruleset.IsRuleSuppressed(dummyIssueRule.ID())).Should(BeTrue())
|
|
|
|
})
|
2017-07-19 23:17:00 +02:00
|
|
|
})
|
|
|
|
})
|