mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-04 20:44:28 +02:00
Fix panic in ParseMessage
There is a chance the function can panic if a comma is included in the input which is invalid.
This commit is contained in:
@ -35,6 +35,8 @@ func ParseMessage(message []byte) Envelope {
|
|||||||
case bytes.Contains(label, []byte("CLOSE")):
|
case bytes.Contains(label, []byte("CLOSE")):
|
||||||
x := CloseEnvelope("")
|
x := CloseEnvelope("")
|
||||||
v = &x
|
v = &x
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := v.UnmarshalJSON(message); err != nil {
|
if err := v.UnmarshalJSON(message); err != nil {
|
||||||
|
@ -2,6 +2,7 @@ package nostr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -110,3 +111,36 @@ func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseMessage(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
Name string
|
||||||
|
Message []byte
|
||||||
|
ExpectedEnvelope interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "nil",
|
||||||
|
Message: nil,
|
||||||
|
ExpectedEnvelope: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "invalid string",
|
||||||
|
Message: []byte("invalid input"),
|
||||||
|
ExpectedEnvelope: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "invalid string with a comma",
|
||||||
|
Message: []byte("invalid, input"),
|
||||||
|
ExpectedEnvelope: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.Name, func(t *testing.T) {
|
||||||
|
envelope := ParseMessage(testCase.Message)
|
||||||
|
if !reflect.DeepEqual(testCase.ExpectedEnvelope, envelope) {
|
||||||
|
t.Fatal("unexpected output")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user