-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.go
268 lines (239 loc) · 6.11 KB
/
checker.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package fileprivate
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/ast/astutil"
)
// Cases that are covered:
//
// * Calling unexported method
// * Assignment to unexported field
// * Read from unexported field
// * Setting unexported field via composite literal
type packageChecker struct {
Types *types.Info
Fset *token.FileSet
currentFunc *ast.FuncDecl
ignoredObjects map[string]struct{}
out []warning
}
type warning struct {
Begin token.Pos
End token.Pos
Message string
ObjectName string
}
func (c *packageChecker) CheckPackage(files []*ast.File) []warning {
for _, f := range files {
c.checkFile(f)
}
out := c.out[:0]
for _, w := range c.out {
if _, ok := c.ignoredObjects[w.ObjectName]; ok {
continue
}
out = append(out, w)
}
return out
}
func (c *packageChecker) checkFile(f *ast.File) {
astutil.Apply(f, func(cursor *astutil.Cursor) bool {
if fn, ok := cursor.Node().(*ast.FuncDecl); ok {
if c.currentFunc != nil {
panic("internal error: overwriting current func")
}
c.currentFunc = fn
}
c.checkNode(cursor.Node())
return true
}, func(cursor *astutil.Cursor) bool {
if fn, ok := cursor.Node().(*ast.FuncDecl); ok {
if c.currentFunc != fn {
panic("internal error: mismatching function to pop")
}
c.currentFunc = nil
}
return true
})
}
func (c *packageChecker) checkNode(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
c.checkFuncDecl(n)
case *ast.GenDecl:
c.checkGenDecl(n)
case *ast.SelectorExpr:
c.checkSelectorExpr(n)
case *ast.CompositeLit:
c.checkCompositeLit(n)
}
}
func (c *packageChecker) checkGenDecl(decl *ast.GenDecl) {
if c.currentFunc != nil {
return // Skip non-global declarations
}
// For exported var/const/type we need to ignore types that
// can leak to the outside through them.
for _, spec := range decl.Specs {
switch spec := spec.(type) {
case *ast.ValueSpec:
// If exported var/const has unexported type,
// ignore the analysis for that unexported type.
for _, name := range spec.Names {
if !ast.IsExported(name.Name) {
continue
}
c.ignoreObjectIfUnexported(c.getObject(name))
}
case *ast.TypeSpec:
// If exported struct has any exported field that has
// an unexported type, ignore the analysis for that unexported type.
if !ast.IsExported(spec.Name.Name) {
continue
}
object := c.getObject(spec.Name)
if object == nil {
continue
}
structType, ok := object.Type().Underlying().(*types.Struct)
if !ok {
continue
}
for i := 0; i < structType.NumFields(); i++ {
field := structType.Field(i)
if !ast.IsExported(field.Name()) {
continue
}
c.ignoreObjectIfUnexported(c.getObjectFromType(field.Type()))
}
}
}
}
func (c *packageChecker) checkFuncDecl(decl *ast.FuncDecl) {
// If exported function returns unexported object of type T,
// we record that type T as something that we need to ignore.
if !ast.IsExported(decl.Name.Name) {
return
}
fnType, ok := c.typeOf(decl.Name).(*types.Signature)
if !ok {
return
}
for i := 0; i < fnType.Results().Len(); i++ {
field := fnType.Results().At(i)
c.ignoreObjectIfUnexported(c.getObjectFromType(field.Type()))
}
}
func (c *packageChecker) ignoreObjectIfUnexported(obj types.Object) {
if obj == nil {
return
}
if !ast.IsExported(obj.Name()) {
c.ignoreObject(obj.Name())
}
}
func (c *packageChecker) checkSelectorExpr(e *ast.SelectorExpr) {
if ast.IsExported(e.Sel.Name) {
return
}
object := c.getObject(e.X)
if object == nil || ast.IsExported(object.Name()) {
return
}
if _, isIface := object.Type().Underlying().(*types.Interface); isIface {
return
}
if !c.canUse(object, e) {
c.warnf(object, e.Sel, "accessing %s.%s outside of the suggested context", object.Name(), e.Sel)
}
}
func (c *packageChecker) checkCompositeLit(e *ast.CompositeLit) {
object := c.getObject(e.Type)
if object == nil || ast.IsExported(object.Name()) {
return
}
allowed := c.canUse(object, e)
if allowed {
return
}
structType, _ := object.Type().Underlying().(*types.Struct)
for i, elt := range e.Elts {
switch elt := elt.(type) {
case *ast.KeyValueExpr:
fieldName, ok := elt.Key.(*ast.Ident)
if !ok || ast.IsExported(fieldName.Name) {
continue
}
if !allowed {
c.warnf(object, elt.Key, "accessing %s.%s outside of the suggested context", object.Name(), fieldName)
}
default:
if structType == nil || i >= structType.NumFields() {
continue
}
field := structType.Field(i)
if ast.IsExported(field.Name()) {
continue
}
if !allowed {
c.warnf(object, elt, "accessing %s.%s outside of the suggested context (composite lit member %d)", object.Name(), field.Name(), i)
}
}
}
}
func (c *packageChecker) canUse(o types.Object, usage ast.Expr) bool {
declPos := c.Fset.Position(o.Pos())
usagePos := c.Fset.Position(usage.Pos())
if declPos.Filename == usagePos.Filename {
return true
}
recv := c.getMethodReceiver()
if recv != nil && c.getObject(recv) == o {
return true
}
return false
}
func (c *packageChecker) ignoreObject(name string) {
if c.ignoredObjects == nil {
c.ignoredObjects = make(map[string]struct{})
}
c.ignoredObjects[name] = struct{}{}
}
func (c *packageChecker) warnf(obj types.Object, n ast.Node, format string, args ...interface{}) {
w := warning{
Begin: n.Pos(),
End: n.End(),
Message: fmt.Sprintf(format, args...),
ObjectName: obj.Name(),
}
c.out = append(c.out, w)
}
func (c *packageChecker) typeOf(e ast.Expr) types.Type {
typ := c.Types.TypeOf(e)
if typ == nil {
return types.Typ[types.Invalid]
}
return typ
}
func (c *packageChecker) getObjectFromType(typ types.Type) types.Object {
switch typ := typ.(type) {
case *types.Named:
return typ.Obj()
case *types.Pointer:
return c.getObjectFromType(typ.Elem())
default:
return nil
}
}
func (c *packageChecker) getObject(e ast.Expr) types.Object {
return c.getObjectFromType(c.typeOf(e))
}
func (c *packageChecker) getMethodReceiver() *ast.Ident {
fn := c.currentFunc
if fn.Recv != nil && len(fn.Recv.List) == 1 && len(fn.Recv.List[0].Names) == 1 {
return fn.Recv.List[0].Names[0]
}
return nil
}