rust-mirror 0.1.0

Prepare pinned Rust toolchain snapshots for offline static hosting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Pinned toolchain snapshots. Upstream manifests are preserved, not rewritten.
pub mod publish;
mod resume;
pub mod selection;
pub use resume::Transfer;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    error::Error,
    fs::{self, File},
    io::{self, Read, Write},
    path::{Path, PathBuf},
    time::Duration,
};

pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
const ORIGIN: &str = "https://static.rust-lang.org/";
const METADATA_LIMIT: u64 = 4 * 1024 * 1024;
const ARTIFACT_LIMIT: u64 = 512 * 1024 * 1024;

#[derive(Debug)]
pub struct Options {
    pub release: String,
    pub host: String,
    pub rustup_version: String,
    /// Must not already exist. The completed tree is returned as `<output>/mirror`.
    pub output: PathBuf,
}

/// Transport boundary shared by the CLI and deterministic tests. Implementations must
/// refuse oversized responses. Sources are required to honor the supplied URL exactly.
pub trait Source {
    fn fetch(&self, url: &str, output: &mut dyn Write, limit: u64) -> Result<()>;
    /// Append bytes beginning at offset, within the remaining byte budget. Restart must
    /// write nothing. Errors may leave a prefix; only a final whole-file hash proves it.
    /// Sources without range support safely restart that file.
    fn fetch_from(
        &self,
        url: &str,
        output: &mut dyn Write,
        offset: u64,
        limit: u64,
    ) -> Result<Transfer> {
        if offset != 0 {
            return Ok(Transfer::Restart);
        }
        self.fetch(url, output, limit)?;
        Ok(Transfer::Complete)
    }
}

