Skip to content

Commit

Permalink
Bug/validate pubkey (#1838)
Browse files Browse the repository at this point in the history
* validate chat before persisting

* add comment to public key generation
  • Loading branch information
cammellos authored Feb 7, 2020
1 parent d27a507 commit 0b6ad66
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.43.0
0.43.1
17 changes: 17 additions & 0 deletions protocol/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"math/rand"

"github.com/status-im/status-go/eth-node/crypto"
Expand Down Expand Up @@ -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 {
Expand Down
72 changes: 72 additions & 0 deletions protocol/chat_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

}
1 change: 1 addition & 0 deletions protocol/identity/alias/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down
5 changes: 5 additions & 0 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions protocol/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions protocol/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions vendor/github.com/status-im/status-go/protocol/chat.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/status-im/status-go/protocol/messenger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/status-im/status-go/protocol/persistence.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b6ad66

Please sign in to comment.