-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardcodedcredsdetect.go
53 lines (45 loc) · 1.29 KB
/
hardcodedcredsdetect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package hardcodedcredsdetect
import (
"go/ast"
"go/token"
"regexp"
"strconv"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var (
passwordRegexp = regexp.MustCompile(`(?i)password|passwd|pwd`)
credentialRegexp = regexp.MustCompile(`(?i)credential|cred|auth.*token|api.*key`)
)
var Analyzer = &analysis.Analyzer{
Name: "hardcodedcredsdetect",
Doc: "detects hardcoded credentials in code",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
var err error
astInsp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
astInsp.Preorder(nil, func(n ast.Node) {
switch n := n.(type) {
case *ast.AssignStmt:
for i, expr := range n.Rhs {
if lit, ok := expr.(*ast.BasicLit); ok && lit.Kind == token.STRING {
if isSensitiveVariableName(n.Lhs[i].(*ast.Ident).Name) {
if value, err := strconv.Unquote(lit.Value); err == nil {
pass.Reportf(lit.Pos(), "hardcoded credential found: %s", value)
}
}
}
}
}
})
return nil, err
}
// judge whether the variable name is sensitive or not
func isSensitiveVariableName(name string) bool {
return passwordRegexp.MatchString(name) || credentialRegexp.MatchString(name)
}