make gosec linter pass (#3294)

* not related to linter: remove obsolete constants:
 - `Insecure` and `Secure` and type `Security` are not used anywhere

* not related to linter: update example

 - NewInsecure was deleted; change example to NewRemoteDB

* address: Binds to all network interfaces (gosec):

 - bind to localhost instead of 0.0.0.0
 - regenerate test key and cert for this purpose (was valid for ::) and
 otherwise we would see:
 transport: authentication handshake failed: x509: certificate is
 valid for ::, not 127.0.0.1\"

(used https://github.com/google/keytransparency/blob/master/scripts/gen_server_keys.sh
to regenerate certs)

* use sha256 in tests instead of md5; time difference is negligible

* nolint usage of math/rand in test and add comment on its import

 - crypto/rand is slower and we do not need sth more secure in tests

* enable linter in circle-ci

* another nolint math/rand in test

* replace another occurrence of md5

* consistent comment about importing math/rand
This commit is contained in:
Ismail Khoffi 2019-02-12 05:54:12 +01:00 committed by Anton Kaliaev
parent 7fd51e6ade
commit b089587b42
11 changed files with 64 additions and 72 deletions

View File

@ -19,7 +19,6 @@ linters:
- lll - lll
- gochecknoglobals - gochecknoglobals
- gocritic - gocritic
- gosec
- gochecknoinits - gochecknoinits
- scopelint - scopelint
- stylecheck - stylecheck

View File

@ -1,6 +1,8 @@
package merkle package merkle
import ( import (
// it is ok to use math/rand here: we do not need a cryptographically secure random
// number generator here and we can run the tests a bit faster
"math/rand" "math/rand"
"testing" "testing"
@ -24,7 +26,7 @@ func TestKeyPath(t *testing.T) {
keys[i][j] = alphanum[rand.Intn(len(alphanum))] keys[i][j] = alphanum[rand.Intn(len(alphanum))]
} }
case KeyEncodingHex: case KeyEncodingHex:
rand.Read(keys[i]) rand.Read(keys[i]) //nolint: gosec
default: default:
panic("Unexpected encoding") panic("Unexpected encoding")
} }

View File

