add size validation to nip19 decoder.

This commit is contained in:
fiatjaf 2023-08-31 09:25:09 -03:00
parent 63614bb152
commit b8eeb658cc
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1

View File

@ -44,6 +44,9 @@ func Decode(bech32string string) (prefix string, value any, err error) {
switch t {
case TLVDefault:
if len(v) < 32 {
return prefix, nil, fmt.Errorf("pubkey is less than 32 bytes (%d)", len(v))
}
result.PublicKey = hex.EncodeToString(v)
case TLVRelay:
result.Relays = append(result.Relays, string(v))
@ -69,10 +72,16 @@ func Decode(bech32string string) (prefix string, value any, err error) {
switch t {
case TLVDefault:
if len(v) < 32 {
return prefix, nil, fmt.Errorf("id is less than 32 bytes (%d)", len(v))
}
result.ID = hex.EncodeToString(v)
case TLVRelay:
result.Relays = append(result.Relays, string(v))
case TLVAuthor:
if len(v) < 32 {
return prefix, nil, fmt.Errorf("author is less than 32 bytes (%d)", len(v))
}
result.Author = hex.EncodeToString(v)
case TLVKind:
result.Kind = int(binary.BigEndian.Uint32(v))
@ -102,6 +111,9 @@ func Decode(bech32string string) (prefix string, value any, err error) {
case TLVRelay:
result.Relays = append(result.Relays, string(v))
case TLVAuthor:
if len(v) < 32 {
return prefix, nil, fmt.Errorf("author is less than 32 bytes (%d)", len(v))
}
result.PublicKey = hex.EncodeToString(v)
case TLVKind:
result.Kind = int(binary.BigEndian.Uint32(v))