Skip to content

Commit 0f133c3

Browse files
Add tests for wrong-length seeds and signed payloads (#130)
1 parent 6d3b96e commit 0f133c3

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

tests/tests.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ fn test_valid_private_keys_via_ed25519_private_key() {
9292

9393
#[test]
9494
fn test_invalid_private_keys() {
95-
// Too long strkey input. Strkey does not validate the private-key
96-
// payload — any `S…` version byte returns `PrivateKey` to route the
97-
// caller to `ed25519::PrivateKey`.
98-
let r: Result<Strkey, _> = "SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJV764SE".parse();
99-
assert_eq!(r, Err(DecodeError::PrivateKey));
95+
// Too short, only 31 bytes but must be 32 bytes.
96+
let mut r: Result<ed25519::PrivateKey, _> =
97+
"SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UALAI".parse();
98+
assert_eq!(r, Err(DecodeError::InvalidPayloadLength));
99+
100+
// Too long, 33 bytes but must be 32 bytes.
101+
r = "SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJV764SE".parse();
102+
assert_eq!(r, Err(DecodeError::TooLong));
100103
}
101104

102105
#[test]
@@ -620,6 +623,28 @@ fn test_signed_payload_from_payload_inner_length_boundary() {
620623
assert!(result.is_ok(), "inner payload length 64 should succeed");
621624
}
622625

626+
/// A zero inner-payload length is invalid, but a payload declaring it can
627+
/// still be exactly MIN_LENGTH (40 bytes) by carrying four trailing zero
628+
/// bytes, check that case is prevented.
629+
#[test]
630+
fn test_signed_payload_from_payload_zero_inner_length_at_min_length() {
631+
let payload: &[u8] = &[
632+
// ed25519 public key (32 bytes)
633+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
634+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
635+
0x00, 0x00, // length prefix (4 bytes, big-endian) = 64 (valid)
636+
0x00, 0x00, 0x00, 0x00, // inner payload (4 bytes)
637+
0x00, 0x00, 0x00, 0x00,
638+
];
639+
640+
let result = stellar_strkey::ed25519::SignedPayload::from_payload(&payload);
641+
assert_eq!(
642+
result,
643+
Err(DecodeError::InvalidPayloadLength),
644+
"40 bytes declaring a 0-byte inner payload should fail"
645+
);
646+
}
647+
623648
#[test]
624649
fn test_valid_liquidity_pool() {
625650
assert_convert_roundtrip(

tests/tools/strkey-encode/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ func main() {
189189
0xff, // one byte too long
190190
}),
191191
},
192+
{
193+
"Valid Private Key",
194+
encode(18<<3|0, []byte{
195+
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
196+
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
197+
0xfc, 0x7f, 0xe8, 0x9a,
198+
}),
199+
},
200+
{
201+
"Invalid too short for Private Key",
202+
encode(18<<3|0, []byte{
203+
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
204+
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
205+
0xfc, 0x7f, 0xe8, // 0x9a, one byte short
206+
}),
207+
},
208+
{
209+
"Invalid too long for Private Key",
210+
encode(18<<3|0, []byte{
211+
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
212+
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
213+
0xfc, 0x7f, 0xe8, 0x9a,
214+
0xff, // one byte too long
215+
}),
216+
},
192217
}
193218
csvw := csv.NewWriter(os.Stdout)
194219
err := csvw.WriteAll(recs)

0 commit comments

Comments
 (0)