pub struct HttpsSource(ureq::Agent);
impl Default for HttpsSource {
    fn default() -> Self {
        Self(
            ureq::AgentBuilder::new()
                .timeout(Duration::from_secs(180))
                .redirects(0)
                .build(),
        )
    }
}
impl Source for HttpsSource {
    fn fetch_from(
        &self,
        url: &str,
        output: &mut dyn Write,
        offset: u64,
        limit: u64,
    ) -> Result<Transfer> {
        safe_path(
            url.strip_prefix(ORIGIN)
                .ok_or("source must be the official HTTPS origin")?,
        )?;
        resume::http_range(&self.0, url, output, offset, limit)
    }
    fn fetch(&self, url: &str, output: &mut dyn Write, limit: u64) -> Result<()> {
        let path = url
            .strip_prefix(ORIGIN)
            .ok_or("source must be the official HTTPS origin")?;
        safe_path(path)?;
        let _ = writeln!(io::stderr().lock(), "Downloading {path}");
        let response = self.0.get(url).call()?;
        if response.status() != 200 {
            return Err("source did not return HTTP 200".into());
        }
        copy_bounded(response.into_reader(), output, limit)
    }
}
fn copy_bounded(input: impl Read, output: &mut dyn Write, limit: u64) -> Result<()> {
    let count = io::copy(&mut input.take(limit + 1), output)?;
    if count > limit {
        return Err("download exceeds size budget".into());
    }
    Ok(())
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Snapshot {
    pub schema: u32,
    pub release: String,
    pub host: String,
    pub rustup_version: String,
    pub profile: String,
    pub files: Vec<SnapshotFile>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SnapshotFile {
    pub path: String,
    pub sha256: String,
    pub bytes: u64,
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Manifest {
    manifest_version: String,
    date: String,
    pkg: BTreeMap<String, Package>,
    profiles: BTreeMap<String, Vec<String>>,
    #[serde(default)]
    renames: BTreeMap<String, Renamed>,
}
#[derive(Deserialize)]
struct Renamed {
    to: String,
}
#[derive(Deserialize)]
struct Package {
    version: String,
    target: BTreeMap<String, Target>,
}
#[derive(Deserialize)]
struct Target {
    available: bool,
    url: Option<String>,
    hash: Option<String>,
    xz_url: Option<String>,
    xz_hash: Option<String>,
    zst_url: Option<String>,
    zst_hash: Option<String>,
    #[serde(default)]
    components: Vec<Component>,
    #[serde(default)]
    extensions: Vec<Component>,
}
#[derive(Deserialize)]
struct Component {
    pkg: String,
    target: String,
}

fn version(value: &str) -> Result<()> {
    let parts: Vec<_> = value.split('.').collect();
    if parts.len() != 3
        || parts
            .iter()
            .any(|p| p.is_empty() || p.len() > 5 || !p.bytes().all(|c| c.is_ascii_digit()))
    {
        return Err("expected a pinned numeric version, such as 1.90.0".into());
    }
    Ok(())
}
fn safe_path(path: &str) -> Result<()> {
    if path.is_empty()
        || path.len() > 512
        || path
            .split('/')
            .any(|p| p.is_empty() || p == "." || p == "..")
        || !path
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || b"-._/".contains(&c))
    {
        return Err("unsafe mirror-relative path".into());
    }
    Ok(())
}
fn valid_hash(value: &str) -> Result<()> {
    if value.len() != 64 || !value.bytes().all(|c| c.is_ascii_hexdigit()) {
        return Err("invalid SHA-256".into());
    }
    Ok(())
}
fn checksum(bytes: &[u8]) -> Result<String> {
    let text = std::str::from_utf8(bytes)?;
    let hash = text
        .split_whitespace()
        .next()
        .ok_or("empty checksum file")?;
    valid_hash(hash)?;
    Ok(hash.to_ascii_lowercase())
}
fn bytes(source: &impl Source, path: &str, limit: u64) -> Result<Vec<u8>> {
    safe_path(path)?;
    let mut output = Vec::new();
    source.fetch(&format!("{ORIGIN}{path}"), &mut output, limit)?;
    if output.len() as u64 > limit {
        return Err("source exceeded byte budget".into());
    }
    Ok(output)
}
fn sha256(bytes: &[u8]) -> String {
    hex::encode(Sha256::digest(bytes))
}

fn plan(raw: &[u8], options: &Options) -> Result<Vec<(String, String)>> {
    let m: Manifest = toml::from_str(std::str::from_utf8(raw)?)?;
    if m.manifest_version != "2" {
        return Err("unsupported upstream manifest schema".into());
    }
    let date = m.date.as_bytes();
    if date.len() != 10
        || date[4] != b'-'
        || date[7] != b'-'
        || date
            .iter()
            .enumerate()
            .any(|(i, c)| i != 4 && i != 7 && !c.is_ascii_digit())
    {
        return Err("invalid manifest date".into());
    }
    let rust = m.pkg.get("rust").ok_or("missing rust metapackage")?;
    if rust.version.split_whitespace().next() != Some(options.release.as_str()) {
        return Err("manifest release does not match request".into());
    }
    let host = rust
        .target
        .get(&options.host)
        .filter(|h| h.available)
        .ok_or("host unavailable")?;
    let profile = m.profiles.get("minimal").ok_or("missing minimal profile")?;
    let mut artifacts = BTreeMap::new();
    let mut selected = Vec::new();
    for component in host
        .components
        .iter()
        .chain(&host.extensions)
        .filter(|c| profile.contains(&c.pkg) && (c.target == options.host || c.target == "*"))
    {
        let package = m
            .pkg
            .get(&component.pkg)
            .ok_or("dangling profile package")?;
        let target = package
            .target
            .get(&component.target)
            .or_else(|| package.target.get("*"))
            .ok_or("dangling component target")?;
        if !target.available {
            return Err("selected component unavailable".into());
        }
        let (url, hash) = if let Some(url) = &target.zst_url {
            (url, target.zst_hash.as_ref())
        } else if let Some(url) = &target.xz_url {
            (url, target.xz_hash.as_ref())
        } else {
            (
                target.url.as_ref().ok_or("component has no archive URL")?,
                target.hash.as_ref(),
            )
        };
        let hash = hash.ok_or("archive URL has no matching checksum")?;
        valid_hash(hash)?;
        let path = url.strip_prefix(ORIGIN).ok_or("foreign artifact origin")?;
        safe_path(path)?;
        if !path.starts_with(&format!("dist/{}/", m.date)) {
            return Err("artifact outside selected release directory".into());
        }
        if let Some(previous) = artifacts.insert(path.to_string(), hash.to_ascii_lowercase()) {
            if previous != hash.to_ascii_lowercase() {
                return Err("conflicting artifact checksums".into());
            }
        }
        selected.push(component.pkg.as_str());
    }
    for required in ["rustc", "cargo", "rust-std"] {
        if !selected.contains(&required) {
            return Err(format!("minimal profile is missing {required}").into());
        }
    }
    if artifacts.len() > 16 {
        return Err("minimal profile exceeds artifact budget".into());
    }
    Ok(artifacts.into_iter().collect())
}

struct HashWriter<'a> {
    file: &'a mut File,
    hash: Sha256,
    bytes: u64,
}
impl Write for HashWriter<'_> {
    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        let count = self.file.write(data)?;
        self.hash.update(&data[..count]);
        self.bytes += count as u64;
        Ok(count)
    }
    fn flush(&mut self) -> io::Result<()> {
        self.file.flush()
    }
}
fn save(root: &Path, path: &str, data: &[u8], files: &mut Vec<SnapshotFile>) -> Result<()> {
    safe_path(path)?;
    let destination = root.join(path);
    fs::create_dir_all(destination.parent().ok_or("missing parent")?)?;
    let mut file = File::options()
        .create_new(true)
        .write(true)
        .open(&destination)?;
    file.write_all(data)?;
    file.sync_all()?;
    files.push(SnapshotFile {
        path: path.into(),
        sha256: sha256(data),
        bytes: data.len() as u64,
    });
    Ok(())
}
fn download(
    root: &Path,
    source: &impl Source,
    path: &str,
    expected: &str,
    files: &mut Vec<SnapshotFile>,
) -> Result<()> {
    safe_path(path)?;
    valid_hash(expected)?;
    let destination = root.join(path);
    fs::create_dir_all(destination.parent().ok_or("missing parent")?)?;
    let mut file = File::options()
        .create_new(true)
        .write(true)
        .open(&destination)?;
    let mut writer = HashWriter {
        file: &mut file,
        hash: Sha256::new(),
        bytes: 0,
    };
    source.fetch(&format!("{ORIGIN}{path}"), &mut writer, ARTIFACT_LIMIT)?;
    let count = writer.bytes;
    let actual = hex::encode(writer.hash.finalize());
    if count > ARTIFACT_LIMIT || actual != expected.to_ascii_lowercase() {
        return Err(format!("size/checksum verification failed: {path}").into());
    }
    file.sync_all()?;
    files.push(SnapshotFile {
        path: path.into(),
        sha256: actual,
        bytes: count,
    });
    Ok(())
}

