This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsafesql.go
408 lines (353 loc) · 10.1 KB
/
safesql.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Command safesql is a tool for performing static analysis on programs to
// ensure that SQL injection attacks are not possible. It does this by ensuring
// package database/sql is only used with compile-time constant queries.
package main
import (
"flag"
"fmt"
"go/build"
"go/token"
"go/types"
"io/ioutil"
"os"
"sort"
"path/filepath"
"strings"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
const IgnoreComment = "//nolint:safesql"
type sqlPackage struct {
packageName string
paramNames []string
enable bool
}
var sqlPackages = []sqlPackage{
{
packageName: "database/sql",
paramNames: []string{"query"},
},
{
packageName: "github.com/jinzhu/gorm",
paramNames: []string{"sql", "query"},
},
{
packageName: "github.com/jmoiron/sqlx",
paramNames: []string{"query"},
},
}
func main() {
var verbose, quiet bool
flag.BoolVar(&verbose, "v", false, "Verbose mode")
flag.BoolVar(&quiet, "q", false, "Only print on failure")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [-q] [-v] package1 [package2 ...]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
pkgs := flag.Args()
if len(pkgs) == 0 {
flag.Usage()
os.Exit(2)
}
c := loader.Config{
FindPackage: FindPackage,
}
for _, pkg := range pkgs {
c.Import(pkg)
}
p, err := c.Load()
if err != nil {
fmt.Printf("error loading packages %v: %v\n", pkgs, err)
os.Exit(2)
}
imports := getImports(p)
existOne := false
for i := range sqlPackages {
if _, exist := imports[sqlPackages[i].packageName]; exist {
if verbose {
fmt.Printf("Enabling support for %s\n", sqlPackages[i].packageName)
}
sqlPackages[i].enable = true
existOne = true
}
}
if !existOne {
fmt.Printf("No packages in %v include a supported database driver", pkgs)
os.Exit(2)
}
s := ssautil.CreateProgram(p, 0)
s.Build()
qms := make([]*QueryMethod, 0)
for i := range sqlPackages {
if sqlPackages[i].enable {
qms = append(qms, FindQueryMethods(sqlPackages[i], p.Package(sqlPackages[i].packageName).Pkg, s)...)
}
}
if verbose {
fmt.Println("database driver functions that accept queries:")
for _, m := range qms {
fmt.Printf("- %s (param %d)\n", m.Func, m.Param)
}
fmt.Println()
}
mains := FindMains(p, s)
if len(mains) == 0 {
fmt.Println("Did not find any commands (i.e., main functions).")
os.Exit(2)
}
res, err := pointer.Analyze(&pointer.Config{
Mains: mains,
BuildCallGraph: true,
})
if err != nil {
fmt.Printf("error performing pointer analysis: %v\n", err)
os.Exit(2)
}
bad := FindNonConstCalls(res.CallGraph, qms)
if len(bad) == 0 {
if !quiet {
fmt.Println(`You're safe from SQL injection! Yay \o/`)
}
return
}
if verbose {
fmt.Printf("Found %d potentially unsafe SQL statements:\n", len(bad))
}
potentialBadStatements := []token.Position{}
for _, ci := range bad {
potentialBadStatements = append(potentialBadStatements, p.Fset.Position(ci.Pos()))
}
issues, err := CheckIssues(potentialBadStatements)
if err != nil {
fmt.Printf("error when checking for ignore comments: %v\n", err)
os.Exit(2)
}
if verbose {
fmt.Println("Please ensure that all SQL queries you use are compile-time constants.")
fmt.Println("You should always use parameterized queries or prepared statements")
fmt.Println("instead of building queries from strings.")
}
hasNonIgnoredUnsafeStatement := false
for _, issue := range issues {
if issue.ignored {
fmt.Printf("- %s is potentially unsafe but ignored by comment\n", issue.statement)
} else {
fmt.Printf("- %s\n", issue.statement)
hasNonIgnoredUnsafeStatement = true
}
}
if hasNonIgnoredUnsafeStatement {
os.Exit(1)
}
}
// QueryMethod represents a method on a type which has a string parameter named
// "query".
type QueryMethod struct {
Func *types.Func
SSA *ssa.Function
ArgCount int
Param int
}
type Issue struct {
statement token.Position
ignored bool
}
// CheckIssues checks lines to see if the line before or the current line has an ignore comment and marks those
// statements that have the ignore comment on the current line or the line before
func CheckIssues(lines []token.Position) ([]Issue, error) {
files := make(map[string][]token.Position)
for _, line := range lines {
files[line.Filename] = append(files[line.Filename], line)
}
issues := []Issue{}
for file, linesInFile := range files {
// ensure we have the lines in ascending order
sort.Slice(linesInFile, func(i, j int) bool { return linesInFile[i].Line < linesInFile[j].Line })
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
fileLines := strings.Split(string(data), "\n")
for _, line := range linesInFile {
// check the line before the problematic statement first
potentialCommentLine := line.Line - 2
// check only if the previous line is strictly a line that begins with
// the ignore comment
if 0 <= potentialCommentLine && BeginsWithComment(fileLines[potentialCommentLine]) {
issues = append(issues, Issue{statement: line, ignored: true})
continue
}
isIgnored := HasIgnoreComment(fileLines[line.Line-1])
issues = append(issues, Issue{statement: line, ignored: isIgnored})
}
}
return issues, nil
}
func BeginsWithComment(line string) bool {
return strings.HasPrefix(strings.TrimSpace(line), IgnoreComment)
}
func HasIgnoreComment(line string) bool {
return strings.HasSuffix(strings.TrimSpace(line), IgnoreComment)
}
// FindQueryMethods locates all methods in the given package (assumed to be
// package database/sql) with a string parameter named "query".
func FindQueryMethods(sqlPackages sqlPackage, sql *types.Package, ssa *ssa.Program) []*QueryMethod {
methods := make([]*QueryMethod, 0)
scope := sql.Scope()
for _, name := range scope.Names() {
o := scope.Lookup(name)
if !o.Exported() {
continue
}
if _, ok := o.(*types.TypeName); !ok {
continue
}
n := o.Type().(*types.Named)
for i := 0; i < n.NumMethods(); i++ {
m := n.Method(i)
if !m.Exported() {
continue
}
s := m.Type().(*types.Signature)
if num, ok := FuncHasQuery(sqlPackages, s); ok {
methods = append(methods, &QueryMethod{
Func: m,
SSA: ssa.FuncValue(m),
ArgCount: s.Params().Len(),
Param: num,
})
}
}
}
return methods
}
// FuncHasQuery returns the offset of the string parameter named "query", or
// none if no such parameter exists.
func FuncHasQuery(sqlPackages sqlPackage, s *types.Signature) (offset int, ok bool) {
params := s.Params()
for i := 0; i < params.Len(); i++ {
v := params.At(i)
for _, paramName := range sqlPackages.paramNames {
if v.Name() == paramName {
return i, true
}
}
}
return 0, false
}
// FindMains returns the set of all packages loaded into the given
// loader.Program which contain main functions
func FindMains(p *loader.Program, s *ssa.Program) []*ssa.Package {
ips := p.InitialPackages()
mains := make([]*ssa.Package, 0, len(ips))
for _, info := range ips {
ssaPkg := s.Package(info.Pkg)
if ssaPkg.Func("main") != nil {
mains = append(mains, ssaPkg)
}
}
return mains
}
func getImports(p *loader.Program) map[string]interface{} {
pkgs := make(map[string]interface{})
for _, pkg := range p.AllPackages {
if pkg.Importable {
pkgs[pkg.Pkg.Path()] = nil
}
}
return pkgs
}
// FindNonConstCalls returns the set of callsites of the given set of methods
// for which the "query" parameter is not a compile-time constant.
func FindNonConstCalls(cg *callgraph.Graph, qms []*QueryMethod) []ssa.CallInstruction {
cg.DeleteSyntheticNodes()
// package database/sql has a couple helper functions which are thin
// wrappers around other sensitive functions. Instead of handling the
// general case by tracing down callsites of wrapper functions
// recursively, let's just whitelist the functions we're already
// tracking, since it happens to be good enough for our use case.
okFuncs := make(map[*ssa.Function]struct{}, len(qms))
for _, m := range qms {
okFuncs[m.SSA] = struct{}{}
}
bad := make([]ssa.CallInstruction, 0)
for _, m := range qms {
node := cg.CreateNode(m.SSA)
for _, edge := range node.In {
if _, ok := okFuncs[edge.Site.Parent()]; ok {
continue
}
isInternalSQLPkg := false
for _, pkg := range sqlPackages {
if pkg.packageName == edge.Caller.Func.Pkg.Pkg.Path() {
isInternalSQLPkg = true
break
}
}
if isInternalSQLPkg {
continue
}
cc := edge.Site.Common()
args := cc.Args
// The first parameter is occasionally the receiver.
if len(args) == m.ArgCount+1 {
args = args[1:]
} else if len(args) != m.ArgCount {
panic("arg count mismatch")
}
v := args[m.Param]
if _, ok := v.(*ssa.Const); !ok {
if inter, ok := v.(*ssa.MakeInterface); ok && types.IsInterface(v.(*ssa.MakeInterface).Type()) {
if inter.X.Referrers() == nil || inter.X.Type() != types.Typ[types.String] {
continue
}
}
bad = append(bad, edge.Site)
}
}
}
return bad
}
// Deal with GO15VENDOREXPERIMENT
func FindPackage(ctxt *build.Context, path, dir string, mode build.ImportMode) (*build.Package, error) {
if !useVendor {
return ctxt.Import(path, dir, mode)
}
// First, walk up the filesystem from dir looking for vendor directories
var vendorDir string
for tmp := dir; vendorDir == "" && tmp != "/"; tmp = filepath.Dir(tmp) {
dname := filepath.Join(tmp, "vendor", filepath.FromSlash(path))
fd, err := os.Open(dname)
if err != nil {
continue
}
// Directories are only valid if they contain at least one file
// with suffix ".go" (this also ensures that the file descriptor
// we have is in fact a directory)
names, err := fd.Readdirnames(-1)
if err != nil {
continue
}
for _, name := range names {
if strings.HasSuffix(name, ".go") {
vendorDir = filepath.ToSlash(dname)
break
}
}
}
if vendorDir != "" {
pkg, err := ctxt.ImportDir(vendorDir, mode)
if err != nil {
return nil, err
}
// Go tries to derive a valid import path for the package, but
// it's wrong (it includes "/vendor/"). Overwrite it here.
pkg.ImportPath = path
return pkg, nil
}
return ctxt.Import(path, dir, mode)
}