dodot-lib 5.0.0

Core library for dodot dotfiles manager
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
//! Archive extraction helpers for `type = "archive"` and
//! `type = "archive-file"` externals.
//!
//! Two formats are supported: gzipped tar (the GitHub-release default)
//! and zip. The format is either inferred from the URL filename or
//! declared explicitly in the TOML.

use std::collections::HashMap;
use std::io::{Cursor, Read};
use std::path::PathBuf;

use crate::external::ArchiveFormat;

/// Error category returned by extraction.
#[derive(Debug, thiserror::Error)]
pub enum ArchiveError {
    #[error("archive format could not be inferred from URL {0}; set `format` explicitly")]
    FormatUnknown(String),
    #[error("archive read failed: {0}")]
    Read(String),
    #[error("archive contains unsafe path: {0}")]
    UnsafePath(String),
    #[error("archive does not contain member: {0}")]
    MissingMember(String),
}

/// In-memory representation of a single entry pulled from an archive.
///
/// `path` is the entry path relative to the archive root, validated
/// safe (no absolute paths, no `..` components). `is_dir` is true for
/// directory entries (no body).
#[derive(Debug, Clone)]
pub struct ArchiveEntry {
    pub path: PathBuf,
    pub is_dir: bool,
    pub bytes: Vec<u8>,
    /// Unix permission mode. None for entries that don't carry one
    /// (zip entries without external attrs). Callers default to 0o644
    /// for files and 0o755 for directories.
    pub mode: Option<u32>,
}

/// Extract every entry from an archive into memory, keyed by relative
/// path. Both regular files and directory markers are returned (callers
/// distinguish via [`ArchiveEntry::is_dir`]) so explicit empty
/// directories survive the round-trip.
///
/// Paths are validated: absolute paths and `..` components are
/// rejected with [`ArchiveError::UnsafePath`] so a malicious archive
/// can never escape the datastore subdir. Tar entries that are
/// neither regular files nor directories (symlinks, hardlinks,
/// devices, FIFOs, …) are rejected explicitly — dodot does not yet
/// have a defensible posture for materialising them, so silently
/// turning them into empty files would be a footgun.
pub fn read_all(
    bytes: &[u8],
    format: ArchiveFormat,
) -> Result<HashMap<PathBuf, ArchiveEntry>, ArchiveError> {
    match format {
        ArchiveFormat::TarGz => read_tar_gz(bytes),
        ArchiveFormat::Zip => read_zip(bytes),
    }
}

/// Extract a single named entry from an archive into memory.
///
/// Streams through the archive and returns the first matching entry
/// without buffering the rest — handy for `type = "archive-file"`
/// against multi-GB release tarballs.
pub fn read_member(
    bytes: &[u8],
    format: ArchiveFormat,
    member: &str,
) -> Result<ArchiveEntry, ArchiveError> {
    let target = std::path::Path::new(member);
    match format {
        ArchiveFormat::TarGz => read_tar_gz_one(bytes, target),
        ArchiveFormat::Zip => read_zip_one(bytes, target),
    }
}

fn read_tar_gz(bytes: &[u8]) -> Result<HashMap<PathBuf, ArchiveEntry>, ArchiveError> {
    let gz = flate2::read::GzDecoder::new(Cursor::new(bytes));
    let mut archive = tar::Archive::new(gz);
    let mut out: HashMap<PathBuf, ArchiveEntry> = HashMap::new();
    for entry in archive
        .entries()
        .map_err(|e| ArchiveError::Read(e.to_string()))?
    {
        let entry = entry.map_err(|e| ArchiveError::Read(e.to_string()))?;
        if let Some(parsed) = read_tar_entry(entry)? {
            out.insert(parsed.path.clone(), parsed);
        }
    }
    Ok(out)
}

