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

fix(config): panic when loading invalid node key file #888

Merged
merged 3 commits into from
Aug 29, 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
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ func (cfg BaseConfig) LoadNodeKeyID() (types.NodeID, error) {
if err != nil {
return "", err
}

if err = nodeKey.Validate(); err != nil {
return "", fmt.Errorf("invalid node key file %s: %w", cfg.NodeKeyFile(), err)
}

nodeKey.ID = types.NodeIDFromPubKey(nodeKey.PubKey())
return nodeKey.ID, nil
}
Expand Down
37 changes: 37 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -223,3 +224,39 @@ func TestP2PConfigValidateBasic(t *testing.T) {
reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
}
}

// Given some invalid node key file, when I try to load it, I get an error
func TestLoadNodeKeyID(t *testing.T) {

testCases := []string{
`{
"type": "tendermint/PrivKeyEd25519",
"value": "wIVaBy3v4bKcrBxGsgFen9qJeqXiK4h18iWCM2LSYxMyH8PomXsANUb3KoucY9EBDj0NQi4LqrmG8DyT5D6xWQ=="
}`,
`{
"id":"0d846d89021b617026c3a3d4051ebcf4cdd09f7c",
"priv_key":{
"type":"tendermint/PrivKeyEd25519",
"value":"J5EWnwSixAZuuw2Gf5nbXXNbyliaURFgBawfwN+zU/N7ucjnxu0GLcVi107XEj2Myq95101jcPPcJE+dCncY1A=="
}
}`,
}

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
cfg := DefaultBaseConfig()
tmpDir := t.TempDir()

// create invalid node key file
cfg.NodeKey = tmpDir + "/node_key.json"
err := os.WriteFile(cfg.NodeKey, []byte(tc), 0600)
require.NoError(t, err)

// when I try to load the node key
_, err = cfg.LoadNodeKeyID()

// then I get an error
assert.Error(t, err)
})
}
}
15 changes: 15 additions & 0 deletions types/node_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"fmt"
"os"

"github.com/dashpay/tenderdash/crypto"
Expand Down Expand Up @@ -38,6 +39,20 @@ func (nk NodeKey) MarshalJSON() ([]byte, error) {
})
}

func (nk *NodeKey) Validate() error {
if nk.PrivKey == nil {
return fmt.Errorf("invalid or empty private key")
}
if err := nk.ID.Validate(); err != nil {
return fmt.Errorf("invalid node ID: %w", err)
}
keyID := NodeIDFromPubKey(nk.PrivKey.PubKey())
if nk.ID != keyID {
return fmt.Errorf("saved node ID %s does not match public key %s", nk.ID, keyID)
}
return nil
}

func (nk *NodeKey) UnmarshalJSON(data []byte) error {
var nkjson nodeKeyJSON
if err := json.Unmarshal(data, &nkjson); err != nil {
Expand Down
Loading