/// Prepare an immutable, minimal-profile static tree. Does not install a toolchain,
/// upload to a server, modify upstream manifests, or claim signature authentication.
/// An existing output is always refused. Failed attempts may leave the reserved
/// output directory; they never produce a completed `mirror` tree.
pub fn prepare(options: &Options, source: &impl Source) -> Result<PathBuf> {
    version(&options.release)?;
    version(&options.rustup_version)?;
    // The first milestone is deliberately narrow; other hosts need their own proof.
    if options.host != "x86_64-unknown-linux-gnu" {
        return Err("this milestone supports only x86_64-unknown-linux-gnu".into());
    }
    if options.output.try_exists()? {
        return Err("output already exists; choose a new snapshot directory".into());
    }
    let manifest_path = format!("dist/channel-rust-{}.toml", options.release);
    let upstream_hash = bytes(source, &format!("{manifest_path}.sha256"), 4096)?;
    let raw = bytes(source, &manifest_path, METADATA_LIMIT)?;
    if sha256(&raw) != checksum(&upstream_hash)? {
        return Err("upstream manifest checksum mismatch".into());
    }
    let artifacts = plan(&raw, options)?;
    let signature = bytes(source, &format!("{manifest_path}.asc"), 64 * 1024)?;
    let bootstrap = format!(
        "rustup/archive/{}/{}/rustup-init",
        options.rustup_version, options.host
    );
    let bootstrap_hash = bytes(source, &format!("{bootstrap}.sha256"), 4096)?;
    let expected_bootstrap = checksum(&bootstrap_hash)?;
    fs::create_dir(&options.output)?;
    let staging = tempfile::Builder::new()
        .prefix(".building-")
        .tempdir_in(&options.output)?;
    let mut files = Vec::new();
    for (path, hash) in artifacts {
        download(staging.path(), source, &path, &hash, &mut files)?;
    }
    download(
        staging.path(),
        source,
        &bootstrap,
        &expected_bootstrap,
        &mut files,
    )?;
    save(
        staging.path(),
        &format!("{bootstrap}.sha256"),
        &bootstrap_hash,
        &mut files,
    )?;
    save(staging.path(), &manifest_path, &raw, &mut files)?;
    save(
        staging.path(),
        &format!("{manifest_path}.sha256"),
        &upstream_hash,
        &mut files,
    )?;
    save(
        staging.path(),
        &format!("{manifest_path}.asc"),
        &signature,
        &mut files,
    )?;
    files.sort_by(|a, b| a.path.cmp(&b.path));
    let snapshot = Snapshot {
        schema: 1,
        release: options.release.clone(),
        host: options.host.clone(),
        rustup_version: options.rustup_version.clone(),
        profile: "minimal".into(),
        files,
    };
    let inventory = toml::to_string_pretty(&snapshot)?;
    let mut file = File::options()
        .create_new(true)
        .write(true)
        .open(staging.path().join("snapshot.toml"))?;
    file.write_all(inventory.as_bytes())?;
    file.sync_all()?;
    drop(file); // Windows requires closing the inventory before renaming its directory.
    let completed = options.output.join("mirror");
    // `output` is an exclusively-created reservation, not a shared cache or live tree.
    if completed.try_exists()? {
        return Err("completed tree unexpectedly exists".into());
    }
    fs::rename(staging.path(), &completed)?;
    Ok(completed)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn paths_reject_traversal_origins_and_encoding() {
        for value in [
            "../file",
            "/file",
            "a/../b",
            "a\\b",
            "a//b",
            "a/%2e%2e/b",
            "a?query",
            "a:stream",
        ] {
            assert!(safe_path(value).is_err(), "{value}");
        }
        assert!(safe_path("dist/2025-09-18/rustc-1.90.0.tar.xz").is_ok());
    }
    #[test]
    fn versions_are_pinned_and_bounded() {
        for value in [
            "stable",
            "nightly",
            "../1.90.0",
            "1.90",
            "1.90.0-beta",
            "123456.0.0",
        ] {
            assert!(version(value).is_err());
        }
        assert!(version("1.90.0").is_ok());
    }
    #[test]
    fn bounded_download_refuses_excess() {
        let mut output = Vec::new();
        assert!(copy_bounded(&b"12345"[..], &mut output, 4).is_err());
        assert!(copy_bounded(&b"1234"[..], &mut Vec::new(), 4).is_ok());
    }
    #[test]
    fn checksum_requires_full_hex() {
        assert!(checksum(b"deadbeef  file").is_err());
        assert_eq!(
            checksum(format!("{}  file\n", "A".repeat(64)).as_bytes()).unwrap(),
            "a".repeat(64)
        );
    }
}