Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no validate for malformed e2ee key in register #1421

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* (versiondb) [#1387](https://github.com/crypto-org-chain/cronos/pull/1387) Add dedicated config section for versiondb, prepare for sdk 0.50 integration.
* (e2ee)[#1413](https://github.com/crypto-org-chain/cronos/pull/1413) Add custom keyring implementation for e2ee module.
* (e2ee)[#1415](https://github.com/crypto-org-chain/cronos/pull/1415) Add batch keys query for e2ee module.
* (e2ee)[#1421](https://github.com/crypto-org-chain/cronos/pull/1421) Validate e2ee key when register.
mmsqe marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/configs/default.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
e2ee: {
keys: [{
address: 'crc16z0herz998946wr659lr84c8c556da55dc34hh',
key: std.base64('key'),
key: 'age1k3mpspxytgvx6e0jja0xgrtzz7vw2p00c2a3xmq5ygfzhwh4wg0s35z4c8',
}],
},
gov: {
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ def query_e2ee_key(self, address):
home=self.data_dir,
output="json",
)
)["key"]
).get("key")

def query_e2ee_keys(self, *addresses):
return json.loads(
Expand All @@ -1864,7 +1864,7 @@ def query_e2ee_keys(self, *addresses):
home=self.data_dir,
output="json",
)
)["keys"]
).get("keys")

def register_e2ee_key(self, key, **kwargs):
kwargs.setdefault("gas_prices", DEFAULT_GAS_PRICE)
Expand Down
7 changes: 7 additions & 0 deletions integration_tests/test_e2ee.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
def test_register(cronos):
cli = cronos.cosmos_cli()
pubkey0 = cli.keygen(keyring_name="key0")
cli.register_e2ee_key(pubkey0 + "malformed", _from="validator")
assert not cli.query_e2ee_key(cli.address("validator"))


def test_encrypt_decrypt(cronos):
cli = cronos.cosmos_cli()

Expand Down
23 changes: 15 additions & 8 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@
}
}

func (k Keeper) registerEncryptionKey(
ctx context.Context,
address string,
key []byte,
) error {
bz, err := k.addressCodec.StringToBytes(address)
if err != nil {
return err

Check warning on line 38 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
sdk.UnwrapSDKContext(ctx).KVStore(k.storeKey).Set(types.KeyPrefix(bz), key)
return nil

Check warning on line 41 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

func (k Keeper) RegisterEncryptionKey(
ctx context.Context,
req *types.MsgRegisterEncryptionKey,
) (*types.MsgRegisterEncryptionKeyResponse, error) {
bz, err := k.addressCodec.StringToBytes(req.Address)
if err != nil {
if err := k.registerEncryptionKey(ctx, req.Address, []byte(req.Key)); err != nil {

Check warning on line 48 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L48

Added line #L48 was not covered by tests
return nil, err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.KVStore(k.storeKey).Set(types.KeyPrefix(bz), []byte(req.Key))
return &types.MsgRegisterEncryptionKeyResponse{}, nil
}

Expand All @@ -46,10 +56,7 @@
state *types.GenesisState,
) error {
for _, key := range state.Keys {
if _, err := k.RegisterEncryptionKey(ctx, &types.MsgRegisterEncryptionKey{
Address: key.Address,
Key: key.Key,
}); err != nil {
if err := k.registerEncryptionKey(ctx, key.Address, []byte(key.Key)); err != nil {

Check warning on line 59 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L59

Added line #L59 was not covered by tests
return err
}
}
Expand Down
7 changes: 1 addition & 6 deletions x/e2ee/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -39,8 +37,5 @@ func (e EncryptionKeyEntry) Validate() error {
if _, err := sdk.AccAddressFromBech32(e.Address); err != nil {
return err
}
if len(e.Key) == 0 {
return errors.New("key can't be nil")
}
return nil
return ValidateRecipientKey(e.Key)
}
14 changes: 7 additions & 7 deletions x/e2ee/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import (
fmt "fmt"

"filippo.io/age"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (m *MsgRegisterEncryptionKey) ValidateBasic() error {
if m.Address == "" {
return fmt.Errorf("address cannot be empty")
}
if len(m.Key) == 0 {
return fmt.Errorf("key cannot be nil")
}
// validate bech32 format of Address
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return fmt.Errorf("invalid address: %s", err)
}
return nil
return ValidateRecipientKey(m.Key)

Check warning on line 15 in x/e2ee/types/msg.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/types/msg.go#L15

Added line #L15 was not covered by tests
}

func ValidateRecipientKey(key string) error {
_, err := age.ParseX25519Recipient(key)
return err
}
Loading