fn read_tar_gz_one(bytes: &[u8], member: &std::path::Path) -> Result<ArchiveEntry, ArchiveError> {
    let gz = flate2::read::GzDecoder::new(Cursor::new(bytes));
    let mut archive = tar::Archive::new(gz);
    for entry in archive
        .entries()
        .map_err(|e| ArchiveError::Read(e.to_string()))?
    {
        let entry = entry.map_err(|e| ArchiveError::Read(e.to_string()))?;
        if let Some(parsed) = read_tar_entry(entry)? {
            if parsed.path == member {
                return Ok(parsed);
            }
        }
    }
    Err(ArchiveError::MissingMember(member.display().to_string()))
}

/// Parse one tar entry into an [`ArchiveEntry`].
///
/// Returns `Ok(None)` when the entry should be silently skipped (root
/// `./` placeholder). Returns `Err(UnsafePath)` for entries whose
/// type is not supported (symlinks, hardlinks, device nodes, etc.) —
/// we don't have a defensible materialisation story for those and
/// silently dropping the body would mislead callers.
fn read_tar_entry<R: std::io::Read>(
    mut entry: tar::Entry<'_, R>,
) -> Result<Option<ArchiveEntry>, ArchiveError> {
    let raw_path = entry
        .path()
        .map_err(|e| ArchiveError::Read(e.to_string()))?
        .into_owned();
    let safe = match validate_safe_archive_path(&raw_path)? {
        Some(p) => p,
        None => return Ok(None), // pure `./` root placeholder
    };
    let header_entry_type = entry.header().entry_type();
    let mode = entry.header().mode().ok();
    let is_dir = header_entry_type.is_dir();
    let is_regular = header_entry_type.is_file();
    if !is_dir && !is_regular {
        // Symlinks, hardlinks, fifos, char/block devices, sparse, etc.
        // Reject explicitly so the caller can surface a clear error
        // rather than silently materialising an empty file.
        return Err(ArchiveError::UnsafePath(format!(
            "unsupported tar entry type {:?} at {}",
            header_entry_type,
            raw_path.display()
        )));
    }
    let mut buf = Vec::new();
    if !is_dir {
        entry
            .read_to_end(&mut buf)
            .map_err(|e| ArchiveError::Read(e.to_string()))?;
    }
    Ok(Some(ArchiveEntry {
        path: safe,
        is_dir,
        bytes: buf,
        mode,
    }))
}

fn read_zip(bytes: &[u8]) -> Result<HashMap<PathBuf, ArchiveEntry>, ArchiveError> {
    let mut archive =
        zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| ArchiveError::Read(e.to_string()))?;
    let mut out: HashMap<PathBuf, ArchiveEntry> = HashMap::new();
    for i in 0..archive.len() {
        if let Some(parsed) = read_zip_index(&mut archive, i)? {
            out.insert(parsed.path.clone(), parsed);
        }
    }
    Ok(out)
}

fn read_zip_one(bytes: &[u8], member: &std::path::Path) -> Result<ArchiveEntry, ArchiveError> {
    let mut archive =
        zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| ArchiveError::Read(e.to_string()))?;
    for i in 0..archive.len() {
        if let Some(parsed) = read_zip_index(&mut archive, i)? {
            if parsed.path == member {
                return Ok(parsed);
            }
        }
    }
    Err(ArchiveError::MissingMember(member.display().to_string()))
}

fn read_zip_index(
    archive: &mut zip::ZipArchive<Cursor<&[u8]>>,
    index: usize,
) -> Result<Option<ArchiveEntry>, ArchiveError> {
    let mut file = archive
        .by_index(index)
        .map_err(|e| ArchiveError::Read(e.to_string()))?;
    // ZipArchive's `enclosed_name()` rejects paths that would escape
    // the extraction root (absolute, `..` segments). It returns
    // `None` in those cases — we treat that as an unsafe entry
    // rather than silently dropping.
    let raw_path = file
        .enclosed_name()
        .ok_or_else(|| ArchiveError::UnsafePath(file.name().to_string()))?;
    let safe = match validate_safe_archive_path(&raw_path)? {
        Some(p) => p,
        None => return Ok(None),
    };
    let is_dir = file.is_dir();
    // `unix_mode()` returns the full st_mode with file-type bits;
    // mask to the permission portion so callers see a familiar
    // 0o755 / 0o644 rather than 0o100644.
    let mode = file.unix_mode().map(|m| m & 0o7777);
    let mut buf = Vec::new();
    if !is_dir {
        file.read_to_end(&mut buf)
            .map_err(|e| ArchiveError::Read(e.to_string()))?;
    }
    Ok(Some(ArchiveEntry {
        path: safe,
        is_dir,
        bytes: buf,
        mode,
    }))
}

