Skip to content

Commit 7e62bcd

Browse files
authored
GODRIVER-2156 Enable gosec linter. (#796)
1 parent 0037d8f commit 7e62bcd

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters:
1010
# - errorlint
1111
- goimports
1212
- gosimple
13+
- gosec
1314
- govet
1415
- ineffassign
1516
- makezero
@@ -70,3 +71,9 @@ issues:
7071
- path: x/mongo/driver/crypt.go
7172
linters:
7273
- unused
74+
# Ignore "TLS MinVersion too low", "TLS InsecureSkipVerify set true", and "Use of weak random
75+
# number generator (math/rand instead of crypto/rand)" in tests.
76+
- path: (.+)_test.go
77+
text: G401|G402|G404
78+
linters:
79+
- gosec

benchmark/harness_main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ func DriverBenchmarkMain() int {
4545

4646
if outputFileName == "" {
4747
fmt.Println(string(evgOutput))
48-
} else if err := ioutil.WriteFile(outputFileName, evgOutput, 0644); err != nil {
49-
fmt.Fprintf(os.Stderr, "problem writing file '%s': %s", outputFileName, err.Error())
50-
return 1
48+
} else {
49+
// Ignore gosec warning "Expect WriteFile permissions to be 0600 or less" for benchmark
50+
// result file.
51+
/* #nosec G306 */
52+
err := ioutil.WriteFile(outputFileName, evgOutput, 0644)
53+
if err != nil {
54+
fmt.Fprintf(os.Stderr, "problem writing file '%s': %s", outputFileName, err.Error())
55+
return 1
56+
}
5157
}
5258

5359
if hasErrors {

internal/randutil/randutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type LockedRand struct {
1616
// values. It is safe to use from multiple goroutines.
1717
func NewLockedRand(src rand.Source) *LockedRand {
1818
return &LockedRand{
19+
// Ignore gosec warning "Use of weak random number generator (math/rand instead of
20+
// crypto/rand)". We intentionally use a pseudo-random number generator.
21+
/* #nosec G404 */
1922
r: rand.New(src),
2023
}
2124
}

mongo/mongocryptd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ func (mc *mcryptClient) disconnect(ctx context.Context) error {
115115
}
116116

117117
func (mc *mcryptClient) spawnProcess() error {
118+
// Ignore gosec warning about subprocess launched with externally-provided path variable.
119+
/* #nosec G204 */
118120
cmd := exec.Command(mc.path, mc.spawnArgs...)
119121
cmd.Stdout = nil
120122
cmd.Stderr = nil

x/mongo/driver/auth/auth_spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func runTestsInFile(t *testing.T, dirname string, filename string) {
5252
filename = filename[:len(filename)-5]
5353

5454
for _, testCase := range container.Tests {
55-
runTest(t, filename, &testCase)
55+
runTest(t, filename, testCase)
5656
}
5757
}
5858

59-
func runTest(t *testing.T, filename string, test *testCase) {
59+
func runTest(t *testing.T, filename string, test testCase) {
6060
t.Run(test.Description, func(t *testing.T) {
6161
opts := options.Client().ApplyURI(test.URI)
6262
if test.Valid {

x/mongo/driver/auth/mongodbcr.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ package auth
88

99
import (
1010
"context"
11-
"crypto/md5"
1211
"fmt"
13-
1412
"io"
1513

14+
// Ignore gosec warning "Blocklisted import crypto/md5: weak cryptographic primitive". We need
15+
// to use MD5 here to implement the MONGODB-CR specification.
16+
/* #nosec G501 */
17+
"crypto/md5"
18+
1619
"go.mongodb.org/mongo-driver/bson"
1720
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
1821
"go.mongodb.org/mongo-driver/x/mongo/driver"
@@ -95,6 +98,9 @@ func (a *MongoDBCRAuthenticator) Auth(ctx context.Context, cfg *Config) error {
9598
}
9699

97100
func (a *MongoDBCRAuthenticator) createKey(nonce string) string {
101+
// Ignore gosec warning "Use of weak cryptographic primitive". We need to use MD5 here to
102+
// implement the MONGODB-CR specification.
103+
/* #nosec G401 */
98104
h := md5.New()
99105

100106
_, _ = io.WriteString(h, nonce)

x/mongo/driver/auth/util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77
package auth
88

99
import (
10-
"crypto/md5"
1110
"fmt"
1211
"io"
12+
13+
// Ignore gosec warning "Blocklisted import crypto/md5: weak cryptographic primitive". We need
14+
// to use MD5 here to implement the SCRAM specification.
15+
/* #nosec G501 */
16+
"crypto/md5"
1317
)
1418

1519
const defaultAuthDB = "admin"
1620

1721
func mongoPasswordDigest(username, password string) string {
22+
// Ignore gosec warning "Use of weak cryptographic primitive". We need to use MD5 here to
23+
// implement the SCRAM specification.
24+
/* #nosec G401 */
1825
h := md5.New()
1926
_, _ = io.WriteString(h, username)
2027
_, _ = io.WriteString(h, ":mongo:")

x/mongo/driver/connstring/connstring_spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func runTestsInFile(t *testing.T, dirname string, filename string, warningsError
9090
filename = filename[:len(filename)-5]
9191

9292
for _, testCase := range container.Tests {
93-
runTest(t, filename, &testCase, warningsError)
93+
runTest(t, filename, testCase, warningsError)
9494
}
9595
}
9696

@@ -105,7 +105,7 @@ var skipKeywords = []string{
105105
"serverSelectionTryOnce",
106106
}
107107

108-
func runTest(t *testing.T, filename string, test *testCase, warningsError bool) {
108+
func runTest(t *testing.T, filename string, test testCase, warningsError bool) {
109109
t.Run(test.Description, func(t *testing.T) {
110110
if _, skip := skipDescriptions[test.Description]; skip {
111111
t.Skip()

x/mongo/driver/crypt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (c *crypt) decryptKey(ctx context.Context, kmsCtx *mongocrypt.KmsContext) e
293293
addr = fmt.Sprintf("%s:%d", host, defaultKmsPort)
294294
}
295295

296-
conn, err := tls.Dial("tcp", addr, &tls.Config{})
296+
conn, err := tls.Dial("tcp", addr, &tls.Config{MinVersion: tls.VersionTLS12})
297297
if err != nil {
298298
return err
299299
}

0 commit comments

Comments
 (0)