Skip to main content

wechat_protocol/
media.rs

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