Skip to content

Commit a5982fb

Browse files
authored
Fix for G402. Check package path instead of package name (#838)
1 parent ea6d49d commit a5982fb

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

helpers.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import (
3939
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
4040
importedName, found := GetImportedName(pkg, c)
4141
if !found {
42-
return nil, false
42+
importedName, found = GetAliasedName(pkg, c)
43+
if !found {
44+
return nil, false
45+
}
4346
}
4447

4548
if callExpr, ok := n.(*ast.CallExpr); ok {
@@ -245,7 +248,7 @@ func GetBinaryExprOperands(be *ast.BinaryExpr) []ast.Node {
245248
}
246249

247250
// GetImportedName returns the name used for the package within the
248-
// code. It will resolve aliases and ignores initialization only imports.
251+
// code. It will ignore initialization only imports.
249252
func GetImportedName(path string, ctx *Context) (string, bool) {
250253
importName, imported := ctx.Imports.Imported[path]
251254
if !imported {
@@ -256,20 +259,39 @@ func GetImportedName(path string, ctx *Context) (string, bool) {
256259
return "", false
257260
}
258261

259-
if alias, ok := ctx.Imports.Aliased[path]; ok {
260-
importName = alias
262+
return importName, true
263+
}
264+
265+
// GetAliasedName returns the aliased name used for the package within the
266+
// code. It will ignore initialization only imports.
267+
func GetAliasedName(path string, ctx *Context) (string, bool) {
268+
importName, imported := ctx.Imports.Aliased[path]
269+
if !imported {
270+
return "", false
261271
}
272+
273+
if _, initonly := ctx.Imports.InitOnly[path]; initonly {
274+
return "", false
275+
}
276+
262277
return importName, true
263278
}
264279

265280
// GetImportPath resolves the full import path of an identifier based on
266-
// the imports in the current context.
281+
// the imports in the current context(including aliases).
267282
func GetImportPath(name string, ctx *Context) (string, bool) {
268283
for path := range ctx.Imports.Imported {
269284
if imported, ok := GetImportedName(path, ctx); ok && imported == name {
270285
return path, true
271286
}
272287
}
288+
289+
for path := range ctx.Imports.Aliased {
290+
if imported, ok := GetAliasedName(path, ctx); ok && imported == name {
291+
return path, true
292+
}
293+
}
294+
273295
return "", false
274296
}
275297

rules/tls.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Cont
122122
t.actualMinVersion = ival
123123
} else {
124124
if se, ok := n.Value.(*ast.SelectorExpr); ok {
125-
if pkg, ok := se.X.(*ast.Ident); ok && pkg.Name == "tls" {
126-
t.actualMinVersion = t.mapVersion(se.Sel.Name)
125+
if pkg, ok := se.X.(*ast.Ident); ok {
126+
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
127+
t.actualMinVersion = t.mapVersion(se.Sel.Name)
128+
}
127129
}
128130
}
129131
}
@@ -133,8 +135,10 @@ func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Cont
133135
t.actualMaxVersion = ival
134136
} else {
135137
if se, ok := n.Value.(*ast.SelectorExpr); ok {
136-
if pkg, ok := se.X.(*ast.Ident); ok && pkg.Name == "tls" {
137-
t.actualMaxVersion = t.mapVersion(se.Sel.Name)
138+
if pkg, ok := se.X.(*ast.Ident); ok {
139+
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
140+
t.actualMaxVersion = t.mapVersion(se.Sel.Name)
141+
}
138142
}
139143
}
140144
}

testutils/source.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,19 @@ package main
30083008
import "crypto/tls"
30093009
30103010
const MinVer = tls.VersionTLS13
3011+
`}, 0, gosec.NewConfig()},
3012+
{[]string{`
3013+
package main
3014+
3015+
import (
3016+
"crypto/tls"
3017+
cryptotls "crypto/tls"
3018+
)
3019+
3020+
func main() {
3021+
_ = tls.Config{MinVersion: tls.VersionTLS12}
3022+
_ = cryptotls.Config{MinVersion: cryptotls.VersionTLS12}
3023+
}
30113024
`}, 0, gosec.NewConfig()},
30123025
}
30133026

0 commit comments

Comments
 (0)