Skip to content

Commit e30cd9c

Browse files
shotasilagadzetaalshota silagadze
andauthored
feat(BPAAS-2064): add gosec linter (#841)
Co-authored-by: shota silagadze <[email protected]>
1 parent 6c38ee8 commit e30cd9c

File tree

24 files changed

+175
-64
lines changed

24 files changed

+175
-64
lines changed

.golangci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ linters:
1212
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
1313
- unused # checks for unused constants, variables, functions and types
1414
- sloglint # Ensure consistent code style when using log/slog
15+
- gosec # inspects source code for security problems
1516
# - asciicheck # checks that your code does not contain non-ASCII identifiers
1617
# - bidichk # checks for dangerous unicode character sequences
1718
# - bodyclose # checks whether HTTP response body is closed successfully
@@ -38,7 +39,6 @@ linters:
3839
# - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
3940
# - gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
4041
# - goprintffuncname # checks that printf-like functions are named with f at the end
41-
# - gosec # inspects source code for security problems
4242
# - lll # reports long lines
4343
# - loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
4444
# - makezero # finds slice declarations with non-zero initial length
@@ -81,6 +81,9 @@ linters:
8181
# - wrapcheck # checks that errors returned from external packages are wrapped
8282
# - zerologlint # detects the wrong usage of zerolog that a user forgets to dispatch zerolog.Event
8383
settings:
84+
gosec:
85+
includes:
86+
- G115
8487
cyclop:
8588
max-complexity: 30
8689
package-average: 10

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type BlocktxConfig struct {
113113
MaxBlockProcessingDuration time.Duration `mapstructure:"maxBlockProcessingDuration"`
114114
MonitorPeers bool `mapstructure:"monitorPeers"`
115115
FillGaps *FillGapsConfig `mapstructure:"fillGaps"`
116-
MaxAllowedBlockHeightMismatch int `mapstructure:"maxAllowedBlockHeightMismatch"`
116+
MaxAllowedBlockHeightMismatch uint64 `mapstructure:"maxAllowedBlockHeightMismatch"`
117117
MessageQueue *MessageQueueConfig `mapstructure:"mq"`
118118
P2pReadBufferSize int `mapstructure:"p2pReadBufferSize"`
119119
IncomingIsLongest bool `mapstructure:"incomingIsLongest"`

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9
77
github.com/bsv-blockchain/go-sdk v1.1.22
88
github.com/cbeuw/connutil v0.0.0-20200411215123-966bfaa51ee3
9+
github.com/ccoveille/go-safecast v1.6.1
910
github.com/cenkalti/backoff/v4 v4.3.0
1011
github.com/docker/docker v28.0.1+incompatible
1112
github.com/enescakir/emoji v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ github.com/bsv-blockchain/go-sdk v1.1.22 h1:R5o9spVEfCAt64We1CdyHkCuYT1sdTSfKXp3
664664
github.com/bsv-blockchain/go-sdk v1.1.22/go.mod h1:d0HXzhHy21t+7z+LBpDhGyJSBJb8S5HiAmHsBtRKddQ=
665665
github.com/cbeuw/connutil v0.0.0-20200411215123-966bfaa51ee3 h1:LRxW8pdmWmyhoNh+TxUjxsAinGtCsVGjsl3xg6zoRSs=
666666
github.com/cbeuw/connutil v0.0.0-20200411215123-966bfaa51ee3/go.mod h1:6jR2SzckGv8hIIS9zWJ160mzGVVOYp4AXZMDtacL6LE=
667+
github.com/ccoveille/go-safecast v1.6.1 h1:Nb9WMDR8PqhnKCVs2sCB+OqhohwO5qaXtCviZkIff5Q=
668+
github.com/ccoveille/go-safecast v1.6.1/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8=
667669
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
668670
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
669671
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

internal/api/handler/default.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/bitcoin-sv/arc/internal/api/handler/internal/merkle_verifier"
1515
"github.com/bitcoin-sv/arc/internal/metamorph"
1616
"github.com/bitcoin-sv/arc/pkg/tracing"
17+
"github.com/ccoveille/go-safecast"
1718

1819
sdkTx "github.com/bsv-blockchain/go-sdk/transaction"
1920
"github.com/labstack/echo/v4"
@@ -44,8 +45,11 @@ var (
4445
)
4546

4647
type ArcDefaultHandler struct {
47-
TransactionHandler metamorph.TransactionHandler
48-
NodePolicy *bitcoin.Settings
48+
TransactionHandler metamorph.TransactionHandler
49+
NodePolicy *bitcoin.Settings
50+
maxTxSizePolicy uint64
51+
maxTxSigopsCountsPolicy uint64
52+
maxscriptsizepolicy uint64
4953

5054
logger *slog.Logger
5155
now func() time.Time
@@ -119,15 +123,37 @@ func NewDefault(
119123
) (*ArcDefaultHandler, error) {
120124
mr := merkle_verifier.New(merkleRootsVerifier)
121125

126+
var maxscriptsizepolicy, maxTxSigopsCountsPolicy, maxTxSizePolicy uint64
127+
var err error
128+
if policy != nil {
129+
maxscriptsizepolicy, err = safecast.ToUint64(policy.MaxScriptSizePolicy)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
maxTxSigopsCountsPolicy, err = safecast.ToUint64(policy.MaxTxSigopsCountsPolicy)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
maxTxSizePolicy, err = safecast.ToUint64(policy.MaxTxSizePolicy)
140+
if err != nil {
141+
return nil, err
142+
}
143+
}
144+
122145
handler := &ArcDefaultHandler{
123-
TransactionHandler: transactionHandler,
124-
NodePolicy: policy,
125-
logger: logger,
126-
now: time.Now,
127-
mrVerifier: mr,
128-
txFinder: cachedFinder,
129-
mapExpiryTime: mapExpiryTimeDefault,
130-
defaultTimeout: timeoutSecondsDefault * time.Second,
146+
TransactionHandler: transactionHandler,
147+
NodePolicy: policy,
148+
logger: logger,
149+
now: time.Now,
150+
mrVerifier: mr,
151+
txFinder: cachedFinder,
152+
mapExpiryTime: mapExpiryTimeDefault,
153+
defaultTimeout: timeoutSecondsDefault * time.Second,
154+
maxTxSizePolicy: maxTxSizePolicy,
155+
maxTxSigopsCountsPolicy: maxTxSigopsCountsPolicy,
156+
maxscriptsizepolicy: maxscriptsizepolicy,
131157
}
132158

133159
// apply options
@@ -147,10 +173,11 @@ func (m ArcDefaultHandler) GETPolicy(ctx echo.Context) (err error) {
147173
satoshis, bytes := calcFeesFromBSVPerKB(m.NodePolicy.MinMiningTxFee)
148174

149175
return ctx.JSON(http.StatusOK, api.PolicyResponse{
176+
150177
Policy: api.Policy{
151-
Maxscriptsizepolicy: uint64(m.NodePolicy.MaxScriptSizePolicy),
152-
Maxtxsigopscountspolicy: uint64(m.NodePolicy.MaxTxSigopsCountsPolicy),
153-
Maxtxsizepolicy: uint64(m.NodePolicy.MaxTxSizePolicy),
178+
Maxscriptsizepolicy: m.maxscriptsizepolicy,
179+
Maxtxsigopscountspolicy: m.maxTxSigopsCountsPolicy,
180+
Maxtxsizepolicy: m.maxTxSizePolicy,
154181
MiningFee: api.FeeAmount{
155182
Bytes: bytes,
156183
Satoshis: satoshis,

internal/api/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import (
55
)
66

77
func FeesToFeeModel(minMiningFee float64) *feemodel.SatoshisPerKilobyte {
8-
satoshisPerKB := int(minMiningFee * 1e8)
9-
return &feemodel.SatoshisPerKilobyte{Satoshis: uint64(satoshisPerKB)}
8+
satoshisPerKB := uint64(minMiningFee * 1e8)
9+
return &feemodel.SatoshisPerKilobyte{Satoshis: satoshisPerKB}
1010
}

internal/beef/beef.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ func decodeTransactionsWithPathIndexes(beefBytes []byte) ([]*TxData, []byte, err
147147

148148
beefBytes = beefBytes[bytesUsed:]
149149

150-
transactions := make([]*TxData, 0, int(nTransactions))
150+
transactions := make([]*TxData, 0, nTransactions)
151151

152-
for i := 0; i < int(nTransactions); i++ {
152+
for i := uint64(0); i < uint64(nTransactions); i++ {
153153
tx, bytesUsed, err := sdkTx.NewTransactionFromStream(beefBytes)
154154
if err != nil {
155155
return nil, nil, err

internal/blocktx/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ type Server struct {
3333
logger *slog.Logger
3434
pm PeerManager
3535
store store.BlocktxStore
36-
maxAllowedBlockHeightMismatch int
36+
maxAllowedBlockHeightMismatch uint64
3737
processor ProcessorI
3838
mqClient mq.MessageQueueClient
3939
}
4040

4141
// NewServer will return a server instance with the logger stored within it.
42-
func NewServer(logger *slog.Logger, store store.BlocktxStore, pm PeerManager, processor ProcessorI, cfg grpc_utils.ServerConfig, maxAllowedBlockHeightMismatch int, mqClient mq.MessageQueueClient) (*Server, error) {
42+
func NewServer(logger *slog.Logger, store store.BlocktxStore, pm PeerManager, processor ProcessorI, cfg grpc_utils.ServerConfig, maxAllowedBlockHeightMismatch uint64, mqClient mq.MessageQueueClient) (*Server, error) {
4343
logger = logger.With(slog.String("module", "server"))
4444

4545
grpcServer, err := grpc_utils.NewGrpcServer(logger, cfg)

internal/blocktx/store/mocks/blocktx_store_mock.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/blocktx/store/postgresql/postgres_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func TestPostgresDB(t *testing.T) {
660660
BlockHeight: 822032,
661661
},
662662
}
663-
maxAllowedBlockHeightMismatch := 10
663+
maxAllowedBlockHeightMismatch := uint64(10)
664664
expectedUnverifiedBlockHeights := []uint64{812011, 822010, 822032}
665665

666666
// when

0 commit comments

Comments
 (0)