swarm/pss: Reduce input vulnerabilities (#18304)

This commit is contained in:
lash
2018-12-18 15:23:32 +01:00
committed by Anton Evangelatov
parent de4265fa02
commit b01cfce362
6 changed files with 116 additions and 60 deletions

View File

@ -92,7 +92,7 @@ func (pssapi *API) Receive(ctx context.Context, topic Topic, raw bool, prox bool
}
func (pssapi *API) GetAddress(topic Topic, asymmetric bool, key string) (PssAddress, error) {
var addr *PssAddress
var addr PssAddress
if asymmetric {
peer, ok := pssapi.Pss.pubKeyPool[key][topic]
if !ok {
@ -107,7 +107,7 @@ func (pssapi *API) GetAddress(topic Topic, asymmetric bool, key string) (PssAddr
addr = peer.address
}
return *addr, nil
return addr, nil
}
// Retrieves the node's base address in hex form
@ -128,7 +128,7 @@ func (pssapi *API) SetPeerPublicKey(pubkey hexutil.Bytes, topic Topic, addr PssA
if err != nil {
return fmt.Errorf("Cannot unmarshal pubkey: %x", pubkey)
}
err = pssapi.Pss.SetPeerPublicKey(pk, topic, &addr)
err = pssapi.Pss.SetPeerPublicKey(pk, topic, addr)
if err != nil {
return fmt.Errorf("Invalid key: %x", pk)
}
@ -141,11 +141,11 @@ func (pssapi *API) GetSymmetricKey(symkeyid string) (hexutil.Bytes, error) {
}
func (pssapi *API) GetSymmetricAddressHint(topic Topic, symkeyid string) (PssAddress, error) {
return *pssapi.Pss.symKeyPool[symkeyid][topic].address, nil
return pssapi.Pss.symKeyPool[symkeyid][topic].address, nil
}
func (pssapi *API) GetAsymmetricAddressHint(topic Topic, pubkeyid string) (PssAddress, error) {
return *pssapi.Pss.pubKeyPool[pubkeyid][topic].address, nil
return pssapi.Pss.pubKeyPool[pubkeyid][topic].address, nil
}
func (pssapi *API) StringToTopic(topicstring string) (Topic, error) {
@ -157,14 +157,23 @@ func (pssapi *API) StringToTopic(topicstring string) (Topic, error) {
}
func (pssapi *API) SendAsym(pubkeyhex string, topic Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendAsym(pubkeyhex, topic, msg[:])
}
func (pssapi *API) SendSym(symkeyhex string, topic Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendSym(symkeyhex, topic, msg[:])
}
func (pssapi *API) SendRaw(addr hexutil.Bytes, topic Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendRaw(PssAddress(addr), topic, msg[:])
}
@ -177,3 +186,10 @@ func (pssapi *API) GetPeerTopics(pubkeyhex string) ([]Topic, error) {
func (pssapi *API) GetPeerAddress(pubkeyhex string, topic Topic) (PssAddress, error) {
return pssapi.Pss.getPeerAddress(pubkeyhex, topic)
}
func validateMsg(msg []byte) error {
if len(msg) == 0 {
return errors.New("invalid message length")
}
return nil
}