Skip to content

Commit 9cee24c

Browse files
ccojocargcmurphy
authored andcommitted
Add a rule which detects when pprof endpoint is automatically exposed
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 73fbc9b commit 9cee24c

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

rules/pprof.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package rules
2+
3+
import (
4+
"go/ast"
5+
6+
"github.com/securego/gosec"
7+
)
8+
9+
type pprofCheck struct {
10+
gosec.MetaData
11+
importPath string
12+
importName string
13+
}
14+
15+
// ID returns the ID of the check
16+
func (p *pprofCheck) ID() string {
17+
return p.MetaData.ID
18+
}
19+
20+
// Match checks for pprof imports
21+
func (p *pprofCheck) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
22+
if node, ok := n.(*ast.ImportSpec); ok {
23+
if p.importPath == unquote(node.Path.Value) && p.importName == node.Name.Name {
24+
return gosec.NewIssue(c, node, p.ID(), p.What, p.Severity, p.Confidence), nil
25+
}
26+
}
27+
return nil, nil
28+
}
29+
30+
// NewPprofCheck detects when the profiling endpoint is automatically exposed
31+
func NewPprofCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
32+
return &pprofCheck{
33+
MetaData: gosec.MetaData{
34+
ID: id,
35+
Severity: gosec.High,
36+
Confidence: gosec.High,
37+
What: "Profiling endpoint is automatically exposed on /debug/pprof",
38+
},
39+
importPath: "net/http/pprof",
40+
importName: "_",
41+
}, []ast.Node{(*ast.ImportSpec)(nil)}
42+
}

rules/rulelist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func Generate(filters ...RuleFilter) RuleList {
6565
{"G104", "Audit errors not checked", NewNoErrorCheck},
6666
{"G106", "Audit the use of ssh.InsecureIgnoreHostKey function", NewSSHHostKey},
6767
{"G107", "Url provided to HTTP request as taint input", NewSSRFCheck},
68+
{"G108", "Profiling endpoint is automatically exposed", NewPprofCheck},
6869

6970
// injection
7071
{"G201", "SQL query construction using format string", NewSQLStrFormat},

rules/rules_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ var _ = Describe("gosec rules", func() {
7979
runner("G107", testutils.SampleCodeG107)
8080
})
8181

82+
It("should detect pprof endpoint", func() {
83+
runner("G108", testutils.SampleCodeG108)
84+
})
85+
8286
It("should detect sql injection via format strings", func() {
8387
runner("G201", testutils.SampleCodeG201)
8488
})

testutils/source.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,23 @@ func main() {
404404
}
405405
fmt.Println(resp.Status)
406406
}`}, 0, gosec.NewConfig()}}
407+
// SampleCodeG108 - pprof endpoint automatically exposed
408+
SampleCodeG108 = []CodeSample{{[]string{`
409+
package main
410+
411+
import (
412+
"fmt"
413+
"log"
414+
"net/http"
415+
_ "net/http/pprof"
416+
)
417+
418+
func main() {
419+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
420+
fmt.Fprintf(w, "Hello World!")
421+
})
422+
log.Fatal(http.ListenAndServe(":8080", nil))
423+
}`}, 1, gosec.NewConfig()}}
407424
// SampleCodeG201 - SQL injection via format string
408425
SampleCodeG201 = []CodeSample{
409426
{[]string{`

0 commit comments

Comments
 (0)