@ -11,7 +11,7 @@ remotedb's RemoteDB implements db.DB so can be used normally
like other databases. One just has to explicitly connect to the like other databases. One just has to explicitly connect to the
remote database with a client setup such as: remote database with a client setup such as:
client, err := remotedb.NewInsecure(addr) client, err := remotedb.NewRemoteDB(addr, cert)
// Make sure to invoke InitRemote! // Make sure to invoke InitRemote!
if err := client.InitRemote(&remotedb.Init{Name: "test-remote-db", Type: "leveldb"}); err != nil { if err := client.InitRemote(&remotedb.Init{Name: "test-remote-db", Type: "leveldb"}); err != nil {
log.Fatalf("Failed to initialize the remote db") log.Fatalf("Failed to initialize the remote db")

View File

@ -7,14 +7,6 @@ import (
protodb "github.com/tendermint/tendermint/libs/db/remotedb/proto" protodb "github.com/tendermint/tendermint/libs/db/remotedb/proto"
) )
// Security defines how the client will talk to the gRPC server.
type Security uint
const (
Insecure Security = iota
Secure
)
// NewClient creates a gRPC client connected to the bound gRPC server at serverAddr. // NewClient creates a gRPC client connected to the bound gRPC server at serverAddr.
// Use kind to set the level of security to either Secure or Insecure. // Use kind to set the level of security to either Secure or Insecure.
func NewClient(serverAddr, serverCert string) (protodb.DBClient, error) { func NewClient(serverAddr, serverCert string) (protodb.DBClient, error) {

View File

@ -14,7 +14,7 @@ import (
func TestRemoteDB(t *testing.T) { func TestRemoteDB(t *testing.T) {
cert := "test.crt" cert := "test.crt"
key := "test.key" key := "test.key"
ln, err := net.Listen("tcp", "0.0.0.0:0") ln, err := net.Listen("tcp", "localhost:0")
require.Nil(t, err, "expecting a port to have been assigned on which we can listen") require.Nil(t, err, "expecting a port to have been assigned on which we can listen")
srv, err := grpcdb.NewServer(cert, key) srv, err := grpcdb.NewServer(cert, key)
require.Nil(t, err) require.Nil(t, err)

View File

@ -1,25 +1,19 @@
-----BEGIN CERTIFICATE----- -----BEGIN CERTIFICATE-----
MIIEQTCCAimgAwIBAgIRANqF1HD19i/uvQ3n62TAKTwwDQYJKoZIhvcNAQELBQAw MIIDAjCCAeqgAwIBAgIJAOGCVedOwRbOMA0GCSqGSIb3DQEBBQUAMCExCzAJBgNV
GTEXMBUGA1UEAxMOdGVuZGVybWludC5jb20wHhcNMTgwNzAyMDMwNzMyWhcNMjAw BAYTAlVTMRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMTkwMjExMTU0NjQ5WhcNMjAw
MTAyMDMwNzMwWjANMQswCQ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1zDri/41fHXi9b6337PZVqc39GM+N74x/O4Q7xEBiWgQ
T0dT8SNwf55kv63MeZh63ImxFV0FNRkCteYLcJMle3ohIY4zyQ==
-----END CERTIFICATE----- -----END CERTIFICATE-----

View File

@ -1,27 +1,27 @@
-----BEGIN RSA PRIVATE KEY----- -----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA65ZQwJLNgma8pTW+yi4NN7s7Gc9Y7ehXQWNIfy8piiLbOllB MIIEogIBAAKCAQEA60S/fNUWoHm1PYI/yrlnZNtrdRqDORHe0hPwl/lttLz7+a7H
ffl1AM+0sMXb+/8A1knmnMwlHzIwy0RDi1bEq4bMnDVmBOAe0BI9uP8zl8OcM7se zQZFnpiXnuxbDJtpIq/h1vhAl0sFy86Ip26LhbWcGjxJL24tVwiOwqYRzTPZ/rK3
qDgPT14tORkBR+IF/oHMPJAgefbYJR7GFpLvRNshP0A4Cg6wZcxnP136IiYoib6B JYuNcIvcztXjMqdzPrHSZy5YZgrQB6yhTiqpBc4Dh/XgWjEt4DhpHwf/zuIK9XkJ
vN+lHnYs1sEZ6wdYaPAi4YXgiey0gywpLAa/YNy3OFYj6L+bkaiLet3VXzP+XNRu w0IaTWjFmoyKRoWW3q4bHzoKNxS9bXP117Tz7tn0AdsQCjt1GKcIROkcOGUHqByI
x/XS5epBxJ9PK/d57piIJaPZby9x1zkf4HorNKUaMBHixFwdMc5Alg70GAD169De NJ2XlBkb7SQPjQVBLDVJKdRDUt+yHkkdbn97UDhqHRTCt5UELWs/53Gj1ffNuhjE
qwk+nTSJ7PXdF2F4lY+1t9SpDLiB2m7wcV5nHwIDAQABAoIBAQCB2/ilPgaUE8d2 COVjG1HkZweLgZjJRQYe8X2OOLNOyfVY1KsDnQIDAQABAoIBAAb5n8+8pZIWaags
ldqWHa5hgw4/2uCdO04ll/GVUczm/PG1BxAnvYL2MIfcTSRGkrjGZjP9SDZKLONi L2X8PzN/Sd1L7u4HOJrz2mM3EuiT3ciWRPgwImpETeJ5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p5cA/KUOQmK5buOrysosLnnm12LaHiF
ViCeRS2qQnVbH74GfHyimeTPDI9cJMiJfDDTPbfosqWSsPEcg2jfsySJ O7IIh8Cmb9TbdldgW+8ndZ4EQ3lfIS0zN3/7rWD34bs19JDYkRY=
-----END RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----

View File

@ -1,8 +1,8 @@
package mempool package mempool
import ( import (
"crypto/md5"
"crypto/rand" "crypto/rand"
"crypto/sha256"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -451,7 +451,7 @@ func TestMempoolMaxMsgSize(t *testing.T) {
} }
func checksumIt(data []byte) string { func checksumIt(data []byte) string {
h := md5.New() h := sha256.New()
h.Write(data) h.Write(data)
return fmt.Sprintf("%x", h.Sum(nil)) return fmt.Sprintf("%x", h.Sum(nil))
} }

View File

@ -1,11 +1,14 @@
package main package main
import ( import (
"crypto/md5" "crypto/sha256"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
// it is ok to use math/rand here: we do not need a cryptographically secure random
// number generator here and we can run the tests a bit faster
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
@ -154,12 +157,12 @@ func (t *transacter) sendLoop(connIndex int) {
}() }()
// hash of the host name is a part of each tx // hash of the host name is a part of each tx
var hostnameHash [md5.Size]byte var hostnameHash [sha256.Size]byte
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
hostname = "127.0.0.1" hostname = "127.0.0.1"
} }
hostnameHash = md5.Sum([]byte(hostname)) hostnameHash = sha256.Sum256([]byte(hostname))
// each transaction embeds connection index, tx number and hash of the hostname // each transaction embeds connection index, tx number and hash of the hostname
// we update the tx number between successive txs // we update the tx number between successive txs
tx := generateTx(connIndex, txNumber, t.Size, hostnameHash) tx := generateTx(connIndex, txNumber, t.Size, hostnameHash)
@ -257,7 +260,7 @@ func connect(host string) (*websocket.Conn, *http.Response, error) {
return websocket.DefaultDialer.Dial(u.String(), nil) return websocket.DefaultDialer.Dial(u.String(), nil)
} }
func generateTx(connIndex int, txNumber int, txSize int, hostnameHash [md5.Size]byte) []byte { func generateTx(connIndex int, txNumber int, txSize int, hostnameHash [sha256.Size]byte) []byte {
tx := make([]byte, txSize) tx := make([]byte, txSize)
binary.PutUvarint(tx[:8], uint64(connIndex)) binary.PutUvarint(tx[:8], uint64(connIndex))
@ -266,7 +269,7 @@ func generateTx(connIndex int, txNumber int, txSize int, hostnameHash [md5.Size]
binary.PutUvarint(tx[32:40], uint64(time.Now().Unix())) binary.PutUvarint(tx[32:40], uint64(time.Now().Unix()))
// 40-* random data // 40-* random data
if _, err := rand.Read(tx[40:]); err != nil { if _, err := rand.Read(tx[40:]); err != nil { //nolint: gosec
panic(errors.Wrap(err, "failed to read random bytes")) panic(errors.Wrap(err, "failed to read random bytes"))
} }

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"crypto/md5" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -28,7 +28,7 @@ func TestGenerateTxUpdateTxConsistentency(t *testing.T) {
} }
for tcIndex, tc := range cases { for tcIndex, tc := range cases {
hostnameHash := md5.Sum([]byte(tc.hostname)) hostnameHash := sha256.Sum256([]byte(tc.hostname))
// Tx generated from update tx. This is defined outside of the loop, since we have // Tx generated from update tx. This is defined outside of the loop, since we have
// to a have something initially to update // to a have something initially to update
updatedTx := generateTx(tc.connIndex, tc.startingTxNumber, tc.txSize, hostnameHash) updatedTx := generateTx(tc.connIndex, tc.startingTxNumber, tc.txSize, hostnameHash)
@ -69,7 +69,7 @@ func BenchmarkIterationOfSendLoop(b *testing.B) {
// something too far away to matter // something too far away to matter
endTime := now.Add(time.Hour) endTime := now.Add(time.Hour)
txNumber := 0 txNumber := 0
hostnameHash := md5.Sum([]byte{0}) hostnameHash := sha256.Sum256([]byte{0})
tx := generateTx(connIndex, txNumber, txSize, hostnameHash) tx := generateTx(connIndex, txNumber, txSize, hostnameHash)
txHex := make([]byte, len(tx)*2) txHex := make([]byte, len(tx)*2)
hex.Encode(txHex, tx) hex.Encode(txHex, tx)

View File

@ -1,6 +1,8 @@
package types package types
import ( import (
// it is ok to use math/rand here: we do not need a cryptographically secure random
// number generator here and we can run the tests a bit faster
"crypto/rand" "crypto/rand"
"math" "math"
"os" "os"
@ -162,8 +164,8 @@ func TestBlockString(t *testing.T) {
func makeBlockIDRandom() BlockID { func makeBlockIDRandom() BlockID {
blockHash := make([]byte, tmhash.Size) blockHash := make([]byte, tmhash.Size)
partSetHash := make([]byte, tmhash.Size) partSetHash := make([]byte, tmhash.Size)
rand.Read(blockHash) rand.Read(blockHash) //nolint: gosec
rand.Read(partSetHash) rand.Read(partSetHash) //nolint: gosec
blockPartsHeader := PartSetHeader{123, partSetHash} blockPartsHeader := PartSetHeader{123, partSetHash}
return BlockID{blockHash, blockPartsHeader} return BlockID{blockHash, blockPartsHeader}
} }