Skip to content

Commit 3b26468

Browse files
committed
safe cast eon to int in message validation for key
1 parent e3e19bb commit 3b26468

File tree

1 file changed

+12
-11
lines changed
  • rolling-shutter/keyper/epochkghandler

1 file changed

+12
-11
lines changed

rolling-shutter/keyper/epochkghandler/key.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package epochkghandler
33
import (
44
"bytes"
55
"context"
6-
"math"
7-
86
"github.com/jackc/pgx/v4"
97
"github.com/jackc/pgx/v4/pgxpool"
108
pubsub "github.com/libp2p/go-libp2p-pubsub"
119
"github.com/pkg/errors"
1210
"github.com/rs/zerolog/log"
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
1312

1413
"github.com/shutter-network/shutter/shlib/puredkg"
1514
"github.com/shutter-network/shutter/shlib/shcrypto"
@@ -39,34 +38,36 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
3938
return pubsub.ValidationReject,
4039
errors.Errorf("instance ID mismatch (want=%d, have=%d)", handler.config.GetInstanceID(), key.GetInstanceID())
4140
}
42-
if key.Eon > math.MaxInt64 {
43-
return pubsub.ValidationReject, errors.Errorf("eon %d overflows int64", key.Eon)
41+
42+
eon, err := medley.Uint64ToInt64Safe(key.Eon)
43+
if err != nil {
44+
return pubsub.ValidationReject, errors.Wrapf(err, "overflow error while converting eon to int64 %d", key.Eon)
4445
}
4546

4647
queries := database.New(handler.dbpool)
4748

48-
_, isKeyper, err := queries.GetKeyperIndex(ctx, int64(key.Eon), handler.config.GetAddress())
49+
_, isKeyper, err := queries.GetKeyperIndex(ctx, eon, handler.config.GetAddress())
4950
if err != nil {
5051
return pubsub.ValidationReject, err
5152
}
5253
if !isKeyper {
53-
log.Debug().Uint64("eon", key.Eon).Msg("Ignoring decryptionKey for eon; we're not a Keyper")
54+
log.Debug().Int64("eon", eon).Msg("Ignoring decryptionKey for eon; we're not a Keyper")
5455
return pubsub.ValidationReject, nil
5556
}
5657

57-
dkgResultDB, err := queries.GetDKGResultForKeyperConfigIndex(ctx, int64(key.Eon))
58+
dkgResultDB, err := queries.GetDKGResultForKeyperConfigIndex(ctx, eon)
5859
if err == pgx.ErrNoRows {
59-
return pubsub.ValidationReject, errors.Errorf("no DKG result found for eon %d", key.Eon)
60+
return pubsub.ValidationReject, errors.Errorf("no DKG result found for eon %d", eon)
6061
}
6162
if err != nil {
62-
return pubsub.ValidationReject, errors.Wrapf(err, "failed to get dkg result for eon %d from db", key.Eon)
63+
return pubsub.ValidationReject, errors.Wrapf(err, "failed to get dkg result for eon %d from db", eon)
6364
}
6465
if !dkgResultDB.Success {
65-
return pubsub.ValidationReject, errors.Errorf("no successful DKG result found for eon %d", key.Eon)
66+
return pubsub.ValidationReject, errors.Errorf("no successful DKG result found for eon %d", eon)
6667
}
6768
pureDKGResult, err := shdb.DecodePureDKGResult(dkgResultDB.PureResult)
6869
if err != nil {
69-
return pubsub.ValidationReject, errors.Wrapf(err, "error while decoding pure DKG result for eon %d", key.Eon)
70+
return pubsub.ValidationReject, errors.Wrapf(err, "error while decoding pure DKG result for eon %d", eon)
7071
}
7172

7273
if len(key.Keys) == 0 {

0 commit comments

Comments
 (0)