mirror of
https://github.com/status-im/status-go.git
synced 2025-02-19 18:28:18 +00:00
Bug/validate pubkey (#1838)
* validate chat before persisting * add comment to public key generation
This commit is contained in:
parent
d27a507e0d
commit
0b6ad662b5
@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/status-im/status-go/eth-node/crypto"
|
"github.com/status-im/status-go/eth-node/crypto"
|
||||||
@ -79,6 +80,22 @@ func (c *Chat) Public() bool {
|
|||||||
return c.ChatType == ChatTypePublic
|
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) {
|
func (c *Chat) MarshalJSON() ([]byte, error) {
|
||||||
type ChatAlias Chat
|
type ChatAlias Chat
|
||||||
item := struct {
|
item := struct {
|
||||||
|
72
protocol/chat_test.go
Normal file
72
protocol/chat_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,6 +24,7 @@ func generate(seed uint64) string {
|
|||||||
|
|
||||||
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
|
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
|
||||||
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
|
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
|
||||||
|
// Here we truncate the public key to the least significant 64 bits
|
||||||
return generate(uint64(publicKey.X.Int64()))
|
return generate(uint64(publicKey.X.Int64()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -401,6 +401,11 @@ func (m *Messenger) Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, chat := range chats {
|
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
|
m.allChats[chat.ID] = chat
|
||||||
if !chat.Active {
|
if !chat.Active {
|
||||||
continue
|
continue
|
||||||
|
@ -183,6 +183,7 @@ func (s *MessengerSuite) TestInit() {
|
|||||||
key2, err := crypto.GenerateKey()
|
key2, err := crypto.GenerateKey()
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
groupChat := Chat{
|
groupChat := Chat{
|
||||||
|
ID: "some-id",
|
||||||
ChatType: ChatTypePrivateGroupChat,
|
ChatType: ChatTypePrivateGroupChat,
|
||||||
Active: true,
|
Active: true,
|
||||||
Members: []ChatMember{
|
Members: []ChatMember{
|
||||||
|
@ -22,6 +22,10 @@ type sqlitePersistence struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db sqlitePersistence) SaveChat(chat Chat) error {
|
func (db sqlitePersistence) SaveChat(chat Chat) error {
|
||||||
|
err := chat.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return db.saveChat(nil, chat)
|
return db.saveChat(nil, chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
vendor/github.com/status-im/status-go/protocol/chat.go
generated
vendored
17
vendor/github.com/status-im/status-go/protocol/chat.go
generated
vendored
@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/status-im/status-go/eth-node/crypto"
|
"github.com/status-im/status-go/eth-node/crypto"
|
||||||
@ -79,6 +80,22 @@ func (c *Chat) Public() bool {
|
|||||||
return c.ChatType == ChatTypePublic
|
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) {
|
func (c *Chat) MarshalJSON() ([]byte, error) {
|
||||||
type ChatAlias Chat
|
type ChatAlias Chat
|
||||||
item := struct {
|
item := struct {
|
||||||
|
1
vendor/github.com/status-im/status-go/protocol/identity/alias/generate.go
generated
vendored
1
vendor/github.com/status-im/status-go/protocol/identity/alias/generate.go
generated
vendored
@ -24,6 +24,7 @@ func generate(seed uint64) string {
|
|||||||
|
|
||||||
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
|
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
|
||||||
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
|
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
|
||||||
|
// Here we truncate the public key to the least significant 64 bits
|
||||||
return generate(uint64(publicKey.X.Int64()))
|
return generate(uint64(publicKey.X.Int64()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
vendor/github.com/status-im/status-go/protocol/messenger.go
generated
vendored
5
vendor/github.com/status-im/status-go/protocol/messenger.go
generated
vendored
@ -401,6 +401,11 @@ func (m *Messenger) Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, chat := range chats {
|
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
|
m.allChats[chat.ID] = chat
|
||||||
if !chat.Active {
|
if !chat.Active {
|
||||||
continue
|
continue
|
||||||
|
4
vendor/github.com/status-im/status-go/protocol/persistence.go
generated
vendored
4
vendor/github.com/status-im/status-go/protocol/persistence.go
generated
vendored
@ -22,6 +22,10 @@ type sqlitePersistence struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db sqlitePersistence) SaveChat(chat Chat) error {
|
func (db sqlitePersistence) SaveChat(chat Chat) error {
|
||||||
|
err := chat.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return db.saveChat(nil, chat)
|
return db.saveChat(nil, chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user