Skip to content

Commit 7741996

Browse files
committed
rules/sdk: sdk allow unsafe+*/rand in specific packages
There are some package whose core functionality relies on unsafe imports as well randomization code for example: * codegen * crypto/* * simapp * simulation * testutil and other testing code thus allow them to fly with unsafe imports. Fixes #44
1 parent aa9df55 commit 7741996

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

rules/sdk/blocklist.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,25 @@ func (r *blocklistedImport) ID() string {
3636
return r.MetaData.ID
3737
}
3838

39+
// forbiddenFromBlockedImports returns true if the package isn't allowed to import blocklisted/unsafe
40+
// packages; there are some packages though that we should allow unsafe imports given that they
41+
// critically need randomness for example cryptographic code, testing and simulation packages.
42+
// Please see https://github.com/cosmos/gosec/issues/44.
43+
func forbiddenFromBlockedImports(ctx *gosec.Context) bool {
44+
switch pkg := ctx.Pkg.Name(); pkg {
45+
case "codegen", "crypto", "secp256k1", "simapp", "simulation", "testutil":
46+
// These packages rely on imports of "unsafe", "crypto/rand", "math/rand"
47+
// for their core functionality like randomization e.g. in simulation or get
48+
// data for randomizing data.
49+
return false
50+
default:
51+
// Everything else is forbidden from unsafe imports.
52+
return true
53+
}
54+
}
55+
3956
func (r *blocklistedImport) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
40-
if node, ok := n.(*ast.ImportSpec); ok {
57+
if node, ok := n.(*ast.ImportSpec); ok && forbiddenFromBlockedImports(c) {
4158
if description, ok := r.Blocklisted[unquote(node.Path.Value)]; ok {
4259
return gosec.NewIssue(c, node, r.ID(), description, r.Severity, r.Confidence), nil
4360
}

0 commit comments

Comments
 (0)