/// Validate an archive-entry path against the rules that keep
/// extraction inside the datastore subdir.
///
/// - `Ok(Some(path))` — safe relative path, materialise this entry.
/// - `Ok(None)` — entry resolves to the archive root (e.g. a bare
///   `./` placeholder). Skip it silently; tarballs produced by GNU
///   `tar` often start with such a row.
/// - `Err(UnsafePath)` — the entry has an absolute path, a `..`
///   segment, or a Windows-style prefix. Refuse the whole archive.
fn validate_safe_archive_path(raw: &std::path::Path) -> Result<Option<PathBuf>, ArchiveError> {
    use std::path::Component;
    let mut cleaned = PathBuf::new();
    for component in raw.components() {
        match component {
            Component::Normal(n) => cleaned.push(n),
            // Skip pure `./` segments rather than fail (tar archives
            // produced by `tar` itself frequently start with `./`).
            Component::CurDir => {}
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                return Err(ArchiveError::UnsafePath(raw.display().to_string()));
            }
        }
    }
    if cleaned.as_os_str().is_empty() {
        Ok(None)
    } else {
        Ok(Some(cleaned))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::write::GzEncoder;
    use flate2::Compression;
    use std::io::Write;

    /// Build a tiny tar.gz with two entries for tests.
    fn fake_tar_gz() -> Vec<u8> {
        let mut tar_buf: Vec<u8> = Vec::new();
        {
            let mut builder = tar::Builder::new(&mut tar_buf);
            // Plain file.
            let mut header = tar::Header::new_gnu();
            let body = b"hello tar\n";
            header.set_path("themes/alpha.zsh-theme").unwrap();
            header.set_size(body.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append(&header, &body[..]).unwrap();

            // Nested file.
            let mut header = tar::Header::new_gnu();
            let body = b"#!/bin/sh\necho hi\n";
            header.set_path("themes/scripts/setup.sh").unwrap();
            header.set_size(body.len() as u64);
            header.set_mode(0o755);
            header.set_cksum();
            builder.append(&header, &body[..]).unwrap();
            builder.finish().unwrap();
        }
        let mut gz = GzEncoder::new(Vec::new(), Compression::default());
        gz.write_all(&tar_buf).unwrap();
        gz.finish().unwrap()
    }

    /// Build a tiny zip with one file for tests.
    fn fake_zip() -> Vec<u8> {
        let mut buf: Vec<u8> = Vec::new();
        {
            let mut writer = zip::ZipWriter::new(Cursor::new(&mut buf));
            let opts: zip::write::FileOptions<'_, ()> = zip::write::FileOptions::default()
                .compression_method(zip::CompressionMethod::Deflated)
                .unix_permissions(0o644);
            writer.start_file("hello.txt", opts).unwrap();
            writer.write_all(b"zipped hello").unwrap();
            writer.finish().unwrap();
        }
        buf
    }

    #[test]
    fn read_all_tar_gz_returns_entries() {
        let entries = read_all(&fake_tar_gz(), ArchiveFormat::TarGz).unwrap();
        assert!(entries.contains_key(&PathBuf::from("themes/alpha.zsh-theme")));
        assert!(entries.contains_key(&PathBuf::from("themes/scripts/setup.sh")));
        let e = entries
            .get(&PathBuf::from("themes/scripts/setup.sh"))
            .unwrap();
        assert_eq!(e.bytes, b"#!/bin/sh\necho hi\n");
        assert_eq!(e.mode, Some(0o755));
    }

    #[test]
    fn read_member_tar_gz_finds_named_entry() {
        let e = read_member(
            &fake_tar_gz(),
            ArchiveFormat::TarGz,
            "themes/alpha.zsh-theme",
        )
        .unwrap();
        assert_eq!(e.bytes, b"hello tar\n");
    }

    #[test]
    fn read_member_tar_gz_missing_errors_clearly() {
        let err = read_member(&fake_tar_gz(), ArchiveFormat::TarGz, "no/such.txt").unwrap_err();
        assert!(matches!(err, ArchiveError::MissingMember(_)), "{err:?}");
    }

    #[test]
    fn read_all_zip_returns_entries() {
        let entries = read_all(&fake_zip(), ArchiveFormat::Zip).unwrap();
        let e = entries.get(&PathBuf::from("hello.txt")).unwrap();
        assert_eq!(e.bytes, b"zipped hello");
        assert_eq!(e.mode, Some(0o644));
    }

    #[test]
    fn unsafe_paths_rejected() {
        // The high-level `tar::Builder` refuses to write `..` paths,
        // and `zip::ZipArchive::enclosed_name` filters them out on
        // read — so directly exercise our validator on a synthetic
        // path that would otherwise reach an extraction site.
        let cases = ["../escape", "subdir/../../escape", "/absolute/escape"];
        for p in cases {
            let err = validate_safe_archive_path(std::path::Path::new(p)).unwrap_err();
            assert!(
                matches!(err, ArchiveError::UnsafePath(_)),
                "expected UnsafePath for {p:?}, got {err:?}"
            );
        }
    }

    #[test]
    fn root_placeholder_returns_none_not_err() {
        // A bare `./` entry (common in tarballs produced by GNU tar)
        // resolves to an empty cleaned path. The validator returns
        // `Ok(None)` so the reader can skip it silently — earlier
        // behaviour was `Err(UnsafePath)`, which would have failed
        // every otherwise-valid tarball with a root placeholder.
        let result = validate_safe_archive_path(std::path::Path::new("./")).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn tar_symlink_entries_are_rejected() {
        // Build a tar that contains a symlink entry — should be
        // refused so we don't silently extract an empty file.
        let mut tar_buf: Vec<u8> = Vec::new();
        {
            let mut builder = tar::Builder::new(&mut tar_buf);
            let mut header = tar::Header::new_gnu();
            header.set_path("link").unwrap();
            header.set_size(0);
            header.set_mode(0o644);
            header.set_entry_type(tar::EntryType::Symlink);
            header.set_link_name("target").unwrap();
            header.set_cksum();
            builder.append(&header, std::io::empty()).unwrap();
            builder.finish().unwrap();
        }
        let mut gz = GzEncoder::new(Vec::new(), Compression::default());
        gz.write_all(&tar_buf).unwrap();
        let bytes = gz.finish().unwrap();
        let err = read_all(&bytes, ArchiveFormat::TarGz).unwrap_err();
        assert!(
            matches!(err, ArchiveError::UnsafePath(ref m) if m.contains("Symlink")),
            "expected UnsafePath about Symlink, got {err:?}"
        );
    }

    #[test]
    fn infer_format_from_url() {
        assert_eq!(
            ArchiveFormat::infer_from_url("https://x/foo.tar.gz"),
            Some(ArchiveFormat::TarGz)
        );
        assert_eq!(
            ArchiveFormat::infer_from_url("https://x/foo.tgz?v=1"),
            Some(ArchiveFormat::TarGz)
        );
        assert_eq!(
            ArchiveFormat::infer_from_url("https://x/Foo.ZIP"),
            Some(ArchiveFormat::Zip)
        );
        assert_eq!(ArchiveFormat::infer_from_url("https://x/foo.unknown"), None);
    }
}