use std::io::Write;
use crate::connection::{Framing, RecvBuf, Transport};
use crate::raw::core::{gzip_packed, msg_container};
use crate::raw::{Deserializable, Error, Serializable, base, types};
use crate::storage::{SessionData, session_string};
const MAX_UNPACKED: usize = 32 << 20;
const VECTOR: u32 = 0x1cb5c415;
const RPC_RESULT: u32 = 0xf35c6d01;
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn below(&mut self, n: usize) -> usize {
(self.next() % n.max(1) as u64) as usize
}
fn bytes(&mut self, n: usize) -> Vec<u8> {
(0..n).map(|_| self.next() as u8).collect()
}
fn mutate(&mut self, seed: &[u8]) -> Vec<u8> {
let mut b = seed.to_vec();
for _ in 0..1 + self.below(4) {
let i = self.below(b.len());
match self.below(6) {
0 if i < b.len() => b[i] ^= 1 << self.below(8),
1 if i < b.len() => b[i] = [0, 1, 0x7f, 0x80, 0xfe, 0xff][self.below(6)],
2 if i + 4 <= b.len() => {
let j = self.below(b.len() - 3);
let word = match self.below(3) {
0 => [0xff; 4],
1 => i32::MAX.to_le_bytes(),
_ => b[j..j + 4].try_into().unwrap(),
};
b[i..i + 4].copy_from_slice(&word);
}
3 => b.truncate(i),
4 => {
let n = self.below(16);
let noise = self.bytes(n);
b.splice(i..i, noise);
}
_ => {
let end = i + self.below(b.len() - i + 1);
let chunk = b[i..end].to_vec();
let at = self.below(b.len() + 1);
b.splice(at..at, chunk);
}
}
}
b
}
}
fn tl(parts: &[&dyn Serializable]) -> Vec<u8> {
let mut b = Vec::new();
parts.iter().for_each(|p| p.serialize(&mut b));
b
}
fn gzip(data: &[u8]) -> Vec<u8> {
let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
gz.write_all(data).unwrap();
tl(&[&gzip_packed::ID, &gz.finish().unwrap()])
}
fn message() -> Vec<u8> {
message_with(&tl(&[&0xe216eb63u32, &0u32]))
}
fn message_with(media: &[u8]) -> Vec<u8> {
let flags = 1u32 << 1 | 1 << 7 | 1 << 9;
let (text, url) = ("hello *bold*".to_string(), "https://t.me".to_string());
let (bold, text_url) = (0xbd610bc9u32, 0x76a6d327u32);
let head = tl(&[&0x7600b9d3u32, &flags, &0u32, &42, &0x59511722u32, &777000i64, &1_700_000_000, &text]);
let entities = tl(&[&VECTOR, &2u32, &bold, &6, &6, &text_url, &0, &5, &url]);
[head, media.to_vec(), entities].concat()
}
fn web_page(bold: usize) -> Vec<u8> {
let url = "https://t.me".to_string();
let text = [tl(&[&0x6724abc4u32]).repeat(bold), tl(&[&0x744694e0u32, &url])].concat();
let page = tl(&[&0x98657f0du32, &0u32, &url, &VECTOR, &1u32, &0x467a0766u32]);
let web_page = tl(&[&0xddf10c3bu32, &0u32, &0xe89c45b2u32, &(1u32 << 10), &1i64, &url, &url, &0]);
[web_page, page, text, tl(&[&VECTOR, &0u32, &VECTOR, &0u32])].concat()
}
fn story(depth: usize) -> Vec<u8> {
let media = tl(&[&0x68cb6283u32, &1u32, &0x59511722u32, &1i64, &1, &0x16a4b93cu32, &0u32, &1, &0, &0]);
[media.repeat(depth), tl(&[&0x68cb6283u32, &0u32, &0x59511722u32, &1i64, &1])].concat()
}
fn deepest(media: fn(usize) -> Vec<u8>) -> Vec<u8> {
let message = |n| message_with(&media(n));
let n = (1..).take_while(|&n| base::Message::from_bytes(&message(n)).is_ok()).last().unwrap();
assert_eq!(base::Message::from_bytes(&message(n + 1)).err(), Some(Error::TooDeep));
message(n)
}
fn updates() -> Vec<u8> {
let new_message = [tl(&[&0x74ae4240u32, &VECTOR, &2u32, &0x1f2b0afdu32]), message(), tl(&[&40, &1])].concat();
let deleted = tl(&[&0xa20db0e5u32, &VECTOR, &2u32, &1, &2, &42, &2]);
let rest = tl(&[&VECTOR, &1u32, &0xd3bc4b7au32, &777000i64, &VECTOR, &0u32, &1_700_000_000, &5]);
[new_message, deleted, rest].concat()
}
fn container(messages: &[Vec<u8>]) -> Vec<u8> {
let mut b = tl(&[&msg_container::ID, &(messages.len() as u32)]);
for (i, m) in messages.iter().enumerate() {
b.extend(tl(&[&(i as i64 * 4 + 1), &(i as i32 * 2 + 1), &(m.len() as u32)]));
b.extend(m);
}
b
}
fn corpus() -> Vec<Vec<u8>> {
let updates = updates();
let rpc_result = |result: &[u8]| [tl(&[&RPC_RESULT, &7i64]), result.to_vec()].concat();
let flood = tl(&[&0x2144ca19u32, &420, &"FLOOD_WAIT_30".to_string()]);
let short = tl(&[&0x78d4dec1u32, &0xa20db0e5u32, &VECTOR, &1u32, &5, &10, &1, &1_700_000_000]);
let mut all =
vec![message(), short, rpc_result(&updates), rpc_result(&flood), rpc_result(&gzip(&updates)), gzip(&updates)];
all.push(container(&all));
all.extend([deepest(web_page), deepest(story)]);
all.push(updates);
all
}
fn constructor_id(b: &[u8]) -> Option<u32> {
Some(u32::from_le_bytes(b.get(..4)?.try_into().unwrap()))
}
fn packet(body: &[u8]) -> bool {
if constructor_id(body) == Some(msg_container::ID) {
msg_container::parse(body).unwrap_or_default().iter().fold(false, |deep, m| message_body(m.body) | deep)
} else {
message_body(body)
}
}
fn message_body(body: &[u8]) -> bool {
match constructor_id(body) {
Some(gzip_packed::ID) => gzip_packed::unpack(body).is_ok_and(|b| {
assert!(b.len() <= MAX_UNPACKED);
message_body(&b)
}),
Some(RPC_RESULT) => message_body(body.get(12..).unwrap_or_default()),
_ => {
let too_deep = |r: Result<(), Error>| r == Err(Error::TooDeep);
[
base::Updates::from_bytes(body).map(drop),
base::Update::from_bytes(body).map(drop),
base::Message::from_bytes(body).map(drop),
base::messages::Messages::from_bytes(body).map(drop),
base::RpcError::from_bytes(body).map(drop),
]
.into_iter()
.any(too_deep)
}
}
}
#[test]
fn mutated_tl_never_panics() {
let corpus = corpus();
for c in &corpus {
assert!(!packet(c), "a seed is too deep already");
}
assert!(base::Message::from_bytes(&corpus[0]).is_ok());
assert!(base::Updates::from_bytes(&corpus[corpus.len() - 1]).is_ok());
let mut rng = Rng(0x9e37_79b9_7f4a_7c15);
let mut too_deep = 0;
for _ in 0..20_000 {
let seed = &corpus[rng.below(corpus.len())];
too_deep += usize::from(packet(&rng.mutate(seed)));
}
assert!(too_deep > 0, "no mutation reached the nesting limit");
for len in 0..512 {
let mut b = rng.bytes(len);
let seed = &corpus[rng.below(corpus.len())];
let n = len.min(4);
b[..n].copy_from_slice(&seed[..n]);
packet(&b);
}
}
fn gzip_bomb(unpacked: usize) -> Vec<u8> {
let mut gz = vec![0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 0xff];
let (mut acc, mut bits) = (0u32, 0);
let mut put = |gz: &mut Vec<u8>, code: u32, n: u32| {
for i in (0..n).rev() {
acc |= (code >> i & 1) << bits;
bits += 1;
}
while bits >= 8 {
gz.push(acc as u8);
acc >>= 8;
bits -= 8;
}
};
put(&mut gz, 0b110, 3);
put(&mut gz, 0x30, 8);
for _ in 0..unpacked / 258 {
put(&mut gz, 0xc5, 8);
put(&mut gz, 0, 5);
}
put(&mut gz, 0, 7 + 7);
gz.extend([0; 8]);
tl(&[&gzip_packed::ID, &gz])
}
#[test]
fn gzip_bombs_stop_at_the_cap() {
assert_eq!(gzip_packed::unpack(&gzip_bomb(MAX_UNPACKED + (1 << 20))).map(|b| b.len()), Ok(MAX_UNPACKED));
}
#[test]
fn deep_nesting_is_an_error_not_a_stack_overflow() {
let bold = |depth: usize| [tl(&[&0x6724abc4u32]).repeat(depth), tl(&[&0xdc3d824fu32])].concat();
assert!(base::RichText::from_bytes(&bold(20)).is_ok());
assert!(base::RichText::from_bytes(&bold(100_000)).is_err());
let array = tl(&[&0xf7444763u32, &VECTOR, &1u32]).repeat(100_000);
assert!(base::JSONValue::from_bytes(&[array, tl(&[&0x3f6d7b68u32])].concat()).is_err());
}
#[test]
fn transports_survive_garbage() {
let encrypted: Vec<u8> = (0..24 + 32).map(|i| i as u8 | 1).collect();
let plain = [vec![0; 16], 8u32.to_le_bytes().to_vec(), b"abcd1234".to_vec()].concat();
let mut rng = Rng(7);
for transport in [Transport::Abridged, Transport::Intermediate, Transport::PaddedIntermediate, Transport::Full] {
let mut frames = Vec::new();
for payload in [&encrypted[..], &plain, &(-404i32).to_le_bytes()] {
let mut frame = vec![0; Framing::ROOM];
frame.extend_from_slice(payload);
let start = Framing::new(transport).frame(&mut frame);
frames.push(frame.split_off(start));
}
for i in 0..5000 {
let input = match i % 3 {
2 => {
let n = rng.below(64);
rng.bytes(n)
}
_ => {
let frame = &frames[rng.below(frames.len())];
rng.mutate(frame)
}
};
let mut buf = RecvBuf::default();
buf.extend_from_slice(&input);
if let Ok(Some(p)) = Framing::new(transport).decode(&mut buf) {
assert!(p.len() <= input.len(), "{transport:?} {input:?}");
}
}
for header in [[0xff; 4], [0x7f, 0xff, 0xff, 0xff], (16u32 << 20 | 4).to_le_bytes()] {
let mut buf = RecvBuf::default();
buf.extend_from_slice(&header);
let decoded = Framing::new(transport).decode(&mut buf);
assert!(!matches!(decoded, Ok(Some(_))), "{transport:?} {header:?}");
}
}
}
#[test]
fn session_strings_survive_garbage() {
let mut s = SessionData::new(2, [7; 256]);
(s.api_id, s.user_id) = (12345, 777000);
s.addr = Some("149.154.167.51:443".parse().unwrap());
let with_address = session_string::export(&s);
s.addr = None;
let seeds = [with_address, session_string::export(&s)];
let mut rng = Rng(3);
for _ in 0..10_000 {
let seed = seeds[rng.below(2)].as_bytes();
let b = rng.mutate(seed);
let _ = session_string::parse(&String::from_utf8_lossy(&b));
}
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
for len in (0..450).chain([351, 352, 362, 367, 368, 392, 435]) {
let s: String = (0..len).map(|_| ALPHABET[rng.below(64)] as char).collect();
let _ = session_string::parse(&s);
let _ = session_string::parse(&format!("{}{s}", session_string::PREFIX));
let _ = session_string::parse(&format!("1{s}"));
}
}
#[test]
fn extreme_peer_ids_do_not_overflow() {
use crate::utils::{get_peer, get_peer_id};
for id in [i64::MIN, i64::MAX] {
get_peer_id(&base::Peer::PeerChat(types::PeerChat { chat_id: id }));
get_peer_id(&base::Peer::PeerChannel(types::PeerChannel { channel_id: id }));
get_peer(id);
}
}