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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"golang.org/x/mod/modfile"
)
var (
file = flag.String("mod", "go.mod", "go.mod file")
cache = flag.String("modcache", getmodcache(), "location of go mod cache")
excheckm = flag.String("exempt", "golang.org", "domains exempt from the search, seperated by space")
checkInit = flag.Bool("ci", false, "check for implementations of init()")
checkUnsafe = flag.Bool("cu", false, "check for imports of unsafe")
checkCgo = flag.Bool("cc", false, "check for imports of cgo")
checkPprof = flag.Bool("cp", false, "check for imports of net/http/pprof")
exceptions []string
relcache string
)
func getmodcache() string {
if c := os.Getenv("GOMODCACHE"); c != "" {
return c
} else if c = os.Getenv("GOPATH"); c != "" {
return c + "/pkg/mod"
} else {
return os.Getenv("HOME") + "/pkg/mod"
}
}
func main() {
flag.Parse()
exceptions = strings.Fields(*excheckm)
relcache = strings.Replace(*cache, os.Getenv("HOME"), "~", 1)
if !(*checkInit || *checkUnsafe || *checkCgo || *checkPprof) {
fmt.Println("Nothing to check. Use -ci|-cu|-cc|-cp")
return
}
data, err := ioutil.ReadFile(*file)
if err != nil {
log.Fatal(err)
}
mf, err := modfile.Parse(*file, data, nil)
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(len(mf.Require))
for _, r := range mf.Require {
path := strings.Join(r.Syntax.Token, "@")
go func() {
defer wg.Done()
checkPath(*cache + "/" + path)
}()
}
wg.Wait()
}
func isExempt(path string) bool {
for _, pattern := range exceptions {
if strings.Contains(path, pattern) {
return true
}
}
return false
}
func modpath(filename string) string {
return filepath.Join(relcache, strings.TrimPrefix(filename, *cache+"/"))
}
func checkPath(path string) {
var fset = token.NewFileSet()
filter := func(inf fs.FileInfo) bool {
return !strings.HasSuffix(inf.Name(), "_test.go")
}
filepath.WalkDir(path, func(p string, info fs.DirEntry, err error) error {
if err != nil {
return err
} else if !info.IsDir() {
return nil
} else if isExempt(p) {
return nil
}
pkgs, err := parser.ParseDir(fset, p, filter, parser.Mode(0))
if err != nil {
return err
}
for _, pkg := range pkgs {
for filename, file := range pkg.Files {
if *checkUnsafe || *checkCgo || *checkPprof {
for _, imp := range file.Imports {
if *checkUnsafe && imp.Path.Value == `"unsafe"` {
fmt.Println("unsafe in", modpath(filename))
} else if *checkCgo && imp.Path.Value == `"C"` {
fmt.Println("cgo in", modpath(filename))
} else if *checkPprof && imp.Path.Value == `"net/http/pprof"` {
fmt.Println("pprof in", modpath(filename))
}
}
}
if !*checkInit {
break
}
INIT:
for _, decl := range file.Decls {
if f, ok := decl.(*ast.FuncDecl); ok {
if f.Name.Name == "init" {
fmt.Println("init in", modpath(filename))
break INIT
}
}
}
}
}
return nil
})
}
|