2019-09-20 09:46:06 +01:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
|
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"
|
2019-09-20 09:46:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type pprofCheck struct {
|
2023-02-15 19:44:13 +00:00
|
|
|
issue.MetaData
|
2019-09-20 09:46:06 +01:00
|
|
|
importPath string
|
|
|
|
importName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the ID of the check
|
|
|
|
func (p *pprofCheck) ID() string {
|
|
|
|
return p.MetaData.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match checks for pprof imports
|
2023-02-15 19:44:13 +00:00
|
|
|
func (p *pprofCheck) Match(n ast.Node, c *gosec.Context) (*issue.Issue, error) {
|
2019-09-20 09:46:06 +01:00
|
|
|
if node, ok := n.(*ast.ImportSpec); ok {
|
2019-09-24 09:10:00 +01:00
|
|
|
if p.importPath == unquote(node.Path.Value) && node.Name != nil && p.importName == node.Name.Name {
|
2023-02-15 19:44:13 +00:00
|
|
|
return c.NewIssue(node, p.ID(), p.What, p.Severity, p.Confidence), nil
|
2019-09-20 09:46:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPprofCheck detects when the profiling endpoint is automatically exposed
|
|
|
|
func NewPprofCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
|
|
|
return &pprofCheck{
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2019-09-20 09:46:06 +01:00
|
|
|
ID: id,
|
2023-02-15 19:44:13 +00:00
|
|
|
Severity: issue.High,
|
|
|
|
Confidence: issue.High,
|
2019-09-20 09:46:06 +01:00
|
|
|
What: "Profiling endpoint is automatically exposed on /debug/pprof",
|
|
|
|
},
|
|
|
|
importPath: "net/http/pprof",
|
|
|
|
importName: "_",
|
|
|
|
}, []ast.Node{(*ast.ImportSpec)(nil)}
|
|
|
|
}
|