|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +//! Regression tests for deserializing BSER payloads with a negative |
| 9 | +//! bytestring/utf8string length. The wire length is a signed integer and used |
| 10 | +//! to index into the buffer, so a negative value must be rejected instead of |
| 11 | +//! being reinterpreted as a huge `usize`. |
| 12 | +
|
| 13 | +use std::io::Cursor; |
| 14 | + |
| 15 | +use serde_bser::de::from_reader; |
| 16 | +use serde_bser::de::from_slice; |
| 17 | + |
| 18 | +fn negative_len_bytestring() -> Vec<u8> { |
| 19 | + // BSER magic + capabilities + PDU length (INT8 = 3) + a bytestring whose |
| 20 | + // length is INT8 = -1 (0xFF). |
| 21 | + let mut msg = vec![0x00, 0x02]; |
| 22 | + msg.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); |
| 23 | + msg.extend_from_slice(&[0x03, 0x03]); |
| 24 | + msg.extend_from_slice(&[0x02, 0x03, 0xFF]); |
| 25 | + msg |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn negative_bytestring_length_is_rejected() { |
| 30 | + let msg = negative_len_bytestring(); |
| 31 | + let r: Result<String, _> = from_slice(&msg); |
| 32 | + assert!( |
| 33 | + r.is_err(), |
| 34 | + "expected a deserialization error for a negative bytestring length, got {:?}", |
| 35 | + r |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn negative_bytestring_length_is_rejected_from_reader() { |
| 41 | + let msg = negative_len_bytestring(); |
| 42 | + let r: Result<String, _> = from_reader(Cursor::new(msg)); |
| 43 | + assert!( |
| 44 | + r.is_err(), |
| 45 | + "expected a deserialization error for a negative bytestring length, got {:?}", |
| 46 | + r |
| 47 | + ); |
| 48 | +} |
0 commit comments