gosec/rules/http_serve.go

40 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 httpServeWithoutTimeouts struct {
2023-02-15 19:44:13 +00:00
issue.MetaData
pkg string
calls []string
}
func (r *httpServeWithoutTimeouts) ID() string {
return r.MetaData.ID
}
2023-02-15 19:44:13 +00:00
func (r *httpServeWithoutTimeouts) Match(n ast.Node, c *gosec.Context) (gi *issue.Issue, err error) {
if _, matches := gosec.MatchCallByPackage(n, c, r.pkg, r.calls...); matches {
2023-02-15 19:44:13 +00:00
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
}
return nil, nil
}
// NewHTTPServeWithoutTimeouts detects use of net/http serve functions that have no support for setting timeouts.
func NewHTTPServeWithoutTimeouts(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
return &httpServeWithoutTimeouts{
pkg: "net/http",
calls: []string{"ListenAndServe", "ListenAndServeTLS", "Serve", "ServeTLS"},
2023-02-15 19:44:13 +00:00
MetaData: issue.MetaData{
ID: id,
What: "Use of net/http serve function that has no support for setting timeouts",
2023-02-15 19:44:13 +00:00
Severity: issue.Medium,
Confidence: issue.High,
},
}, []ast.Node{(*ast.CallExpr)(nil)}
}