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::{
4    crypto::{md5, random, sha256},
5    protobuf as p, AccountState,
6};
7use anyhow::{bail, ensure, Result};
8use openssl::{
9    hash::{Hasher, MessageDigest},
10    symm::{Cipher, Crypter, Mode},
11};
12use serde::{Deserialize, Serialize};
13use std::{collections::BTreeMap, fmt, io::Read, net::Ipv4Addr, time::Duration};
14use zeroize::Zeroize;
15pub type CdnFields = BTreeMap<String, Vec<u8>>;
16const MAX_CDN_BODY: usize = 2 * 1024 * 1024;
17const UPLOAD_CHUNK: usize = 65536;
18pub const MAX_CDN_TRANSFER_BYTES: usize = 25 * 1024 * 1024;
19pub const MAX_NATIVE_UPLOAD_BYTES: usize = 25 * 1024 * 1024 - 17;
20pub const MAX_LARGE_FILE_BYTES: usize = wechat_media::MAX_FILE_BYTES;
21/// Incoming video plaintext limit, matching the ordinary-file download and
22/// automatic-download ceiling. Every such transfer stays bound to the
23/// advertised length and MD5. Outgoing preparation keeps the separate
24/// `wechat_media::MAX_VIDEO_SOURCE_BYTES` source limit.
25pub const MAX_VIDEO_DOWNLOAD_BYTES: usize = wechat_media::MAX_FILE_BYTES;
26/// A video advertised at or below the ordinary transfer threshold is fetched
27/// before its length is compared, so its reply keeps the earlier 50 MiB bound.
28const MAX_SMALL_VIDEO_REPLY_BYTES: usize = 50 * 1024 * 1024;
29#[path = "large_cdn_config.rs"]
30mod large_cdn_config;
31#[path = "large_file.rs"]
32mod large_file;
33pub use large_cdn_config::{decode_large_cdn_config, LargeCdnRoute};
34#[path = "avatar.rs"]
35mod avatar;
36pub use avatar::*;
37#[path = "link_thumbnail.rs"]
38mod link_thumbnail;
39pub use link_thumbnail::*;
40#[path = "sticker.rs"]
41mod sticker;
42pub use sticker::*;
43
44#[derive(Clone, Copy, Serialize, Deserialize)]
45pub struct CdnLocation {
46    pub province: u32,
47    pub city: u32,
48    pub isp: u32,
49}
50#[derive(Clone, Serialize, Deserialize)]
51pub struct CdnConfig {
52    pub version: u32,
53    pub uin: u64,
54    pub client_version: u32,
55    pub client_os_type: String,
56    pub auth_key: Vec<u8>,
57    pub server_ips: Vec<Ipv4Addr>,
58    #[serde(default)]
59    pub front_id: u32,
60    #[serde(default)]
61    pub location: Option<CdnLocation>,
62}
63impl Drop for CdnConfig {
64    fn drop(&mut self) {
65        self.auth_key.zeroize();
66    }
67}
68impl fmt::Debug for CdnConfig {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_struct("CdnConfig")
71            .field("credentials", &"[redacted]")
72            .finish()
73    }
74}
75impl CdnConfig {
76    pub fn validate(&self) -> Result<()> {
77        ensure!(
78            self.uin != 0
79                && !self.auth_key.is_empty()
80                && self.auth_key.len() <= 65536
81                && !self.client_os_type.is_empty()
82                && self.client_os_type.len() <= 1024
83                && !self.server_ips.is_empty()
84                && self.server_ips.len() <= 32,
85            "Invalid CDN config"
86        );
87        for ip in &self.server_ips {
88            ensure!(public_ipv4(*ip), "CDN endpoint is not public IPv4");
89        }
90        Ok(())
91    }
92}
93pub fn decode_cdn_config(input: &[u8], account: &AccountState) -> Result<CdnConfig> {
94    let f = p::decode(input)?;
95    let code = p::base_code(&f)?;
96    ensure!(code == 0, "CDN bootstrap returned {code}");
97    let group = p::decode(p::data(&f, 2)?)?;
98    let version = u32::try_from(p::scalar(&group, 1)?)?;
99    let uin = p::scalar(&group, 2)?;
100    ensure!(uin == account.uin as u64, "CDN account identity mismatch");
101    let auth_key = p::unbuffer(p::data(&group, 8)?)?;
102    let mut server_ips = vec![];
103    for field in group.iter().filter(|f| f.number == 6) {
104        let p::Value::Bytes(raw) = &field.value else {
105            bail!("Invalid CDN endpoint record");
106        };
107        let ip = p::decode(raw)?;
108        server_ips.push(std::str::from_utf8(p::data(&ip, 1)?)?.parse()?);
109    }
110    let conf = CdnConfig {
111        version,
112        uin,
113        client_version: account.device.client_version as u32,
114        client_os_type: account.device.device_type.clone(),
115        auth_key,
116        server_ips,
117        front_id: 0,
118        location: None,
119    };
120    conf.validate()?;
121    Ok(conf)
122}
123fn public_ipv4(ip: Ipv4Addr) -> bool {
124    let [a, b, c, _] = ip.octets();
125    !(a == 0
126        || a == 10
127        || a == 127
128        || a >= 224
129        || (a == 100 && (64..=127).contains(&b))
130        || (a == 169 && b == 254)
131        || (a == 172 && (16..=31).contains(&b))
132        || (a == 192 && (b == 0 || b == 168 || (b == 2 && c == 0)))
133        || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100)))
134        || (a == 203 && b == 0 && c == 113))
135}
136pub fn pack_cdn(fields: &CdnFields) -> Result<Vec<u8>> {
137    // A 512 MiB completion can contain 1025 ordered block etags. The byte
138    // envelope remains independently bounded, including individual values.
139    ensure!(fields.len() <= 2048, "Too many CDN fields");
140    let mut out = vec![];
141    for (k, v) in fields {
142        ensure!(
143            !k.is_empty()
144                && k.len() <= 128
145                && k.bytes()
146                    .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'),
147            "Invalid CDN key"
148        );
149        for b in [k.as_bytes(), v.as_slice()] {
150            ensure!(b.len() <= MAX_CDN_BODY, "CDN field too large");
151            out.extend((b.len() as u32).to_be_bytes());
152            out.extend(b);
153            ensure!(out.len() <= MAX_CDN_BODY, "CDN request exceeds limit");
154        }
155    }
156    Ok(out)
157}
158pub fn unpack_cdn(input: &[u8]) -> Result<CdnFields> {
159    ensure!(input.len() <= MAX_CDN_BODY, "CDN reply exceeds limit");
160    let mut c = crate::mmtls::Cursor::new(input);
161    let mut out = CdnFields::new();
162    let mut consumed = 0;
163    while consumed < input.len() {
164        ensure!(out.len() < 256, "Too many CDN reply fields");
165        let key = c.sized(4)?;
166        let value = c.sized(4)?;
167        consumed += 8 + key.len() + value.len();
168        ensure!(
169            !key.is_empty()
170                && key.len() <= 128
171                && key
172                    .iter()
173                    .all(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'-'),
174            "Invalid CDN reply key"
175        );
176        let key = std::str::from_utf8(key)?.to_string();
177        ensure!(
178            out.insert(key, value.to_vec()).is_none(),
179            "Duplicate CDN reply key"
180        );
181    }
182    c.end()?;
183    Ok(out)
184}
185fn integer(fields: &CdnFields, key: &str) -> Result<i64> {
186    let raw = fields
187        .get(key)
188        .ok_or_else(|| anyhow::anyhow!("Missing CDN integer {key}"))?;
189    let text = std::str::from_utf8(raw)?;
190    ensure!(
191        !text.is_empty()
192            && text.len() <= 16
193            && text
194                .bytes()
195                .enumerate()
196                .all(|(i, b)| b.is_ascii_digit() || (i == 0 && b == b'-')),
197        "Invalid CDN integer {key}"
198    );
199    Ok(text.parse()?)
200}
201fn put(fields: &mut CdnFields, key: &str, value: impl ToString) {
202    fields.insert(key.to_string(), value.to_string().into_bytes());
203}
204fn shared(conf: &CdnConfig, sequence: u32, filetype: u32) -> CdnFields {
205    let mut f = CdnFields::new();
206    for (k, v) in [
207        ("ver", conf.version as u64),
208        ("weixinnum", conf.uin),
209        ("seq", sequence as u64),
210        ("clientversion", conf.client_version as u64),
211        ("nettype", 1),
212        ("acceptdupack", 1),
213        ("safeproto", 1),
214        ("filetype", filetype as u64),
215        ("wxchattype", 0),
216        ("lastretcode", 0),
217        ("ipseq", 0),
218        ("largesvideo", 0),
219        ("sourceflag", 0),
220    ] {
221        put(&mut f, k, v);
222    }
223    f.insert(
224        "clientostype".into(),
225        conf.client_os_type.as_bytes().to_vec(),
226    );
227    f.insert("authkey".into(), conf.auth_key.clone());
228    f
229}
230pub fn encrypt_recipient(destination: &str) -> Result<String> {
231    ensure!(
232        !destination.is_empty()
233            && destination.len() <= 512
234            && !destination
235                .chars()
236                .any(|c| c.is_control() || c.is_whitespace()),
237        "Invalid CDN recipient"
238    );
239    let key = b"wxusrname2016cdn";
240    let encrypted = openssl::symm::encrypt(
241        Cipher::aes_128_cbc(),
242        key,
243        Some(key),
244        destination.as_bytes(),
245    )?;
246    Ok(format!("@cdn2_{}", hex::encode(encrypted)))
247}
248fn ecb_encrypt(raw: &[u8], key: &[u8]) -> Result<Vec<u8>> {
249    ensure!(key.len() == 16, "Invalid CDN key");
250    Ok(openssl::symm::encrypt(
251        Cipher::aes_128_ecb(),
252        key,
253        None,
254        raw,
255    )?)
256}
257pub fn decrypt_cdn(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> {
258    ensure!(
259        key.len() == 16
260            && !encrypted.is_empty()
261            && encrypted.len().is_multiple_of(16)
262            && encrypted.len() <= MAX_LARGE_FILE_BYTES + 16,
263        "Invalid CDN ciphertext"
264    );
265    Ok(openssl::symm::decrypt(
266        Cipher::aes_128_ecb(),
267        key,
268        None,
269        encrypted,
270    )?)
271}
272
273#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
274#[serde(rename_all = "snake_case")]
275pub enum MediaQuality {
276    Original,
277    Standard,
278    Thumbnail,
279    Video,
280    File,
281    LargeFile,
282}
283impl MediaQuality {
284    pub fn filetype(self) -> u32 {
285        match self {
286            Self::Original => 1,
287            Self::Standard => 2,
288            Self::Thumbnail => 3,
289            Self::Video => 4,
290            Self::File => 5,
291            Self::LargeFile => 7,
292        }
293    }
294}
295fn upload_plaintext_limit(kind: MediaQuality) -> usize {
296    match kind {
297        MediaQuality::File => MAX_CDN_TRANSFER_BYTES,
298        MediaQuality::LargeFile => MAX_LARGE_FILE_BYTES,
299        MediaQuality::Video => wechat_media::MAX_VIDEO_SOURCE_BYTES,
300        _ => MAX_NATIVE_UPLOAD_BYTES,
301    }
302}
303#[derive(Clone, Serialize, Deserialize)]
304pub struct MediaDescriptor {
305    pub file_id: String,
306    pub aes_key: Vec<u8>,
307    pub quality: MediaQuality,
308    pub expected_bytes: Option<u64>,
309    pub expected_md5: Option<String>,
310    pub strict_size: bool,
311    pub expires_at: Option<u64>,
312}
313impl Drop for MediaDescriptor {
314    fn drop(&mut self) {
315        self.aes_key.zeroize();
316        self.file_id.zeroize();
317    }
318}
319impl fmt::Debug for MediaDescriptor {
320    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321        f.debug_struct("MediaDescriptor")
322            .field("quality", &self.quality)
323            .field("metadata", &"[redacted]")
324            .finish()
325    }
326}
327pub struct DownloadedMedia {
328    pub bytes: Vec<u8>,
329    pub requested_quality: MediaQuality,
330    pub substituted_filetype: Option<u32>,
331    pub picture_format: Option<u32>,
332    pub md5: String,
333    pub sha256: String,
334    pub advertised_size_matches: Option<bool>,
335}
336#[derive(Clone, Serialize, Deserialize)]
337pub struct PreparedUpload {
338    pub kind: MediaQuality,
339    pub aes_key: Vec<u8>,
340    pub file_key: String,
341    pub encrypted: Vec<u8>,
342    pub plaintext_bytes: u32,
343    pub md5: String,
344    pub sha256: String,
345    pub file_name: String,
346    pub destination: String,
347}
348impl Drop for PreparedUpload {
349    fn drop(&mut self) {
350        self.aes_key.zeroize();
351        self.file_key.zeroize();
352        self.encrypted.zeroize();
353    }
354}
355impl fmt::Debug for PreparedUpload {
356    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
357        f.debug_struct("PreparedUpload")
358            .field("kind", &self.kind)
359            .field("plaintext_bytes", &self.plaintext_bytes)
360            .field("material", &"[redacted]")
361            .finish()
362    }
363}
364impl PreparedUpload {
365    /// Generate independent upload material. Persist either this result or the
366    /// source bytes and its key/file-key before the first upload request.
367    pub fn prepare(
368        raw: &[u8],
369        kind: MediaQuality,
370        file_name: &str,
371        destination: &str,
372    ) -> Result<Self> {
373        Self::with_material(
374            raw,
375            kind,
376            file_name,
377            destination,
378            &random::<16>()?,
379            &hex::encode(random::<16>()?),
380        )
381    }
382    /// Rebuild exactly the same encrypted upload from durable owned source bytes.
383    pub fn with_material(
384        raw: &[u8],
385        kind: MediaQuality,
386        file_name: &str,
387        destination: &str,
388        key: &[u8],
389        file_key: &str,
390    ) -> Result<Self> {
391        ensure!(
392            !raw.is_empty()
393                && raw.len() <= upload_plaintext_limit(kind)
394                && [
395                    MediaQuality::File,
396                    MediaQuality::LargeFile,
397                    MediaQuality::Original,
398                    MediaQuality::Video
399                ]
400                .contains(&kind),
401            "Unsupported upload profile"
402        );
403        ensure!(
404            !file_name.is_empty()
405                && file_name.len() <= 255
406                && file_name != "."
407                && file_name != ".."
408                && !file_name.contains(['/', '\\'])
409                && !file_name.chars().any(|c| c.is_control()),
410            "Invalid upload filename"
411        );
412        ensure!(
413            key.len() == 16
414                && file_key.len() == 32
415                && file_key.bytes().all(|b| b.is_ascii_hexdigit()),
416            "Invalid durable upload material"
417        );
418        encrypt_recipient(destination)?;
419        Ok(Self {
420            kind,
421            aes_key: key.to_vec(),
422            file_key: file_key.to_string(),
423            encrypted: ecb_encrypt(raw, key)?,
424            plaintext_bytes: raw.len() as u32,
425            md5: hex::encode(md5(raw)),
426            sha256: hex::encode(sha256(raw)),
427            file_name: file_name.into(),
428            destination: destination.into(),
429        })
430    }
431    fn validate(&self) -> Result<()> {
432        ensure!(
433            self.aes_key.len() == 16
434                && self.file_key.len() == 32
435                && self.file_key.bytes().all(|b| b.is_ascii_hexdigit())
436                && self.plaintext_bytes > 0
437                && self.plaintext_bytes as usize <= upload_plaintext_limit(self.kind)
438                && self.encrypted.len() == ((self.plaintext_bytes as usize / 16) + 1) * 16,
439            "Invalid prepared upload"
440        );
441        // Validate persisted ciphertext without allocating a second whole file.
442        let mut decryptor =
443            Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, &self.aes_key, None)?;
444        let mut buffer = zeroize::Zeroizing::new(vec![0; 128 * 1024 + 16]);
445        let mut md5 = Hasher::new(MessageDigest::md5())?;
446        let mut sha256 = Hasher::new(MessageDigest::sha256())?;
447        let mut plaintext_bytes = 0usize;
448        for chunk in self.encrypted.chunks(128 * 1024) {
449            let written = decryptor.update(chunk, &mut buffer)?;
450            md5.update(&buffer[..written])?;
451            sha256.update(&buffer[..written])?;
452            plaintext_bytes += written;
453        }
454        let written = decryptor.finalize(&mut buffer)?;
455        md5.update(&buffer[..written])?;
456        sha256.update(&buffer[..written])?;
457        plaintext_bytes += written;
458        ensure!(
459            plaintext_bytes == self.plaintext_bytes as usize
460                && hex::encode(md5.finish()?) == self.md5
461                && hex::encode(sha256.finish()?) == self.sha256,
462            "Prepared upload digest mismatch"
463        );
464        Ok(())
465    }
466}
467#[derive(Clone, Serialize, Deserialize)]
468pub struct UploadedMedia {
469    pub descriptor: MediaDescriptor,
470    pub plaintext_bytes: u32,
471    pub md5: String,
472    pub sha256: String,
473    pub file_name: String,
474    pub width: Option<u32>,
475    pub height: Option<u32>,
476}
477pub struct CdnClient {
478    config: CdnConfig,
479    timeout: Duration,
480}
481fn large_file_path(path: &str, uin: u64, sequence: u32) -> bool {
482    let parts: Vec<_> = path.split('/').collect();
483    parts.len() == 6
484        && parts[0].is_empty()
485        && parts[1] == "cdn"
486        && parts[2] == uin.to_string()
487        && sequence > 0
488        && parts[3] == sequence.to_string()
489        && matches!(parts[4], "4" | "5" | "7")
490        && matches!(parts[5], "apply" | "uploadblock" | "fin" | "complete")
491}
492impl CdnClient {
493    pub fn new(config: CdnConfig, timeout: Duration) -> Result<Self> {
494        config.validate()?;
495        ensure!(
496            !timeout.is_zero() && timeout <= Duration::from_secs(120),
497            "Invalid CDN timeout"
498        );
499        Ok(Self { config, timeout })
500    }
501    fn post(&self, path: &str, fields: &CdnFields) -> Result<CdnFields> {
502        let sequence = u32::try_from(integer(fields, "seq")?)?;
503        ensure!(sequence != 0, "Invalid CDN sequence");
504        ensure!(
505            ["/download", "/downloadbig", "/uploadv3"].contains(&path)
506                || large_file_path(path, self.config.uin, sequence),
507            "Invalid CDN path"
508        );
509        let body = pack_cdn(fields)?;
510        let client = reqwest::blocking::Client::builder()
511            .timeout(self.timeout)
512            .redirect(reqwest::redirect::Policy::none())
513            .no_proxy()
514            .build()?;
515        let mut response = client
516            .post(format!("http://{}:443{path}", self.config.server_ips[0]))
517            .header("Accept", "*/*")
518            .header("Content-Type", "application/octet-stream")
519            .header("User-Agent", "MicroMessenger Client")
520            .header("Connection", "close")
521            .header("C-Seq", sequence)
522            .body(body)
523            .send()?;
524        ensure!(
525            response.status() == 200,
526            "CDN HTTP status {}",
527            response.status().as_u16()
528        );
529        let mut raw = vec![];
530        response
531            .by_ref()
532            .take((MAX_CDN_BODY + 1) as u64)
533            .read_to_end(&mut raw)?;
534        unpack_cdn(&raw)
535    }
536    pub fn download(&self, descriptor: &MediaDescriptor) -> Result<DownloadedMedia> {
537        self.download_with_progress(descriptor, |_, _| Ok(()))
538    }
539    pub fn download_with_progress(
540        &self,
541        descriptor: &MediaDescriptor,
542        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
543    ) -> Result<DownloadedMedia> {
544        self.download_with_transport(descriptor, progress, |path, fields| self.post(path, fields))
545    }
546    fn download_with_transport(
547        &self,
548        descriptor: &MediaDescriptor,
549        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
550        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
551    ) -> Result<DownloadedMedia> {
552        ensure!(
553            !descriptor.file_id.is_empty()
554                && descriptor.file_id.len() <= 8192
555                && !descriptor.file_id.chars().any(|c| c.is_control())
556                && descriptor.aes_key.len() == 16,
557            "Invalid download descriptor"
558        );
559        if let Some(expiry) = descriptor.expires_at {
560            ensure!(expiry > crate::wire::unix_time()? as u64, "media_expired");
561        }
562        let large = descriptor.quality == MediaQuality::LargeFile;
563        let large_video = descriptor.quality == MediaQuality::Video
564            && descriptor.expected_bytes.unwrap_or(0) > MAX_CDN_TRANSFER_BYTES as u64;
565        let strict_transfer = large || large_video;
566        let plaintext_limit = if large {
567            MAX_LARGE_FILE_BYTES
568        } else if large_video {
569            MAX_VIDEO_DOWNLOAD_BYTES
570        } else if descriptor.quality == MediaQuality::Video && descriptor.expected_bytes.is_some() {
571            MAX_SMALL_VIDEO_REPLY_BYTES
572        } else {
573            MAX_CDN_TRANSFER_BYTES
574        };
575        let chunk_bytes = if strict_transfer { 512 * 1024 } else { 65536 };
576        let ciphertext_limit = (plaintext_limit / 16 + 1) * 16;
577        let expected_ciphertext = if strict_transfer {
578            let expected = usize::try_from(
579                descriptor
580                    .expected_bytes
581                    .ok_or_else(|| anyhow::anyhow!("Large file size is required"))?,
582            )?;
583            ensure!(
584                expected > 0 && expected <= plaintext_limit,
585                "Invalid large file size"
586            );
587            Some((expected / 16 + 1) * 16)
588        } else {
589            ensure!(
590                descriptor
591                    .expected_bytes
592                    .is_none_or(|size| size <= plaintext_limit as u64),
593                "Advertised media size outside limit"
594            );
595            None
596        };
597        if large_video {
598            ensure!(
599                descriptor.expected_md5.as_ref().is_some_and(
600                    |md5| md5.len() == 32 && md5.bytes().all(|b| b.is_ascii_hexdigit())
601                ),
602                "Large video source checksum is required"
603            );
604        }
605        let mut position = 0usize;
606        let mut total = 0usize;
607        let mut encrypted = vec![];
608        let mut substituted_filetype = None;
609        let mut picture_format = None;
610        let mut server_md5 = None;
611        let observe_video = descriptor.quality == MediaQuality::Video && !strict_transfer;
612        let mut video_reply = VideoReplyMetadata::default();
613        for sequence in 1..=u32::try_from(ciphertext_limit.div_ceil(chunk_bytes))? {
614            progress(
615                position as u64,
616                if total == 0 { None } else { Some(total as u64) },
617            )?;
618            let mut fields = shared(&self.config, sequence, descriptor.quality.filetype());
619            let requested_end = if total == 0 {
620                chunk_bytes - 1
621            } else {
622                (total - 1).min(position + chunk_bytes - 1)
623            };
624            for (k, v) in [
625                ("wxautostart", 0),
626                ("downpicformat", 1),
627                ("rangestart", position as u64),
628                ("rangeend", requested_end as u64),
629            ] {
630                put(&mut fields, k, v);
631            }
632            fields.insert("wxmsgflag".into(), vec![]);
633            fields.insert("fileid".into(), descriptor.file_id.as_bytes().to_vec());
634            let reply = post(if large { "/downloadbig" } else { "/download" }, &fields)?;
635            let code = integer(&reply, "retcode")?;
636            ensure!(code == 0, "CDN download returned {code}");
637            if strict_transfer || reply.contains_key("seq") {
638                ensure!(
639                    integer(&reply, "seq")? == i64::from(sequence),
640                    "CDN download reply sequence mismatch"
641                );
642            }
643            if large {
644                validate_large_download_reply(&reply, descriptor, &mut server_md5)?;
645            } else if large_video {
646                validate_large_video_download_reply(&reply, descriptor, &mut server_md5)?;
647            }
648            let start = usize::try_from(integer(&reply, "rangestart")?)?;
649            let end = usize::try_from(integer(&reply, "rangeend")?)?;
650            let size = usize::try_from(integer(&reply, "totalsize")?)?;
651            let data = reply
652                .get("filedata")
653                .ok_or_else(|| anyhow::anyhow!("Missing CDN payload"))?;
654            ensure!(
655                start == position
656                    && !data.is_empty()
657                    && size > 0
658                    && size <= ciphertext_limit
659                    && expected_ciphertext.is_none_or(|expected| expected == size)
660                    && size >= position
661                    && data.len() <= size - position
662                    && end <= requested_end
663                    && (!strict_transfer || data.len() == (size - position).min(chunk_bytes))
664                    && (total == 0 || total == size)
665                    && end == start + data.len() - 1,
666                "Inconsistent CDN range response"
667            );
668            total = size;
669            if encrypted.is_empty() {
670                encrypted.try_reserve_exact(total)?;
671            }
672            position += data.len();
673            encrypted.extend(data);
674            if !large {
675                for (key, target) in [
676                    ("substituteftype", &mut substituted_filetype),
677                    ("rsppicformat", &mut picture_format),
678                ] {
679                    if reply.contains_key(key) {
680                        let value = u32::try_from(integer(&reply, key)?)?;
681                        ensure!(
682                            target.is_none_or(|previous| previous == value),
683                            "CDN representation changed during transfer"
684                        );
685                        *target = Some(value);
686                    }
687                }
688            }
689            if observe_video {
690                video_reply.observe(&reply);
691            }
692            progress(position as u64, Some(total as u64))?;
693            if position == total {
694                break;
695            }
696        }
697        ensure!(
698            total > 0 && position == total,
699            "CDN transfer did not finish within bound"
700        );
701        let bytes = decrypt_cdn(&encrypted, &descriptor.aes_key)?;
702        ensure!(bytes.len() <= plaintext_limit, "Decoded CDN body too large");
703        let size_matches = descriptor.expected_bytes.map(|n| n == bytes.len() as u64);
704        let size_rejected =
705            (strict_transfer || descriptor.strict_size) && size_matches == Some(false);
706        let digest = hex::encode(md5(&bytes));
707        let digest_matches = descriptor.expected_md5.as_ref().map(|expected| {
708            expected.len() == 32
709                && expected.bytes().all(|b| b.is_ascii_hexdigit())
710                && digest.eq_ignore_ascii_case(expected)
711        });
712        if observe_video && (size_rejected || digest_matches == Some(false)) {
713            // Reject exactly as before, but keep comparison facts so a different
714            // server-selected representation can be told apart from corruption.
715            return Err(video_reply
716                .mismatch(
717                    descriptor,
718                    bytes.len(),
719                    &digest,
720                    digest_matches,
721                    substituted_filetype,
722                )
723                .into());
724        }
725        ensure!(!size_rejected, "Advertised media size mismatch");
726        ensure!(
727            digest_matches != Some(false),
728            "Advertised media checksum mismatch"
729        );
730        if let Some(expected) = server_md5 {
731            ensure!(
732                digest.eq_ignore_ascii_case(&expected),
733                "CDN source checksum mismatch"
734            );
735        }
736        let sha = hex::encode(sha256(&bytes));
737        Ok(DownloadedMedia {
738            bytes,
739            requested_quality: descriptor.quality,
740            substituted_filetype,
741            picture_format,
742            md5: digest,
743            sha256: sha,
744            advertised_size_matches: size_matches,
745        })
746    }
747    pub fn upload(&self, prepared: &PreparedUpload) -> Result<UploadedMedia> {
748        self.upload_with_progress(prepared, |_, _| Ok(()))
749    }
750    pub fn upload_with_progress(
751        &self,
752        prepared: &PreparedUpload,
753        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
754    ) -> Result<UploadedMedia> {
755        self.upload_with_transport(prepared, progress, |path, fields| self.post(path, fields))
756    }
757    fn upload_with_transport(
758        &self,
759        prepared: &PreparedUpload,
760        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
761        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
762    ) -> Result<UploadedMedia> {
763        prepared.validate()?;
764        let path = legacy_upload_path(
765            prepared.kind,
766            prepared.plaintext_bytes as usize,
767            prepared.encrypted.len(),
768        )?;
769        let mut file_id = None;
770        for (sequence, chunk) in prepared.encrypted.chunks(UPLOAD_CHUNK).enumerate() {
771            let start = sequence * UPLOAD_CHUNK;
772            progress(start as u64, Some(prepared.encrypted.len() as u64))?;
773            let end = start + chunk.len() - 1;
774            let mut fields =
775                upload_fields(&self.config, prepared, sequence as u32 + 1, start, end)?;
776            fields.insert("filedata".into(), chunk.to_vec());
777            let reply = post(path, &fields)?;
778            validate_upload_reply(&reply, sequence as u32 + 1, start, end, &mut file_id)?;
779            // The final correlated acknowledgement must reach the caller so it
780            // can save completion before applying cancellation/logout fences.
781            if end + 1 < prepared.encrypted.len() {
782                progress((end + 1) as u64, Some(prepared.encrypted.len() as u64))?;
783            }
784        }
785        let file_id =
786            file_id.ok_or_else(|| anyhow::anyhow!("CDN upload returned no file identifier"))?;
787        Ok(UploadedMedia {
788            descriptor: MediaDescriptor {
789                file_id,
790                aes_key: prepared.aes_key.clone(),
791                quality: prepared.kind,
792                expected_bytes: Some(prepared.plaintext_bytes as u64),
793                expected_md5: Some(prepared.md5.clone()),
794                strict_size: true,
795                expires_at: None,
796            },
797            plaintext_bytes: prepared.plaintext_bytes,
798            md5: prepared.md5.clone(),
799            sha256: prepared.sha256.clone(),
800            file_name: prepared.file_name.clone(),
801            width: None,
802            height: None,
803        })
804    }
805}
806/// Only these fixed, nonsecret numeric controls can enter a stored diagnostic.
807#[derive(Clone, Copy, Debug, PartialEq, Eq)]
808pub enum LargeFileDownloadControl {
809    Version,
810    VideoFormat,
811}
812impl LargeFileDownloadControl {
813    pub const fn field_name(self) -> &'static str {
814        match self {
815            Self::Version => "ver",
816            Self::VideoFormat => "videoformat",
817        }
818    }
819}
820/// Strict rejection details. Unknown keys and response bytes are never retained.
821#[derive(Clone, Copy, Debug, PartialEq, Eq)]
822pub enum LargeFileDownloadError {
823    UnknownField,
824    FixedControlMismatch {
825        control: LargeFileDownloadControl,
826        actual: i64,
827    },
828}
829impl fmt::Display for LargeFileDownloadError {
830    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
831        f.write_str(match self {
832            Self::UnknownField => "Unsupported large-file download response field",
833            Self::FixedControlMismatch { .. } => "Unsupported large-file download control",
834        })
835    }
836}
837impl std::error::Error for LargeFileDownloadError {}
838
839/// How an optional reply value relates to the selected message tuple and to
840/// the bytes actually delivered. Values are compared, never retained.
841#[derive(Clone, Copy, Debug, PartialEq, Eq)]
842pub struct SourceEvidence {
843    pub matches_advertised: bool,
844    pub matches_delivered: bool,
845}
846
847/// A complete video transfer at or below the ordinary transfer threshold
848/// decrypted successfully but did not match the selected message tuple. The
849/// download is still rejected. Only fixed numeric controls and comparison
850/// results are kept, so a caller can store a diagnostic without reply bytes or
851/// digests. Fields use wire key names; their meanings are not established.
852#[derive(Clone, Copy, Debug, PartialEq, Eq)]
853pub struct VideoRepresentationMismatch {
854    pub advertised_bytes: Option<u64>,
855    pub delivered_bytes: u64,
856    pub advertised_digest_matches: Option<bool>,
857    pub substituted_filetype: Option<u32>,
858    pub istrans: Option<i64>,
859    pub videoformat: Option<i64>,
860    pub rawfilesize: Option<SourceEvidence>,
861    pub rawfilemd5: Option<SourceEvidence>,
862}
863impl fmt::Display for VideoRepresentationMismatch {
864    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
865        f.write_str("Advertised video representation mismatch")
866    }
867}
868impl std::error::Error for VideoRepresentationMismatch {}
869
870/// First value of each optional video reply field on the ordinary path. This is
871/// diagnostic only: an absent or malformed value never rejects a reply.
872#[derive(Default)]
873struct VideoReplyMetadata {
874    istrans: Option<i64>,
875    videoformat: Option<i64>,
876    rawfilesize: Option<Option<u64>>,
877    rawfilemd5: Option<Option<String>>,
878}
879impl VideoReplyMetadata {
880    fn observe(&mut self, reply: &CdnFields) {
881        for (key, target) in [
882            ("istrans", &mut self.istrans),
883            ("videoformat", &mut self.videoformat),
884        ] {
885            if target.is_none() {
886                *target = integer(reply, key).ok();
887            }
888        }
889        if self.rawfilesize.is_none() && reply.contains_key("rawfilesize") {
890            self.rawfilesize = Some(
891                integer(reply, "rawfilesize")
892                    .ok()
893                    .and_then(|value| u64::try_from(value).ok()),
894            );
895        }
896        if self.rawfilemd5.is_none() {
897            if let Some(raw) = reply.get("rawfilemd5") {
898                self.rawfilemd5 = Some(
899                    (raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit))
900                        .then(|| String::from_utf8_lossy(raw).to_ascii_lowercase()),
901                );
902            }
903        }
904    }
905    fn mismatch(
906        &self,
907        descriptor: &MediaDescriptor,
908        delivered_bytes: usize,
909        delivered_md5: &str,
910        advertised_digest_matches: Option<bool>,
911        substituted_filetype: Option<u32>,
912    ) -> VideoRepresentationMismatch {
913        let delivered_bytes = delivered_bytes as u64;
914        VideoRepresentationMismatch {
915            advertised_bytes: descriptor.expected_bytes,
916            delivered_bytes,
917            advertised_digest_matches,
918            substituted_filetype,
919            istrans: self.istrans,
920            videoformat: self.videoformat,
921            rawfilesize: self.rawfilesize.map(|value| SourceEvidence {
922                matches_advertised: value.is_some() && value == descriptor.expected_bytes,
923                matches_delivered: value == Some(delivered_bytes),
924            }),
925            rawfilemd5: self.rawfilemd5.as_ref().map(|value| SourceEvidence {
926                matches_advertised: value
927                    .as_ref()
928                    .zip(descriptor.expected_md5.as_ref())
929                    .is_some_and(|(value, expected)| value.eq_ignore_ascii_case(expected)),
930                matches_delivered: value.as_deref() == Some(delivered_md5),
931            }),
932        }
933    }
934}
935
936// Type 4 retains the normal download endpoint. Unlike file7, representation
937// and routing metadata cannot be ignored for a byte-exact original video.
938fn validate_large_video_download_reply(
939    reply: &CdnFields,
940    descriptor: &MediaDescriptor,
941    server_md5: &mut Option<String>,
942) -> Result<()> {
943    const FIELDS: &[&str] = &[
944        "ver",
945        "seq",
946        "retcode",
947        "filedata",
948        "rangestart",
949        "rangeend",
950        "totalsize",
951        "retrysec",
952        "isretry",
953        "isoverload",
954        "isgetcdn",
955        "istrans",
956        "backsrcnum",
957        "rawfilesize",
958        "rawfilemd5",
959        "substituteftype",
960        "rsppicformat",
961        "videoformat",
962        "x-ClientIp",
963    ];
964    ensure!(
965        reply.keys().all(|key| FIELDS.contains(&key.as_str())),
966        "Unsupported large-video download response field"
967    );
968    ensure!(
969        integer(reply, "ver")? == 0,
970        "Unsupported large-video download version"
971    );
972    for key in [
973        "retrysec",
974        "isretry",
975        "isoverload",
976        "isgetcdn",
977        "backsrcnum",
978        "rsppicformat",
979    ] {
980        if reply.contains_key(key) {
981            ensure!(
982                integer(reply, key)? == 0,
983                "Unsupported large-video download control"
984            );
985        }
986    }
987    // Linux 4.1.13.26's normal pipeline response map discards `istrans`.
988    // Its success handler's format lookup is literal `KEY_VIDEOFORMAT`
989    // (CRC fb520bf9), not the retained wire key `videoformat` (dbe097e1).
990    // Neither affects the selected ECB/range writer. Admit only the values
991    // observed with byte-identical original-video ciphertext; retain the
992    // independent representation, source size and digest checks below.
993    for (key, observed) in [("istrans", 999), ("videoformat", 1)] {
994        if reply.contains_key(key) {
995            let value = integer(reply, key)?;
996            ensure!(
997                value == 0 || value == observed,
998                "Unsupported large-video download control"
999            );
1000        }
1001    }
1002    if reply.contains_key("substituteftype") {
1003        ensure!(
1004            matches!(integer(reply, "substituteftype")?, 0 | 4),
1005            "Unsupported large-video download representation"
1006        );
1007    }
1008    if reply.contains_key("rawfilesize") {
1009        ensure!(
1010            u64::try_from(integer(reply, "rawfilesize")?)?
1011                == descriptor.expected_bytes.unwrap_or(0),
1012            "Large-video download source size mismatch"
1013        );
1014    }
1015    if let Some(raw) = reply.get("rawfilemd5") {
1016        ensure!(
1017            raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1018            "Invalid CDN source checksum"
1019        );
1020        let value = std::str::from_utf8(raw)?;
1021        ensure!(
1022            server_md5
1023                .as_ref()
1024                .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1025                && descriptor
1026                    .expected_md5
1027                    .as_ref()
1028                    .is_some_and(|expected| expected.eq_ignore_ascii_case(value)),
1029            "CDN source checksum changed"
1030        );
1031        *server_md5 = Some(value.to_string());
1032    }
1033    Ok(())
1034}
1035
1036fn validate_large_download_reply(
1037    reply: &CdnFields,
1038    descriptor: &MediaDescriptor,
1039    server_md5: &mut Option<String>,
1040) -> Result<()> {
1041    const FIELDS: &[&str] = &[
1042        "ver",
1043        "seq",
1044        "retcode",
1045        "filedata",
1046        "rangestart",
1047        "rangeend",
1048        "totalsize",
1049        "retrysec",
1050        "isretry",
1051        "isoverload",
1052        "isgetcdn",
1053        "istrans",
1054        "backsrcnum",
1055        "rawfilesize",
1056        "rawfilemd5",
1057        "substituteftype",
1058        "rsppicformat",
1059        "videoformat",
1060        "x-ClientIp",
1061    ];
1062    if !reply.keys().all(|key| FIELDS.contains(&key.as_str())) {
1063        return Err(LargeFileDownloadError::UnknownField.into());
1064    }
1065    use LargeFileDownloadControl as Control;
1066    for (control, expected) in [(Control::Version, 0), (Control::VideoFormat, 0)] {
1067        let actual = integer(reply, control.field_name())?;
1068        if actual != expected {
1069            return Err(LargeFileDownloadError::FixedControlMismatch { control, actual }.into());
1070        }
1071    }
1072    // Linux 4.1.13.26's file7 pipeline (0x8d12d60 -> 0x8ce3000 ->
1073    // 0x8ca0ec0) does not consume these on a valid retcode-zero response.
1074    // The first four are optional template values; the remaining four never
1075    // enter its response template. They do not trigger retries or substitution.
1076    for key in [
1077        "retrysec",
1078        "isretry",
1079        "isoverload",
1080        "isgetcdn",
1081        "istrans",
1082        "backsrcnum",
1083        "substituteftype",
1084        "rsppicformat",
1085    ] {
1086        if let Some(raw) = reply.get(key) {
1087            // Keep the existing 16-byte integer parser bound, with unsigned
1088            // syntax. This is a native parsing policy, not a service limit.
1089            ensure!(
1090                raw.iter().all(u8::is_ascii_digit),
1091                "Invalid large-file metadata integer"
1092            );
1093            integer(reply, key)?;
1094        }
1095    }
1096    ensure!(
1097        u64::try_from(integer(reply, "rawfilesize")?)? == descriptor.expected_bytes.unwrap_or(0),
1098        "Large-file download source size mismatch"
1099    );
1100    let raw = reply
1101        .get("rawfilemd5")
1102        .ok_or_else(|| anyhow::anyhow!("Missing CDN source checksum"))?;
1103    ensure!(
1104        raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1105        "Invalid CDN source checksum"
1106    );
1107    let value = std::str::from_utf8(raw)?;
1108    ensure!(
1109        server_md5
1110            .as_ref()
1111            .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1112            && descriptor
1113                .expected_md5
1114                .as_ref()
1115                .is_none_or(|expected| expected.eq_ignore_ascii_case(value)),
1116        "CDN source checksum changed"
1117    );
1118    *server_md5 = Some(value.to_string());
1119    Ok(())
1120}
1121fn legacy_upload_path(
1122    kind: MediaQuality,
1123    plaintext: usize,
1124    ciphertext: usize,
1125) -> Result<&'static str> {
1126    ensure!(
1127        kind != MediaQuality::LargeFile
1128            && (kind != MediaQuality::File || plaintext <= MAX_NATIVE_UPLOAD_BYTES)
1129            && ciphertext < MAX_CDN_TRANSFER_BYTES,
1130        "File requires multiphase upload"
1131    );
1132    Ok("/uploadv3")
1133}
1134fn escaped_component(s: &str) -> String {
1135    s.bytes()
1136        .map(|b| {
1137            if b.is_ascii_alphanumeric() || b"-_.!~*'()".contains(&b) {
1138                (b as char).to_string()
1139            } else {
1140                format!("%{b:02X}")
1141            }
1142        })
1143        .collect()
1144}
1145fn upload_fields(
1146    conf: &CdnConfig,
1147    p: &PreparedUpload,
1148    sequence: u32,
1149    start: usize,
1150    end: usize,
1151) -> Result<CdnFields> {
1152    ensure!(
1153        sequence > 0 && start <= end && end < p.encrypted.len() && end - start < UPLOAD_CHUNK,
1154        "Invalid upload range"
1155    );
1156    let mut f = shared(conf, sequence, p.kind.filetype());
1157    for (k, v) in [
1158        ("hasthumb", 0),
1159        ("compresstype", 0),
1160        ("nocheckaeskey", 1),
1161        ("enablehit", 0),
1162        ("existancecheck", 0),
1163        ("apptype", 0),
1164        ("totalsize", p.encrypted.len() as u64),
1165        ("rawtotalsize", p.plaintext_bytes as u64),
1166        ("thumbtotalsize", 0),
1167        ("rawthumbsize", 0),
1168        ("encthumbcrc", 0),
1169        ("advideoflag", 0),
1170        ("rangestart", start as u64),
1171        ("rangeend", end as u64),
1172    ] {
1173        put(&mut f, k, v);
1174    }
1175    for (k, v) in [
1176        ("touser", encrypt_recipient(&p.destination)?),
1177        ("filekey", p.file_key.clone()),
1178        ("localname", escaped_component(&p.file_name)),
1179        ("filedatamd5", hex::encode(md5(&p.encrypted[start..=end]))),
1180        ("filemd5", p.md5.clone()),
1181        ("rawfilemd5", p.md5.clone()),
1182        ("blockmd5", hex::encode(md5(&p.encrypted[start..=end]))),
1183    ] {
1184        f.insert(k.into(), v.into_bytes());
1185    }
1186    f.insert("rawthumbmd5".into(), vec![]);
1187    Ok(f)
1188}
1189fn validate_upload_reply(
1190    reply: &CdnFields,
1191    sequence: u32,
1192    start: usize,
1193    end: usize,
1194    file_id: &mut Option<String>,
1195) -> Result<()> {
1196    let code = integer(reply, "retcode")?;
1197    ensure!(code == 0, "CDN upload returned {code}");
1198    for (key, expected) in [
1199        ("seq", sequence as u64),
1200        ("rangestart", start as u64),
1201        ("rangeend", end as u64),
1202    ] {
1203        if reply.contains_key(key) {
1204            ensure!(
1205                u64::try_from(integer(reply, key)?)? == expected,
1206                "CDN upload correlation mismatch"
1207            );
1208        }
1209    }
1210    if let Some(raw) = reply.get("fileid") {
1211        let value = std::str::from_utf8(raw)?;
1212        ensure!(
1213            !value.is_empty()
1214                && value.len() <= 4096
1215                && value
1216                    .bytes()
1217                    .all(|b| b.is_ascii_alphanumeric() || b"-_@.+/=".contains(&b)),
1218            "Invalid uploaded file identifier"
1219        );
1220        ensure!(
1221            file_id.as_deref().is_none_or(|previous| previous == value),
1222            "CDN upload file identifier changed"
1223        );
1224        *file_id = Some(value.to_string());
1225    }
1226    Ok(())
1227}
1228
1229#[derive(Clone, Serialize, Deserialize)]
1230pub struct PreparedVariant {
1231    pub encrypted: Vec<u8>,
1232    pub plaintext_bytes: u32,
1233    pub width: u32,
1234    pub height: u32,
1235    pub md5: String,
1236    pub sha256: String,
1237    pub checksum: u32,
1238    pub encrypted_checksum: u32,
1239}
1240#[derive(Clone, Serialize, Deserialize)]
1241pub struct PreparedImageUpload {
1242    pub original: PreparedUpload,
1243    pub standard: PreparedVariant,
1244    pub thumbnail: PreparedVariant,
1245    pub width: u32,
1246    pub height: u32,
1247}
1248#[derive(Clone, Serialize, Deserialize)]
1249pub struct PreparedVideoUpload {
1250    pub video: PreparedUpload,
1251    pub thumbnail: PreparedVariant,
1252}
1253#[derive(Clone, Serialize, Deserialize)]
1254pub struct UploadedImage {
1255    pub original: UploadedMedia,
1256    pub standard: UploadedMedia,
1257    pub thumbnail: UploadedMedia,
1258}
1259#[derive(Clone, Serialize, Deserialize)]
1260pub struct UploadedVideo {
1261    pub video: UploadedMedia,
1262    pub thumbnail: UploadedMedia,
1263    #[serde(default)]
1264    pub original: Option<UploadedMedia>,
1265}
1266fn jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1267    use image::GenericImageView;
1268    ensure!(
1269        !bytes.is_empty() && bytes.len() <= 65520,
1270        "JPEG variant outside small-object profile"
1271    );
1272    ensure!(
1273        image::guess_format(bytes)? == image::ImageFormat::Jpeg,
1274        "Expected JPEG variant"
1275    );
1276    let mut reader =
1277        image::ImageReader::with_format(std::io::Cursor::new(bytes), image::ImageFormat::Jpeg);
1278    let mut limits = image::Limits::default();
1279    limits.max_image_width = Some(4096);
1280    limits.max_image_height = Some(4096);
1281    limits.max_alloc = Some(64 * 1024 * 1024);
1282    reader.limits(limits);
1283    let image = reader.decode()?;
1284    let (w, h) = image.dimensions();
1285    ensure!(
1286        w > 0 && h > 0 && w <= 4096 && h <= 4096,
1287        "JPEG dimensions outside limit"
1288    );
1289    Ok((w, h))
1290}
1291pub fn adler32_zero(bytes: &[u8]) -> u32 {
1292    let mut a = 0u32;
1293    let mut b = 0u32;
1294    for &v in bytes {
1295        a = (a + v as u32) % 65521;
1296        b = (b + a) % 65521;
1297    }
1298    (b << 16) | a
1299}
1300impl PreparedVariant {
1301    fn new(raw: &[u8], key: &[u8]) -> Result<Self> {
1302        let (width, height) = jpeg_dimensions(raw)?;
1303        let encrypted = ecb_encrypt(raw, key)?;
1304        Ok(Self {
1305            checksum: adler32_zero(raw),
1306            encrypted_checksum: adler32_zero(&encrypted),
1307            encrypted,
1308            plaintext_bytes: raw.len() as u32,
1309            width,
1310            height,
1311            md5: hex::encode(md5(raw)),
1312            sha256: hex::encode(sha256(raw)),
1313        })
1314    }
1315    fn validate(&self, key: &[u8]) -> Result<()> {
1316        let raw = decrypt_cdn(&self.encrypted, key)?;
1317        let (w, h) = jpeg_dimensions(&raw)?;
1318        ensure!(
1319            raw.len() == self.plaintext_bytes as usize
1320                && w == self.width
1321                && h == self.height
1322                && hex::encode(md5(&raw)) == self.md5
1323                && hex::encode(sha256(&raw)) == self.sha256
1324                && adler32_zero(&raw) == self.checksum
1325                && adler32_zero(&self.encrypted) == self.encrypted_checksum,
1326            "Prepared JPEG variant mismatch"
1327        );
1328        Ok(())
1329    }
1330}
1331impl PreparedImageUpload {
1332    pub fn with_material(
1333        original: &[u8],
1334        standard: &[u8],
1335        thumbnail: &[u8],
1336        file_name: &str,
1337        destination: &str,
1338        key: &[u8],
1339        file_key: &str,
1340    ) -> Result<Self> {
1341        let (width, height) = jpeg_dimensions(original)?;
1342        let original = PreparedUpload::with_material(
1343            original,
1344            MediaQuality::Original,
1345            file_name,
1346            destination,
1347            key,
1348            file_key,
1349        )?;
1350        let standard = PreparedVariant::new(standard, key)?;
1351        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1352        let p = Self {
1353            original,
1354            standard,
1355            thumbnail,
1356            width,
1357            height,
1358        };
1359        p.validate()?;
1360        Ok(p)
1361    }
1362    /// Supplied variants must be independent real JPEGs. This function decodes
1363    /// them fully but does not resize or invent an unsupplied representation.
1364    pub fn prepare(
1365        original: &[u8],
1366        standard: &[u8],
1367        thumbnail: &[u8],
1368        file_name: &str,
1369        destination: &str,
1370    ) -> Result<Self> {
1371        let (width, height) = jpeg_dimensions(original)?;
1372        let original =
1373            PreparedUpload::prepare(original, MediaQuality::Original, file_name, destination)?;
1374        let standard = PreparedVariant::new(standard, &original.aes_key)?;
1375        let thumbnail = PreparedVariant::new(thumbnail, &original.aes_key)?;
1376        let p = Self {
1377            original,
1378            standard,
1379            thumbnail,
1380            width,
1381            height,
1382        };
1383        p.validate()?;
1384        Ok(p)
1385    }
1386    fn validate(&self) -> Result<()> {
1387        self.original.validate()?;
1388        let raw = decrypt_cdn(&self.original.encrypted, &self.original.aes_key)?;
1389        ensure!(
1390            jpeg_dimensions(&raw)? == (self.width, self.height),
1391            "Prepared original dimensions differ"
1392        );
1393        self.standard.validate(&self.original.aes_key)?;
1394        self.thumbnail.validate(&self.original.aes_key)?;
1395        ensure!(
1396            self.original.plaintext_bytes <= 65520
1397                && self.standard.width <= self.width
1398                && self.standard.height <= self.height
1399                && self.thumbnail.width <= self.standard.width
1400                && self.thumbnail.height <= self.standard.height
1401                && self.thumbnail.width <= 320
1402                && self.thumbnail.height <= 320,
1403            "Invalid JPEG variant sizes"
1404        );
1405        Ok(())
1406    }
1407}
1408impl PreparedVideoUpload {
1409    pub fn with_material(
1410        video: &[u8],
1411        thumbnail: &[u8],
1412        file_name: &str,
1413        destination: &str,
1414        key: &[u8],
1415        file_key: &str,
1416    ) -> Result<Self> {
1417        ensure!(
1418            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1419            "Expected bounded MP4 container"
1420        );
1421        let video = PreparedUpload::with_material(
1422            video,
1423            MediaQuality::Video,
1424            file_name,
1425            destination,
1426            key,
1427            file_key,
1428        )?;
1429        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1430        ensure!(
1431            thumbnail.width <= 320 && thumbnail.height <= 320,
1432            "Video thumbnail outside limit"
1433        );
1434        Ok(Self { video, thumbnail })
1435    }
1436    pub fn prepare(
1437        video: &[u8],
1438        thumbnail: &[u8],
1439        file_name: &str,
1440        destination: &str,
1441    ) -> Result<Self> {
1442        ensure!(
1443            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1444            "Expected bounded MP4 container"
1445        );
1446        let video = PreparedUpload::prepare(video, MediaQuality::Video, file_name, destination)?;
1447        let thumbnail = PreparedVariant::new(thumbnail, &video.aes_key)?;
1448        ensure!(
1449            thumbnail.width <= 320 && thumbnail.height <= 320,
1450            "Video thumbnail outside limit"
1451        );
1452        Ok(Self { video, thumbnail })
1453    }
1454}
1455fn add_thumbnail(fields: &mut CdnFields, v: &PreparedVariant) {
1456    for (k, n) in [
1457        ("hasthumb", 1),
1458        ("thumbfiletype", 3),
1459        ("thumbtotalsize", v.encrypted.len() as u64),
1460        ("rawthumbsize", v.plaintext_bytes as u64),
1461        ("thumbcrc", v.checksum as u64),
1462        ("encthumbcrc", v.encrypted_checksum as u64),
1463    ] {
1464        put(fields, k, n);
1465    }
1466    fields.insert("rawthumbmd5".into(), v.md5.as_bytes().to_vec());
1467    fields.insert("thumbdata".into(), v.encrypted.clone());
1468}
1469fn uploaded_primary(
1470    prepared: &PreparedUpload,
1471    file_id: &str,
1472    width: Option<u32>,
1473    height: Option<u32>,
1474) -> UploadedMedia {
1475    UploadedMedia {
1476        descriptor: MediaDescriptor {
1477            file_id: file_id.to_string(),
1478            aes_key: prepared.aes_key.clone(),
1479            quality: prepared.kind,
1480            expected_bytes: Some(prepared.plaintext_bytes as u64),
1481            expected_md5: Some(prepared.md5.clone()),
1482            strict_size: true,
1483            expires_at: None,
1484        },
1485        plaintext_bytes: prepared.plaintext_bytes,
1486        md5: prepared.md5.clone(),
1487        sha256: prepared.sha256.clone(),
1488        file_name: prepared.file_name.clone(),
1489        width,
1490        height,
1491    }
1492}
1493fn uploaded_variant(
1494    primary: &PreparedUpload,
1495    v: &PreparedVariant,
1496    file_id: &str,
1497    quality: MediaQuality,
1498) -> UploadedMedia {
1499    UploadedMedia {
1500        descriptor: MediaDescriptor {
1501            file_id: file_id.to_string(),
1502            aes_key: primary.aes_key.clone(),
1503            quality,
1504            expected_bytes: Some(v.plaintext_bytes as u64),
1505            expected_md5: Some(v.md5.clone()),
1506            strict_size: true,
1507            expires_at: None,
1508        },
1509        plaintext_bytes: v.plaintext_bytes,
1510        md5: v.md5.clone(),
1511        sha256: v.sha256.clone(),
1512        file_name: primary.file_name.clone(),
1513        width: Some(v.width),
1514        height: Some(v.height),
1515    }
1516}
1517impl CdnClient {
1518    pub fn upload_image(&self, p: &PreparedImageUpload) -> Result<UploadedImage> {
1519        p.validate()?;
1520        let end = p.original.encrypted.len() - 1;
1521        let mut f = upload_fields(&self.config, &p.original, 1, 0, end)?;
1522        f.insert("filedata".into(), p.original.encrypted.clone());
1523        add_thumbnail(&mut f, &p.thumbnail);
1524        for (k, v) in [
1525            ("imgheight", p.height as u64),
1526            ("imgwidth", p.width as u64),
1527            ("midimgrawsize", p.standard.plaintext_bytes as u64),
1528            ("midimgtotalsize", p.standard.encrypted.len() as u64),
1529            ("midimgchecksum", p.standard.encrypted_checksum as u64),
1530            ("file_format", 0),
1531            ("mid_format", 0),
1532        ] {
1533            put(&mut f, k, v);
1534        }
1535        f.insert("midimgmd5".into(), p.standard.md5.as_bytes().to_vec());
1536        f.insert("midimgdata".into(), p.standard.encrypted.clone());
1537        let reply = self.post("/uploadv3", &f)?;
1538        let mut file_id = None;
1539        validate_upload_reply(&reply, 1, 0, end, &mut file_id)?;
1540        let id =
1541            file_id.ok_or_else(|| anyhow::anyhow!("Image upload returned no file identifier"))?;
1542        Ok(UploadedImage {
1543            original: uploaded_primary(&p.original, &id, Some(p.width), Some(p.height)),
1544            standard: uploaded_variant(&p.original, &p.standard, &id, MediaQuality::Standard),
1545            thumbnail: uploaded_variant(&p.original, &p.thumbnail, &id, MediaQuality::Thumbnail),
1546        })
1547    }
1548    pub fn upload_video(&self, p: &PreparedVideoUpload) -> Result<UploadedVideo> {
1549        self.upload_video_with_progress(p, |_, _| Ok(()))
1550    }
1551    pub fn upload_video_with_progress(
1552        &self,
1553        p: &PreparedVideoUpload,
1554        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1555    ) -> Result<UploadedVideo> {
1556        self.upload_video_with_transport(p, progress, |path, fields| self.post(path, fields))
1557    }
1558    fn upload_video_with_transport(
1559        &self,
1560        p: &PreparedVideoUpload,
1561        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1562        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1563    ) -> Result<UploadedVideo> {
1564        p.video.validate()?;
1565        ensure!(
1566            p.video.kind == MediaQuality::Video
1567                && p.video.plaintext_bytes as usize <= MAX_NATIVE_UPLOAD_BYTES,
1568            "Invalid normal video upload profile"
1569        );
1570        p.thumbnail.validate(&p.video.aes_key)?;
1571        ensure!(
1572            p.thumbnail.width <= 320 && p.thumbnail.height <= 320,
1573            "Video thumbnail outside limit"
1574        );
1575        let mut file_id = None;
1576        for (sequence, chunk) in p.video.encrypted.chunks(UPLOAD_CHUNK).enumerate() {
1577            let start = sequence * UPLOAD_CHUNK;
1578            let end = start + chunk.len() - 1;
1579            progress(start as u64, Some(p.video.encrypted.len() as u64))?;
1580            let mut fields =
1581                upload_fields(&self.config, &p.video, sequence as u32 + 1, start, end)?;
1582            fields.insert("filedata".into(), chunk.to_vec());
1583            if start == 0 {
1584                add_thumbnail(&mut fields, &p.thumbnail);
1585            }
1586            put(&mut fields, "smallvideoflag", 0);
1587            let reply = post("/uploadv3", &fields)?;
1588            validate_upload_reply(&reply, sequence as u32 + 1, start, end, &mut file_id)?;
1589            if end + 1 < p.video.encrypted.len() {
1590                progress((end + 1) as u64, Some(p.video.encrypted.len() as u64))?;
1591            }
1592        }
1593        let id =
1594            file_id.ok_or_else(|| anyhow::anyhow!("Video upload returned no file identifier"))?;
1595        Ok(UploadedVideo {
1596            video: uploaded_primary(&p.video, &id, None, None),
1597            thumbnail: uploaded_variant(&p.video, &p.thumbnail, &id, MediaQuality::Thumbnail),
1598            original: None,
1599        })
1600    }
1601}
1602
1603#[cfg(test)]
1604mod tests {
1605    use super::*;
1606    fn client() -> CdnClient {
1607        CdnClient::new(
1608            CdnConfig {
1609                version: 1,
1610                uin: 12345,
1611                client_version: 456,
1612                client_os_type: "synthetic-client".into(),
1613                auth_key: b"synthetic-auth".to_vec(),
1614                server_ips: vec![Ipv4Addr::new(1, 1, 1, 1)],
1615                front_id: 0,
1616                location: None,
1617            },
1618            Duration::from_secs(25),
1619        )
1620        .unwrap()
1621    }
1622    fn download_fixture(size: usize, quality: MediaQuality) -> (MediaDescriptor, Vec<u8>, Vec<u8>) {
1623        let raw = vec![0x5a; size];
1624        let key = vec![0x31; 16];
1625        let ciphertext = ecb_encrypt(&raw, &key).unwrap();
1626        let descriptor = MediaDescriptor {
1627            file_id: "synthetic-object".into(),
1628            aes_key: key,
1629            quality,
1630            expected_bytes: Some(size as u64),
1631            expected_md5: Some(hex::encode(md5(&raw))),
1632            strict_size: true,
1633            expires_at: None,
1634        };
1635        (descriptor, raw, ciphertext)
1636    }
1637    fn download_reply(
1638        fields: &CdnFields,
1639        descriptor: &MediaDescriptor,
1640        ciphertext: &[u8],
1641    ) -> CdnFields {
1642        let mut reply = CdnFields::new();
1643        let start = integer(fields, "rangestart").unwrap() as usize;
1644        let end = (integer(fields, "rangeend").unwrap() as usize).min(ciphertext.len() - 1);
1645        assert_eq!(fields["fileid"], descriptor.file_id.as_bytes());
1646        assert_eq!(integer(fields, "weixinnum").unwrap(), 12345);
1647        assert!(!fields.contains_key("aeskey"));
1648        for (key, value) in [
1649            ("ver", 0),
1650            ("seq", integer(fields, "seq").unwrap()),
1651            ("retcode", 0),
1652            ("rangestart", start as i64),
1653            ("rangeend", end as i64),
1654            ("totalsize", ciphertext.len() as i64),
1655        ] {
1656            put(&mut reply, key, value);
1657        }
1658        reply.insert("filedata".into(), ciphertext[start..=end].to_vec());
1659        if descriptor.quality == MediaQuality::LargeFile {
1660            for (key, value) in [
1661                ("retrysec", 0),
1662                ("isretry", 0),
1663                ("isoverload", 0),
1664                ("isgetcdn", 0),
1665                ("istrans", 999),
1666                ("backsrcnum", 0),
1667                ("substituteftype", 7),
1668                ("rsppicformat", 0),
1669                ("videoformat", 0),
1670                ("rawfilesize", descriptor.expected_bytes.unwrap() as i64),
1671            ] {
1672                put(&mut reply, key, value);
1673            }
1674            reply.insert(
1675                "rawfilemd5".into(),
1676                descriptor
1677                    .expected_md5
1678                    .as_ref()
1679                    .unwrap()
1680                    .as_bytes()
1681                    .to_vec(),
1682            );
1683        }
1684        reply
1685    }
1686    #[test]
1687    fn large_video_keeps_normal_endpoint_type_and_exact_source_bytes() {
1688        let (descriptor, raw, ciphertext) =
1689            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
1690        let mut requests = 0;
1691        let downloaded = client()
1692            .download_with_transport(
1693                &descriptor,
1694                |_, _| Ok(()),
1695                |path, fields| {
1696                    requests += 1;
1697                    assert_eq!(path, "/download");
1698                    assert_eq!(integer(fields, "filetype")?, 4);
1699                    assert_eq!(integer(fields, "seq")?, requests);
1700                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
1701                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
1702                    put(&mut reply, "substituteftype", 4);
1703                    put(&mut reply, "videoformat", 1);
1704                    put(&mut reply, "istrans", 999);
1705                    Ok(reply)
1706                },
1707            )
1708            .unwrap();
1709        assert_eq!(requests, 51);
1710        assert_eq!(downloaded.bytes, raw);
1711        assert_eq!(downloaded.md5, descriptor.expected_md5.as_deref().unwrap());
1712        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
1713        assert_eq!(downloaded.substituted_filetype, Some(4));
1714    }
1715
1716    #[test]
1717    fn large_video_rejects_unknown_controls_substitution_and_unbound_source() {
1718        let (mut descriptor, _, ciphertext) = download_fixture(16, MediaQuality::Video);
1719        descriptor.expected_bytes = Some(MAX_CDN_TRANSFER_BYTES as u64 + 1);
1720        let fields = BTreeMap::from([
1721            ("rangestart".into(), b"0".to_vec()),
1722            ("rangeend".into(), b"31".to_vec()),
1723            ("seq".into(), b"1".to_vec()),
1724            ("weixinnum".into(), b"12345".to_vec()),
1725            ("fileid".into(), descriptor.file_id.as_bytes().to_vec()),
1726        ]);
1727        let valid = download_reply(&fields, &descriptor, &ciphertext);
1728        for (key, value) in [
1729            ("unknown", "private"),
1730            ("ver", "1"),
1731            ("videoformat", "2"),
1732            ("istrans", "1"),
1733            ("KEY_VIDEOFORMAT", "1"),
1734            ("isretry", "1"),
1735            ("isgetcdn", "1"),
1736            ("rsppicformat", "3"),
1737            ("substituteftype", "7"),
1738            ("rawfilesize", "1"),
1739            ("rawfilemd5", "00000000000000000000000000000000"),
1740        ] {
1741            let mut reply = valid.clone();
1742            reply.insert(key.into(), value.as_bytes().to_vec());
1743            let error = validate_large_video_download_reply(&reply, &descriptor, &mut None)
1744                .unwrap_err()
1745                .to_string();
1746            assert!(!error.contains("private"));
1747        }
1748        for (size, checksum) in [
1749            (
1750                Some(MAX_VIDEO_DOWNLOAD_BYTES as u64 + 1),
1751                descriptor.expected_md5.clone(),
1752            ),
1753            (descriptor.expected_bytes, None),
1754        ] {
1755            descriptor.expected_bytes = size;
1756            descriptor.expected_md5 = checksum;
1757            let mut requests = 0;
1758            assert!(client()
1759                .download_with_transport(
1760                    &descriptor,
1761                    |_, _| Ok(()),
1762                    |_, _| {
1763                        requests += 1;
1764                        bail!("must not request")
1765                    }
1766                )
1767                .is_err());
1768            assert_eq!(requests, 0);
1769        }
1770    }
1771
1772    #[test]
1773    fn cold_large_descriptor_downloads_with_padding_and_hashes() {
1774        let (descriptor, raw, ciphertext) =
1775            download_fixture(32 * 1024 * 1024, MediaQuality::LargeFile);
1776        assert_eq!(ciphertext.len(), 32 * 1024 * 1024 + 16);
1777        let stored = serde_json::to_vec(&descriptor).unwrap();
1778        let cold: MediaDescriptor = serde_json::from_slice(&stored).unwrap();
1779        let events = std::cell::RefCell::new(Vec::new());
1780        let mut requests = 0;
1781        let mut previous = 0;
1782        let downloaded = client()
1783            .download_with_transport(
1784                &cold,
1785                |done, total| {
1786                    events.borrow_mut().push("progress");
1787                    assert!(done >= previous);
1788                    previous = done;
1789                    if let Some(total) = total {
1790                        assert_eq!(total, ciphertext.len() as u64);
1791                    }
1792                    Ok(())
1793                },
1794                |path, fields| {
1795                    events.borrow_mut().push("request");
1796                    requests += 1;
1797                    assert_eq!(path, "/downloadbig");
1798                    assert_eq!(integer(fields, "filetype")?, 7);
1799                    assert_eq!(integer(fields, "seq")?, requests);
1800                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
1801                    Ok(download_reply(fields, &cold, &ciphertext))
1802                },
1803            )
1804            .unwrap();
1805        assert_eq!(requests, 65);
1806        assert_eq!(previous, ciphertext.len() as u64);
1807        for chunk in events.borrow().chunks(3) {
1808            assert_eq!(chunk, ["progress", "request", "progress"]);
1809        }
1810        assert_eq!(downloaded.bytes, raw);
1811        assert_eq!(
1812            downloaded.md5,
1813            descriptor.expected_md5.as_ref().unwrap().as_str()
1814        );
1815        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
1816        assert_eq!(downloaded.requested_quality, MediaQuality::LargeFile);
1817        assert_eq!(downloaded.substituted_filetype, None);
1818        assert_eq!(downloaded.picture_format, None);
1819        assert_eq!(downloaded.advertised_size_matches, Some(true));
1820    }
1821    #[test]
1822    fn large_download_rejections_expose_only_typed_static_control_details() {
1823        use LargeFileDownloadControl as Control;
1824        let (descriptor, _, ciphertext) =
1825            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
1826        for (control, actual) in [(Control::Version, 1), (Control::VideoFormat, 1)] {
1827            let mut calls = 0;
1828            let error = client()
1829                .download_with_transport(
1830                    &descriptor,
1831                    |_, _| Ok(()),
1832                    |_, fields| {
1833                        calls += 1;
1834                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
1835                        put(&mut reply, control.field_name(), actual);
1836                        Ok(reply)
1837                    },
1838                )
1839                .err()
1840                .unwrap();
1841            assert_eq!(calls, 1);
1842            assert_eq!(
1843                error.downcast_ref::<LargeFileDownloadError>(),
1844                Some(&LargeFileDownloadError::FixedControlMismatch { control, actual })
1845            );
1846            assert_eq!(error.to_string(), "Unsupported large-file download control");
1847        }
1848        let secret = "private-response-token-and-file-id";
1849        let error = client()
1850            .download_with_transport(
1851                &descriptor,
1852                |_, _| Ok(()),
1853                |_, fields| {
1854                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
1855                    reply.insert(secret.into(), secret.as_bytes().to_vec());
1856                    Ok(reply)
1857                },
1858            )
1859            .err()
1860            .unwrap();
1861        assert_eq!(
1862            error.downcast_ref::<LargeFileDownloadError>(),
1863            Some(&LargeFileDownloadError::UnknownField)
1864        );
1865        assert_eq!(
1866            error.to_string(),
1867            "Unsupported large-file download response field"
1868        );
1869        assert!(!format!("{error:?}").contains(secret));
1870    }
1871    #[test]
1872    fn large_download_rejects_correlation_format_unknown_fields_and_ranges() {
1873        let (descriptor, _, ciphertext) =
1874            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
1875        for (key, value) in [
1876            ("seq", 2),
1877            ("ver", 1),
1878            ("retcode", -1),
1879            ("videoformat", 1),
1880            ("rangestart", 1),
1881            ("rangeend", 512 * 1024),
1882            ("totalsize", ciphertext.len() as i64 + 16),
1883            ("rawfilesize", 1),
1884            ("futureauth", 1),
1885        ] {
1886            let mut requests = 0;
1887            let outcome = client().download_with_transport(
1888                &descriptor,
1889                |_, _| Ok(()),
1890                |_, fields| {
1891                    requests += 1;
1892                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
1893                    put(&mut reply, key, value);
1894                    Ok(reply)
1895                },
1896            );
1897            assert!(outcome.is_err(), "Accepted invalid {key}");
1898            assert_eq!(requests, 1);
1899        }
1900        for missing in ["seq", "ver", "videoformat", "rawfilemd5", "filedata"] {
1901            assert!(
1902                client()
1903                    .download_with_transport(
1904                        &descriptor,
1905                        |_, _| Ok(()),
1906                        |_, fields| {
1907                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
1908                            reply.remove(missing);
1909                            Ok(reply)
1910                        }
1911                    )
1912                    .is_err(),
1913                "Accepted missing {missing}"
1914            );
1915        }
1916    }
1917    #[test]
1918    fn file7_success_metadata_is_optional_and_does_not_change_verified_bytes() {
1919        // These eight names come from the reference template/consumer join,
1920        // independently of the native validator's allowlist.
1921        let metadata = [
1922            "isretry",
1923            "retrysec",
1924            "isoverload",
1925            "isgetcdn",
1926            "istrans",
1927            "backsrcnum",
1928            "substituteftype",
1929            "rsppicformat",
1930        ];
1931        let (descriptor, raw, ciphertext) = download_fixture(1024 * 1024, MediaQuality::LargeFile);
1932        assert_eq!(ciphertext.len(), 1024 * 1024 + 16);
1933        // Each field may be absent or vary independently on the first, middle,
1934        // and final padding-only ranges, without creating another request.
1935        for selected in metadata.into_iter().map(Some).chain([None]) {
1936            for omit in [false, true] {
1937                let mut requests = 0;
1938                let result = client()
1939                    .download_with_transport(
1940                        &descriptor,
1941                        |_, _| Ok(()),
1942                        |path, fields| {
1943                            requests += 1;
1944                            assert_eq!(path, "/downloadbig");
1945                            assert_eq!(integer(fields, "seq")?, requests);
1946                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
1947                            for key in metadata {
1948                                if selected.is_none_or(|selected| selected == key) {
1949                                    if omit {
1950                                        reply.remove(key);
1951                                    } else {
1952                                        put(
1953                                            &mut reply,
1954                                            key,
1955                                            match requests {
1956                                                1 => 1,
1957                                                2 => 999,
1958                                                3 => 9_999_999_999_999_999i64,
1959                                                _ => panic!("Unexpected retry or extra range"),
1960                                            },
1961                                        );
1962                                    }
1963                                }
1964                            }
1965                            Ok(reply)
1966                        },
1967                    )
1968                    .unwrap();
1969                assert_eq!(requests, 3);
1970                assert_eq!(result.bytes, raw);
1971                assert_eq!(result.md5, descriptor.expected_md5.as_deref().unwrap());
1972                assert_eq!(result.sha256, hex::encode(sha256(&raw)));
1973                assert_eq!(result.requested_quality, MediaQuality::LargeFile);
1974                assert_eq!(result.substituted_filetype, None);
1975                assert_eq!(result.picture_format, None);
1976            }
1977        }
1978    }
1979    #[test]
1980    fn file7_metadata_parser_bounds_and_errors_remain_strict() {
1981        let (descriptor, _, ciphertext) = download_fixture(512 * 1024, MediaQuality::LargeFile);
1982        for key in [
1983            "isretry",
1984            "retrysec",
1985            "isoverload",
1986            "isgetcdn",
1987            "istrans",
1988            "backsrcnum",
1989            "substituteftype",
1990            "rsppicformat",
1991        ] {
1992            for malformed in [
1993                b"".as_slice(),
1994                b"-1",
1995                b"-0",
1996                b"+1",
1997                b" 1",
1998                b"1x",
1999                b"99999999999999999",
2000                b"\xff",
2001            ] {
2002                let mut requests = 0;
2003                assert!(
2004                    client()
2005                        .download_with_transport(
2006                            &descriptor,
2007                            |_, _| Ok(()),
2008                            |_, fields| {
2009                                requests += 1;
2010                                let mut reply = download_reply(fields, &descriptor, &ciphertext);
2011                                reply.insert(key.into(), malformed.to_vec());
2012                                Ok(reply)
2013                            },
2014                        )
2015                        .is_err(),
2016                    "Accepted malformed {key}"
2017                );
2018                assert_eq!(requests, 1);
2019            }
2020        }
2021        let mut requests = 0;
2022        let error = client()
2023            .download_with_transport(
2024                &descriptor,
2025                |_, _| Ok(()),
2026                |_, fields| {
2027                    requests += 1;
2028                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2029                    for key in ["isretry", "retrysec", "isoverload", "isgetcdn"] {
2030                        put(&mut reply, key, 1);
2031                    }
2032                    put(&mut reply, "retcode", -1);
2033                    Ok(reply)
2034                },
2035            )
2036            .err()
2037            .unwrap();
2038        assert_eq!(error.to_string(), "CDN download returned -1");
2039        assert_eq!(requests, 1);
2040    }
2041    #[test]
2042    fn normal_download_keeps_representation_metadata_and_consistency_checks() {
2043        let (descriptor, raw, ciphertext) = download_fixture(65536, MediaQuality::File);
2044        let result = client()
2045            .download_with_transport(
2046                &descriptor,
2047                |_, _| Ok(()),
2048                |path, fields| {
2049                    assert_eq!(path, "/download");
2050                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2051                    put(&mut reply, "substituteftype", 5);
2052                    put(&mut reply, "rsppicformat", 3);
2053                    Ok(reply)
2054                },
2055            )
2056            .unwrap();
2057        assert_eq!(result.bytes, raw);
2058        assert_eq!(result.substituted_filetype, Some(5));
2059        assert_eq!(result.picture_format, Some(3));
2060        let mut requests = 0;
2061        assert!(client()
2062            .download_with_transport(
2063                &descriptor,
2064                |_, _| Ok(()),
2065                |_, fields| {
2066                    requests += 1;
2067                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2068                    put(&mut reply, "rsppicformat", requests);
2069                    Ok(reply)
2070                },
2071            )
2072            .is_err());
2073        assert_eq!(requests, 2);
2074    }
2075    #[test]
2076    fn download_refuses_truncated_overlong_and_changing_source_bytes() {
2077        let (descriptor, _, ciphertext) =
2078            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2079        for failure in [
2080            "empty",
2081            "overlong",
2082            "source-checksum",
2083            "changed-checksum",
2084            "changed-total",
2085            "short-nonfinal",
2086        ] {
2087            let mut requests = 0;
2088            let outcome = client().download_with_transport(
2089                &descriptor,
2090                |_, _| Ok(()),
2091                |_, fields| {
2092                    requests += 1;
2093                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2094                    match failure {
2095                        "empty" => {
2096                            reply.insert("filedata".into(), vec![]);
2097                        }
2098                        "overlong" => {
2099                            reply.get_mut("filedata").unwrap().push(1);
2100                            put(&mut reply, "rangeend", 512 * 1024);
2101                        }
2102                        "source-checksum" => {
2103                            reply.insert(
2104                                "rawfilemd5".into(),
2105                                b"0123456789abcdef0123456789abcdef".to_vec(),
2106                            );
2107                        }
2108                        "changed-checksum" if requests == 2 => {
2109                            reply.insert(
2110                                "rawfilemd5".into(),
2111                                b"0123456789abcdef0123456789abcdef".to_vec(),
2112                            );
2113                        }
2114                        "changed-total" if requests == 2 => {
2115                            put(&mut reply, "totalsize", ciphertext.len() + 16)
2116                        }
2117                        "short-nonfinal" => {
2118                            reply.insert("filedata".into(), vec![1]);
2119                            put(&mut reply, "rangeend", integer(fields, "rangestart")?);
2120                        }
2121                        _ => {}
2122                    }
2123                    Ok(reply)
2124                },
2125            );
2126            assert!(outcome.is_err(), "Accepted {failure}");
2127            assert!(requests <= 2);
2128        }
2129    }
2130    #[test]
2131    fn large_download_checks_padding_actual_size_and_digest_independently() {
2132        let (descriptor, _, ciphertext) =
2133            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2134        let mut invalid_padding = ciphertext.clone();
2135        let mut encoder = openssl::symm::Crypter::new(
2136            Cipher::aes_128_ecb(),
2137            openssl::symm::Mode::Encrypt,
2138            &descriptor.aes_key,
2139            None,
2140        )
2141        .unwrap();
2142        encoder.pad(false);
2143        let mut block = [0u8; 32];
2144        let written = encoder.update(&[0u8; 16], &mut block).unwrap();
2145        assert_eq!(
2146            written + encoder.finalize(&mut block[written..]).unwrap(),
2147            16
2148        );
2149        let last = invalid_padding.len() - 16;
2150        invalid_padding[last..].copy_from_slice(&block[..16]);
2151        assert!(client()
2152            .download_with_transport(
2153                &descriptor,
2154                |_, _| Ok(()),
2155                |_, fields| Ok(download_reply(fields, &descriptor, &invalid_padding))
2156            )
2157            .is_err());
2158        let mut without_advertised_md5 = descriptor.clone();
2159        without_advertised_md5.expected_md5 = None;
2160        for change_later in [false, true] {
2161            let mut requests = 0;
2162            assert!(client()
2163                .download_with_transport(
2164                    &without_advertised_md5,
2165                    |_, _| Ok(()),
2166                    |_, fields| {
2167                        requests += 1;
2168                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2169                        if !change_later || requests == 2 {
2170                            reply.insert(
2171                                "rawfilemd5".into(),
2172                                b"0123456789abcdef0123456789abcdef".to_vec(),
2173                            );
2174                        }
2175                        Ok(reply)
2176                    }
2177                )
2178                .is_err());
2179            assert_eq!(requests, 2);
2180        }
2181        let mut wrong_source = ciphertext.clone();
2182        wrong_source[0] ^= 1;
2183        assert!(client()
2184            .download_with_transport(
2185                &descriptor,
2186                |_, _| Ok(()),
2187                |_, fields| Ok(download_reply(fields, &descriptor, &wrong_source))
2188            )
2189            .is_err());
2190        let mut wrong_size = descriptor.clone();
2191        wrong_size.expected_bytes = Some(descriptor.expected_bytes.unwrap() + 1);
2192        wrong_size.strict_size = false;
2193        assert!(client()
2194            .download_with_transport(
2195                &wrong_size,
2196                |_, _| Ok(()),
2197                |_, fields| Ok(download_reply(fields, &wrong_size, &ciphertext))
2198            )
2199            .is_err());
2200    }
2201    #[test]
2202    fn cancellation_and_transport_failure_never_start_another_download_request() {
2203        let (descriptor, _, ciphertext) =
2204            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2205        for stop in [1, 2, 3, 4] {
2206            let mut callbacks = 0;
2207            let mut requests = 0;
2208            let outcome = client().download_with_transport(
2209                &descriptor,
2210                |_, _| {
2211                    callbacks += 1;
2212                    ensure!(callbacks != stop, "cancelled");
2213                    Ok(())
2214                },
2215                |_, fields| {
2216                    requests += 1;
2217                    Ok(download_reply(fields, &descriptor, &ciphertext))
2218                },
2219            );
2220            assert!(outcome.is_err());
2221            assert_eq!(callbacks, stop);
2222            assert_eq!(requests, stop / 2);
2223        }
2224        let mut requests = 0;
2225        assert!(client()
2226            .download_with_transport(
2227                &descriptor,
2228                |_, _| Ok(()),
2229                |_, _| {
2230                    requests += 1;
2231                    bail!("transport failure")
2232                }
2233            )
2234            .is_err());
2235        assert_eq!(requests, 1);
2236    }
2237    #[test]
2238    fn normal_download_keeps_optional_sequence_and_original_limits() {
2239        for quality in [
2240            MediaQuality::Original,
2241            MediaQuality::Standard,
2242            MediaQuality::Thumbnail,
2243            MediaQuality::Video,
2244            MediaQuality::File,
2245        ] {
2246            let (descriptor, raw, ciphertext) = download_fixture(65536 + 17, quality);
2247            let mut requests = 0;
2248            let downloaded = client()
2249                .download_with_transport(
2250                    &descriptor,
2251                    |_, _| Ok(()),
2252                    |path, fields| {
2253                        requests += 1;
2254                        assert_eq!(path, "/download");
2255                        assert_eq!(
2256                            integer(fields, "rangeend")? - integer(fields, "rangestart")? + 1,
2257                            if requests == 1 { 65536 } else { 32 }
2258                        );
2259                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2260                        reply.remove("seq");
2261                        Ok(reply)
2262                    },
2263                )
2264                .unwrap();
2265            assert_eq!(downloaded.bytes, raw);
2266            assert_eq!(requests, 2);
2267            assert!(client()
2268                .download_with_transport(
2269                    &descriptor,
2270                    |_, _| Ok(()),
2271                    |_, fields| {
2272                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2273                        put(&mut reply, "seq", 99);
2274                        Ok(reply)
2275                    }
2276                )
2277                .is_err());
2278            assert!(client()
2279                .download_with_transport(
2280                    &descriptor,
2281                    |_, _| Ok(()),
2282                    |_, fields| {
2283                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2284                        put(&mut reply, "totalsize", MAX_CDN_TRANSFER_BYTES + 32);
2285                        Ok(reply)
2286                    }
2287                )
2288                .is_err());
2289        }
2290        assert!(unpack_cdn(&vec![0; MAX_CDN_BODY + 1]).is_err());
2291    }
2292    #[test]
2293    fn large_descriptor_limits_fail_before_transport() {
2294        let (descriptor, _, _) = download_fixture(16, MediaQuality::LargeFile);
2295        for expected in [
2296            None,
2297            Some(0),
2298            Some(MAX_LARGE_FILE_BYTES as u64 + 1),
2299            Some(u64::MAX),
2300        ] {
2301            let mut descriptor = descriptor.clone();
2302            descriptor.expected_bytes = expected;
2303            let mut requests = 0;
2304            assert!(client()
2305                .download_with_transport(
2306                    &descriptor,
2307                    |_, _| Ok(()),
2308                    |_, _| {
2309                        requests += 1;
2310                        bail!("must not request")
2311                    }
2312                )
2313                .is_err());
2314            assert_eq!(requests, 0);
2315        }
2316    }
2317
2318    #[test]
2319    fn legacy_completion_receipt_survives_cancellation_after_final_ack() {
2320        let prepared = PreparedUpload::prepare(
2321            b"synthetic source",
2322            MediaQuality::File,
2323            "source.bin",
2324            "filehelper",
2325        )
2326        .unwrap();
2327        let cancelled = std::cell::Cell::new(false);
2328        let calls = std::cell::Cell::new(0);
2329        let upload = client()
2330            .upload_with_transport(
2331                &prepared,
2332                |_, _| {
2333                    ensure!(!cancelled.get(), "cancelled");
2334                    Ok(())
2335                },
2336                |path, fields| {
2337                    assert_eq!(path, "/uploadv3");
2338                    calls.set(calls.get() + 1);
2339                    let mut reply = CdnFields::new();
2340                    put(&mut reply, "retcode", 0);
2341                    for key in ["seq", "rangestart", "rangeend"] {
2342                        reply.insert(key.into(), fields[key].clone());
2343                    }
2344                    reply.insert("fileid".into(), b"synthetic-completed-object".to_vec());
2345                    cancelled.set(true);
2346                    Ok(reply)
2347                },
2348            )
2349            .unwrap();
2350        assert!(cancelled.get());
2351        assert_eq!(calls.get(), 1);
2352        assert_eq!(upload.descriptor.file_id, "synthetic-completed-object");
2353    }
2354
2355    #[test]
2356    fn paired_video_preserves_final_receipt_and_does_not_admit_large_normal() {
2357        let mut thumbnail = Vec::new();
2358        image::codecs::jpeg::JpegEncoder::new(&mut thumbnail)
2359            .encode_image(&image::DynamicImage::new_rgb8(16, 12))
2360            .unwrap();
2361        let p = PreparedVideoUpload::with_material(
2362            b"\0\0\0\x0cftypisom",
2363            &thumbnail,
2364            "normal.mp4",
2365            "filehelper",
2366            &[0x31; 16],
2367            "00112233445566778899aabbccddeeff",
2368        )
2369        .unwrap();
2370        let cancelled = std::cell::Cell::new(false);
2371        let uploaded = client()
2372            .upload_video_with_transport(
2373                &p,
2374                |_, _| {
2375                    ensure!(!cancelled.get(), "cancelled");
2376                    Ok(())
2377                },
2378                |path, fields| {
2379                    assert_eq!(path, "/uploadv3");
2380                    assert!(fields.contains_key("thumbdata"));
2381                    assert_eq!(integer(fields, "filetype")?, 4);
2382                    let mut reply = CdnFields::new();
2383                    put(&mut reply, "retcode", 0);
2384                    for key in ["seq", "rangestart", "rangeend"] {
2385                        reply.insert(key.into(), fields[key].clone());
2386                    }
2387                    reply.insert("fileid".into(), b"paired-object".to_vec());
2388                    cancelled.set(true);
2389                    Ok(reply)
2390                },
2391            )
2392            .unwrap();
2393        assert!(uploaded.original.is_none());
2394        assert_eq!(uploaded.video.descriptor.file_id, "paired-object");
2395        assert_eq!(uploaded.thumbnail.descriptor.file_id, "paired-object");
2396        let mut altered = p;
2397        altered.video.plaintext_bytes = MAX_NATIVE_UPLOAD_BYTES as u32 + 1;
2398        assert!(client()
2399            .upload_video_with_transport(
2400                &altered,
2401                |_, _| Ok(()),
2402                |_, _| { panic!("Large normal video reached legacy transport") }
2403            )
2404            .is_err());
2405    }
2406    #[test]
2407    fn boundary_matrix_keeps_ciphertext_and_media_kind_limits_separate() {
2408        let boundary = MAX_CDN_TRANSFER_BYTES;
2409        for (size, padded, legacy) in [
2410            (boundary - 17, boundary - 16, Some("/uploadv3")),
2411            (boundary - 16, boundary, None),
2412            (boundary - 1, boundary, None),
2413            (boundary, boundary + 16, None),
2414            (boundary + 1, boundary + 16, None),
2415            (MAX_LARGE_FILE_BYTES, MAX_LARGE_FILE_BYTES + 16, None),
2416            (MAX_LARGE_FILE_BYTES + 1, MAX_LARGE_FILE_BYTES + 16, None),
2417        ] {
2418            assert_eq!((size / 16 + 1) * 16, padded);
2419            let kind = if size > boundary {
2420                MediaQuality::LargeFile
2421            } else {
2422                MediaQuality::File
2423            };
2424            assert_eq!(legacy_upload_path(kind, size, padded).ok(), legacy);
2425            assert_eq!(
2426                size <= upload_plaintext_limit(kind),
2427                size <= MAX_LARGE_FILE_BYTES
2428            );
2429            assert_eq!(
2430                size <= upload_plaintext_limit(MediaQuality::Original),
2431                size <= MAX_NATIVE_UPLOAD_BYTES
2432            );
2433            assert_eq!(
2434                size <= upload_plaintext_limit(MediaQuality::Video),
2435                size <= wechat_media::MAX_VIDEO_SOURCE_BYTES
2436            );
2437        }
2438        assert_eq!(MAX_LARGE_FILE_BYTES, 536_870_912);
2439        assert_eq!(MAX_NATIVE_UPLOAD_BYTES, 26_214_383);
2440        assert_eq!(MAX_CDN_TRANSFER_BYTES, 26_214_400);
2441    }
2442    #[test]
2443    fn multiphase_paths_bind_account_sequence_type_and_phase() {
2444        for filetype in [4, 5, 7] {
2445            for phase in ["apply", "uploadblock", "fin", "complete"] {
2446                assert!(large_file_path(
2447                    &format!("/cdn/12345/3/{filetype}/{phase}"),
2448                    12345,
2449                    3
2450                ));
2451            }
2452        }
2453        for path in [
2454            "/cdn/999/3/7/apply",
2455            "/cdn/12345/2/7/apply",
2456            "/cdn/12345/03/7/apply",
2457            "/cdn/12345/0/7/apply",
2458            "/cdn/12345/3/3/apply",
2459            "/cdn/12345/3/7/unknown",
2460            "/cdn/12345/3/7/apply?x=1",
2461            "/cdn/12345/3/7/apply/extra",
2462            "//cdn/12345/3/7/apply",
2463            "/cdn/12345/3/7/../apply",
2464        ] {
2465            assert!(!large_file_path(path, 12345, 3), "Accepted {path}");
2466        }
2467        assert!(!large_file_path("/cdn/12345/0/7/apply", 12345, 0));
2468    }
2469    #[test]
2470    fn cdn_codec_rejects_duplicate_and_truncated() {
2471        let fields = BTreeMap::from([
2472            ("retcode".to_string(), b"0".to_vec()),
2473            ("filedata".into(), vec![0, 1, 2, 255]),
2474        ]);
2475        let packed = pack_cdn(&fields).unwrap();
2476        assert_eq!(unpack_cdn(&packed).unwrap(), fields);
2477        let one = pack_cdn(&BTreeMap::from([("x".into(), vec![1])])).unwrap();
2478        for n in 1..one.len() {
2479            assert!(unpack_cdn(&one[..n]).is_err());
2480        }
2481        let duplicate = [one.clone(), one].concat();
2482        assert!(unpack_cdn(&duplicate).is_err());
2483    }
2484    #[test]
2485    fn prepared_upload_roundtrip() {
2486        let p = PreparedUpload::prepare(
2487            b"synthetic attachment",
2488            MediaQuality::File,
2489            "test.txt",
2490            "filehelper",
2491        )
2492        .unwrap();
2493        p.validate().unwrap();
2494        assert_eq!(
2495            decrypt_cdn(&p.encrypted, &p.aes_key).unwrap(),
2496            b"synthetic attachment"
2497        );
2498        let mut corrupt = p.clone();
2499        corrupt.encrypted[0] ^= 1;
2500        assert!(corrupt.validate().is_err());
2501    }
2502    #[test]
2503    fn nonpublic_addresses_rejected() {
2504        for ip in [
2505            "0.1.2.3",
2506            "10.0.0.1",
2507            "100.64.0.1",
2508            "127.0.0.1",
2509            "169.254.1.1",
2510            "172.16.0.1",
2511            "192.168.0.1",
2512            "198.18.0.1",
2513            "192.0.2.1",
2514            "198.51.100.1",
2515            "203.0.113.1",
2516            "224.1.2.3",
2517        ] {
2518            assert!(!public_ipv4(ip.parse().unwrap()));
2519        }
2520        assert!(public_ipv4("8.8.8.8".parse().unwrap()));
2521    }
2522    #[test]
2523    fn upload_reply_correlates_ranges() {
2524        let mut f = CdnFields::from([
2525            ("retcode".into(), b"0".to_vec()),
2526            ("seq".into(), b"2".to_vec()),
2527        ]);
2528        assert!(validate_upload_reply(&f, 1, 0, 15, &mut None).is_err());
2529        put(&mut f, "seq", 1);
2530        put(&mut f, "rangestart", 0);
2531        put(&mut f, "rangeend", 15);
2532        f.insert("fileid".into(), b"synthetic-file-id".to_vec());
2533        let mut id = None;
2534        validate_upload_reply(&f, 1, 0, 15, &mut id).unwrap();
2535        assert_eq!(id, Some("synthetic-file-id".into()));
2536    }
2537    #[test]
2538    fn prepared_jpeg_variants_are_real_and_independent() {
2539        fn jpeg(w: u32, h: u32) -> Vec<u8> {
2540            let rgb = image::RgbImage::from_pixel(w, h, image::Rgb([12, 40, 80]));
2541            let mut out = vec![];
2542            image::codecs::jpeg::JpegEncoder::new(&mut out)
2543                .encode_image(&rgb)
2544                .unwrap();
2545            out
2546        }
2547        let p = PreparedImageUpload::prepare(
2548            &jpeg(64, 48),
2549            &jpeg(32, 24),
2550            &jpeg(16, 12),
2551            "image.jpg",
2552            "filehelper",
2553        )
2554        .unwrap();
2555        p.validate().unwrap();
2556        assert_eq!((p.width, p.height), (64, 48));
2557        assert_eq!((p.thumbnail.width, p.thumbnail.height), (16, 12));
2558        assert!(PreparedImageUpload::prepare(
2559            &jpeg(8, 8),
2560            &jpeg(16, 16),
2561            &jpeg(4, 4),
2562            "image.jpg",
2563            "filehelper"
2564        )
2565        .is_err());
2566        assert!(PreparedImageUpload::prepare(
2567            b"not image",
2568            &jpeg(4, 4),
2569            &jpeg(2, 2),
2570            "image.jpg",
2571            "filehelper"
2572        )
2573        .is_err());
2574    }
2575    fn video_mismatch(
2576        descriptor: &MediaDescriptor,
2577        ciphertext: &[u8],
2578        extra: &[(&str, &[u8])],
2579    ) -> VideoRepresentationMismatch {
2580        let error = client()
2581            .download_with_transport(
2582                descriptor,
2583                |_, _| Ok(()),
2584                |_, fields| {
2585                    let mut reply = download_reply(fields, descriptor, ciphertext);
2586                    for (key, value) in extra {
2587                        reply.insert((*key).into(), value.to_vec());
2588                    }
2589                    Ok(reply)
2590                },
2591            )
2592            .err()
2593            .expect("mismatched video must be rejected");
2594        let text = format!("{error:#} {error:?}");
2595        assert!(!text.contains(descriptor.expected_md5.as_deref().unwrap()));
2596        *error
2597            .downcast_ref::<VideoRepresentationMismatch>()
2598            .expect("typed video mismatch")
2599    }
2600    #[test]
2601    fn normal_video_of_another_length_is_rejected_with_comparison_facts_only() {
2602        // Synthetic values: the advertised tuple describes one object while the
2603        // CDN returns a complete, correctly encrypted object of another length.
2604        let (descriptor, _, _) = download_fixture(9_390, MediaQuality::Video);
2605        let delivered = vec![0x6b; 15_272];
2606        let delivered_md5 = hex::encode(md5(&delivered));
2607        let ciphertext = ecb_encrypt(&delivered, &descriptor.aes_key).unwrap();
2608        let bare = video_mismatch(&descriptor, &ciphertext, &[]);
2609        assert_eq!(
2610            bare,
2611            VideoRepresentationMismatch {
2612                advertised_bytes: Some(9_390),
2613                delivered_bytes: 15_272,
2614                advertised_digest_matches: Some(false),
2615                substituted_filetype: None,
2616                istrans: None,
2617                videoformat: None,
2618                rawfilesize: None,
2619                rawfilemd5: None,
2620            }
2621        );
2622        let delivered_size = delivered.len().to_string();
2623        let described = video_mismatch(
2624            &descriptor,
2625            &ciphertext,
2626            &[
2627                ("substituteftype", b"4"),
2628                ("istrans", b"7"),
2629                ("videoformat", b"3"),
2630                ("rawfilesize", delivered_size.as_bytes()),
2631                ("rawfilemd5", delivered_md5.to_uppercase().as_bytes()),
2632            ],
2633        );
2634        assert_eq!(described.substituted_filetype, Some(4));
2635        assert_eq!(
2636            (described.istrans, described.videoformat),
2637            (Some(7), Some(3))
2638        );
2639        let delivered_only = Some(SourceEvidence {
2640            matches_advertised: false,
2641            matches_delivered: true,
2642        });
2643        assert_eq!(described.rawfilesize, delivered_only);
2644        assert_eq!(described.rawfilemd5, delivered_only);
2645        let advertised_size = descriptor.expected_bytes.unwrap().to_string();
2646        let advertised_md5 = descriptor.expected_md5.clone().unwrap();
2647        let source = video_mismatch(
2648            &descriptor,
2649            &ciphertext,
2650            &[
2651                ("rawfilesize", advertised_size.as_bytes()),
2652                ("rawfilemd5", advertised_md5.as_bytes()),
2653            ],
2654        );
2655        let advertised_only = Some(SourceEvidence {
2656            matches_advertised: true,
2657            matches_delivered: false,
2658        });
2659        assert_eq!(source.rawfilesize, advertised_only);
2660        assert_eq!(source.rawfilemd5, advertised_only);
2661        let malformed = video_mismatch(
2662            &descriptor,
2663            &ciphertext,
2664            &[("rawfilesize", b"-1"), ("rawfilemd5", b"not-a-digest")],
2665        );
2666        let neither = Some(SourceEvidence {
2667            matches_advertised: false,
2668            matches_delivered: false,
2669        });
2670        assert_eq!(
2671            (malformed.rawfilesize, malformed.rawfilemd5),
2672            (neither, neither)
2673        );
2674        // A representation that only some chunks label is still refused.
2675        assert!(client()
2676            .download_with_transport(
2677                &descriptor,
2678                |_, _| Ok(()),
2679                |_, fields| {
2680                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2681                    put(&mut reply, "substituteftype", integer(fields, "seq")?);
2682                    Ok(reply)
2683                },
2684            )
2685            .is_err());
2686    }
2687    #[test]
2688    fn normal_video_digest_mismatch_and_valid_metadata_keep_existing_outcomes() {
2689        let (mut descriptor, raw, ciphertext) = download_fixture(9_390, MediaQuality::Video);
2690        // Optional metadata is diagnostic only; malformed values never reject
2691        // a transfer that matches its advertised tuple.
2692        for (key, value) in [
2693            ("rawfilesize", "-1"),
2694            ("rawfilemd5", "not-a-digest"),
2695            ("istrans", "x"),
2696            ("videoformat", "12345678901234567"),
2697        ] {
2698            let downloaded = client()
2699                .download_with_transport(
2700                    &descriptor,
2701                    |_, _| Ok(()),
2702                    |_, fields| {
2703                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2704                        reply.insert(key.into(), value.as_bytes().to_vec());
2705                        Ok(reply)
2706                    },
2707                )
2708                .unwrap();
2709            assert_eq!(downloaded.bytes, raw);
2710            assert_eq!(downloaded.advertised_size_matches, Some(true));
2711        }
2712        descriptor.expected_md5 = Some("0".repeat(32));
2713        let same_size = video_mismatch(&descriptor, &ciphertext, &[]);
2714        assert_eq!(same_size.delivered_bytes, 9_390);
2715        assert_eq!(same_size.advertised_digest_matches, Some(false));
2716        // Other kinds keep their plain messages.
2717        let (mut image, _, image_ciphertext) = download_fixture(9_390, MediaQuality::Original);
2718        image.expected_bytes = Some(9_391);
2719        let error = client()
2720            .download_with_transport(
2721                &image,
2722                |_, _| Ok(()),
2723                |_, fields| Ok(download_reply(fields, &image, &image_ciphertext)),
2724            )
2725            .err()
2726            .expect("download must fail");
2727        assert!(error
2728            .downcast_ref::<VideoRepresentationMismatch>()
2729            .is_none());
2730        assert_eq!(error.to_string(), "Advertised media size mismatch");
2731    }
2732    #[test]
2733    fn video_limits_admit_sizes_above_the_former_50_mib_bound() {
2734        assert_eq!(MAX_VIDEO_DOWNLOAD_BYTES, 536_870_912);
2735        assert_eq!(wechat_media::MAX_VIDEO_SOURCE_BYTES, 536_870_912);
2736        assert_eq!(
2737            upload_plaintext_limit(MediaQuality::Video),
2738            wechat_media::MAX_VIDEO_SOURCE_BYTES
2739        );
2740        // A small advertised video keeps its own bound on an unchecked reply.
2741        assert_eq!(MAX_SMALL_VIDEO_REPLY_BYTES, 52_428_800);
2742        let (mut descriptor, _, _) = download_fixture(16, MediaQuality::Video);
2743        for size in [MAX_SMALL_VIDEO_REPLY_BYTES + 1, MAX_VIDEO_DOWNLOAD_BYTES] {
2744            descriptor.expected_bytes = Some(size as u64);
2745            let mut requests = 0;
2746            let error = client()
2747                .download_with_transport(
2748                    &descriptor,
2749                    |_, _| Ok(()),
2750                    |path, fields| {
2751                        requests += 1;
2752                        assert_eq!(path, "/download");
2753                        assert_eq!(integer(fields, "filetype")?, 4);
2754                        assert_eq!(integer(fields, "rangeend")?, 512 * 1024 - 1);
2755                        bail!("synthetic transport stop")
2756                    },
2757                )
2758                .err()
2759                .expect("download must stop");
2760            assert_eq!(error.to_string(), "synthetic transport stop");
2761            assert_eq!(requests, 1);
2762        }
2763    }
2764}