Skip to main content

wechat_protocol/
media.rs

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