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