gosec/rules/math_big_rat.go

46 lines
1.1 KiB
Go
Raw Normal View History

package rules
import (
"go/ast"
"github.com/securego/gosec/v2"
2023-02-15 19:44:13 +00:00
"github.com/securego/gosec/v2/issue"
)
type usingOldMathBig struct {
2023-02-15 19:44:13 +00:00
issue.MetaData
calls gosec.CallList
}
func (r *usingOldMathBig) ID() string {
return r.MetaData.ID
}
2023-02-15 19:44:13 +00:00
func (r *usingOldMathBig) Match(node ast.Node, ctx *gosec.Context) (gi *issue.Issue, err error) {
if callExpr := r.calls.ContainsPkgCallExpr(node, ctx, false); callExpr == nil {
return nil, nil
}
2023-02-15 19:44:13 +00:00
confidence := issue.Low
major, minor, build := gosec.GoVersion()
if major == 1 && (minor == 16 && build < 14 || minor == 17 && build < 7) {
2023-02-15 19:44:13 +00:00
confidence = issue.Medium
}
2023-02-15 19:44:13 +00:00
return ctx.NewIssue(node, r.ID(), r.What, r.Severity, confidence), nil
}
// NewUsingOldMathBig rule detects the use of Rat.SetString from math/big.
func NewUsingOldMathBig(id string, _ gosec.Config) (gosec.Rule, []ast.Node) {
calls := gosec.NewCallList()
calls.Add("math/big.Rat", "SetString")
return &usingOldMathBig{
calls: calls,
2023-02-15 19:44:13 +00:00
MetaData: issue.MetaData{
ID: id,
What: "Potential uncontrolled memory consumption in Rat.SetString (CVE-2022-23772)",
2023-02-15 19:44:13 +00:00
Severity: issue.High,
},
}, []ast.Node{(*ast.CallExpr)(nil)}
}