-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiterate_over_maps.go
279 lines (248 loc) · 8.78 KB
/
iterate_over_maps.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
269
270
271
272
273
274
275
276
277
278
279
// (c) Copyright 2021 Hewlett Packard Enterprise Development LP
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdk
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/types"
"github.com/cosmos/gosec/v2"
)
// This pass enforces ONLY key retrieval from maps. It resolves a problem that was
// discovered in the Cosmos-SDK in which maps were being iterated on by key and value
// and that produced non-determinism in upgrades.
type mapRanging struct {
gosec.MetaData
calls gosec.CallList
}
func (mr *mapRanging) ID() string {
return mr.MetaData.ID
}
// There are some packages that inherently need map ranging such as "testutil"
// so return true if we detect such.
func pkgExcusedFromMapRangingChecks(ctx *gosec.Context) bool {
switch pkg := ctx.Pkg.Name(); pkg {
case "core", "gogoreflection", "proto", "runtime", "simapp", "simulation", "testutil":
return true
default:
return false
}
}
func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
if pkgExcusedFromMapRangingChecks(ctx) {
// Do nothing for such packages like "testutil".
// Please see https://github.com/cosmos/gosec/issues/50
return nil, nil
}
rangeStmt, ok := node.(*ast.RangeStmt)
if !ok {
return nil, nil
}
if rangeStmt.X == nil {
return nil, nil
}
// Algorithm:
// 1. Ensure that right hand side's eventual type is a map.
// 2. Ensure that only the form:
// for k := range m
// is allowed, and NOT:
// for k, v := range m
// NOR
// for _, v := range m
// 3. Ensure that only keys are appended
// 4. Exceptions:
// * The map clearing idiom
// * `for k, v := range m`` is permitted for map copying
// 1. Ensure that the type of right hand side of the range is eventually a map.
if typ := ctx.Info.TypeOf(rangeStmt.X); typ != nil {
if _, ok := typ.Underlying().(*types.Map); !ok {
return nil, nil
}
} else {
return nil, fmt.Errorf("unable to get type of expr %#v", rangeStmt.X)
}
// Ensure that the range body has only one statement.
rangeBody := rangeStmt.Body
if n := len(rangeBody.List); n != 1 {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("expected exactly 1 statement (either append, delete, or copying to another map) in a range with a map, got %d", n), mr.Severity, mr.Confidence), nil
}
stmt0 := rangeBody.List[0]
// 2. Let's be pedantic to only permit the keys to be iterated upon:
// Allow only:
// for key := range m {
// AND NOT:
// for _, value := range m {
// NOR*
// for key, value := range m {
// * the value can be used when copying a map
if rangeStmt.Key == nil {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), "the key in the range statement should not be _: want: for key := range m", mr.Severity, mr.Confidence), nil
}
// If this is a map copy, rangeStmt.Value is allowed to be non-nil.
if stmt, ok := stmt0.(*ast.AssignStmt); ok {
mapCopy, err := isMapCopy(ctx, stmt, rangeStmt)
if err != nil {
return nil, err
}
if mapCopy {
return nil, nil
}
}
if rangeStmt.Value != nil {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), "the value in the range statement should be _ unless copying a map: want: for key := range m", mr.Severity, mr.Confidence), nil
}
// Ensure that only either an "append" or "delete" statement is present in the range.
switch stmt := stmt0.(type) {
case *ast.ExprStmt:
call := stmt.X.(*ast.CallExpr)
if name, ok := onlyDeleteCall(call); !ok {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("expected either an append, delete, or copy to another map in a range with a map, got: %q", name), mr.Severity, mr.Confidence), nil
}
// We got "delete", so this is safe to recognize
// as this is the fast map clearing idiom.
return nil, nil
case *ast.AssignStmt:
lhs0, ok := stmt.Lhs[0].(*ast.Ident)
if !ok {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), "expected either an append, delete, or copy to another map in a range with a map", mr.Severity, mr.Confidence), nil
}
if lhs0.Obj == nil {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), "expected an array/slice being used to retrieve keys, got _", mr.Severity, mr.Confidence), nil
}
if typ := ctx.Info.TypeOf(lhs0); typ != nil {
switch typ := ctx.Info.Types[lhs0].Type; typ.(type) {
case *types.Array:
case *types.Slice:
default:
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("expected an array/slice being used to retrieve keys, got %T", typ), mr.Severity, mr.Confidence), nil
}
} else {
return nil, fmt.Errorf("unable to get type of %#v", lhs0)
}
rhs0, ok := stmt.Rhs[0].(*ast.CallExpr)
if !ok {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("expected only an append(), got: %#v", stmt.Rhs[0]), mr.Severity, mr.Confidence), nil
}
// The Right Hand Side should only contain the "append".
if name, ok := onlyAppendCall(rhs0); !ok {
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("expected only an append(), got: %#v", name), mr.Severity, mr.Confidence), nil
}
return nil, nil
default:
return gosec.NewIssue(ctx, rangeStmt, mr.ID(), fmt.Sprintf("got %T; expected exactly 1 statement (either append or delete) in a range with a map", stmt), mr.Severity, mr.Confidence), nil
}
}
// isMapCopy returns true if:
// * stmt is a statement that writes a value to a map
// * the key used to write to the map is the same as rangeStmt.Key
// * the value written to the map is rangeStmt.Value
func isMapCopy(ctx *gosec.Context, stmt *ast.AssignStmt, rangeStmt *ast.RangeStmt) (bool, error) {
// Ensure that the lhs is a map.
if len(stmt.Lhs) != 1 {
return false, nil
}
lhs, ok := stmt.Lhs[0].(*ast.IndexExpr)
if !ok {
return false, nil
}
if typ := ctx.Info.TypeOf(lhs.X); typ != nil {
if _, ok := typ.Underlying().(*types.Map); !ok {
return false, nil
}
} else {
return false, fmt.Errorf("unable to get type of expr %#v", lhs.X)
}
// Ensure that the key from the range is used to write to the map.
lhsKey, ok := lhs.Index.(*ast.Ident)
if !ok {
return false, nil
}
rangeKey, ok := rangeStmt.Key.(*ast.Ident)
if !ok {
return false, nil
}
if ctx.Info.ObjectOf(lhsKey) != ctx.Info.ObjectOf(rangeKey) {
return false, nil
}
// If rangeStmt.Value if present, ensure it is being written to the destination map.
if rangeStmt.Value != nil {
rhsValue, ok := stmt.Rhs[0].(*ast.Ident)
if !ok {
return false, nil
}
rangeValue, ok := rangeStmt.Value.(*ast.Ident)
if !ok {
return false, nil
}
return ctx.Info.ObjectOf(rhsValue) == ctx.Info.ObjectOf(rangeValue), nil
}
// Otherwise, ensure that:
// 1. stmt.Rhs is an index expression and rangeStmt.Key is the index.
// 2. The map being read in stmt.Rhs is the the source map (rangeStmt.X).
// 1. Ensure that stmt.Rhs is an index expression and rangeStmt.Key is the index.
indexExpr, ok := stmt.Rhs[0].(*ast.IndexExpr)
if !ok {
return false, nil
}
readKey, ok := indexExpr.Index.(*ast.Ident)
if !ok {
return false, nil
}
if ctx.Info.ObjectOf(readKey) != ctx.Info.ObjectOf(rangeKey) {
return false, nil
}
// 2. Ensure that the map being read in stmt.Rhs is the same as the source map (rangeStmt.X).
rangeXString := &bytes.Buffer{}
err := printer.Fprint(rangeXString, ctx.FileSet, rangeStmt.X)
if err != nil {
return false, err
}
indexExprXString := &bytes.Buffer{}
err = printer.Fprint(indexExprXString, ctx.FileSet, indexExpr.X)
if err != nil {
return false, err
}
return bytes.Equal(rangeXString.Bytes(), indexExprXString.Bytes()), nil
}
func onlyAppendCall(callExpr *ast.CallExpr) (string, bool) {
fn, ok := callExpr.Fun.(*ast.Ident)
if !ok {
return "", false
}
return fn.Name, fn.Name == "append"
}
func onlyDeleteCall(callExpr *ast.CallExpr) (string, bool) {
fn, ok := callExpr.Fun.(*ast.Ident)
if !ok {
return "", false
}
return fn.Name, fn.Name == "delete"
}
// NewMapRangingCheck returns an error if a map is being iterated over in a for loop outside
// of the context of keys being retrieved for sorting, or the delete map clearing idiom.
func NewMapRangingCheck(id string, config gosec.Config) (rule gosec.Rule, nodes []ast.Node) {
calls := gosec.NewCallList()
mr := &mapRanging{
MetaData: gosec.MetaData{
ID: id,
Severity: gosec.High,
Confidence: gosec.Medium,
What: "Non-determinism from ranging over maps",
},
calls: calls,
}
nodes = append(nodes, (*ast.RangeStmt)(nil))
return mr, nodes
}