-
Notifications
You must be signed in to change notification settings - Fork 62
/
banned_api.go
135 lines (119 loc) · 3.61 KB
/
banned_api.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
// Copyright 2020 Google LLC
//
// 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
//
// https://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 bannedapi provides the tools for doing static analysis
// and checking for usage of banned APIs.
package bannedapi
import (
"errors"
"flag"
"fmt"
"go/token"
"go/types"
"path/filepath"
"strings"
"github.com/google/go-safeweb/cmd/bancheck/config"
"golang.org/x/tools/go/analysis"
)
// NewAnalyzer returns an analyzer that checks for usage of banned APIs.
func NewAnalyzer() *analysis.Analyzer {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.String("configs", "", "Config files with banned APIs separated by a comma")
a := &analysis.Analyzer{
Name: "bannedAPI",
Doc: "Checks for usage of banned APIs",
Run: checkBannedAPIs,
Flags: *fs,
}
return a
}
func checkBannedAPIs(pass *analysis.Pass) (interface{}, error) {
cfgFiles := pass.Analyzer.Flags.Lookup("configs").Value.String()
if cfgFiles == "" {
return nil, errors.New("missing config files")
}
cfg, err := config.ReadConfigs(strings.Split(cfgFiles, ","))
if err != nil {
return nil, err
}
checkBannedImports(pass, bannedAPIMap(cfg.Imports))
checkBannedFunctions(pass, bannedAPIMap(cfg.Functions))
return nil, nil
}
func checkBannedImports(pass *analysis.Pass, bannedImports map[string][]config.BannedAPI) (interface{}, error) {
for _, f := range pass.Files {
for _, i := range f.Imports {
importName := strings.Trim(i.Path.Value, "\"")
err := reportIfBanned(importName, bannedImports, i.Pos(), pass)
if err != nil {
return false, err
}
}
}
return nil, nil
}
func checkBannedFunctions(pass *analysis.Pass, bannedFns map[string][]config.BannedAPI) (interface{}, error) {
for id, obj := range pass.TypesInfo.Uses {
fn, ok := obj.(*types.Func)
if !ok {
continue
}
fnName := fmt.Sprintf("%s.%s", fn.Pkg().Path(), fn.Name())
err := reportIfBanned(fnName, bannedFns, id.Pos(), pass)
if err != nil {
return false, err
}
}
return nil, nil
}
func reportIfBanned(apiName string, bannedAPIs map[string][]config.BannedAPI, position token.Pos, pass *analysis.Pass) error {
for _, banCfg := range bannedAPIs[apiName] {
if apiName != banCfg.Name {
return nil
}
pkgAllowed, err := isPkgAllowed(pass.Pkg, banCfg)
if err != nil {
return err
}
if pkgAllowed {
continue
}
pass.Report(analysis.Diagnostic{
Pos: position,
Message: fmt.Sprintf("Banned API found %q. Additional info: %s", apiName, banCfg.Msg),
})
}
return nil
}
// isPkgAllowed checks if the Go package should be exempted from reporting banned API usages.
func isPkgAllowed(pkg *types.Package, bannedAPI config.BannedAPI) (bool, error) {
for _, e := range bannedAPI.Exemptions {
match, err := filepath.Match(e.AllowedPkg, pkg.Path())
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}
// bannedAPIMap builds a mapping of fully qualified API name to a list of
// all its config.BannedAPI entries.
func bannedAPIMap(bannedAPIs []config.BannedAPI) map[string][]config.BannedAPI {
m := make(map[string][]config.BannedAPI)
for _, API := range bannedAPIs {
m[API.Name] = append(m[API.Name], API)
}
return m
}