chore: audit server package (#14359)

This commit is contained in:
Aleksandr Bezobchuk
2022-12-19 19:42:09 +00:00
committed by GitHub
parent c918b1421d
commit bbd7e31305
6 changed files with 46 additions and 23 deletions
+2 -1
View File
@@ -166,7 +166,8 @@ type StateSyncConfig struct {
SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"` SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"`
} }
// MempoolConfig defines the configurations for the appside mempool // MempoolConfig defines the configurations for the SDK built-in app-side mempool
// implementations.
type MempoolConfig struct { type MempoolConfig struct {
// MaxTxs defines the behavior of the mempool. A negative value indicates // MaxTxs defines the behavior of the mempool. A negative value indicates
// the mempool is disabled entirely, zero indicates that the mempool is // the mempool is disabled entirely, zero indicates that the mempool is
+4 -1
View File
@@ -230,7 +230,10 @@ fsync = "{{ .Streamers.File.Fsync }}"
[mempool] [mempool]
# Setting max-txs to 0 will allow for a unbounded amount of transactions in the mempool. # Setting max-txs to 0 will allow for a unbounded amount of transactions in the mempool.
# Setting max_txs to negative 1 (-1) will disable transactions from being inserted into the mempool. # Setting max_txs to negative 1 (-1) will disable transactions from being inserted into the mempool.
# Setting max_txs to a positive number (> 0) will limit the number of transactions in the mempool, by the specified amount. # Setting max_txs to a positive number (> 0) will limit the number of transactions in the mempool, by the specified amount.
#
# Note, this configuration only applies to SDK built-in app-side mempool
# implementations.
max-txs = "{{ .Mempool.MaxTxs }}" max-txs = "{{ .Mempool.MaxTxs }}"
` `
+15 -8
View File
@@ -14,7 +14,8 @@ import (
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
) )
// importsToFix lets us now that we're only fixing gogoproto/gogoproto.proto imports, we're not fixing cosmos protos. // importsToFix lets us now that we're only fixing gogoproto/gogoproto.proto
// imports, we're not fixing cosmos Proto schemas.
var importsToFix = map[string]string{ var importsToFix = map[string]string{
"gogo.proto": "gogoproto/gogo.proto", "gogo.proto": "gogoproto/gogo.proto",
} }
@@ -41,15 +42,15 @@ func fixRegistration(registeredAs, importedAs string) error {
if err != nil { if err != nil {
return fmt.Errorf("unable to compress: %w", err) return fmt.Errorf("unable to compress: %w", err)
} }
gogoproto.RegisterFile(importedAs, fixedRaw) gogoproto.RegisterFile(importedAs, fixedRaw)
return nil return nil
} }
func init() { func init() {
// we need to fix the gogoproto filedesc to match the import path // We need to fix the gogoproto file descriptor to match the import path, in
// in theory this shouldn't be required, generally speaking // theory this shouldn't be required, generally speaking proto files should be
// proto files should be imported as their registration path // imported as their registration path.
for registeredAs, importedAs := range importsToFix { for registeredAs, importedAs := range importsToFix {
err := fixRegistration(registeredAs, importedAs) err := fixRegistration(registeredAs, importedAs)
if err != nil { if err != nil {
@@ -66,23 +67,27 @@ func compress(fd *dpb.FileDescriptorProto) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cw := gzip.NewWriter(buf) cw := gzip.NewWriter(buf)
_, err = cw.Write(fdBytes) _, err = cw.Write(fdBytes)
if err != nil { if err != nil {
cw.Close() cw.Close()
return nil, err return nil, err
} }
err = cw.Close() err = cw.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func getFileDescriptor(filePath string) []byte { func getFileDescriptor(filePath string) []byte {
// since we got well known descriptors which are not registered into gogoproto registry // Since we got well known descriptors which are not registered into gogoproto
// but are instead registered into the proto one, we need to check both // registry but are instead registered into the proto one, we need to check both.
fd := gogoproto.FileDescriptor(filePath) fd := gogoproto.FileDescriptor(filePath)
if len(fd) != 0 { if len(fd) != 0 {
return fd return fd
@@ -109,7 +114,7 @@ func getExtension(extID int32, m proto.Message) *gogoproto.ExtensionDesc {
} }
// check into proto registry // check into proto registry
//nolint:staticcheck // Seems likely that we should refactor this file. //nolint:staticcheck
for id, desc := range proto.RegisteredExtensions(m) { for id, desc := range proto.RegisteredExtensions(m) {
if id == extID { if id == extID {
return &gogoproto.ExtensionDesc{ return &gogoproto.ExtensionDesc{
@@ -128,6 +133,7 @@ func getExtension(extID int32, m proto.Message) *gogoproto.ExtensionDesc {
func getExtensionsNumbers(m proto.Message) []int32 { func getExtensionsNumbers(m proto.Message) []int32 {
gogoProtoExts := gogoproto.RegisteredExtensions(m) gogoProtoExts := gogoproto.RegisteredExtensions(m)
out := make([]int32, 0, len(gogoProtoExts)) out := make([]int32, 0, len(gogoProtoExts))
for id := range gogoProtoExts { for id := range gogoProtoExts {
out = append(out, id) out = append(out, id)
@@ -141,5 +147,6 @@ func getExtensionsNumbers(m proto.Message) []int32 {
for id := range protoExts { for id := range protoExts {
out = append(out, id) out = append(out, id)
} }
return out return out
} }
+1 -1
View File
@@ -27,7 +27,7 @@ func StartGRPCWeb(grpcSrv *grpc.Server, config config.Config) (*http.Server, err
grpcWebSrv := &http.Server{ grpcWebSrv := &http.Server{
Addr: config.GRPCWeb.Address, Addr: config.GRPCWeb.Address,
Handler: wrappedServer, Handler: wrappedServer,
ReadHeaderTimeout: 500000000, // added because G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server ReadHeaderTimeout: 500000000,
} }
errCh := make(chan error) errCh := make(chan error)
+9 -3
View File
@@ -3,15 +3,21 @@ package mock
import ( import (
"fmt" "fmt"
"os" "os"
"testing"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
tmlog "github.com/tendermint/tendermint/libs/log" tmlog "github.com/tendermint/tendermint/libs/log"
) )
// SetupApp returns an application as well as a clean-up function // SetupApp returns an application as well as a clean-up function to be used to
// to be used to quickly setup a test case with an app. // quickly setup a test case with an app.
func SetupApp() (abci.Application, func(), error) { func SetupApp() (abci.Application, func(), error) {
logger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout)).With("module", "mock") var logger tmlog.Logger
if testing.Verbose() {
logger = tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout)).With("module", "mock")
} else {
logger = tmlog.NewNopLogger()
}
rootDir, err := os.MkdirTemp("", "mock-sdk") rootDir, err := os.MkdirTemp("", "mock-sdk")
if err != nil { if err != nil {
+15 -9
View File
@@ -89,7 +89,8 @@ func bindFlags(basename string, cmd *cobra.Command, v *viper.Viper) (err error)
panic(err) panic(err)
} }
// Apply the viper config value to the flag when the flag is not set and viper has a value // Apply the viper config value to the flag when the flag is not set and
// viper has a value.
if !f.Changed && v.IsSet(f.Name) { if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name) val := v.Get(f.Name)
err = cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)) err = cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
@@ -117,7 +118,7 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s
// Get the executable name and configure the viper instance so that environmental // Get the executable name and configure the viper instance so that environmental
// variables are checked based off that name. The underscore character is used // variables are checked based off that name. The underscore character is used
// as a separator // as a separator.
executableName, err := os.Executable() executableName, err := os.Executable()
if err != nil { if err != nil {
return err return err
@@ -125,13 +126,14 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s
basename := path.Base(executableName) basename := path.Base(executableName)
// Configure the viper instance // configure the viper instance
if err := serverCtx.Viper.BindPFlags(cmd.Flags()); err != nil { if err := serverCtx.Viper.BindPFlags(cmd.Flags()); err != nil {
return err return err
} }
if err := serverCtx.Viper.BindPFlags(cmd.PersistentFlags()); err != nil { if err := serverCtx.Viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err return err
} }
serverCtx.Viper.SetEnvPrefix(basename) serverCtx.Viper.SetEnvPrefix(basename)
serverCtx.Viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) serverCtx.Viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
serverCtx.Viper.AutomaticEnv() serverCtx.Viper.AutomaticEnv()
@@ -147,19 +149,20 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s
if err = bindFlags(basename, cmd, serverCtx.Viper); err != nil { if err = bindFlags(basename, cmd, serverCtx.Viper); err != nil {
return err return err
} }
logger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout)) logger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, tmcfg.DefaultLogLevel) logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, tmcfg.DefaultLogLevel)
if err != nil { if err != nil {
return err return err
} }
// Check if the tendermint flag for trace logging is set // Check if the tendermint flag for trace logging is set if it is then setup
// if it is then setup a tracing logger in this app as well // a tracing logger in this app as well.
if serverCtx.Viper.GetBool(tmcli.TraceFlag) { if serverCtx.Viper.GetBool(tmcli.TraceFlag) {
logger = tmlog.NewTracingLogger(logger) logger = tmlog.NewTracingLogger(logger)
} }
serverCtx.Logger = logger.With("module", "main") serverCtx.Logger = logger.With("module", "server")
return SetCmdServerContext(cmd, serverCtx) return SetCmdServerContext(cmd, serverCtx)
} }
@@ -362,13 +365,13 @@ func WaitForQuitSignals() ErrorCode {
// GetAppDBBackend gets the backend type to use for the application DBs. // GetAppDBBackend gets the backend type to use for the application DBs.
func GetAppDBBackend(opts types.AppOptions) dbm.BackendType { func GetAppDBBackend(opts types.AppOptions) dbm.BackendType {
rv := cast.ToString(opts.Get("app-db-backend")) rv := cast.ToString(opts.Get("app-db-backend"))
if len(rv) == 0 { if len(rv) == 0 {
rv = cast.ToString(opts.Get("db-backend")) rv = cast.ToString(opts.Get("db-backend"))
} }
if len(rv) != 0 { if len(rv) != 0 {
return dbm.BackendType(rv) return dbm.BackendType(rv)
} }
return dbm.GoLevelDBBackend return dbm.GoLevelDBBackend
} }
@@ -452,7 +455,10 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
baseapp.SetSnapshot(snapshotStore, snapshotOptions), baseapp.SetSnapshot(snapshotStore, snapshotOptions),
baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(FlagIAVLCacheSize))), baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(FlagIAVLCacheSize))),
baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(FlagDisableIAVLFastNode))), baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(FlagDisableIAVLFastNode))),
baseapp.SetMempool(mempool.NewSenderNonceMempool( baseapp.SetMempool(
mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(FlagMempoolMaxTxs))))), mempool.NewSenderNonceMempool(
mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(FlagMempoolMaxTxs))),
),
),
} }
} }