Skip to main content

wechat_protocol/
media.rs

1//! Bounded native CDN transport. This uses the observed HTTP carrier on port 443,
2//! not HTTPS. Media encryption and advertised size/digests are checked separately.
3use crate::errors::{Service, ServiceRejected};
4use crate::{
5    crypto::{md5, random, sha256},
6    protobuf as p, AccountState,
7};
8use anyhow::{bail, ensure, Result};
9use openssl::{
10    hash::{Hasher, MessageDigest},
11    symm::{Cipher, Crypter, Mode},
12};
13use serde::{Deserialize, Serialize};
14use std::{collections::BTreeMap, fmt, io::Read, net::Ipv4Addr, time::Duration};
15use zeroize::Zeroize;
16pub type CdnFields = BTreeMap<String, Vec<u8>>;
17const MAX_CDN_BODY: usize = 2 * 1024 * 1024;
18const UPLOAD_CHUNK: usize = 65536;
19pub const MAX_CDN_TRANSFER_BYTES: usize = 25 * 1024 * 1024;
20pub const MAX_NATIVE_UPLOAD_BYTES: usize = 25 * 1024 * 1024 - 17;
21pub const MAX_LARGE_FILE_BYTES: usize = wechat_media::MAX_FILE_BYTES;
22/// Incoming video plaintext limit, matching the ordinary-file download and
23/// automatic-download ceiling. Every such transfer stays bound to the
24/// advertised length and MD5. Outgoing preparation keeps the separate
25/// `wechat_media::MAX_VIDEO_SOURCE_BYTES` source limit.
26pub const MAX_VIDEO_DOWNLOAD_BYTES: usize = wechat_media::MAX_FILE_BYTES;
27/// A video advertised at or below the ordinary transfer threshold is fetched
28/// before its length is compared, so its reply keeps the earlier 50 MiB bound.
29const MAX_SMALL_VIDEO_REPLY_BYTES: usize = 50 * 1024 * 1024;
30#[path = "large_cdn_config.rs"]
31mod large_cdn_config;
32#[path = "large_file.rs"]
33mod large_file;
34pub use large_cdn_config::{decode_large_cdn_config, LargeCdnRoute};
35#[path = "avatar.rs"]
36mod avatar;
37pub use avatar::*;
38#[path = "link_thumbnail.rs"]
39mod link_thumbnail;
40pub use link_thumbnail::*;
41#[path = "channels_video.rs"]
42mod channels_video;
43pub use channels_video::*;
44#[path = "sticker.rs"]
45mod sticker;
46pub use sticker::*;
47#[path = "upload_source.rs"]
48mod upload_source;
49pub use upload_source::PreparedFileUpload;
50use upload_source::UploadRef;
51
52#[derive(Clone, Copy, Serialize, Deserialize)]
53pub struct CdnLocation {
54    pub province: u32,
55    pub city: u32,
56    pub isp: u32,
57}
58#[derive(Clone, Serialize, Deserialize)]
59pub struct CdnConfig {
60    pub version: u32,
61    pub uin: u64,
62    pub client_version: u32,
63    pub client_os_type: String,
64    pub auth_key: Vec<u8>,
65    pub server_ips: Vec<Ipv4Addr>,
66    #[serde(default)]
67    pub front_id: u32,
68    #[serde(default)]
69    pub location: Option<CdnLocation>,
70}
71impl Drop for CdnConfig {
72    fn drop(&mut self) {
73        self.auth_key.zeroize();
74    }
75}
76impl fmt::Debug for CdnConfig {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        f.debug_struct("CdnConfig")
79            .field("credentials", &"[redacted]")
80            .finish()
81    }
82}
83impl CdnConfig {
84    pub fn validate(&self) -> Result<()> {
85        ensure!(
86            self.uin != 0
87                && !self.auth_key.is_empty()
88                && self.auth_key.len() <= 65536
89                && !self.client_os_type.is_empty()
90                && self.client_os_type.len() <= 1024
91                && !self.server_ips.is_empty()
92                && self.server_ips.len() <= 32,
93            "Invalid CDN config"
94        );
95        for ip in &self.server_ips {
96            ensure!(public_ipv4(*ip), "CDN endpoint is not public IPv4");
97        }
98        Ok(())
99    }
100}
101pub fn decode_cdn_config(input: &[u8], account: &AccountState) -> Result<CdnConfig> {
102    let f = p::decode(input)?;
103    let code = p::base_code(&f)?;
104    ServiceRejected::check(Service::CdnBootstrap, code)?;
105    let group = p::decode(p::data(&f, 2)?)?;
106    let version = u32::try_from(p::scalar(&group, 1)?)?;
107    let uin = p::scalar(&group, 2)?;
108    ensure!(uin == account.uin as u64, "CDN account identity mismatch");
109    let auth_key = p::unbuffer(p::data(&group, 8)?)?;
110    let mut server_ips = vec![];
111    for field in group.iter().filter(|f| f.number == 6) {
112        let p::Value::Bytes(raw) = &field.value else {
113            bail!("Invalid CDN endpoint record");
114        };
115        let ip = p::decode(raw)?;
116        server_ips.push(std::str::from_utf8(p::data(&ip, 1)?)?.parse()?);
117    }
118    let conf = CdnConfig {
119        version,
120        uin,
121        client_version: account.device.client_version as u32,
122        client_os_type: account.device.device_type.clone(),
123        auth_key,
124        server_ips,
125        front_id: 0,
126        location: None,
127    };
128    conf.validate()?;
129    Ok(conf)
130}
131fn public_ipv4(ip: Ipv4Addr) -> bool {
132    let [a, b, c, _] = ip.octets();
133    !(a == 0
134        || a == 10
135        || a == 127
136        || a >= 224
137        || (a == 100 && (64..=127).contains(&b))
138        || (a == 169 && b == 254)
139        || (a == 172 && (16..=31).contains(&b))
140        || (a == 192 && (b == 0 || b == 168 || (b == 2 && c == 0)))
141        || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100)))
142        || (a == 203 && b == 0 && c == 113))
143}
144pub fn pack_cdn(fields: &CdnFields) -> Result<Vec<u8>> {
145    // A 512 MiB completion can contain 1025 ordered block etags. The byte
146    // envelope remains independently bounded, including individual values.
147    ensure!(fields.len() <= 2048, "Too many CDN fields");
148    let mut out = vec![];
149    for (k, v) in fields {
150        ensure!(
151            !k.is_empty()
152                && k.len() <= 128
153                && k.bytes()
154                    .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'),
155            "Invalid CDN key"
156        );
157        for b in [k.as_bytes(), v.as_slice()] {
158            ensure!(b.len() <= MAX_CDN_BODY, "CDN field too large");
159            out.extend((b.len() as u32).to_be_bytes());
160            out.extend(b);
161            ensure!(out.len() <= MAX_CDN_BODY, "CDN request exceeds limit");
162        }
163    }
164    Ok(out)
165}
166pub fn unpack_cdn(input: &[u8]) -> Result<CdnFields> {
167    ensure!(input.len() <= MAX_CDN_BODY, "CDN reply exceeds limit");
168    let mut c = crate::mmtls::Cursor::new(input);
169    let mut out = CdnFields::new();
170    let mut consumed = 0;
171    while consumed < input.len() {
172        ensure!(out.len() < 256, "Too many CDN reply fields");
173        let key = c.sized(4)?;
174        let value = c.sized(4)?;
175        consumed += 8 + key.len() + value.len();
176        ensure!(
177            !key.is_empty()
178                && key.len() <= 128
179                && key
180                    .iter()
181                    .all(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'-'),
182            "Invalid CDN reply key"
183        );
184        let key = std::str::from_utf8(key)?.to_string();
185        ensure!(
186            out.insert(key, value.to_vec()).is_none(),
187            "Duplicate CDN reply key"
188        );
189    }
190    c.end()?;
191    Ok(out)
192}
193fn integer(fields: &CdnFields, key: &str) -> Result<i64> {
194    let raw = fields
195        .get(key)
196        .ok_or_else(|| anyhow::anyhow!("Missing CDN integer {key}"))?;
197    let text = std::str::from_utf8(raw)?;
198    ensure!(
199        !text.is_empty()
200            && text.len() <= 16
201            && text
202                .bytes()
203                .enumerate()
204                .all(|(i, b)| b.is_ascii_digit() || (i == 0 && b == b'-')),
205        "Invalid CDN integer {key}"
206    );
207    Ok(text.parse()?)
208}
209fn put(fields: &mut CdnFields, key: &str, value: impl ToString) {
210    fields.insert(key.to_string(), value.to_string().into_bytes());
211}
212fn shared(conf: &CdnConfig, sequence: u32, filetype: u32) -> CdnFields {
213    let mut f = CdnFields::new();
214    for (k, v) in [
215        ("ver", conf.version as u64),
216        ("weixinnum", conf.uin),
217        ("seq", sequence as u64),
218        ("clientversion", conf.client_version as u64),
219        ("nettype", 1),
220        ("acceptdupack", 1),
221        ("safeproto", 1),
222        ("filetype", filetype as u64),
223        ("wxchattype", 0),
224        ("lastretcode", 0),
225        ("ipseq", 0),
226        ("largesvideo", 0),
227        ("sourceflag", 0),
228    ] {
229        put(&mut f, k, v);
230    }
231    f.insert(
232        "clientostype".into(),
233        conf.client_os_type.as_bytes().to_vec(),
234    );
235    f.insert("authkey".into(), conf.auth_key.clone());
236    f
237}
238pub fn encrypt_recipient(destination: &str) -> Result<String> {
239    ensure!(
240        !destination.is_empty()
241            && destination.len() <= 512
242            && !destination
243                .chars()
244                .any(|c| c.is_control() || c.is_whitespace()),
245        "Invalid CDN recipient"
246    );
247    let key = b"wxusrname2016cdn";
248    let encrypted = openssl::symm::encrypt(
249        Cipher::aes_128_cbc(),
250        key,
251        Some(key),
252        destination.as_bytes(),
253    )?;
254    Ok(format!("@cdn2_{}", hex::encode(encrypted)))
255}
256fn ecb_encrypt(raw: &[u8], key: &[u8]) -> Result<Vec<u8>> {
257    ensure!(key.len() == 16, "Invalid CDN key");
258    Ok(openssl::symm::encrypt(
259        Cipher::aes_128_ecb(),
260        key,
261        None,
262        raw,
263    )?)
264}
265pub fn decrypt_cdn(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> {
266    ensure!(
267        key.len() == 16
268            && !encrypted.is_empty()
269            && encrypted.len().is_multiple_of(16)
270            && encrypted.len() <= MAX_LARGE_FILE_BYTES + 16,
271        "Invalid CDN ciphertext"
272    );
273    Ok(openssl::symm::decrypt(
274        Cipher::aes_128_ecb(),
275        key,
276        None,
277        encrypted,
278    )?)
279}
280
281#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
282#[serde(rename_all = "snake_case")]
283pub enum MediaQuality {
284    Original,
285    Standard,
286    Thumbnail,
287    Video,
288    File,
289    LargeFile,
290}
291impl MediaQuality {
292    pub fn filetype(self) -> u32 {
293        match self {
294            Self::Original => 1,
295            Self::Standard => 2,
296            Self::Thumbnail => 3,
297            Self::Video => 4,
298            Self::File => 5,
299            Self::LargeFile => 7,
300        }
301    }
302}
303fn upload_plaintext_limit(kind: MediaQuality) -> usize {
304    match kind {
305        MediaQuality::File => MAX_CDN_TRANSFER_BYTES,
306        MediaQuality::LargeFile => MAX_LARGE_FILE_BYTES,
307        MediaQuality::Video => wechat_media::MAX_VIDEO_SOURCE_BYTES,
308        _ => MAX_NATIVE_UPLOAD_BYTES,
309    }
310}
311#[derive(Clone, Serialize, Deserialize)]
312pub struct MediaDescriptor {
313    pub file_id: String,
314    pub aes_key: Vec<u8>,
315    pub quality: MediaQuality,
316    pub expected_bytes: Option<u64>,
317    pub expected_md5: Option<String>,
318    pub strict_size: bool,
319    pub expires_at: Option<u64>,
320}
321impl Drop for MediaDescriptor {
322    fn drop(&mut self) {
323        self.aes_key.zeroize();
324        self.file_id.zeroize();
325    }
326}
327impl fmt::Debug for MediaDescriptor {
328    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
329        f.debug_struct("MediaDescriptor")
330            .field("quality", &self.quality)
331            .field("metadata", &"[redacted]")
332            .finish()
333    }
334}
335pub struct DownloadedMedia {
336    pub bytes: Vec<u8>,
337    pub requested_quality: MediaQuality,
338    pub substituted_filetype: Option<u32>,
339    pub picture_format: Option<u32>,
340    pub md5: String,
341    pub sha256: String,
342    pub advertised_size_matches: Option<bool>,
343}
344/// What a download into a writer verified: everything [`DownloadedMedia`]
345/// reports except the bytes, which went to the writer.
346#[derive(Clone, Debug)]
347pub struct DownloadReceipt {
348    /// Plaintext bytes written.
349    pub byte_length: u64,
350    /// Quality the download asked for.
351    pub requested_quality: MediaQuality,
352    /// File type the CDN substituted for the requested one, when it said so.
353    pub substituted_filetype: Option<u32>,
354    /// Picture format the CDN reported, when it said so.
355    pub picture_format: Option<u32>,
356    /// Lowercase hex MD5 of the plaintext.
357    pub md5: String,
358    /// Lowercase hex SHA-256 of the plaintext.
359    pub sha256: String,
360    /// Whether the plaintext length matched the advertised size, when one was advertised.
361    pub advertised_size_matches: Option<bool>,
362}
363impl DownloadedMedia {
364    /// The bytes and the receipt describing them.
365    pub fn into_parts(self) -> (Vec<u8>, DownloadReceipt) {
366        let receipt = DownloadReceipt {
367            byte_length: self.bytes.len() as u64,
368            requested_quality: self.requested_quality,
369            substituted_filetype: self.substituted_filetype,
370            picture_format: self.picture_format,
371            md5: self.md5,
372            sha256: self.sha256,
373            advertised_size_matches: self.advertised_size_matches,
374        };
375        (self.bytes, receipt)
376    }
377    fn from_receipt(receipt: DownloadReceipt, bytes: Vec<u8>) -> Self {
378        Self {
379            bytes,
380            requested_quality: receipt.requested_quality,
381            substituted_filetype: receipt.substituted_filetype,
382            picture_format: receipt.picture_format,
383            md5: receipt.md5,
384            sha256: receipt.sha256,
385            advertised_size_matches: receipt.advertised_size_matches,
386        }
387    }
388}
389#[derive(Clone, Serialize, Deserialize)]
390pub struct PreparedUpload {
391    pub kind: MediaQuality,
392    pub aes_key: Vec<u8>,
393    pub file_key: String,
394    pub encrypted: Vec<u8>,
395    pub plaintext_bytes: u32,
396    pub md5: String,
397    pub sha256: String,
398    pub file_name: String,
399    pub destination: String,
400}
401impl Drop for PreparedUpload {
402    fn drop(&mut self) {
403        self.aes_key.zeroize();
404        self.file_key.zeroize();
405        self.encrypted.zeroize();
406    }
407}
408impl fmt::Debug for PreparedUpload {
409    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
410        f.debug_struct("PreparedUpload")
411            .field("kind", &self.kind)
412            .field("plaintext_bytes", &self.plaintext_bytes)
413            .field("material", &"[redacted]")
414            .finish()
415    }
416}
417impl PreparedUpload {
418    /// Generate independent upload material. Persist either this result or the
419    /// source bytes and its key/file-key before the first upload request.
420    pub fn prepare(
421        raw: &[u8],
422        kind: MediaQuality,
423        file_name: &str,
424        destination: &str,
425    ) -> Result<Self> {
426        Self::with_material(
427            raw,
428            kind,
429            file_name,
430            destination,
431            &random::<16>()?,
432            &hex::encode(random::<16>()?),
433        )
434    }
435    /// Rebuild exactly the same encrypted upload from durable owned source bytes.
436    pub fn with_material(
437        raw: &[u8],
438        kind: MediaQuality,
439        file_name: &str,
440        destination: &str,
441        key: &[u8],
442        file_key: &str,
443    ) -> Result<Self> {
444        ensure!(
445            !raw.is_empty()
446                && raw.len() <= upload_plaintext_limit(kind)
447                && [
448                    MediaQuality::File,
449                    MediaQuality::LargeFile,
450                    MediaQuality::Original,
451                    MediaQuality::Video
452                ]
453                .contains(&kind),
454            "Unsupported upload profile"
455        );
456        ensure!(
457            !file_name.is_empty()
458                && file_name.len() <= 255
459                && file_name != "."
460                && file_name != ".."
461                && !file_name.contains(['/', '\\'])
462                && !file_name.chars().any(|c| c.is_control()),
463            "Invalid upload filename"
464        );
465        ensure!(
466            key.len() == 16
467                && file_key.len() == 32
468                && file_key.bytes().all(|b| b.is_ascii_hexdigit()),
469            "Invalid durable upload material"
470        );
471        encrypt_recipient(destination)?;
472        Ok(Self {
473            kind,
474            aes_key: key.to_vec(),
475            file_key: file_key.to_string(),
476            encrypted: ecb_encrypt(raw, key)?,
477            plaintext_bytes: raw.len() as u32,
478            md5: hex::encode(md5(raw)),
479            sha256: hex::encode(sha256(raw)),
480            file_name: file_name.into(),
481            destination: destination.into(),
482        })
483    }
484    fn validate(&self) -> Result<()> {
485        ensure!(
486            self.aes_key.len() == 16
487                && self.file_key.len() == 32
488                && self.file_key.bytes().all(|b| b.is_ascii_hexdigit())
489                && self.plaintext_bytes > 0
490                && self.plaintext_bytes as usize <= upload_plaintext_limit(self.kind)
491                && self.encrypted.len() == ((self.plaintext_bytes as usize / 16) + 1) * 16,
492            "Invalid prepared upload"
493        );
494        // Validate persisted ciphertext without allocating a second whole file.
495        let mut decryptor =
496            Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, &self.aes_key, None)?;
497        let mut buffer = zeroize::Zeroizing::new(vec![0; 128 * 1024 + 16]);
498        let mut md5 = Hasher::new(MessageDigest::md5())?;
499        let mut sha256 = Hasher::new(MessageDigest::sha256())?;
500        let mut plaintext_bytes = 0usize;
501        for chunk in self.encrypted.chunks(128 * 1024) {
502            let written = decryptor.update(chunk, &mut buffer)?;
503            md5.update(&buffer[..written])?;
504            sha256.update(&buffer[..written])?;
505            plaintext_bytes += written;
506        }
507        let written = decryptor.finalize(&mut buffer)?;
508        md5.update(&buffer[..written])?;
509        sha256.update(&buffer[..written])?;
510        plaintext_bytes += written;
511        ensure!(
512            plaintext_bytes == self.plaintext_bytes as usize
513                && hex::encode(md5.finish()?) == self.md5
514                && hex::encode(sha256.finish()?) == self.sha256,
515            "Prepared upload digest mismatch"
516        );
517        Ok(())
518    }
519}
520#[derive(Clone, Serialize, Deserialize)]
521pub struct UploadedMedia {
522    pub descriptor: MediaDescriptor,
523    pub plaintext_bytes: u32,
524    pub md5: String,
525    pub sha256: String,
526    pub file_name: String,
527    pub width: Option<u32>,
528    pub height: Option<u32>,
529}
530pub struct CdnClient {
531    config: CdnConfig,
532    timeout: Duration,
533}
534fn large_file_path(path: &str, uin: u64, sequence: u32) -> bool {
535    let parts: Vec<_> = path.split('/').collect();
536    parts.len() == 6
537        && parts[0].is_empty()
538        && parts[1] == "cdn"
539        && parts[2] == uin.to_string()
540        && sequence > 0
541        && parts[3] == sequence.to_string()
542        && matches!(parts[4], "4" | "5" | "7")
543        && matches!(parts[5], "apply" | "uploadblock" | "fin" | "complete")
544}
545impl CdnClient {
546    pub fn new(config: CdnConfig, timeout: Duration) -> Result<Self> {
547        config.validate()?;
548        ensure!(
549            !timeout.is_zero() && timeout <= Duration::from_secs(120),
550            "Invalid CDN timeout"
551        );
552        Ok(Self { config, timeout })
553    }
554    fn post(&self, path: &str, fields: &CdnFields) -> Result<CdnFields> {
555        let sequence = u32::try_from(integer(fields, "seq")?)?;
556        ensure!(sequence != 0, "Invalid CDN sequence");
557        ensure!(
558            ["/download", "/downloadbig", "/downloadstorage", "/uploadv3"].contains(&path)
559                || large_file_path(path, self.config.uin, sequence),
560            "Invalid CDN path"
561        );
562        let body = pack_cdn(fields)?;
563        let client = reqwest::blocking::Client::builder()
564            .timeout(self.timeout)
565            .redirect(reqwest::redirect::Policy::none())
566            .no_proxy()
567            .build()?;
568        let mut response = client
569            .post(format!("http://{}:443{path}", self.config.server_ips[0]))
570            .header("Accept", "*/*")
571            .header("Content-Type", "application/octet-stream")
572            .header("User-Agent", "MicroMessenger Client")
573            .header("Connection", "close")
574            .header("C-Seq", sequence)
575            .body(body)
576            .send()?;
577        ensure!(
578            response.status() == 200,
579            "CDN HTTP status {}",
580            response.status().as_u16()
581        );
582        let mut raw = vec![];
583        response
584            .by_ref()
585            .take((MAX_CDN_BODY + 1) as u64)
586            .read_to_end(&mut raw)?;
587        unpack_cdn(&raw)
588    }
589    pub fn download(&self, descriptor: &MediaDescriptor) -> Result<DownloadedMedia> {
590        self.download_with_progress(descriptor, |_, _| Ok(()))
591    }
592    pub fn download_with_progress(
593        &self,
594        descriptor: &MediaDescriptor,
595        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
596    ) -> Result<DownloadedMedia> {
597        self.download_with_transport(descriptor, progress, |path, fields| self.post(path, fields))
598    }
599    /// Download into `sink`, decrypting each piece as it arrives, so the object
600    /// is never held in memory. Every check [`Self::download_with_progress`]
601    /// makes is made on the returned receipt. Until this returns `Ok`, the
602    /// bytes in `sink` are unverified: on an error the caller must discard them.
603    pub fn download_to_with_progress(
604        &self,
605        descriptor: &MediaDescriptor,
606        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
607        sink: &mut dyn std::io::Write,
608    ) -> Result<DownloadReceipt> {
609        self.download_on_route(
610            descriptor,
611            None,
612            progress,
613            |path, fields| self.post(path, fields),
614            sink,
615        )
616    }
617    /// Download a storage object, as the official client does for sticker pack
618    /// archives: `POST /downloadstorage` with the storage request fields
619    /// (`apptype` 10, `isstorevideo` 0, `nocheckaeskey` 1, `enablehit` 1,
620    /// `existancecheck` 0 and `filekey`). `file_key` is the caller's task ID
621    /// for the object; it correlates requests and is not a credential. Only
622    /// whole-file descriptors are accepted; the downloaded bytes get the same
623    /// size and integrity checks as an ordinary file download.
624    pub fn download_storage_with_progress(
625        &self,
626        descriptor: &MediaDescriptor,
627        file_key: &str,
628        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
629    ) -> Result<DownloadedMedia> {
630        let mut bytes = vec![];
631        let receipt = self.download_on_route(
632            descriptor,
633            Some(file_key),
634            progress,
635            |path, fields| self.post(path, fields),
636            &mut bytes,
637        )?;
638        Ok(DownloadedMedia::from_receipt(receipt, bytes))
639    }
640    fn download_with_transport(
641        &self,
642        descriptor: &MediaDescriptor,
643        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
644        post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
645    ) -> Result<DownloadedMedia> {
646        let mut bytes = vec![];
647        let receipt = self.download_on_route(descriptor, None, progress, post, &mut bytes)?;
648        Ok(DownloadedMedia::from_receipt(receipt, bytes))
649    }
650    fn download_on_route(
651        &self,
652        descriptor: &MediaDescriptor,
653        storage_key: Option<&str>,
654        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
655        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
656        sink: &mut dyn std::io::Write,
657    ) -> Result<DownloadReceipt> {
658        if let Some(key) = storage_key {
659            ensure!(
660                descriptor.quality == MediaQuality::File
661                    && !key.is_empty()
662                    && key.len() <= 1024
663                    && !key.chars().any(char::is_control),
664                "Invalid storage download"
665            );
666        }
667        ensure!(
668            !descriptor.file_id.is_empty()
669                && descriptor.file_id.len() <= 8192
670                && !descriptor.file_id.chars().any(|c| c.is_control())
671                && descriptor.aes_key.len() == 16,
672            "Invalid download descriptor"
673        );
674        if let Some(expiry) = descriptor.expires_at {
675            ensure!(expiry > crate::wire::unix_time()? as u64, "media_expired");
676        }
677        let large = descriptor.quality == MediaQuality::LargeFile;
678        let large_video = descriptor.quality == MediaQuality::Video
679            && descriptor.expected_bytes.unwrap_or(0) > MAX_CDN_TRANSFER_BYTES as u64;
680        let strict_transfer = large || large_video;
681        let plaintext_limit = if large {
682            MAX_LARGE_FILE_BYTES
683        } else if large_video {
684            MAX_VIDEO_DOWNLOAD_BYTES
685        } else if descriptor.quality == MediaQuality::Video && descriptor.expected_bytes.is_some() {
686            MAX_SMALL_VIDEO_REPLY_BYTES
687        } else {
688            MAX_CDN_TRANSFER_BYTES
689        };
690        let chunk_bytes = if strict_transfer { 512 * 1024 } else { 65536 };
691        let ciphertext_limit = (plaintext_limit / 16 + 1) * 16;
692        let expected_ciphertext = if strict_transfer {
693            let expected = usize::try_from(
694                descriptor
695                    .expected_bytes
696                    .ok_or_else(|| anyhow::anyhow!("Large file size is required"))?,
697            )?;
698            ensure!(
699                expected > 0 && expected <= plaintext_limit,
700                "Invalid large file size"
701            );
702            Some((expected / 16 + 1) * 16)
703        } else {
704            ensure!(
705                descriptor
706                    .expected_bytes
707                    .is_none_or(|size| size <= plaintext_limit as u64),
708                "Advertised media size outside limit"
709            );
710            None
711        };
712        if large_video {
713            ensure!(
714                descriptor.expected_md5.as_ref().is_some_and(
715                    |md5| md5.len() == 32 && md5.bytes().all(|b| b.is_ascii_hexdigit())
716                ),
717                "Large video source checksum is required"
718            );
719        }
720        let mut position = 0usize;
721        let mut total = 0usize;
722        // Pieces are decrypted as they arrive. The decryptor keeps the last
723        // block back until the padding is known.
724        let mut decryptor = Crypter::new(
725            Cipher::aes_128_ecb(),
726            Mode::Decrypt,
727            &descriptor.aes_key,
728            None,
729        )?;
730        let mut plain = zeroize::Zeroizing::new(vec![]);
731        let mut plaintext_bytes = 0usize;
732        let mut plaintext_md5 = Hasher::new(MessageDigest::md5())?;
733        let mut plaintext_sha256 = Hasher::new(MessageDigest::sha256())?;
734        let mut substituted_filetype = None;
735        let mut picture_format = None;
736        let mut server_md5 = None;
737        let observe_video = descriptor.quality == MediaQuality::Video && !strict_transfer;
738        let mut video_reply = VideoReplyMetadata::default();
739        let mut video_selected_response: Option<bool> = None;
740        let mut video_response_md5: Option<String> = None;
741        let mut video_response_bytes: Option<u64> = None;
742        for sequence in 1..=u32::try_from(ciphertext_limit.div_ceil(chunk_bytes))? {
743            progress(
744                position as u64,
745                if total == 0 { None } else { Some(total as u64) },
746            )?;
747            let mut fields = shared(&self.config, sequence, descriptor.quality.filetype());
748            let requested_end = if total == 0 {
749                chunk_bytes - 1
750            } else {
751                (total - 1).min(position + chunk_bytes - 1)
752            };
753            for (k, v) in [
754                ("wxautostart", 0),
755                ("downpicformat", 1),
756                ("rangestart", position as u64),
757                ("rangeend", requested_end as u64),
758            ] {
759                put(&mut fields, k, v);
760            }
761            fields.insert("wxmsgflag".into(), vec![]);
762            fields.insert("fileid".into(), descriptor.file_id.as_bytes().to_vec());
763            if let Some(key) = storage_key {
764                // The descriptor always carries its AES key, so the official
765                // client's conditional `nocheckaeskey` is always 1 here.
766                for (k, v) in [
767                    ("apptype", 10),
768                    ("isstorevideo", 0),
769                    ("nocheckaeskey", 1),
770                    ("enablehit", 1),
771                    ("existancecheck", 0),
772                ] {
773                    put(&mut fields, k, v);
774                }
775                fields.insert("filekey".into(), key.as_bytes().to_vec());
776            }
777            let path = if storage_key.is_some() {
778                "/downloadstorage"
779            } else if large {
780                "/downloadbig"
781            } else {
782                "/download"
783            };
784            let reply = post(path, &fields)?;
785            let code = integer(&reply, "retcode")?;
786            ServiceRejected::check_wide(Service::CdnDownload, code)?;
787            let response_digest = reply
788                .get("rawfilemd5")
789                .and_then(|v| std::str::from_utf8(v).ok())
790                .filter(|v| v.len() == 32 && v.bytes().all(|b| b.is_ascii_hexdigit()))
791                .map(|v| v.to_ascii_lowercase());
792            let server_selected = descriptor.quality == MediaQuality::Video
793                && !descriptor.strict_size
794                && matches!(integer(&reply, "istrans").ok(), Some(13 | 999))
795                && integer(&reply, "videoformat").ok() == Some(1)
796                && integer(&reply, "ver").ok() == Some(0);
797            if server_selected {
798                ensure!(response_digest.is_some(), "media_integrity_failed");
799                validate_large_video_reply_fields(&reply)?;
800                if reply.contains_key("rawfilesize") {
801                    let size = u64::try_from(integer(&reply, "rawfilesize")?)?;
802                    ensure!(
803                        size > 0
804                            && size <= plaintext_limit as u64
805                            && video_response_bytes.is_none_or(|previous| previous == size),
806                        "Invalid video response size"
807                    );
808                    video_response_bytes = Some(size);
809                }
810                for key in [
811                    "retrysec",
812                    "isretry",
813                    "isoverload",
814                    "isgetcdn",
815                    "backsrcnum",
816                    "rsppicformat",
817                ] {
818                    ensure!(
819                        !reply.contains_key(key)
820                            || (integer(&reply, key)? == 0
821                                || (key == "backsrcnum" && matches!(integer(&reply, key)?, 0..=2))),
822                        "Unsupported server_selected-video control: {key}"
823                    );
824                }
825                ensure!(
826                    !reply.contains_key("substituteftype")
827                        || matches!(integer(&reply, "substituteftype")?, 0 | 4 | 9),
828                    "Unsupported server_selected-video representation"
829                );
830            }
831            if descriptor.quality == MediaQuality::Video {
832                ensure!(
833                    video_selected_response.is_none_or(|v| v == server_selected),
834                    "CDN representation changed during transfer"
835                );
836                video_selected_response = Some(server_selected);
837                if let Some(value) = response_digest {
838                    ensure!(
839                        video_response_md5.as_ref().is_none_or(|v| v == &value),
840                        "CDN representation changed during transfer"
841                    );
842                    video_response_md5 = Some(value);
843                } else if server_selected {
844                    anyhow::bail!("media_integrity_failed");
845                }
846            }
847            if strict_transfer || reply.contains_key("seq") {
848                ensure!(
849                    integer(&reply, "seq")? == i64::from(sequence),
850                    "CDN download reply sequence mismatch"
851                );
852            }
853            if large {
854                validate_large_download_reply(&reply, descriptor, &mut server_md5)?;
855            } else if large_video && !server_selected {
856                validate_large_video_download_reply(&reply, descriptor, &mut server_md5)?;
857            }
858            let start = usize::try_from(integer(&reply, "rangestart")?)?;
859            let end = usize::try_from(integer(&reply, "rangeend")?)?;
860            let size = usize::try_from(integer(&reply, "totalsize")?)?;
861            let data = reply
862                .get("filedata")
863                .ok_or_else(|| anyhow::anyhow!("Missing CDN payload"))?;
864            ensure!(
865                start == position
866                    && !data.is_empty()
867                    && size > 0
868                    && size <= ciphertext_limit
869                    && (server_selected
870                        || expected_ciphertext.is_none_or(|expected| expected == size))
871                    && size >= position
872                    && data.len() <= size - position
873                    && end <= requested_end
874                    && (!strict_transfer
875                        || server_selected
876                        || data.len() == (size - position).min(chunk_bytes))
877                    && (total == 0 || total == size)
878                    && end == start + data.len() - 1,
879                "Inconsistent CDN range response"
880            );
881            total = size;
882            position += data.len();
883            plain.resize(data.len() + 16, 0);
884            let count = decryptor.update(data, &mut plain)?;
885            plaintext_bytes += count;
886            ensure!(
887                plaintext_bytes <= plaintext_limit,
888                "Decoded CDN body too large"
889            );
890            plaintext_md5.update(&plain[..count])?;
891            plaintext_sha256.update(&plain[..count])?;
892            sink.write_all(&plain[..count])?;
893            if !large {
894                for (key, target) in [
895                    ("substituteftype", &mut substituted_filetype),
896                    ("rsppicformat", &mut picture_format),
897                ] {
898                    if reply.contains_key(key) {
899                        let value = u32::try_from(integer(&reply, key)?)?;
900                        ensure!(
901                            target.is_none_or(|previous| previous == value),
902                            "CDN representation changed during transfer"
903                        );
904                        *target = Some(value);
905                    }
906                }
907            }
908            if observe_video {
909                video_reply.observe(&reply);
910            }
911            progress(position as u64, Some(total as u64))?;
912            if position == total {
913                break;
914            }
915        }
916        ensure!(
917            total > 0 && position == total,
918            "CDN transfer did not finish within bound"
919        );
920        ensure!(
921            total.is_multiple_of(16) && total <= MAX_LARGE_FILE_BYTES + 16,
922            "Invalid CDN ciphertext"
923        );
924        plain.resize(32, 0);
925        let count = decryptor.finalize(&mut plain)?;
926        plaintext_bytes += count;
927        ensure!(
928            plaintext_bytes <= plaintext_limit,
929            "Decoded CDN body too large"
930        );
931        plaintext_md5.update(&plain[..count])?;
932        plaintext_sha256.update(&plain[..count])?;
933        sink.write_all(&plain[..count])?;
934        sink.flush()?;
935        let size_matches = descriptor
936            .expected_bytes
937            .map(|n| n == plaintext_bytes as u64);
938        // Normal-video plans are no longer strict so that a selected copy can be accepted, but
939        // a video reply the CDN did not mark as selected must still match the advertised size.
940        let size_rejected = video_selected_response != Some(true)
941            && (strict_transfer
942                || descriptor.strict_size
943                || descriptor.quality == MediaQuality::Video)
944            && size_matches == Some(false);
945        let digest = hex::encode(plaintext_md5.finish()?);
946        let digest_matches = descriptor.expected_md5.as_ref().map(|expected| {
947            expected.len() == 32
948                && expected.bytes().all(|b| b.is_ascii_hexdigit())
949                && digest.eq_ignore_ascii_case(expected)
950        });
951        if observe_video
952            && video_selected_response != Some(true)
953            && (size_rejected || digest_matches == Some(false))
954        {
955            // Reject exactly as before, but keep comparison facts so a different
956            // server-selected representation can be told apart from corruption.
957            return Err(video_reply
958                .mismatch(
959                    descriptor,
960                    plaintext_bytes,
961                    &digest,
962                    digest_matches,
963                    substituted_filetype,
964                )
965                .into());
966        }
967        if video_selected_response == Some(true) {
968            ensure!(
969                video_response_bytes.is_none_or(|size| size == plaintext_bytes as u64),
970                "CDN video response size mismatch"
971            );
972            ensure!(
973                video_response_md5.as_deref() == Some(digest.as_str()),
974                "CDN video response checksum mismatch"
975            );
976        } else {
977            ensure!(!size_rejected, "Advertised media size mismatch");
978            ensure!(
979                digest_matches != Some(false),
980                "Advertised media checksum mismatch"
981            );
982        }
983        if let Some(expected) = server_md5 {
984            ensure!(
985                digest.eq_ignore_ascii_case(&expected),
986                "CDN source checksum mismatch"
987            );
988        }
989        Ok(DownloadReceipt {
990            byte_length: plaintext_bytes as u64,
991            sha256: hex::encode(plaintext_sha256.finish()?),
992            requested_quality: descriptor.quality,
993            substituted_filetype,
994            picture_format,
995            md5: digest,
996            advertised_size_matches: size_matches,
997        })
998    }
999    pub fn upload(&self, prepared: &PreparedUpload) -> Result<UploadedMedia> {
1000        self.upload_with_progress(prepared, |_, _| Ok(()))
1001    }
1002    pub fn upload_with_progress(
1003        &self,
1004        prepared: &PreparedUpload,
1005        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1006    ) -> Result<UploadedMedia> {
1007        self.upload_with_transport(UploadRef::Memory(prepared), progress, |path, fields| {
1008            self.post(path, fields)
1009        })
1010    }
1011    /// [`Self::upload_with_progress`] for an upload read from a file in steps.
1012    /// The requests are the same as for the equivalent in-memory upload.
1013    pub fn upload_file_with_progress(
1014        &self,
1015        prepared: &PreparedFileUpload,
1016        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1017    ) -> Result<UploadedMedia> {
1018        self.upload_with_transport(UploadRef::File(prepared), progress, |path, fields| {
1019            self.post(path, fields)
1020        })
1021    }
1022    fn upload_with_transport(
1023        &self,
1024        prepared: UploadRef<'_>,
1025        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1026        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1027    ) -> Result<UploadedMedia> {
1028        prepared.validate()?;
1029        let total = prepared.encrypted_len();
1030        let path = legacy_upload_path(prepared.kind(), prepared.plaintext_bytes() as usize, total)?;
1031        let mut file_id = None;
1032        let mut chunks = prepared.chunks();
1033        let mut sequence = 0u32;
1034        while chunks.position() < total {
1035            let start = chunks.position();
1036            sequence += 1;
1037            progress(start as u64, Some(total as u64))?;
1038            let chunk = chunks.next(UPLOAD_CHUNK)?;
1039            let end = start + chunk.len() - 1;
1040            let mut fields = upload_fields(&self.config, prepared, sequence, start, &chunk)?;
1041            fields.insert("filedata".into(), chunk.to_vec());
1042            let reply = post(path, &fields)?;
1043            validate_upload_reply(&reply, sequence, start, end, &mut file_id)?;
1044            // The final correlated acknowledgement must reach the caller so it
1045            // can save completion before applying cancellation/logout fences.
1046            if end + 1 < total {
1047                progress((end + 1) as u64, Some(total as u64))?;
1048            }
1049        }
1050        let file_id =
1051            file_id.ok_or_else(|| anyhow::anyhow!("CDN upload returned no file identifier"))?;
1052        Ok(prepared.uploaded(file_id, None, None))
1053    }
1054}
1055/// Only these fixed, nonsecret numeric controls can enter a stored diagnostic.
1056#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1057pub enum LargeFileDownloadControl {
1058    Version,
1059    VideoFormat,
1060}
1061impl LargeFileDownloadControl {
1062    pub const fn field_name(self) -> &'static str {
1063        match self {
1064            Self::Version => "ver",
1065            Self::VideoFormat => "videoformat",
1066        }
1067    }
1068}
1069/// Strict rejection details. Unknown keys and response bytes are never retained.
1070#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1071pub enum LargeFileDownloadError {
1072    UnknownField,
1073    FixedControlMismatch {
1074        control: LargeFileDownloadControl,
1075        actual: i64,
1076    },
1077}
1078impl fmt::Display for LargeFileDownloadError {
1079    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1080        f.write_str(match self {
1081            Self::UnknownField => "Unsupported large-file download response field",
1082            Self::FixedControlMismatch { .. } => "Unsupported large-file download control",
1083        })
1084    }
1085}
1086impl std::error::Error for LargeFileDownloadError {}
1087
1088/// How an optional reply value relates to the selected message tuple and to
1089/// the bytes actually delivered. Values are compared, never retained.
1090#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1091pub struct SourceEvidence {
1092    pub matches_advertised: bool,
1093    pub matches_delivered: bool,
1094}
1095
1096/// A complete video transfer at or below the ordinary transfer threshold
1097/// decrypted successfully but did not match the selected message tuple. The
1098/// download is still rejected. Only fixed numeric controls and comparison
1099/// results are kept, so a caller can store a diagnostic without reply bytes or
1100/// digests. Fields use wire key names; their meanings are not established.
1101#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1102pub struct VideoRepresentationMismatch {
1103    pub advertised_bytes: Option<u64>,
1104    pub delivered_bytes: u64,
1105    pub advertised_digest_matches: Option<bool>,
1106    pub substituted_filetype: Option<u32>,
1107    pub istrans: Option<i64>,
1108    pub videoformat: Option<i64>,
1109    pub rawfilesize: Option<SourceEvidence>,
1110    pub rawfilemd5: Option<SourceEvidence>,
1111}
1112impl fmt::Display for VideoRepresentationMismatch {
1113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1114        f.write_str("Advertised video representation mismatch")
1115    }
1116}
1117impl std::error::Error for VideoRepresentationMismatch {}
1118
1119/// First value of each optional video reply field on the ordinary path. This is
1120/// diagnostic only: an absent or malformed value never rejects a reply.
1121#[derive(Default)]
1122struct VideoReplyMetadata {
1123    istrans: Option<i64>,
1124    videoformat: Option<i64>,
1125    rawfilesize: Option<Option<u64>>,
1126    rawfilemd5: Option<Option<String>>,
1127}
1128impl VideoReplyMetadata {
1129    fn observe(&mut self, reply: &CdnFields) {
1130        for (key, target) in [
1131            ("istrans", &mut self.istrans),
1132            ("videoformat", &mut self.videoformat),
1133        ] {
1134            if target.is_none() {
1135                *target = integer(reply, key).ok();
1136            }
1137        }
1138        if self.rawfilesize.is_none() && reply.contains_key("rawfilesize") {
1139            self.rawfilesize = Some(
1140                integer(reply, "rawfilesize")
1141                    .ok()
1142                    .and_then(|value| u64::try_from(value).ok()),
1143            );
1144        }
1145        if self.rawfilemd5.is_none() {
1146            if let Some(raw) = reply.get("rawfilemd5") {
1147                self.rawfilemd5 = Some(
1148                    (raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit))
1149                        .then(|| String::from_utf8_lossy(raw).to_ascii_lowercase()),
1150                );
1151            }
1152        }
1153    }
1154    fn mismatch(
1155        &self,
1156        descriptor: &MediaDescriptor,
1157        delivered_bytes: usize,
1158        delivered_md5: &str,
1159        advertised_digest_matches: Option<bool>,
1160        substituted_filetype: Option<u32>,
1161    ) -> VideoRepresentationMismatch {
1162        let delivered_bytes = delivered_bytes as u64;
1163        VideoRepresentationMismatch {
1164            advertised_bytes: descriptor.expected_bytes,
1165            delivered_bytes,
1166            advertised_digest_matches,
1167            substituted_filetype,
1168            istrans: self.istrans,
1169            videoformat: self.videoformat,
1170            rawfilesize: self.rawfilesize.map(|value| SourceEvidence {
1171                matches_advertised: value.is_some() && value == descriptor.expected_bytes,
1172                matches_delivered: value == Some(delivered_bytes),
1173            }),
1174            rawfilemd5: self.rawfilemd5.as_ref().map(|value| SourceEvidence {
1175                matches_advertised: value
1176                    .as_ref()
1177                    .zip(descriptor.expected_md5.as_ref())
1178                    .is_some_and(|(value, expected)| value.eq_ignore_ascii_case(expected)),
1179                matches_delivered: value.as_deref() == Some(delivered_md5),
1180            }),
1181        }
1182    }
1183}
1184
1185fn validate_large_video_reply_fields(reply: &CdnFields) -> Result<()> {
1186    const FIELDS: &[&str] = &[
1187        "ver",
1188        "seq",
1189        "retcode",
1190        "filedata",
1191        "rangestart",
1192        "rangeend",
1193        "totalsize",
1194        "retrysec",
1195        "isretry",
1196        "isoverload",
1197        "isgetcdn",
1198        "istrans",
1199        "backsrcnum",
1200        "rawfilesize",
1201        "rawfilemd5",
1202        "substituteftype",
1203        "rsppicformat",
1204        "videoformat",
1205        "x-ClientIp",
1206    ];
1207    ensure!(
1208        reply.keys().all(|key| FIELDS.contains(&key.as_str())),
1209        "Unsupported large-video download response field"
1210    );
1211    Ok(())
1212}
1213
1214// Type 4 retains the normal download endpoint. Unlike file7, representation
1215// and routing metadata cannot be ignored for a byte-exact original video.
1216fn validate_large_video_download_reply(
1217    reply: &CdnFields,
1218    descriptor: &MediaDescriptor,
1219    server_md5: &mut Option<String>,
1220) -> Result<()> {
1221    validate_large_video_reply_fields(reply)?;
1222    ensure!(
1223        integer(reply, "ver")? == 0,
1224        "Unsupported large-video download version"
1225    );
1226    for key in [
1227        "retrysec",
1228        "isretry",
1229        "isoverload",
1230        "isgetcdn",
1231        "backsrcnum",
1232        "rsppicformat",
1233    ] {
1234        if reply.contains_key(key) {
1235            ensure!(
1236                integer(reply, key)? == 0,
1237                "Unsupported large-video download control"
1238            );
1239        }
1240    }
1241    // Linux 4.1.13.26's normal pipeline response map discards `istrans`.
1242    // Its success handler's format lookup is literal `KEY_VIDEOFORMAT`
1243    // (CRC fb520bf9), not the retained wire key `videoformat` (dbe097e1).
1244    // Neither affects the selected ECB/range writer. Admit only the values
1245    // observed with byte-identical original-video ciphertext; retain the
1246    // independent representation, source size and digest checks below.
1247    for (key, observed) in [("istrans", 999), ("videoformat", 1)] {
1248        if reply.contains_key(key) {
1249            let value = integer(reply, key)?;
1250            ensure!(
1251                value == 0 || value == observed,
1252                "Unsupported large-video download control"
1253            );
1254        }
1255    }
1256    if reply.contains_key("substituteftype") {
1257        ensure!(
1258            matches!(integer(reply, "substituteftype")?, 0 | 4),
1259            "Unsupported large-video download representation"
1260        );
1261    }
1262    if reply.contains_key("rawfilesize") {
1263        ensure!(
1264            u64::try_from(integer(reply, "rawfilesize")?)?
1265                == descriptor.expected_bytes.unwrap_or(0),
1266            "Large-video download source size mismatch"
1267        );
1268    }
1269    if let Some(raw) = reply.get("rawfilemd5") {
1270        ensure!(
1271            raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1272            "Invalid CDN source checksum"
1273        );
1274        let value = std::str::from_utf8(raw)?;
1275        ensure!(
1276            server_md5
1277                .as_ref()
1278                .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1279                && descriptor
1280                    .expected_md5
1281                    .as_ref()
1282                    .is_some_and(|expected| expected.eq_ignore_ascii_case(value)),
1283            "CDN source checksum changed"
1284        );
1285        *server_md5 = Some(value.to_string());
1286    }
1287    Ok(())
1288}
1289
1290fn validate_large_download_reply(
1291    reply: &CdnFields,
1292    descriptor: &MediaDescriptor,
1293    server_md5: &mut Option<String>,
1294) -> Result<()> {
1295    const FIELDS: &[&str] = &[
1296        "ver",
1297        "seq",
1298        "retcode",
1299        "filedata",
1300        "rangestart",
1301        "rangeend",
1302        "totalsize",
1303        "retrysec",
1304        "isretry",
1305        "isoverload",
1306        "isgetcdn",
1307        "istrans",
1308        "backsrcnum",
1309        "rawfilesize",
1310        "rawfilemd5",
1311        "substituteftype",
1312        "rsppicformat",
1313        "videoformat",
1314        "x-ClientIp",
1315    ];
1316    if !reply.keys().all(|key| FIELDS.contains(&key.as_str())) {
1317        return Err(LargeFileDownloadError::UnknownField.into());
1318    }
1319    use LargeFileDownloadControl as Control;
1320    for (control, expected) in [(Control::Version, 0), (Control::VideoFormat, 0)] {
1321        let actual = integer(reply, control.field_name())?;
1322        if actual != expected {
1323            return Err(LargeFileDownloadError::FixedControlMismatch { control, actual }.into());
1324        }
1325    }
1326    // Linux 4.1.13.26's file7 pipeline (0x8d12d60 -> 0x8ce3000 ->
1327    // 0x8ca0ec0) does not consume these on a valid retcode-zero response.
1328    // The first four are optional template values; the remaining four never
1329    // enter its response template. They do not trigger retries or substitution.
1330    for key in [
1331        "retrysec",
1332        "isretry",
1333        "isoverload",
1334        "isgetcdn",
1335        "istrans",
1336        "backsrcnum",
1337        "substituteftype",
1338        "rsppicformat",
1339    ] {
1340        if let Some(raw) = reply.get(key) {
1341            // Keep the existing 16-byte integer parser bound, with unsigned
1342            // syntax. This is a native parsing policy, not a service limit.
1343            ensure!(
1344                raw.iter().all(u8::is_ascii_digit),
1345                "Invalid large-file metadata integer"
1346            );
1347            integer(reply, key)?;
1348        }
1349    }
1350    // File replies can omit this redundant source size. The transfer still
1351    // checks the encrypted length and the final decrypted length against the
1352    // message descriptor. Reject conflicting metadata when it is supplied.
1353    if reply.contains_key("rawfilesize") {
1354        ensure!(
1355            u64::try_from(integer(reply, "rawfilesize")?)?
1356                == descriptor.expected_bytes.unwrap_or(0),
1357            "Large-file download source size mismatch"
1358        );
1359    }
1360    let raw = reply
1361        .get("rawfilemd5")
1362        .ok_or_else(|| anyhow::anyhow!("Missing CDN source checksum"))?;
1363    ensure!(
1364        raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1365        "Invalid CDN source checksum"
1366    );
1367    let value = std::str::from_utf8(raw)?;
1368    ensure!(
1369        server_md5
1370            .as_ref()
1371            .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1372            && descriptor
1373                .expected_md5
1374                .as_ref()
1375                .is_none_or(|expected| expected.eq_ignore_ascii_case(value)),
1376        "CDN source checksum changed"
1377    );
1378    *server_md5 = Some(value.to_string());
1379    Ok(())
1380}
1381fn legacy_upload_path(
1382    kind: MediaQuality,
1383    plaintext: usize,
1384    ciphertext: usize,
1385) -> Result<&'static str> {
1386    ensure!(
1387        kind != MediaQuality::LargeFile
1388            && (kind != MediaQuality::File || plaintext <= MAX_NATIVE_UPLOAD_BYTES)
1389            && ciphertext < MAX_CDN_TRANSFER_BYTES,
1390        "File requires multiphase upload"
1391    );
1392    Ok("/uploadv3")
1393}
1394fn escaped_component(s: &str) -> String {
1395    s.bytes()
1396        .map(|b| {
1397            if b.is_ascii_alphanumeric() || b"-_.!~*'()".contains(&b) {
1398                (b as char).to_string()
1399            } else {
1400                format!("%{b:02X}")
1401            }
1402        })
1403        .collect()
1404}
1405/// Fields for one `/uploadv3` request carrying `chunk`, the ciphertext that
1406/// starts at `start`.
1407fn upload_fields(
1408    conf: &CdnConfig,
1409    p: UploadRef<'_>,
1410    sequence: u32,
1411    start: usize,
1412    chunk: &[u8],
1413) -> Result<CdnFields> {
1414    let total = p.encrypted_len();
1415    ensure!(
1416        sequence > 0 && !chunk.is_empty() && chunk.len() <= UPLOAD_CHUNK && start < total,
1417        "Invalid upload range"
1418    );
1419    let end = start + chunk.len() - 1;
1420    ensure!(end < total, "Invalid upload range");
1421    let mut f = shared(conf, sequence, p.kind().filetype());
1422    for (k, v) in [
1423        ("hasthumb", 0),
1424        ("compresstype", 0),
1425        ("nocheckaeskey", 1),
1426        ("enablehit", 0),
1427        ("existancecheck", 0),
1428        ("apptype", 0),
1429        ("totalsize", total as u64),
1430        ("rawtotalsize", p.plaintext_bytes() as u64),
1431        ("thumbtotalsize", 0),
1432        ("rawthumbsize", 0),
1433        ("encthumbcrc", 0),
1434        ("advideoflag", 0),
1435        ("rangestart", start as u64),
1436        ("rangeend", end as u64),
1437    ] {
1438        put(&mut f, k, v);
1439    }
1440    for (k, v) in [
1441        ("touser", encrypt_recipient(p.destination())?),
1442        ("filekey", p.file_key().to_string()),
1443        ("localname", escaped_component(p.file_name())),
1444        ("filedatamd5", hex::encode(md5(chunk))),
1445        ("filemd5", p.md5().to_string()),
1446        ("rawfilemd5", p.md5().to_string()),
1447        ("blockmd5", hex::encode(md5(chunk))),
1448    ] {
1449        f.insert(k.into(), v.into_bytes());
1450    }
1451    f.insert("rawthumbmd5".into(), vec![]);
1452    Ok(f)
1453}
1454fn validate_upload_reply(
1455    reply: &CdnFields,
1456    sequence: u32,
1457    start: usize,
1458    end: usize,
1459    file_id: &mut Option<String>,
1460) -> Result<()> {
1461    let code = integer(reply, "retcode")?;
1462    ServiceRejected::check_wide(Service::CdnUpload, code)?;
1463    for (key, expected) in [
1464        ("seq", sequence as u64),
1465        ("rangestart", start as u64),
1466        ("rangeend", end as u64),
1467    ] {
1468        if reply.contains_key(key) {
1469            ensure!(
1470                u64::try_from(integer(reply, key)?)? == expected,
1471                "CDN upload correlation mismatch"
1472            );
1473        }
1474    }
1475    if let Some(raw) = reply.get("fileid") {
1476        let value = std::str::from_utf8(raw)?;
1477        ensure!(
1478            !value.is_empty()
1479                && value.len() <= 4096
1480                && value
1481                    .bytes()
1482                    .all(|b| b.is_ascii_alphanumeric() || b"-_@.+/=".contains(&b)),
1483            "Invalid uploaded file identifier"
1484        );
1485        ensure!(
1486            file_id.as_deref().is_none_or(|previous| previous == value),
1487            "CDN upload file identifier changed"
1488        );
1489        *file_id = Some(value.to_string());
1490    }
1491    Ok(())
1492}
1493
1494#[derive(Clone, Serialize, Deserialize)]
1495pub struct PreparedVariant {
1496    pub encrypted: Vec<u8>,
1497    pub plaintext_bytes: u32,
1498    pub width: u32,
1499    pub height: u32,
1500    pub md5: String,
1501    pub sha256: String,
1502    pub checksum: u32,
1503    pub encrypted_checksum: u32,
1504}
1505#[derive(Clone, Serialize, Deserialize)]
1506pub struct PreparedImageUpload {
1507    pub original: PreparedUpload,
1508    pub standard: PreparedVariant,
1509    pub thumbnail: PreparedVariant,
1510    pub width: u32,
1511    pub height: u32,
1512}
1513#[derive(Clone, Serialize, Deserialize)]
1514pub struct PreparedVideoUpload {
1515    pub video: PreparedUpload,
1516    pub thumbnail: PreparedVariant,
1517}
1518/// Uploaded copies offered by an image message, with an optional original.
1519#[derive(Clone, Serialize, Deserialize)]
1520pub struct UploadedImage {
1521    /// Original copy to offer alongside the previews; `None` offers only standard and thumbnail.
1522    #[serde(default)]
1523    pub original: Option<UploadedMedia>,
1524    /// Compressed main image copy.
1525    pub standard: UploadedMedia,
1526    /// Small preview image copy.
1527    pub thumbnail: UploadedMedia,
1528}
1529#[derive(Clone, Serialize, Deserialize)]
1530pub struct UploadedVideo {
1531    pub video: UploadedMedia,
1532    pub thumbnail: UploadedMedia,
1533    #[serde(default)]
1534    pub original: Option<UploadedMedia>,
1535}
1536/// Dimensions of an image original after decoding it once. An original is not
1537/// held to the 65,520-byte limit of the standard and thumbnail copies, and may
1538/// be an unchanged camera JPEG larger than their 4,096-pixel edge limit.
1539fn original_jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1540    ensure!(
1541        !bytes.is_empty() && bytes.len() <= MAX_NATIVE_UPLOAD_BYTES,
1542        "JPEG original outside upload limit"
1543    );
1544    let inspection = inspect_image(bytes)?;
1545    ensure!(
1546        inspection.content_type == "image/jpeg",
1547        "Expected JPEG original"
1548    );
1549    Ok((inspection.width, inspection.height))
1550}
1551/// Dimensions from the JPEG frame header, without decoding its pixels.
1552fn jpeg_header_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1553    Ok(
1554        image::ImageReader::with_format(std::io::Cursor::new(bytes), image::ImageFormat::Jpeg)
1555            .into_dimensions()?,
1556    )
1557}
1558fn jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1559    use image::GenericImageView;
1560    ensure!(
1561        !bytes.is_empty() && bytes.len() <= 65520,
1562        "JPEG variant outside small-object profile"
1563    );
1564    ensure!(
1565        image::guess_format(bytes)? == image::ImageFormat::Jpeg,
1566        "Expected JPEG variant"
1567    );
1568    let mut reader =
1569        image::ImageReader::with_format(std::io::Cursor::new(bytes), image::ImageFormat::Jpeg);
1570    let mut limits = image::Limits::default();
1571    limits.max_image_width = Some(4096);
1572    limits.max_image_height = Some(4096);
1573    limits.max_alloc = Some(64 * 1024 * 1024);
1574    reader.limits(limits);
1575    let image = reader.decode()?;
1576    let (w, h) = image.dimensions();
1577    ensure!(
1578        w > 0 && h > 0 && w <= 4096 && h <= 4096,
1579        "JPEG dimensions outside limit"
1580    );
1581    Ok((w, h))
1582}
1583pub fn adler32_zero(bytes: &[u8]) -> u32 {
1584    let mut a = 0u32;
1585    let mut b = 0u32;
1586    for &v in bytes {
1587        a = (a + v as u32) % 65521;
1588        b = (b + a) % 65521;
1589    }
1590    (b << 16) | a
1591}
1592impl PreparedVariant {
1593    fn new(raw: &[u8], key: &[u8]) -> Result<Self> {
1594        let (width, height) = jpeg_dimensions(raw)?;
1595        let encrypted = ecb_encrypt(raw, key)?;
1596        Ok(Self {
1597            checksum: adler32_zero(raw),
1598            encrypted_checksum: adler32_zero(&encrypted),
1599            encrypted,
1600            plaintext_bytes: raw.len() as u32,
1601            width,
1602            height,
1603            md5: hex::encode(md5(raw)),
1604            sha256: hex::encode(sha256(raw)),
1605        })
1606    }
1607    fn validate(&self, key: &[u8]) -> Result<()> {
1608        let raw = decrypt_cdn(&self.encrypted, key)?;
1609        let (w, h) = jpeg_dimensions(&raw)?;
1610        ensure!(
1611            raw.len() == self.plaintext_bytes as usize
1612                && w == self.width
1613                && h == self.height
1614                && hex::encode(md5(&raw)) == self.md5
1615                && hex::encode(sha256(&raw)) == self.sha256
1616                && adler32_zero(&raw) == self.checksum
1617                && adler32_zero(&self.encrypted) == self.encrypted_checksum,
1618            "Prepared JPEG variant mismatch"
1619        );
1620        Ok(())
1621    }
1622}
1623impl PreparedImageUpload {
1624    pub fn with_material(
1625        original: &[u8],
1626        standard: &[u8],
1627        thumbnail: &[u8],
1628        file_name: &str,
1629        destination: &str,
1630        key: &[u8],
1631        file_key: &str,
1632    ) -> Result<Self> {
1633        let (width, height) = original_jpeg_dimensions(original)?;
1634        let original = PreparedUpload::with_material(
1635            original,
1636            MediaQuality::Original,
1637            file_name,
1638            destination,
1639            key,
1640            file_key,
1641        )?;
1642        let standard = PreparedVariant::new(standard, key)?;
1643        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1644        let p = Self {
1645            original,
1646            standard,
1647            thumbnail,
1648            width,
1649            height,
1650        };
1651        p.validate()?;
1652        Ok(p)
1653    }
1654    /// Supplied variants must be independent real JPEGs. This function decodes
1655    /// them fully but does not resize or invent an unsupplied representation.
1656    pub fn prepare(
1657        original: &[u8],
1658        standard: &[u8],
1659        thumbnail: &[u8],
1660        file_name: &str,
1661        destination: &str,
1662    ) -> Result<Self> {
1663        let (width, height) = original_jpeg_dimensions(original)?;
1664        let original =
1665            PreparedUpload::prepare(original, MediaQuality::Original, file_name, destination)?;
1666        let standard = PreparedVariant::new(standard, &original.aes_key)?;
1667        let thumbnail = PreparedVariant::new(thumbnail, &original.aes_key)?;
1668        let p = Self {
1669            original,
1670            standard,
1671            thumbnail,
1672            width,
1673            height,
1674        };
1675        p.validate()?;
1676        Ok(p)
1677    }
1678    fn validate(&self) -> Result<()> {
1679        self.original.validate()?;
1680        let raw = decrypt_cdn(&self.original.encrypted, &self.original.aes_key)?;
1681        ensure!(
1682            jpeg_header_dimensions(&raw)? == (self.width, self.height),
1683            "Prepared original dimensions differ"
1684        );
1685        self.standard.validate(&self.original.aes_key)?;
1686        self.thumbnail.validate(&self.original.aes_key)?;
1687        ensure!(
1688            self.original.plaintext_bytes <= MAX_NATIVE_UPLOAD_BYTES as u32
1689                // A preview of a rotated camera original is drawn upright, so its
1690                // edges may be the original's stored edges exchanged.
1691                && ((self.standard.width <= self.width && self.standard.height <= self.height)
1692                    || (self.standard.width <= self.height
1693                        && self.standard.height <= self.width))
1694                && self.thumbnail.width <= self.standard.width
1695                && self.thumbnail.height <= self.standard.height
1696                && self.thumbnail.width <= 320
1697                && self.thumbnail.height <= 320,
1698            "Invalid JPEG variant sizes"
1699        );
1700        Ok(())
1701    }
1702}
1703impl PreparedVideoUpload {
1704    pub fn with_material(
1705        video: &[u8],
1706        thumbnail: &[u8],
1707        file_name: &str,
1708        destination: &str,
1709        key: &[u8],
1710        file_key: &str,
1711    ) -> Result<Self> {
1712        ensure!(
1713            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1714            "Expected bounded MP4 container"
1715        );
1716        let video = PreparedUpload::with_material(
1717            video,
1718            MediaQuality::Video,
1719            file_name,
1720            destination,
1721            key,
1722            file_key,
1723        )?;
1724        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1725        ensure!(
1726            thumbnail.width <= 320 && thumbnail.height <= 320,
1727            "Video thumbnail outside limit"
1728        );
1729        Ok(Self { video, thumbnail })
1730    }
1731    pub fn prepare(
1732        video: &[u8],
1733        thumbnail: &[u8],
1734        file_name: &str,
1735        destination: &str,
1736    ) -> Result<Self> {
1737        ensure!(
1738            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1739            "Expected bounded MP4 container"
1740        );
1741        let video = PreparedUpload::prepare(video, MediaQuality::Video, file_name, destination)?;
1742        let thumbnail = PreparedVariant::new(thumbnail, &video.aes_key)?;
1743        ensure!(
1744            thumbnail.width <= 320 && thumbnail.height <= 320,
1745            "Video thumbnail outside limit"
1746        );
1747        Ok(Self { video, thumbnail })
1748    }
1749}
1750fn add_thumbnail(fields: &mut CdnFields, v: &PreparedVariant) {
1751    for (k, n) in [
1752        ("hasthumb", 1),
1753        ("thumbfiletype", 3),
1754        ("thumbtotalsize", v.encrypted.len() as u64),
1755        ("rawthumbsize", v.plaintext_bytes as u64),
1756        ("thumbcrc", v.checksum as u64),
1757        ("encthumbcrc", v.encrypted_checksum as u64),
1758    ] {
1759        put(fields, k, n);
1760    }
1761    fields.insert("rawthumbmd5".into(), v.md5.as_bytes().to_vec());
1762    fields.insert("thumbdata".into(), v.encrypted.clone());
1763}
1764fn uploaded_primary(
1765    prepared: &PreparedUpload,
1766    file_id: &str,
1767    width: Option<u32>,
1768    height: Option<u32>,
1769) -> UploadedMedia {
1770    UploadedMedia {
1771        descriptor: MediaDescriptor {
1772            file_id: file_id.to_string(),
1773            aes_key: prepared.aes_key.clone(),
1774            quality: prepared.kind,
1775            expected_bytes: Some(prepared.plaintext_bytes as u64),
1776            expected_md5: Some(prepared.md5.clone()),
1777            strict_size: true,
1778            expires_at: None,
1779        },
1780        plaintext_bytes: prepared.plaintext_bytes,
1781        md5: prepared.md5.clone(),
1782        sha256: prepared.sha256.clone(),
1783        file_name: prepared.file_name.clone(),
1784        width,
1785        height,
1786    }
1787}
1788fn uploaded_variant(
1789    primary: &PreparedUpload,
1790    v: &PreparedVariant,
1791    file_id: &str,
1792    quality: MediaQuality,
1793) -> UploadedMedia {
1794    UploadedMedia {
1795        descriptor: MediaDescriptor {
1796            file_id: file_id.to_string(),
1797            aes_key: primary.aes_key.clone(),
1798            quality,
1799            expected_bytes: Some(v.plaintext_bytes as u64),
1800            expected_md5: Some(v.md5.clone()),
1801            strict_size: true,
1802            expires_at: None,
1803        },
1804        plaintext_bytes: v.plaintext_bytes,
1805        md5: v.md5.clone(),
1806        sha256: v.sha256.clone(),
1807        file_name: primary.file_name.clone(),
1808        width: Some(v.width),
1809        height: Some(v.height),
1810    }
1811}
1812impl CdnClient {
1813    pub fn upload_image(&self, p: &PreparedImageUpload) -> Result<UploadedImage> {
1814        self.upload_image_with_progress(p, |_, _| Ok(()))
1815    }
1816    /// Upload an image's original, standard and thumbnail copies, reporting the encrypted bytes
1817    /// of the original sent so far and their total before each request.
1818    pub fn upload_image_with_progress(
1819        &self,
1820        p: &PreparedImageUpload,
1821        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1822    ) -> Result<UploadedImage> {
1823        self.upload_image_with_transport(p, progress, |path, fields| self.post(path, fields))
1824    }
1825    /// Upload the original in 64 KiB ranges like a video. The first request
1826    /// also carries the standard and thumbnail copies and the image metadata,
1827    /// so an original that fits one range is still a single request.
1828    fn upload_image_with_transport(
1829        &self,
1830        p: &PreparedImageUpload,
1831        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1832        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1833    ) -> Result<UploadedImage> {
1834        p.validate()?;
1835        let original = UploadRef::Memory(&p.original);
1836        let total = original.encrypted_len();
1837        let mut file_id = None;
1838        let mut chunks = original.chunks();
1839        let mut sequence = 0u32;
1840        while chunks.position() < total {
1841            let start = chunks.position();
1842            sequence += 1;
1843            progress(start as u64, Some(total as u64))?;
1844            let chunk = chunks.next(UPLOAD_CHUNK)?;
1845            let end = start + chunk.len() - 1;
1846            let mut f = upload_fields(&self.config, original, sequence, start, &chunk)?;
1847            f.insert("filedata".into(), chunk.to_vec());
1848            if start == 0 {
1849                add_thumbnail(&mut f, &p.thumbnail);
1850                for (k, v) in [
1851                    ("imgheight", p.height as u64),
1852                    ("imgwidth", p.width as u64),
1853                    ("midimgrawsize", p.standard.plaintext_bytes as u64),
1854                    ("midimgtotalsize", p.standard.encrypted.len() as u64),
1855                    ("midimgchecksum", p.standard.encrypted_checksum as u64),
1856                    ("file_format", 0),
1857                    ("mid_format", 0),
1858                ] {
1859                    put(&mut f, k, v);
1860                }
1861                f.insert("midimgmd5".into(), p.standard.md5.as_bytes().to_vec());
1862                f.insert("midimgdata".into(), p.standard.encrypted.clone());
1863            }
1864            let reply = post("/uploadv3", &f)?;
1865            validate_upload_reply(&reply, sequence, start, end, &mut file_id)?;
1866            if end + 1 < total {
1867                progress((end + 1) as u64, Some(total as u64))?;
1868            }
1869        }
1870        let id =
1871            file_id.ok_or_else(|| anyhow::anyhow!("Image upload returned no file identifier"))?;
1872        Ok(UploadedImage {
1873            original: Some(uploaded_primary(
1874                &p.original,
1875                &id,
1876                Some(p.width),
1877                Some(p.height),
1878            )),
1879            standard: uploaded_variant(&p.original, &p.standard, &id, MediaQuality::Standard),
1880            thumbnail: uploaded_variant(&p.original, &p.thumbnail, &id, MediaQuality::Thumbnail),
1881        })
1882    }
1883    pub fn upload_video(&self, p: &PreparedVideoUpload) -> Result<UploadedVideo> {
1884        self.upload_video_with_progress(p, |_, _| Ok(()))
1885    }
1886    pub fn upload_video_with_progress(
1887        &self,
1888        p: &PreparedVideoUpload,
1889        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1890    ) -> Result<UploadedVideo> {
1891        self.upload_video_with_transport(p, progress, |path, fields| self.post(path, fields))
1892    }
1893    fn upload_video_with_transport(
1894        &self,
1895        p: &PreparedVideoUpload,
1896        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1897        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1898    ) -> Result<UploadedVideo> {
1899        p.video.validate()?;
1900        ensure!(
1901            p.video.kind == MediaQuality::Video
1902                && p.video.plaintext_bytes as usize <= MAX_NATIVE_UPLOAD_BYTES,
1903            "Invalid normal video upload profile"
1904        );
1905        p.thumbnail.validate(&p.video.aes_key)?;
1906        ensure!(
1907            p.thumbnail.width <= 320 && p.thumbnail.height <= 320,
1908            "Video thumbnail outside limit"
1909        );
1910        let mut file_id = None;
1911        let video = UploadRef::Memory(&p.video);
1912        let total = video.encrypted_len();
1913        let mut chunks = video.chunks();
1914        let mut sequence = 0u32;
1915        while chunks.position() < total {
1916            let start = chunks.position();
1917            sequence += 1;
1918            progress(start as u64, Some(total as u64))?;
1919            let chunk = chunks.next(UPLOAD_CHUNK)?;
1920            let end = start + chunk.len() - 1;
1921            let mut fields = upload_fields(&self.config, video, sequence, start, &chunk)?;
1922            fields.insert("filedata".into(), chunk.to_vec());
1923            if start == 0 {
1924                add_thumbnail(&mut fields, &p.thumbnail);
1925            }
1926            put(&mut fields, "smallvideoflag", 0);
1927            let reply = post("/uploadv3", &fields)?;
1928            validate_upload_reply(&reply, sequence, start, end, &mut file_id)?;
1929            if end + 1 < total {
1930                progress((end + 1) as u64, Some(total as u64))?;
1931            }
1932        }
1933        let id =
1934            file_id.ok_or_else(|| anyhow::anyhow!("Video upload returned no file identifier"))?;
1935        Ok(UploadedVideo {
1936            video: uploaded_primary(&p.video, &id, None, None),
1937            thumbnail: uploaded_variant(&p.video, &p.thumbnail, &id, MediaQuality::Thumbnail),
1938            original: None,
1939        })
1940    }
1941}
1942
1943#[cfg(test)]
1944mod tests {
1945    use super::*;
1946    fn client() -> CdnClient {
1947        CdnClient::new(
1948            CdnConfig {
1949                version: 1,
1950                uin: 12345,
1951                client_version: 456,
1952                client_os_type: "synthetic-client".into(),
1953                auth_key: b"synthetic-auth".to_vec(),
1954                server_ips: vec![Ipv4Addr::new(1, 1, 1, 1)],
1955                front_id: 0,
1956                location: None,
1957            },
1958            Duration::from_secs(25),
1959        )
1960        .unwrap()
1961    }
1962    fn download_fixture(size: usize, quality: MediaQuality) -> (MediaDescriptor, Vec<u8>, Vec<u8>) {
1963        let raw = vec![0x5a; size];
1964        let key = vec![0x31; 16];
1965        let ciphertext = ecb_encrypt(&raw, &key).unwrap();
1966        let descriptor = MediaDescriptor {
1967            file_id: "synthetic-object".into(),
1968            aes_key: key,
1969            quality,
1970            expected_bytes: Some(size as u64),
1971            expected_md5: Some(hex::encode(md5(&raw))),
1972            strict_size: true,
1973            expires_at: None,
1974        };
1975        (descriptor, raw, ciphertext)
1976    }
1977    fn download_reply(
1978        fields: &CdnFields,
1979        descriptor: &MediaDescriptor,
1980        ciphertext: &[u8],
1981    ) -> CdnFields {
1982        let mut reply = CdnFields::new();
1983        let start = integer(fields, "rangestart").unwrap() as usize;
1984        let end = (integer(fields, "rangeend").unwrap() as usize).min(ciphertext.len() - 1);
1985        assert_eq!(fields["fileid"], descriptor.file_id.as_bytes());
1986        assert_eq!(integer(fields, "weixinnum").unwrap(), 12345);
1987        assert!(!fields.contains_key("aeskey"));
1988        for (key, value) in [
1989            ("ver", 0),
1990            ("seq", integer(fields, "seq").unwrap()),
1991            ("retcode", 0),
1992            ("rangestart", start as i64),
1993            ("rangeend", end as i64),
1994            ("totalsize", ciphertext.len() as i64),
1995        ] {
1996            put(&mut reply, key, value);
1997        }
1998        reply.insert("filedata".into(), ciphertext[start..=end].to_vec());
1999        if descriptor.quality == MediaQuality::LargeFile {
2000            for (key, value) in [
2001                ("retrysec", 0),
2002                ("isretry", 0),
2003                ("isoverload", 0),
2004                ("isgetcdn", 0),
2005                ("istrans", 999),
2006                ("backsrcnum", 0),
2007                ("substituteftype", 7),
2008                ("rsppicformat", 0),
2009                ("videoformat", 0),
2010                ("rawfilesize", descriptor.expected_bytes.unwrap() as i64),
2011            ] {
2012                put(&mut reply, key, value);
2013            }
2014            reply.insert(
2015                "rawfilemd5".into(),
2016                descriptor
2017                    .expected_md5
2018                    .as_ref()
2019                    .unwrap()
2020                    .as_bytes()
2021                    .to_vec(),
2022            );
2023        }
2024        reply
2025    }
2026    #[test]
2027    fn large_video_keeps_normal_endpoint_type_and_exact_source_bytes() {
2028        let (descriptor, raw, ciphertext) =
2029            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
2030        let mut requests = 0;
2031        let downloaded = client()
2032            .download_with_transport(
2033                &descriptor,
2034                |_, _| Ok(()),
2035                |path, fields| {
2036                    requests += 1;
2037                    assert_eq!(path, "/download");
2038                    assert_eq!(integer(fields, "filetype")?, 4);
2039                    assert_eq!(integer(fields, "seq")?, requests);
2040                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
2041                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2042                    put(&mut reply, "substituteftype", 4);
2043                    put(&mut reply, "videoformat", 1);
2044                    put(&mut reply, "istrans", 999);
2045                    Ok(reply)
2046                },
2047            )
2048            .unwrap();
2049        assert_eq!(requests, 51);
2050        assert_eq!(downloaded.bytes, raw);
2051        assert_eq!(downloaded.md5, descriptor.expected_md5.as_deref().unwrap());
2052        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
2053        assert_eq!(downloaded.substituted_filetype, Some(4));
2054    }
2055
2056    #[test]
2057    fn large_video_rejects_unknown_controls_substitution_and_unbound_source() {
2058        let (mut descriptor, _, ciphertext) = download_fixture(16, MediaQuality::Video);
2059        descriptor.expected_bytes = Some(MAX_CDN_TRANSFER_BYTES as u64 + 1);
2060        let fields = BTreeMap::from([
2061            ("rangestart".into(), b"0".to_vec()),
2062            ("rangeend".into(), b"31".to_vec()),
2063            ("seq".into(), b"1".to_vec()),
2064            ("weixinnum".into(), b"12345".to_vec()),
2065            ("fileid".into(), descriptor.file_id.as_bytes().to_vec()),
2066        ]);
2067        let valid = download_reply(&fields, &descriptor, &ciphertext);
2068        for (key, value) in [
2069            ("unknown", "private"),
2070            ("ver", "1"),
2071            ("videoformat", "2"),
2072            ("istrans", "1"),
2073            ("KEY_VIDEOFORMAT", "1"),
2074            ("isretry", "1"),
2075            ("isgetcdn", "1"),
2076            ("rsppicformat", "3"),
2077            ("substituteftype", "7"),
2078            ("rawfilesize", "1"),
2079            ("rawfilemd5", "00000000000000000000000000000000"),
2080        ] {
2081            let mut reply = valid.clone();
2082            reply.insert(key.into(), value.as_bytes().to_vec());
2083            let error = validate_large_video_download_reply(&reply, &descriptor, &mut None)
2084                .unwrap_err()
2085                .to_string();
2086            assert!(!error.contains("private"));
2087        }
2088        for (size, checksum) in [
2089            (
2090                Some(MAX_VIDEO_DOWNLOAD_BYTES as u64 + 1),
2091                descriptor.expected_md5.clone(),
2092            ),
2093            (descriptor.expected_bytes, None),
2094        ] {
2095            descriptor.expected_bytes = size;
2096            descriptor.expected_md5 = checksum;
2097            let mut requests = 0;
2098            assert!(client()
2099                .download_with_transport(
2100                    &descriptor,
2101                    |_, _| Ok(()),
2102                    |_, _| {
2103                        requests += 1;
2104                        bail!("must not request")
2105                    }
2106                )
2107                .is_err());
2108            assert_eq!(requests, 0);
2109        }
2110    }
2111
2112    #[test]
2113    fn cold_large_descriptor_downloads_with_padding_and_hashes() {
2114        let (descriptor, raw, ciphertext) =
2115            download_fixture(32 * 1024 * 1024, MediaQuality::LargeFile);
2116        assert_eq!(ciphertext.len(), 32 * 1024 * 1024 + 16);
2117        let stored = serde_json::to_vec(&descriptor).unwrap();
2118        let cold: MediaDescriptor = serde_json::from_slice(&stored).unwrap();
2119        let events = std::cell::RefCell::new(Vec::new());
2120        let mut requests = 0;
2121        let mut previous = 0;
2122        let downloaded = client()
2123            .download_with_transport(
2124                &cold,
2125                |done, total| {
2126                    events.borrow_mut().push("progress");
2127                    assert!(done >= previous);
2128                    previous = done;
2129                    if let Some(total) = total {
2130                        assert_eq!(total, ciphertext.len() as u64);
2131                    }
2132                    Ok(())
2133                },
2134                |path, fields| {
2135                    events.borrow_mut().push("request");
2136                    requests += 1;
2137                    assert_eq!(path, "/downloadbig");
2138                    assert_eq!(integer(fields, "filetype")?, 7);
2139                    assert_eq!(integer(fields, "seq")?, requests);
2140                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
2141                    Ok(download_reply(fields, &cold, &ciphertext))
2142                },
2143            )
2144            .unwrap();
2145        assert_eq!(requests, 65);
2146        assert_eq!(previous, ciphertext.len() as u64);
2147        for chunk in events.borrow().chunks(3) {
2148            assert_eq!(chunk, ["progress", "request", "progress"]);
2149        }
2150        assert_eq!(downloaded.bytes, raw);
2151        assert_eq!(
2152            downloaded.md5,
2153            descriptor.expected_md5.as_ref().unwrap().as_str()
2154        );
2155        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
2156        assert_eq!(downloaded.requested_quality, MediaQuality::LargeFile);
2157        assert_eq!(downloaded.substituted_filetype, None);
2158        assert_eq!(downloaded.picture_format, None);
2159        assert_eq!(downloaded.advertised_size_matches, Some(true));
2160    }
2161    #[test]
2162    fn large_download_accepts_missing_size_but_checks_supplied_size() {
2163        let (descriptor, raw, ciphertext) =
2164            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2165        let downloaded = client()
2166            .download_with_transport(
2167                &descriptor,
2168                |_, _| Ok(()),
2169                |_, fields| {
2170                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2171                    reply.remove("rawfilesize");
2172                    Ok(reply)
2173                },
2174            )
2175            .unwrap();
2176        assert_eq!(downloaded.bytes, raw);
2177        for value in ["", "invalid", "-1", "1"] {
2178            let result = client().download_with_transport(
2179                &descriptor,
2180                |_, _| Ok(()),
2181                |_, fields| {
2182                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2183                    reply.insert("rawfilesize".into(), value.as_bytes().to_vec());
2184                    Ok(reply)
2185                },
2186            );
2187            assert!(result.is_err(), "Accepted invalid source size {value}");
2188        }
2189    }
2190    #[test]
2191    fn large_download_rejections_expose_only_typed_static_control_details() {
2192        use LargeFileDownloadControl as Control;
2193        let (descriptor, _, ciphertext) =
2194            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2195        for (control, actual) in [(Control::Version, 1), (Control::VideoFormat, 1)] {
2196            let mut calls = 0;
2197            let error = client()
2198                .download_with_transport(
2199                    &descriptor,
2200                    |_, _| Ok(()),
2201                    |_, fields| {
2202                        calls += 1;
2203                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2204                        put(&mut reply, control.field_name(), actual);
2205                        Ok(reply)
2206                    },
2207                )
2208                .err()
2209                .unwrap();
2210            assert_eq!(calls, 1);
2211            assert_eq!(
2212                error.downcast_ref::<LargeFileDownloadError>(),
2213                Some(&LargeFileDownloadError::FixedControlMismatch { control, actual })
2214            );
2215            assert_eq!(error.to_string(), "Unsupported large-file download control");
2216        }
2217        let secret = "private-response-token-and-file-id";
2218        let error = client()
2219            .download_with_transport(
2220                &descriptor,
2221                |_, _| Ok(()),
2222                |_, fields| {
2223                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2224                    reply.insert(secret.into(), secret.as_bytes().to_vec());
2225                    Ok(reply)
2226                },
2227            )
2228            .err()
2229            .unwrap();
2230        assert_eq!(
2231            error.downcast_ref::<LargeFileDownloadError>(),
2232            Some(&LargeFileDownloadError::UnknownField)
2233        );
2234        assert_eq!(
2235            error.to_string(),
2236            "Unsupported large-file download response field"
2237        );
2238        assert!(!format!("{error:?}").contains(secret));
2239    }
2240    #[test]
2241    fn large_download_rejects_correlation_format_unknown_fields_and_ranges() {
2242        let (descriptor, _, ciphertext) =
2243            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2244        for (key, value) in [
2245            ("seq", 2),
2246            ("ver", 1),
2247            ("retcode", -1),
2248            ("videoformat", 1),
2249            ("rangestart", 1),
2250            ("rangeend", 512 * 1024),
2251            ("totalsize", ciphertext.len() as i64 + 16),
2252            ("rawfilesize", 1),
2253            ("futureauth", 1),
2254        ] {
2255            let mut requests = 0;
2256            let outcome = client().download_with_transport(
2257                &descriptor,
2258                |_, _| Ok(()),
2259                |_, fields| {
2260                    requests += 1;
2261                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2262                    put(&mut reply, key, value);
2263                    Ok(reply)
2264                },
2265            );
2266            assert!(outcome.is_err(), "Accepted invalid {key}");
2267            assert_eq!(requests, 1);
2268        }
2269        for missing in ["seq", "ver", "videoformat", "rawfilemd5", "filedata"] {
2270            assert!(
2271                client()
2272                    .download_with_transport(
2273                        &descriptor,
2274                        |_, _| Ok(()),
2275                        |_, fields| {
2276                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
2277                            reply.remove(missing);
2278                            Ok(reply)
2279                        }
2280                    )
2281                    .is_err(),
2282                "Accepted missing {missing}"
2283            );
2284        }
2285    }
2286    #[test]
2287    fn file7_success_metadata_is_optional_and_does_not_change_verified_bytes() {
2288        // These eight names come from the reference template/consumer join,
2289        // independently of the native validator's allowlist.
2290        let metadata = [
2291            "isretry",
2292            "retrysec",
2293            "isoverload",
2294            "isgetcdn",
2295            "istrans",
2296            "backsrcnum",
2297            "substituteftype",
2298            "rsppicformat",
2299        ];
2300        let (descriptor, raw, ciphertext) = download_fixture(1024 * 1024, MediaQuality::LargeFile);
2301        assert_eq!(ciphertext.len(), 1024 * 1024 + 16);
2302        // Each field may be absent or vary independently on the first, middle,
2303        // and final padding-only ranges, without creating another request.
2304        for selected in metadata.into_iter().map(Some).chain([None]) {
2305            for omit in [false, true] {
2306                let mut requests = 0;
2307                let result = client()
2308                    .download_with_transport(
2309                        &descriptor,
2310                        |_, _| Ok(()),
2311                        |path, fields| {
2312                            requests += 1;
2313                            assert_eq!(path, "/downloadbig");
2314                            assert_eq!(integer(fields, "seq")?, requests);
2315                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
2316                            for key in metadata {
2317                                if selected.is_none_or(|selected| selected == key) {
2318                                    if omit {
2319                                        reply.remove(key);
2320                                    } else {
2321                                        put(
2322                                            &mut reply,
2323                                            key,
2324                                            match requests {
2325                                                1 => 1,
2326                                                2 => 999,
2327                                                3 => 9_999_999_999_999_999i64,
2328                                                _ => panic!("Unexpected retry or extra range"),
2329                                            },
2330                                        );
2331                                    }
2332                                }
2333                            }
2334                            Ok(reply)
2335                        },
2336                    )
2337                    .unwrap();
2338                assert_eq!(requests, 3);
2339                assert_eq!(result.bytes, raw);
2340                assert_eq!(result.md5, descriptor.expected_md5.as_deref().unwrap());
2341                assert_eq!(result.sha256, hex::encode(sha256(&raw)));
2342                assert_eq!(result.requested_quality, MediaQuality::LargeFile);
2343                assert_eq!(result.substituted_filetype, None);
2344                assert_eq!(result.picture_format, None);
2345            }
2346        }
2347    }
2348    #[test]
2349    fn file7_metadata_parser_bounds_and_errors_remain_strict() {
2350        let (descriptor, _, ciphertext) = download_fixture(512 * 1024, MediaQuality::LargeFile);
2351        for key in [
2352            "isretry",
2353            "retrysec",
2354            "isoverload",
2355            "isgetcdn",
2356            "istrans",
2357            "backsrcnum",
2358            "substituteftype",
2359            "rsppicformat",
2360        ] {
2361            for malformed in [
2362                b"".as_slice(),
2363                b"-1",
2364                b"-0",
2365                b"+1",
2366                b" 1",
2367                b"1x",
2368                b"99999999999999999",
2369                b"\xff",
2370            ] {
2371                let mut requests = 0;
2372                assert!(
2373                    client()
2374                        .download_with_transport(
2375                            &descriptor,
2376                            |_, _| Ok(()),
2377                            |_, fields| {
2378                                requests += 1;
2379                                let mut reply = download_reply(fields, &descriptor, &ciphertext);
2380                                reply.insert(key.into(), malformed.to_vec());
2381                                Ok(reply)
2382                            },
2383                        )
2384                        .is_err(),
2385                    "Accepted malformed {key}"
2386                );
2387                assert_eq!(requests, 1);
2388            }
2389        }
2390        let mut requests = 0;
2391        let error = client()
2392            .download_with_transport(
2393                &descriptor,
2394                |_, _| Ok(()),
2395                |_, fields| {
2396                    requests += 1;
2397                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2398                    for key in ["isretry", "retrysec", "isoverload", "isgetcdn"] {
2399                        put(&mut reply, key, 1);
2400                    }
2401                    put(&mut reply, "retcode", -1);
2402                    Ok(reply)
2403                },
2404            )
2405            .err()
2406            .unwrap();
2407        assert_eq!(error.to_string(), "CDN download returned -1");
2408        assert_eq!(requests, 1);
2409    }
2410    #[test]
2411    fn normal_download_keeps_representation_metadata_and_consistency_checks() {
2412        let (descriptor, raw, ciphertext) = download_fixture(65536, MediaQuality::File);
2413        let result = client()
2414            .download_with_transport(
2415                &descriptor,
2416                |_, _| Ok(()),
2417                |path, fields| {
2418                    assert_eq!(path, "/download");
2419                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2420                    put(&mut reply, "substituteftype", 5);
2421                    put(&mut reply, "rsppicformat", 3);
2422                    Ok(reply)
2423                },
2424            )
2425            .unwrap();
2426        assert_eq!(result.bytes, raw);
2427        assert_eq!(result.substituted_filetype, Some(5));
2428        assert_eq!(result.picture_format, Some(3));
2429        let mut requests = 0;
2430        assert!(client()
2431            .download_with_transport(
2432                &descriptor,
2433                |_, _| Ok(()),
2434                |_, fields| {
2435                    requests += 1;
2436                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2437                    put(&mut reply, "rsppicformat", requests);
2438                    Ok(reply)
2439                },
2440            )
2441            .is_err());
2442        assert_eq!(requests, 2);
2443    }
2444    #[test]
2445    fn storage_download_uses_the_storage_route_and_fields() {
2446        let (descriptor, raw, ciphertext) = download_fixture(100_000, MediaQuality::File);
2447        let mut paths = vec![];
2448        let mut bytes = vec![];
2449        let result = client()
2450            .download_on_route(
2451                &descriptor,
2452                Some("2_pack.fixture"),
2453                |_, _| Ok(()),
2454                |path, fields| {
2455                    paths.push(path.to_string());
2456                    assert_eq!(integer(fields, "filetype")?, 5);
2457                    for (key, value) in [
2458                        ("apptype", 10),
2459                        ("isstorevideo", 0),
2460                        ("nocheckaeskey", 1),
2461                        ("enablehit", 1),
2462                        ("existancecheck", 0),
2463                    ] {
2464                        assert_eq!(integer(fields, key)?, value, "{key}");
2465                    }
2466                    assert_eq!(fields["filekey"], b"2_pack.fixture");
2467                    Ok(download_reply(fields, &descriptor, &ciphertext))
2468                },
2469                &mut bytes,
2470            )
2471            .unwrap();
2472        assert_eq!(bytes, raw);
2473        assert_eq!(result.byte_length, raw.len() as u64);
2474        assert_eq!(result.sha256, hex::encode(sha256(&raw)));
2475        assert_eq!(paths, ["/downloadstorage", "/downloadstorage"]);
2476        // Ordinary downloads keep their route and send no storage fields.
2477        client()
2478            .download_with_transport(
2479                &descriptor,
2480                |_, _| Ok(()),
2481                |path, fields| {
2482                    assert_eq!(path, "/download");
2483                    assert!(!fields.contains_key("filekey") && !fields.contains_key("apptype"));
2484                    Ok(download_reply(fields, &descriptor, &ciphertext))
2485                },
2486            )
2487            .unwrap();
2488        // Storage keys must be usable and only whole-file descriptors qualify.
2489        let unused = |_: &str, _: &CdnFields| -> Result<CdnFields> { unreachable!() };
2490        assert!(client()
2491            .download_on_route(&descriptor, Some(""), |_, _| Ok(()), unused, &mut vec![])
2492            .is_err());
2493        let (video, _, _) = download_fixture(1000, MediaQuality::Video);
2494        assert!(client()
2495            .download_on_route(
2496                &video,
2497                Some("2_pack.fixture"),
2498                |_, _| Ok(()),
2499                unused,
2500                &mut vec![]
2501            )
2502            .is_err());
2503    }
2504    #[test]
2505    fn download_refuses_truncated_overlong_and_changing_source_bytes() {
2506        let (descriptor, _, ciphertext) =
2507            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2508        for failure in [
2509            "empty",
2510            "overlong",
2511            "source-checksum",
2512            "changed-checksum",
2513            "changed-total",
2514            "short-nonfinal",
2515        ] {
2516            let mut requests = 0;
2517            let outcome = client().download_with_transport(
2518                &descriptor,
2519                |_, _| Ok(()),
2520                |_, fields| {
2521                    requests += 1;
2522                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2523                    match failure {
2524                        "empty" => {
2525                            reply.insert("filedata".into(), vec![]);
2526                        }
2527                        "overlong" => {
2528                            reply.get_mut("filedata").unwrap().push(1);
2529                            put(&mut reply, "rangeend", 512 * 1024);
2530                        }
2531                        "source-checksum" => {
2532                            reply.insert(
2533                                "rawfilemd5".into(),
2534                                b"0123456789abcdef0123456789abcdef".to_vec(),
2535                            );
2536                        }
2537                        "changed-checksum" if requests == 2 => {
2538                            reply.insert(
2539                                "rawfilemd5".into(),
2540                                b"0123456789abcdef0123456789abcdef".to_vec(),
2541                            );
2542                        }
2543                        "changed-total" if requests == 2 => {
2544                            put(&mut reply, "totalsize", ciphertext.len() + 16)
2545                        }
2546                        "short-nonfinal" => {
2547                            reply.insert("filedata".into(), vec![1]);
2548                            put(&mut reply, "rangeend", integer(fields, "rangestart")?);
2549                        }
2550                        _ => {}
2551                    }
2552                    Ok(reply)
2553                },
2554            );
2555            assert!(outcome.is_err(), "Accepted {failure}");
2556            assert!(requests <= 2);
2557        }
2558    }
2559    #[test]
2560    fn large_download_checks_padding_actual_size_and_digest_independently() {
2561        for omit_source_size in [false, true] {
2562            let reply_for_test =
2563                |fields: &CdnFields, descriptor: &MediaDescriptor, bytes: &[u8]| {
2564                    let mut reply = download_reply(fields, descriptor, bytes);
2565                    if omit_source_size {
2566                        reply.remove("rawfilesize");
2567                    }
2568                    reply
2569                };
2570            let (descriptor, _, ciphertext) =
2571                download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2572            let mut invalid_padding = ciphertext.clone();
2573            let mut encoder = openssl::symm::Crypter::new(
2574                Cipher::aes_128_ecb(),
2575                openssl::symm::Mode::Encrypt,
2576                &descriptor.aes_key,
2577                None,
2578            )
2579            .unwrap();
2580            encoder.pad(false);
2581            let mut block = [0u8; 32];
2582            let written = encoder.update(&[0u8; 16], &mut block).unwrap();
2583            assert_eq!(
2584                written + encoder.finalize(&mut block[written..]).unwrap(),
2585                16
2586            );
2587            let last = invalid_padding.len() - 16;
2588            invalid_padding[last..].copy_from_slice(&block[..16]);
2589            assert!(client()
2590                .download_with_transport(
2591                    &descriptor,
2592                    |_, _| Ok(()),
2593                    |_, fields| Ok(reply_for_test(fields, &descriptor, &invalid_padding))
2594                )
2595                .is_err());
2596            let mut without_advertised_md5 = descriptor.clone();
2597            without_advertised_md5.expected_md5 = None;
2598            for change_later in [false, true] {
2599                let mut requests = 0;
2600                assert!(client()
2601                    .download_with_transport(
2602                        &without_advertised_md5,
2603                        |_, _| Ok(()),
2604                        |_, fields| {
2605                            requests += 1;
2606                            let mut reply = reply_for_test(fields, &descriptor, &ciphertext);
2607                            if !change_later || requests == 2 {
2608                                reply.insert(
2609                                    "rawfilemd5".into(),
2610                                    b"0123456789abcdef0123456789abcdef".to_vec(),
2611                                );
2612                            }
2613                            Ok(reply)
2614                        }
2615                    )
2616                    .is_err());
2617                assert_eq!(requests, 2);
2618            }
2619            let mut wrong_source = ciphertext.clone();
2620            wrong_source[0] ^= 1;
2621            assert!(client()
2622                .download_with_transport(
2623                    &descriptor,
2624                    |_, _| Ok(()),
2625                    |_, fields| Ok(reply_for_test(fields, &descriptor, &wrong_source))
2626                )
2627                .is_err());
2628            let mut wrong_size = descriptor.clone();
2629            wrong_size.expected_bytes = Some(descriptor.expected_bytes.unwrap() + 1);
2630            wrong_size.strict_size = false;
2631            assert!(client()
2632                .download_with_transport(
2633                    &wrong_size,
2634                    |_, _| Ok(()),
2635                    |_, fields| Ok(reply_for_test(fields, &wrong_size, &ciphertext))
2636                )
2637                .is_err());
2638        }
2639    }
2640    #[test]
2641    fn cancellation_and_transport_failure_never_start_another_download_request() {
2642        let (descriptor, _, ciphertext) =
2643            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2644        for stop in [1, 2, 3, 4] {
2645            let mut callbacks = 0;
2646            let mut requests = 0;
2647            let outcome = client().download_with_transport(
2648                &descriptor,
2649                |_, _| {
2650                    callbacks += 1;
2651                    ensure!(callbacks != stop, "cancelled");
2652                    Ok(())
2653                },
2654                |_, fields| {
2655                    requests += 1;
2656                    Ok(download_reply(fields, &descriptor, &ciphertext))
2657                },
2658            );
2659            assert!(outcome.is_err());
2660            assert_eq!(callbacks, stop);
2661            assert_eq!(requests, stop / 2);
2662        }
2663        let mut requests = 0;
2664        assert!(client()
2665            .download_with_transport(
2666                &descriptor,
2667                |_, _| Ok(()),
2668                |_, _| {
2669                    requests += 1;
2670                    bail!("transport failure")
2671                }
2672            )
2673            .is_err());
2674        assert_eq!(requests, 1);
2675    }
2676    #[test]
2677    fn normal_download_keeps_optional_sequence_and_original_limits() {
2678        for quality in [
2679            MediaQuality::Original,
2680            MediaQuality::Standard,
2681            MediaQuality::Thumbnail,
2682            MediaQuality::Video,
2683            MediaQuality::File,
2684        ] {
2685            let (descriptor, raw, ciphertext) = download_fixture(65536 + 17, quality);
2686            let mut requests = 0;
2687            let downloaded = client()
2688                .download_with_transport(
2689                    &descriptor,
2690                    |_, _| Ok(()),
2691                    |path, fields| {
2692                        requests += 1;
2693                        assert_eq!(path, "/download");
2694                        assert_eq!(
2695                            integer(fields, "rangeend")? - integer(fields, "rangestart")? + 1,
2696                            if requests == 1 { 65536 } else { 32 }
2697                        );
2698                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2699                        reply.remove("seq");
2700                        Ok(reply)
2701                    },
2702                )
2703                .unwrap();
2704            assert_eq!(downloaded.bytes, raw);
2705            assert_eq!(requests, 2);
2706            assert!(client()
2707                .download_with_transport(
2708                    &descriptor,
2709                    |_, _| Ok(()),
2710                    |_, fields| {
2711                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2712                        put(&mut reply, "seq", 99);
2713                        Ok(reply)
2714                    }
2715                )
2716                .is_err());
2717            assert!(client()
2718                .download_with_transport(
2719                    &descriptor,
2720                    |_, _| Ok(()),
2721                    |_, fields| {
2722                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2723                        put(&mut reply, "totalsize", MAX_CDN_TRANSFER_BYTES + 32);
2724                        Ok(reply)
2725                    }
2726                )
2727                .is_err());
2728        }
2729        assert!(unpack_cdn(&vec![0; MAX_CDN_BODY + 1]).is_err());
2730    }
2731    #[test]
2732    fn large_descriptor_limits_fail_before_transport() {
2733        let (descriptor, _, _) = download_fixture(16, MediaQuality::LargeFile);
2734        for expected in [
2735            None,
2736            Some(0),
2737            Some(MAX_LARGE_FILE_BYTES as u64 + 1),
2738            Some(u64::MAX),
2739        ] {
2740            let mut descriptor = descriptor.clone();
2741            descriptor.expected_bytes = expected;
2742            let mut requests = 0;
2743            assert!(client()
2744                .download_with_transport(
2745                    &descriptor,
2746                    |_, _| Ok(()),
2747                    |_, _| {
2748                        requests += 1;
2749                        bail!("must not request")
2750                    }
2751                )
2752                .is_err());
2753            assert_eq!(requests, 0);
2754        }
2755    }
2756
2757    #[test]
2758    fn legacy_completion_receipt_survives_cancellation_after_final_ack() {
2759        let prepared = PreparedUpload::prepare(
2760            b"synthetic source",
2761            MediaQuality::File,
2762            "source.bin",
2763            "filehelper",
2764        )
2765        .unwrap();
2766        let cancelled = std::cell::Cell::new(false);
2767        let calls = std::cell::Cell::new(0);
2768        let upload = client()
2769            .upload_with_transport(
2770                UploadRef::Memory(&prepared),
2771                |_, _| {
2772                    ensure!(!cancelled.get(), "cancelled");
2773                    Ok(())
2774                },
2775                |path, fields| {
2776                    assert_eq!(path, "/uploadv3");
2777                    calls.set(calls.get() + 1);
2778                    let mut reply = CdnFields::new();
2779                    put(&mut reply, "retcode", 0);
2780                    for key in ["seq", "rangestart", "rangeend"] {
2781                        reply.insert(key.into(), fields[key].clone());
2782                    }
2783                    reply.insert("fileid".into(), b"synthetic-completed-object".to_vec());
2784                    cancelled.set(true);
2785                    Ok(reply)
2786                },
2787            )
2788            .unwrap();
2789        assert!(cancelled.get());
2790        assert_eq!(calls.get(), 1);
2791        assert_eq!(upload.descriptor.file_id, "synthetic-completed-object");
2792    }
2793
2794    #[test]
2795    fn paired_video_preserves_final_receipt_and_does_not_admit_large_normal() {
2796        let mut thumbnail = Vec::new();
2797        image::codecs::jpeg::JpegEncoder::new(&mut thumbnail)
2798            .encode_image(&image::DynamicImage::new_rgb8(16, 12))
2799            .unwrap();
2800        let p = PreparedVideoUpload::with_material(
2801            b"\0\0\0\x0cftypisom",
2802            &thumbnail,
2803            "normal.mp4",
2804            "filehelper",
2805            &[0x31; 16],
2806            "00112233445566778899aabbccddeeff",
2807        )
2808        .unwrap();
2809        let cancelled = std::cell::Cell::new(false);
2810        let uploaded = client()
2811            .upload_video_with_transport(
2812                &p,
2813                |_, _| {
2814                    ensure!(!cancelled.get(), "cancelled");
2815                    Ok(())
2816                },
2817                |path, fields| {
2818                    assert_eq!(path, "/uploadv3");
2819                    assert!(fields.contains_key("thumbdata"));
2820                    assert_eq!(integer(fields, "filetype")?, 4);
2821                    let mut reply = CdnFields::new();
2822                    put(&mut reply, "retcode", 0);
2823                    for key in ["seq", "rangestart", "rangeend"] {
2824                        reply.insert(key.into(), fields[key].clone());
2825                    }
2826                    reply.insert("fileid".into(), b"paired-object".to_vec());
2827                    cancelled.set(true);
2828                    Ok(reply)
2829                },
2830            )
2831            .unwrap();
2832        assert!(uploaded.original.is_none());
2833        assert_eq!(uploaded.video.descriptor.file_id, "paired-object");
2834        assert_eq!(uploaded.thumbnail.descriptor.file_id, "paired-object");
2835        let mut altered = p;
2836        altered.video.plaintext_bytes = MAX_NATIVE_UPLOAD_BYTES as u32 + 1;
2837        assert!(client()
2838            .upload_video_with_transport(
2839                &altered,
2840                |_, _| Ok(()),
2841                |_, _| { panic!("Large normal video reached legacy transport") }
2842            )
2843            .is_err());
2844    }
2845    #[test]
2846    fn boundary_matrix_keeps_ciphertext_and_media_kind_limits_separate() {
2847        let boundary = MAX_CDN_TRANSFER_BYTES;
2848        for (size, padded, legacy) in [
2849            (boundary - 17, boundary - 16, Some("/uploadv3")),
2850            (boundary - 16, boundary, None),
2851            (boundary - 1, boundary, None),
2852            (boundary, boundary + 16, None),
2853            (boundary + 1, boundary + 16, None),
2854            (MAX_LARGE_FILE_BYTES, MAX_LARGE_FILE_BYTES + 16, None),
2855            (MAX_LARGE_FILE_BYTES + 1, MAX_LARGE_FILE_BYTES + 16, None),
2856        ] {
2857            assert_eq!((size / 16 + 1) * 16, padded);
2858            let kind = if size > boundary {
2859                MediaQuality::LargeFile
2860            } else {
2861                MediaQuality::File
2862            };
2863            assert_eq!(legacy_upload_path(kind, size, padded).ok(), legacy);
2864            assert_eq!(
2865                size <= upload_plaintext_limit(kind),
2866                size <= MAX_LARGE_FILE_BYTES
2867            );
2868            assert_eq!(
2869                size <= upload_plaintext_limit(MediaQuality::Original),
2870                size <= MAX_NATIVE_UPLOAD_BYTES
2871            );
2872            assert_eq!(
2873                size <= upload_plaintext_limit(MediaQuality::Video),
2874                size <= wechat_media::MAX_VIDEO_SOURCE_BYTES
2875            );
2876        }
2877        assert_eq!(MAX_LARGE_FILE_BYTES, 536_870_912);
2878        assert_eq!(MAX_NATIVE_UPLOAD_BYTES, 26_214_383);
2879        assert_eq!(MAX_CDN_TRANSFER_BYTES, 26_214_400);
2880    }
2881    #[test]
2882    fn multiphase_paths_bind_account_sequence_type_and_phase() {
2883        for filetype in [4, 5, 7] {
2884            for phase in ["apply", "uploadblock", "fin", "complete"] {
2885                assert!(large_file_path(
2886                    &format!("/cdn/12345/3/{filetype}/{phase}"),
2887                    12345,
2888                    3
2889                ));
2890            }
2891        }
2892        for path in [
2893            "/cdn/999/3/7/apply",
2894            "/cdn/12345/2/7/apply",
2895            "/cdn/12345/03/7/apply",
2896            "/cdn/12345/0/7/apply",
2897            "/cdn/12345/3/3/apply",
2898            "/cdn/12345/3/7/unknown",
2899            "/cdn/12345/3/7/apply?x=1",
2900            "/cdn/12345/3/7/apply/extra",
2901            "//cdn/12345/3/7/apply",
2902            "/cdn/12345/3/7/../apply",
2903        ] {
2904            assert!(!large_file_path(path, 12345, 3), "Accepted {path}");
2905        }
2906        assert!(!large_file_path("/cdn/12345/0/7/apply", 12345, 0));
2907    }
2908    #[test]
2909    fn cdn_codec_rejects_duplicate_and_truncated() {
2910        let fields = BTreeMap::from([
2911            ("retcode".to_string(), b"0".to_vec()),
2912            ("filedata".into(), vec![0, 1, 2, 255]),
2913        ]);
2914        let packed = pack_cdn(&fields).unwrap();
2915        assert_eq!(unpack_cdn(&packed).unwrap(), fields);
2916        let one = pack_cdn(&BTreeMap::from([("x".into(), vec![1])])).unwrap();
2917        for n in 1..one.len() {
2918            assert!(unpack_cdn(&one[..n]).is_err());
2919        }
2920        let duplicate = [one.clone(), one].concat();
2921        assert!(unpack_cdn(&duplicate).is_err());
2922    }
2923    #[test]
2924    fn prepared_upload_roundtrip() {
2925        let p = PreparedUpload::prepare(
2926            b"synthetic attachment",
2927            MediaQuality::File,
2928            "test.txt",
2929            "filehelper",
2930        )
2931        .unwrap();
2932        p.validate().unwrap();
2933        assert_eq!(
2934            decrypt_cdn(&p.encrypted, &p.aes_key).unwrap(),
2935            b"synthetic attachment"
2936        );
2937        let mut corrupt = p.clone();
2938        corrupt.encrypted[0] ^= 1;
2939        assert!(corrupt.validate().is_err());
2940    }
2941    #[test]
2942    fn nonpublic_addresses_rejected() {
2943        for ip in [
2944            "0.1.2.3",
2945            "10.0.0.1",
2946            "100.64.0.1",
2947            "127.0.0.1",
2948            "169.254.1.1",
2949            "172.16.0.1",
2950            "192.168.0.1",
2951            "198.18.0.1",
2952            "192.0.2.1",
2953            "198.51.100.1",
2954            "203.0.113.1",
2955            "224.1.2.3",
2956        ] {
2957            assert!(!public_ipv4(ip.parse().unwrap()));
2958        }
2959        assert!(public_ipv4("8.8.8.8".parse().unwrap()));
2960    }
2961    #[test]
2962    fn upload_reply_correlates_ranges() {
2963        let mut f = CdnFields::from([
2964            ("retcode".into(), b"0".to_vec()),
2965            ("seq".into(), b"2".to_vec()),
2966        ]);
2967        assert!(validate_upload_reply(&f, 1, 0, 15, &mut None).is_err());
2968        put(&mut f, "seq", 1);
2969        put(&mut f, "rangestart", 0);
2970        put(&mut f, "rangeend", 15);
2971        f.insert("fileid".into(), b"synthetic-file-id".to_vec());
2972        let mut id = None;
2973        validate_upload_reply(&f, 1, 0, 15, &mut id).unwrap();
2974        assert_eq!(id, Some("synthetic-file-id".into()));
2975    }
2976    #[test]
2977    fn prepared_jpeg_variants_are_real_and_independent() {
2978        fn jpeg(w: u32, h: u32) -> Vec<u8> {
2979            let rgb = image::RgbImage::from_pixel(w, h, image::Rgb([12, 40, 80]));
2980            let mut out = vec![];
2981            image::codecs::jpeg::JpegEncoder::new(&mut out)
2982                .encode_image(&rgb)
2983                .unwrap();
2984            out
2985        }
2986        let p = PreparedImageUpload::prepare(
2987            &jpeg(64, 48),
2988            &jpeg(32, 24),
2989            &jpeg(16, 12),
2990            "image.jpg",
2991            "filehelper",
2992        )
2993        .unwrap();
2994        p.validate().unwrap();
2995        assert_eq!((p.width, p.height), (64, 48));
2996        assert_eq!((p.thumbnail.width, p.thumbnail.height), (16, 12));
2997        assert!(PreparedImageUpload::prepare(
2998            &jpeg(8, 8),
2999            &jpeg(16, 16),
3000            &jpeg(4, 4),
3001            "image.jpg",
3002            "filehelper"
3003        )
3004        .is_err());
3005        assert!(PreparedImageUpload::prepare(
3006            b"not image",
3007            &jpeg(4, 4),
3008            &jpeg(2, 2),
3009            "image.jpg",
3010            "filehelper"
3011        )
3012        .is_err());
3013    }
3014    #[test]
3015    fn image_original_uploads_in_ranges_with_previews_in_the_first_request() {
3016        fn jpeg(w: u32, h: u32, noisy: bool) -> Vec<u8> {
3017            let mut seed = 7u32;
3018            let rgb = image::RgbImage::from_fn(w, h, |_, _| {
3019                seed ^= seed << 13;
3020                seed ^= seed >> 17;
3021                seed ^= seed << 5;
3022                let [r, g, b, _] = if noisy { seed } else { 0x0050_280c }.to_le_bytes();
3023                image::Rgb([r, g, b])
3024            });
3025            let mut out = vec![];
3026            image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95)
3027                .encode_image(&rgb)
3028                .unwrap();
3029            out
3030        }
3031        // Wider than the 4,096-pixel preview limit and several ranges long.
3032        let original = jpeg(4200, 64, true);
3033        assert!(original.len() > 3 * UPLOAD_CHUNK);
3034        let p = PreparedImageUpload::prepare(
3035            &original,
3036            &jpeg(640, 10, false),
3037            &jpeg(160, 3, false),
3038            "wide.jpg",
3039            "filehelper",
3040        )
3041        .unwrap();
3042        assert_eq!((p.width, p.height), (4200, 64));
3043        let total = p.original.encrypted.len();
3044        let requests = std::cell::RefCell::new(vec![]);
3045        let reported = std::cell::RefCell::new(vec![]);
3046        let uploaded = client()
3047            .upload_image_with_transport(
3048                &p,
3049                |sent, all| {
3050                    assert_eq!(all, Some(total as u64));
3051                    reported.borrow_mut().push(sent);
3052                    Ok(())
3053                },
3054                |path, fields| {
3055                    assert_eq!(path, "/uploadv3");
3056                    assert_eq!(integer(fields, "filetype")?, 1);
3057                    assert_eq!(integer(fields, "totalsize")?, total as i64);
3058                    // Every request must fit the bounded CDN envelope.
3059                    pack_cdn(fields)?;
3060                    let start = integer(fields, "rangestart")? as usize;
3061                    let end = integer(fields, "rangeend")? as usize;
3062                    assert_eq!(fields["filedata"], p.original.encrypted[start..=end]);
3063                    let first = start == 0;
3064                    for key in ["thumbdata", "midimgdata", "midimgmd5", "imgwidth"] {
3065                        assert_eq!(fields.contains_key(key), first, "{key}");
3066                    }
3067                    assert_eq!(integer(fields, "hasthumb")?, i64::from(first));
3068                    requests.borrow_mut().push((start, end));
3069                    let mut reply = CdnFields::new();
3070                    put(&mut reply, "retcode", 0);
3071                    for key in ["seq", "rangestart", "rangeend"] {
3072                        reply.insert(key.into(), fields[key].clone());
3073                    }
3074                    reply.insert("fileid".into(), b"image-object".to_vec());
3075                    Ok(reply)
3076                },
3077            )
3078            .unwrap();
3079        let requests = requests.into_inner();
3080        assert_eq!(requests.len(), total.div_ceil(UPLOAD_CHUNK));
3081        assert_eq!(requests[0], (0, UPLOAD_CHUNK - 1));
3082        assert!(requests.windows(2).all(|pair| pair[0].1 + 1 == pair[1].0));
3083        assert_eq!(requests.last().unwrap().1, total - 1);
3084        assert_eq!(reported.borrow()[0], 0);
3085        assert!(reported.borrow().iter().all(|sent| *sent < total as u64));
3086        let uploaded_original = uploaded.original.as_ref().unwrap();
3087        assert_eq!(uploaded_original.plaintext_bytes as usize, original.len());
3088        assert_eq!(uploaded_original.width, Some(4200));
3089        for copy in [uploaded_original, &uploaded.standard, &uploaded.thumbnail] {
3090            assert_eq!(copy.descriptor.file_id, "image-object");
3091        }
3092        // A standard copy keeps its own limits.
3093        assert!(PreparedImageUpload::prepare(
3094            &original,
3095            &jpeg(4200, 64, false),
3096            &jpeg(160, 3, false),
3097            "wide.jpg",
3098            "filehelper"
3099        )
3100        .is_err());
3101    }
3102    fn video_mismatch(
3103        descriptor: &MediaDescriptor,
3104        ciphertext: &[u8],
3105        extra: &[(&str, &[u8])],
3106    ) -> VideoRepresentationMismatch {
3107        let error = client()
3108            .download_with_transport(
3109                descriptor,
3110                |_, _| Ok(()),
3111                |_, fields| {
3112                    let mut reply = download_reply(fields, descriptor, ciphertext);
3113                    for (key, value) in extra {
3114                        reply.insert((*key).into(), value.to_vec());
3115                    }
3116                    Ok(reply)
3117                },
3118            )
3119            .err()
3120            .expect("mismatched video must be rejected");
3121        let text = format!("{error:#} {error:?}");
3122        assert!(!text.contains(descriptor.expected_md5.as_deref().unwrap()));
3123        *error
3124            .downcast_ref::<VideoRepresentationMismatch>()
3125            .expect("typed video mismatch")
3126    }
3127    #[test]
3128    fn normal_video_of_another_length_is_rejected_with_comparison_facts_only() {
3129        // Synthetic values: the advertised tuple describes one object while the
3130        // CDN returns a complete, correctly encrypted object of another length.
3131        let (descriptor, _, _) = download_fixture(9_390, MediaQuality::Video);
3132        let delivered = vec![0x6b; 15_272];
3133        let delivered_md5 = hex::encode(md5(&delivered));
3134        let ciphertext = ecb_encrypt(&delivered, &descriptor.aes_key).unwrap();
3135        let bare = video_mismatch(&descriptor, &ciphertext, &[]);
3136        assert_eq!(
3137            bare,
3138            VideoRepresentationMismatch {
3139                advertised_bytes: Some(9_390),
3140                delivered_bytes: 15_272,
3141                advertised_digest_matches: Some(false),
3142                substituted_filetype: None,
3143                istrans: None,
3144                videoformat: None,
3145                rawfilesize: None,
3146                rawfilemd5: None,
3147            }
3148        );
3149        let delivered_size = delivered.len().to_string();
3150        let described = video_mismatch(
3151            &descriptor,
3152            &ciphertext,
3153            &[
3154                ("substituteftype", b"4"),
3155                ("istrans", b"7"),
3156                ("videoformat", b"3"),
3157                ("rawfilesize", delivered_size.as_bytes()),
3158                ("rawfilemd5", delivered_md5.to_uppercase().as_bytes()),
3159            ],
3160        );
3161        assert_eq!(described.substituted_filetype, Some(4));
3162        assert_eq!(
3163            (described.istrans, described.videoformat),
3164            (Some(7), Some(3))
3165        );
3166        let delivered_only = Some(SourceEvidence {
3167            matches_advertised: false,
3168            matches_delivered: true,
3169        });
3170        assert_eq!(described.rawfilesize, delivered_only);
3171        assert_eq!(described.rawfilemd5, delivered_only);
3172        let advertised_size = descriptor.expected_bytes.unwrap().to_string();
3173        let advertised_md5 = descriptor.expected_md5.clone().unwrap();
3174        let source = video_mismatch(
3175            &descriptor,
3176            &ciphertext,
3177            &[
3178                ("rawfilesize", advertised_size.as_bytes()),
3179                ("rawfilemd5", advertised_md5.as_bytes()),
3180            ],
3181        );
3182        let advertised_only = Some(SourceEvidence {
3183            matches_advertised: true,
3184            matches_delivered: false,
3185        });
3186        assert_eq!(source.rawfilesize, advertised_only);
3187        assert_eq!(source.rawfilemd5, advertised_only);
3188        let malformed = video_mismatch(
3189            &descriptor,
3190            &ciphertext,
3191            &[("rawfilesize", b"-1"), ("rawfilemd5", b"not-a-digest")],
3192        );
3193        let neither = Some(SourceEvidence {
3194            matches_advertised: false,
3195            matches_delivered: false,
3196        });
3197        assert_eq!(
3198            (malformed.rawfilesize, malformed.rawfilemd5),
3199            (neither, neither)
3200        );
3201        // A representation that only some chunks label is still refused.
3202        assert!(client()
3203            .download_with_transport(
3204                &descriptor,
3205                |_, _| Ok(()),
3206                |_, fields| {
3207                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
3208                    put(&mut reply, "substituteftype", integer(fields, "seq")?);
3209                    Ok(reply)
3210                },
3211            )
3212            .is_err());
3213    }
3214    #[test]
3215    fn normal_video_digest_mismatch_and_valid_metadata_keep_existing_outcomes() {
3216        let (mut descriptor, raw, ciphertext) = download_fixture(9_390, MediaQuality::Video);
3217        // Optional metadata is diagnostic only; malformed values never reject
3218        // a transfer that matches its advertised tuple.
3219        for (key, value) in [
3220            ("rawfilesize", "-1"),
3221            ("rawfilemd5", "not-a-digest"),
3222            ("istrans", "x"),
3223            ("videoformat", "12345678901234567"),
3224        ] {
3225            let downloaded = client()
3226                .download_with_transport(
3227                    &descriptor,
3228                    |_, _| Ok(()),
3229                    |_, fields| {
3230                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
3231                        reply.insert(key.into(), value.as_bytes().to_vec());
3232                        Ok(reply)
3233                    },
3234                )
3235                .unwrap();
3236            assert_eq!(downloaded.bytes, raw);
3237            assert_eq!(downloaded.advertised_size_matches, Some(true));
3238        }
3239        descriptor.expected_md5 = Some("0".repeat(32));
3240        let same_size = video_mismatch(&descriptor, &ciphertext, &[]);
3241        assert_eq!(same_size.delivered_bytes, 9_390);
3242        assert_eq!(same_size.advertised_digest_matches, Some(false));
3243        // Other kinds keep their plain messages.
3244        let (mut image, _, image_ciphertext) = download_fixture(9_390, MediaQuality::Original);
3245        image.expected_bytes = Some(9_391);
3246        let error = client()
3247            .download_with_transport(
3248                &image,
3249                |_, _| Ok(()),
3250                |_, fields| Ok(download_reply(fields, &image, &image_ciphertext)),
3251            )
3252            .err()
3253            .expect("download must fail");
3254        assert!(error
3255            .downcast_ref::<VideoRepresentationMismatch>()
3256            .is_none());
3257        assert_eq!(error.to_string(), "Advertised media size mismatch");
3258    }
3259    #[test]
3260    fn video_limits_admit_sizes_above_the_former_50_mib_bound() {
3261        assert_eq!(MAX_VIDEO_DOWNLOAD_BYTES, 536_870_912);
3262        assert_eq!(wechat_media::MAX_VIDEO_SOURCE_BYTES, 536_870_912);
3263        assert_eq!(
3264            upload_plaintext_limit(MediaQuality::Video),
3265            wechat_media::MAX_VIDEO_SOURCE_BYTES
3266        );
3267        // A small advertised video keeps its own bound on an unchecked reply.
3268        assert_eq!(MAX_SMALL_VIDEO_REPLY_BYTES, 52_428_800);
3269        let (mut descriptor, _, _) = download_fixture(16, MediaQuality::Video);
3270        for size in [MAX_SMALL_VIDEO_REPLY_BYTES + 1, MAX_VIDEO_DOWNLOAD_BYTES] {
3271            descriptor.expected_bytes = Some(size as u64);
3272            let mut requests = 0;
3273            let error = client()
3274                .download_with_transport(
3275                    &descriptor,
3276                    |_, _| Ok(()),
3277                    |path, fields| {
3278                        requests += 1;
3279                        assert_eq!(path, "/download");
3280                        assert_eq!(integer(fields, "filetype")?, 4);
3281                        assert_eq!(integer(fields, "rangeend")?, 512 * 1024 - 1);
3282                        bail!("synthetic transport stop")
3283                    },
3284                )
3285                .err()
3286                .expect("download must stop");
3287            assert_eq!(error.to_string(), "synthetic transport stop");
3288            assert_eq!(requests, 1);
3289        }
3290    }
3291
3292    #[test]
3293    fn observed_video_controls_do_not_bypass_large_response_validation() {
3294        let (mut descriptor, _, encrypted) =
3295            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3296        descriptor.strict_size = false;
3297        let mut admitted = Vec::new();
3298        for (key, value) in [("unknown", "1"), ("rawfilesize", "1")] {
3299            let result = client().download_with_transport(
3300                &descriptor,
3301                |_, _| Ok(()),
3302                |_, fields| {
3303                    let mut reply = download_reply(fields, &descriptor, &encrypted);
3304                    put(&mut reply, "istrans", 999);
3305                    put(&mut reply, "videoformat", 1);
3306                    put(&mut reply, "substituteftype", 4);
3307                    reply.insert(
3308                        "rawfilemd5".into(),
3309                        descriptor
3310                            .expected_md5
3311                            .as_ref()
3312                            .unwrap()
3313                            .as_bytes()
3314                            .to_vec(),
3315                    );
3316                    reply.insert(key.into(), value.as_bytes().to_vec());
3317                    Ok(reply)
3318                },
3319            );
3320            if result.is_ok() {
3321                admitted.push(key);
3322            }
3323        }
3324        assert!(
3325            admitted.is_empty(),
3326            "Accepted invalid large-video fields: {admitted:?}"
3327        );
3328    }
3329
3330    fn selected_video_reply(
3331        fields: &CdnFields,
3332        descriptor: &MediaDescriptor,
3333        encrypted: &[u8],
3334        raw: &[u8],
3335        control: i64,
3336    ) -> CdnFields {
3337        let mut reply = download_reply(fields, descriptor, encrypted);
3338        put(&mut reply, "istrans", control);
3339        put(&mut reply, "videoformat", 1);
3340        put(
3341            &mut reply,
3342            "backsrcnum",
3343            integer(fields, "seq").unwrap() % 3,
3344        );
3345        put(&mut reply, "substituteftype", 9);
3346        put(&mut reply, "rawfilesize", raw.len() as i64);
3347        reply.insert("rawfilemd5".into(), hex::encode(md5(raw)).into_bytes());
3348        reply
3349    }
3350
3351    #[test]
3352    fn selected_large_normal_video_checks_every_range_and_returned_identity() {
3353        let (mut descriptor, raw, encrypted) =
3354            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3355        descriptor.expected_bytes = Some(raw.len() as u64 + 32);
3356        descriptor.expected_md5 = Some("0".repeat(32));
3357        descriptor.strict_size = false;
3358        for control in [13, 999] {
3359            let mut calls = 0;
3360            let received = client()
3361                .download_with_transport(
3362                    &descriptor,
3363                    |_, _| Ok(()),
3364                    |path, fields| {
3365                        calls += 1;
3366                        assert_eq!(path, "/download");
3367                        assert_eq!(integer(fields, "filetype")?, 4);
3368                        assert_eq!(integer(fields, "seq")?, calls);
3369                        assert_eq!(integer(fields, "rangestart")?, (calls - 1) * 512 * 1024);
3370                        Ok(selected_video_reply(
3371                            fields,
3372                            &descriptor,
3373                            &encrypted,
3374                            &raw,
3375                            control,
3376                        ))
3377                    },
3378                )
3379                .unwrap();
3380            assert_eq!(calls, 51);
3381            assert_eq!(received.bytes, raw);
3382            assert_eq!(received.md5, hex::encode(md5(&raw)));
3383            assert_eq!(received.sha256, hex::encode(sha256(&raw)));
3384            assert_eq!(received.advertised_size_matches, Some(false));
3385            assert_eq!(received.substituted_filetype, Some(9));
3386        }
3387        descriptor.strict_size = true;
3388        for control in [13, 999] {
3389            assert!(
3390                client()
3391                    .download_with_transport(
3392                        &descriptor,
3393                        |_, _| Ok(()),
3394                        |_, fields| Ok(selected_video_reply(
3395                            fields,
3396                            &descriptor,
3397                            &encrypted,
3398                            &raw,
3399                            control
3400                        )),
3401                    )
3402                    .is_err(),
3403                "strict source accepted response-selected bytes"
3404            );
3405        }
3406    }
3407
3408    #[test]
3409    fn selected_video_rejects_mid_transfer_metadata_or_range_changes_without_retry() {
3410        let (mut descriptor, raw, encrypted) =
3411            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3412        descriptor.strict_size = false;
3413        for fault in [
3414            "digest",
3415            "size",
3416            "range",
3417            "control",
3418            "missing_digest",
3419            "selection",
3420            "service",
3421        ] {
3422            let mut calls = 0;
3423            let result = client().download_with_transport(
3424                &descriptor,
3425                |_, _| Ok(()),
3426                |_, fields| {
3427                    calls += 1;
3428                    let mut reply =
3429                        selected_video_reply(fields, &descriptor, &encrypted, &raw, 999);
3430                    if calls == 2 {
3431                        match fault {
3432                            "digest" => {
3433                                reply.insert("rawfilemd5".into(), "0".repeat(32).into_bytes());
3434                            }
3435                            "size" => put(&mut reply, "rawfilesize", raw.len() as i64 + 1),
3436                            "range" => put(&mut reply, "rangestart", 0),
3437                            "control" => put(&mut reply, "backsrcnum", 3),
3438                            "missing_digest" => {
3439                                reply.remove("rawfilemd5");
3440                            }
3441                            "selection" => put(&mut reply, "istrans", 0),
3442                            "service" => {
3443                                reply.clear();
3444                                put(&mut reply, "retcode", -1);
3445                            }
3446                            _ => unreachable!(),
3447                        }
3448                    }
3449                    Ok(reply)
3450                },
3451            );
3452            assert!(result.is_err(), "accepted changed {fault}");
3453            if fault == "service" {
3454                assert!(result
3455                    .err()
3456                    .unwrap()
3457                    .downcast_ref::<ServiceRejected>()
3458                    .is_some());
3459            }
3460            assert_eq!(calls, 2, "retried changed {fault}");
3461        }
3462    }
3463
3464    #[test]
3465    fn server_selected_video_representation_is_accepted_with_validated_response_digest() {
3466        for (istrans, filetype) in [(13, 4), (13, 9), (999, 4), (999, 9)] {
3467            let (mut d, raw, encrypted) = download_fixture(103, MediaQuality::Video);
3468            // The advertised representation differs from the delivered bytes.
3469            d.expected_bytes = Some(90);
3470            d.expected_md5 = Some("0".repeat(32));
3471            d.strict_size = false;
3472            let received = client()
3473                .download_with_transport(
3474                    &d,
3475                    |_, _| Ok(()),
3476                    |_, fields| {
3477                        let mut r = download_reply(fields, &d, &encrypted);
3478                        put(&mut r, "istrans", istrans);
3479                        put(&mut r, "videoformat", 1);
3480                        put(&mut r, "ver", 0);
3481                        put(&mut r, "backsrcnum", 2);
3482                        put(&mut r, "substituteftype", filetype);
3483                        r.insert("rawfilemd5".into(), hex::encode(md5(&raw)).into_bytes());
3484                        Ok(r)
3485                    },
3486                )
3487                .unwrap();
3488            assert_eq!(received.bytes, raw);
3489            assert_eq!(received.md5, hex::encode(md5(&raw)));
3490            assert_eq!(received.advertised_size_matches, Some(false));
3491        }
3492    }
3493
3494    #[test]
3495    fn nonselected_video_without_advertised_digest_still_requires_advertised_size() {
3496        let (mut d, _raw, encrypted) = download_fixture(103, MediaQuality::Video);
3497        d.expected_bytes = Some(90);
3498        d.expected_md5 = None;
3499        d.strict_size = false;
3500        assert!(client()
3501            .download_with_transport(
3502                &d,
3503                |_, _| Ok(()),
3504                |_, fields| Ok(download_reply(fields, &d, &encrypted)),
3505            )
3506            .is_err());
3507    }
3508
3509    #[test]
3510    fn server_selected_video_matching_advertised_size_still_requires_response_digest() {
3511        for response_md5 in [None, Some("not-a-digest".to_string()), Some("0".repeat(32))] {
3512            let (mut d, raw, encrypted) = download_fixture(103, MediaQuality::Video);
3513            d.expected_bytes = Some(raw.len() as u64);
3514            d.expected_md5 = None;
3515            d.strict_size = false;
3516            assert!(client()
3517                .download_with_transport(
3518                    &d,
3519                    |_, _| Ok(()),
3520                    |_, fields| {
3521                        let mut r = download_reply(fields, &d, &encrypted);
3522                        put(&mut r, "istrans", 13);
3523                        put(&mut r, "videoformat", 1);
3524                        put(&mut r, "ver", 0);
3525                        put(&mut r, "substituteftype", 9);
3526                        if let Some(md5) = &response_md5 {
3527                            r.insert("rawfilemd5".into(), md5.as_bytes().to_vec());
3528                        }
3529                        Ok(r)
3530                    },
3531                )
3532                .is_err());
3533        }
3534    }
3535
3536    #[test]
3537    fn server_selected_video_without_matching_response_digest_is_rejected() {
3538        for advertised_md5 in [None, Some("0".repeat(32))] {
3539            for response_md5 in [None, Some("not-a-digest".to_string()), Some("0".repeat(32))] {
3540                let (mut d, _raw, encrypted) = download_fixture(103, MediaQuality::Video);
3541                d.expected_bytes = Some(90);
3542                d.strict_size = false;
3543                d.expected_md5 = advertised_md5.clone();
3544                assert!(client()
3545                    .download_with_transport(
3546                        &d,
3547                        |_, _| Ok(()),
3548                        |_, fields| {
3549                            let mut r = download_reply(fields, &d, &encrypted);
3550                            put(&mut r, "istrans", 13);
3551                            put(&mut r, "videoformat", 1);
3552                            put(&mut r, "ver", 0);
3553                            put(&mut r, "substituteftype", 9);
3554                            if let Some(md5) = &response_md5 {
3555                                r.insert("rawfilemd5".into(), md5.as_bytes().to_vec());
3556                            }
3557                            Ok(r)
3558                        },
3559                    )
3560                    .is_err());
3561            }
3562        }
3563    }
3564}