2018-07-19 17:42:25 +01:00
|
|
|
package gosec_test
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
|
2022-01-03 17:11:35 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2017-07-19 22:17:00 +01:00
|
|
|
. "github.com/onsi/gomega"
|
2023-03-30 08:31:24 +01:00
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2023-02-15 19:44:13 +00:00
|
|
|
"github.com/securego/gosec/v2/issue"
|
2017-07-19 22:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockrule struct {
|
2023-02-15 19:44:13 +00:00
|
|
|
issue *issue.Issue
|
2017-07-19 22:17:00 +01:00
|
|
|
err error
|
2018-07-19 17:42:25 +01:00
|
|
|
callback func(n ast.Node, ctx *gosec.Context) bool
|
2017-07-19 22:17:00 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (m *mockrule) ID() string {
|
|
|
|
return "MOCK"
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:44:13 +00:00
|
|
|
func (m *mockrule) Match(n ast.Node, ctx *gosec.Context) (*issue.Issue, error) {
|
2017-07-19 22:17:00 +01: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 17:42:25 +01:00
|
|
|
ruleset gosec.RuleSet
|
|
|
|
dummyErrorRule gosec.Rule
|
|
|
|
dummyIssueRule gosec.Rule
|
2017-07-19 22:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
JustBeforeEach(func() {
|
2018-07-19 17:42:25 +01:00
|
|
|
ruleset = gosec.NewRuleSet()
|
2017-07-19 22:17:00 +01:00
|
|
|
dummyErrorRule = &mockrule{
|
|
|
|
issue: nil,
|
|
|
|
err: fmt.Errorf("An unexpected error occurred"),
|
2018-07-19 17:42:25 +01:00
|
|
|
callback: func(n ast.Node, ctx *gosec.Context) bool { return false },
|
2017-07-19 22:17:00 +01:00
|
|
|
}
|
|
|
|
dummyIssueRule = &mockrule{
|
2023-02-15 19:44:13 +00:00
|
|
|
issue: &issue.Issue{
|
|
|
|
Severity: issue.High,
|
|
|
|
Confidence: issue.High,
|
2017-07-19 22:17:00 +01:00
|
|
|
What: `Some explanation of the thing`,
|
|
|
|
File: "main.go",
|
|
|
|
Code: `#include <stdio.h> int main(){ puts("hello world"); }`,
|
2017-09-16 01:12:27 +01:00
|
|
|
Line: "42",
|
2017-07-19 22:17:00 +01:00
|
|
|
},
|
|
|
|
err: nil,
|
2018-07-19 17:42:25 +01:00
|
|
|
callback: func(n ast.Node, ctx *gosec.Context) bool { return true },
|
2017-07-19 22:17:00 +01: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 10:53:36 +00:00
|
|
|
ruleset.Register(dummyIssueRule, false, registeredNodeA, registeredNodeB)
|
2017-07-19 22:17:00 +01:00
|
|
|
Expect(ruleset.RegisteredFor(unregisteredNode)).Should(BeEmpty())
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNodeA)).Should(ContainElement(dummyIssueRule))
|
|
|
|
Expect(ruleset.RegisteredFor(registeredNodeB)).Should(ContainElement(dummyIssueRule))
|
2021-12-09 10:53:36 +00:00
|
|
|
Expect(ruleset.IsRuleSuppressed(dummyIssueRule.ID())).Should(BeFalse())
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should not register a rule when no ast.Nodes are specified", func() {
|
2021-12-09 10:53:36 +00:00
|
|
|
ruleset.Register(dummyErrorRule, false)
|
|
|
|
Expect(ruleset.Rules).Should(BeEmpty())
|
2017-07-19 22:17:00 +01: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 10:53:36 +00:00
|
|
|
ruleset.Register(dummyErrorRule, false, registeredNode)
|
|
|
|
ruleset.Register(dummyIssueRule, false, registeredNode)
|
2017-07-19 22:17:00 +01: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 10:53:36 +00: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 22:17:00 +01:00
|
|
|
})
|
|
|
|
})
|