Bug/validate pubkey (#1838)

* validate chat before persisting

* add comment to public key generation
This commit is contained in:
Andrea Maria Piana 2020-02-07 12:56:30 +01:00 committed by GitHub
parent d27a507e0d
commit 0b6ad662b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 128 additions and 1 deletions

View File

@ -1 +1 @@
0.43.0
0.43.1

View File

@ -5,6 +5,7 @@ import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"math/rand"
"github.com/status-im/status-go/eth-node/crypto"
@ -79,6 +80,22 @@ func (c *Chat) Public() bool {
return c.ChatType == ChatTypePublic
}
func (c *Chat) OneToOne() bool {
return c.ChatType == ChatTypeOneToOne
}
func (c *Chat) Validate() error {
if c.ID == "" {
return errors.New("chatID can't be blank")
}
if c.OneToOne() {
_, err := c.PublicKey()
return err
}
return nil
}
func (c *Chat) MarshalJSON() ([]byte, error) {
type ChatAlias Chat
item := struct {

72
protocol/chat_test.go Normal file
View File

@ -0,0 +1,72 @@
package protocol
import (
"testing"
"github.com/stretchr/testify/suite"
)
type ChatTestSuite struct {
suite.Suite
}
func TestChatSuite(t *testing.T) {
suite.Run(t, new(ChatTestSuite))
}
func (s *ChatTestSuite) TestValidateChat() {
testCases := []struct {
Name string
Valid bool
Chat Chat
}{
{
Name: "valid one to one chat",
Valid: true,
Chat: Chat{
ID: "0x0424a68f89ba5fcd5e0640c1e1f591d561fa4125ca4e2a43592bc4123eca10ce064e522c254bb83079ba404327f6eafc01ec90a1444331fe769d3f3a7f90b0dde1",
Name: "",
ChatType: ChatTypeOneToOne,
},
},
{
Name: "valid public chat",
Valid: true,
Chat: Chat{
ID: "status",
Name: "status",
ChatType: ChatTypePublic,
},
},
{
Name: "empty chatID",
Valid: false,
Chat: Chat{
ID: "",
Name: "status",
ChatType: ChatTypePublic,
},
},
{
Name: "invalid one to one chat, wrong public key",
Valid: false,
Chat: Chat{
ID: "0xnotvalid",
Name: "",
ChatType: ChatTypeOneToOne,
},
},
}
for _, tc := range testCases {
s.Run(tc.Name, func() {
err := tc.Chat.Validate()
if tc.Valid {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
})
}
}

View File

@ -24,6 +24,7 @@ func generate(seed uint64) string {
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
// Here we truncate the public key to the least significant 64 bits
return generate(uint64(publicKey.X.Int64()))
}

View File

@ -401,6 +401,11 @@ func (m *Messenger) Init() error {
return err
}
for _, chat := range chats {
if err := chat.Validate(); err != nil {
logger.Warn("failed to validate chat", zap.Error(err))
continue
}
m.allChats[chat.ID] = chat
if !chat.Active {
continue

View File

@ -183,6 +183,7 @@ func (s *MessengerSuite) TestInit() {
key2, err := crypto.GenerateKey()
s.Require().NoError(err)
groupChat := Chat{
ID: "some-id",
ChatType: ChatTypePrivateGroupChat,
Active: true,
Members: []ChatMember{

View File

@ -22,6 +22,10 @@ type sqlitePersistence struct {
}
func (db sqlitePersistence) SaveChat(chat Chat) error {
err := chat.Validate()
if err != nil {
return err
}
return db.saveChat(nil, chat)
}

View File

@ -5,6 +5,7 @@ import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"math/rand"
"github.com/status-im/status-go/eth-node/crypto"
@ -79,6 +80,22 @@ func (c *Chat) Public() bool {
return c.ChatType == ChatTypePublic
}
func (c *Chat) OneToOne() bool {
return c.ChatType == ChatTypeOneToOne
}
func (c *Chat) Validate() error {
if c.ID == "" {
return errors.New("chatID can't be blank")
}
if c.OneToOne() {
_, err := c.PublicKey()
return err
}
return nil
}
func (c *Chat) MarshalJSON() ([]byte, error) {
type ChatAlias Chat
item := struct {

View File

@ -24,6 +24,7 @@ func generate(seed uint64) string {
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
// Here we truncate the public key to the least significant 64 bits
return generate(uint64(publicKey.X.Int64()))
}

View File

@ -401,6 +401,11 @@ func (m *Messenger) Init() error {
return err
}
for _, chat := range chats {
if err := chat.Validate(); err != nil {
logger.Warn("failed to validate chat", zap.Error(err))
continue
}
m.allChats[chat.ID] = chat
if !chat.Active {
continue

View File

@ -22,6 +22,10 @@ type sqlitePersistence struct {
}
func (db sqlitePersistence) SaveChat(chat Chat) error {
err := chat.Validate()
if err != nil {
return err
}
return db.saveChat(nil, chat)
}