croft-software 0.1.758

VSCode-style TUI written in Rust
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
//! Archive browser core (#179): list the members of a zip / jar / whl /
//! tar / tar.gz archive without touching payloads, and extract ONE
//! member on demand into a scratch directory so the standard open
//! dispatch (text, image, PDF, sheet, hex) can render it. Extraction is
//! containment-checked: a member whose path escapes the target
//! directory (zip-slip) is refused.

use std::path::{Path, PathBuf};

/// Per-member extraction cap: a member is opened through the normal
/// in-memory/editor paths, so an unbounded entry could balloon.
pub const MEMBER_CAP: u64 = 100 * 1024 * 1024;

/// Listing cap for tar-family archives (#198 review): walking tar
/// headers must read THROUGH every payload (and a GzDecoder is not
/// seekable), so listing cost is proportional to the archive - unlike
/// zip's central directory. Larger files fall to the hex viewer.
pub const TAR_LIST_CAP: u64 = 100 * 1024 * 1024;

/// A reader that fails once more than `remaining` bytes have been
/// pulled through it (#198 review): the compressed size of a tar.gz
/// says nothing about the DECODED bytes a listing walk must read.
struct CappedRead<R> {
    inner: R,
    remaining: u64,
}

impl<R: std::io::Read> std::io::Read for CappedRead<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.remaining == 0 {
            return Err(std::io::Error::other(
                "archive expands past the listing cap",
            ));
        }
        let take = buf
            .len()
            .min(self.remaining.min(usize::MAX as u64) as usize);
        let n = self.inner.read(&mut buf[..take])?;
        self.remaining -= n as u64;
        Ok(n)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArchiveKind {
    Zip,
    Tar,
    TarGz,
}

impl ArchiveKind {
    pub fn label(self) -> &'static str {
        match self {
            ArchiveKind::Zip => "ZIP",
            ArchiveKind::Tar => "TAR",
            ArchiveKind::TarGz => "TAR.GZ",
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveEntry {
    /// Member path as stored (forward slashes).
    pub path: String,
    pub size: u64,
    pub dir: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveView {
    pub kind: ArchiveKind,
    pub source_byte_size: u64,
    pub entries: Vec<ArchiveEntry>,
    pub selected: usize,
    pub scroll: usize,
    /// Frame truth for mouse hit-testing: first visible row's screen y
    /// and the painted row count.
    pub rows_top: u16,
    pub rows_visible: u16,
}

pub fn kind_from_ext(path: &Path) -> Option<ArchiveKind> {
    let name = path.file_name()?.to_str()?.to_ascii_lowercase();
    if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
        return Some(ArchiveKind::TarGz);
    }
    if name.ends_with(".tar") {
        return Some(ArchiveKind::Tar);
    }
    if name.ends_with(".zip") || name.ends_with(".jar") || name.ends_with(".whl") {
        return Some(ArchiveKind::Zip);
    }
    None
}

/// List members without reading payloads: the zip central directory, or
/// one pass over tar headers (gz inflates header-by-header, still no
/// payload retention).
pub fn list(path: &Path, kind: ArchiveKind) -> std::io::Result<ArchiveView> {
    let meta = std::fs::metadata(path)?;
    let mut entries: Vec<ArchiveEntry> = Vec::new();
    match kind {
        ArchiveKind::Zip => {
            let f = std::fs::File::open(path)?;
            let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
            for i in 0..z.len() {
                let e = z.by_index_raw(i).map_err(std::io::Error::other)?;
                entries.push(ArchiveEntry {
                    path: e.name().to_string(),
                    size: e.size(),
                    dir: e.is_dir(),
                });
            }
        }
        ArchiveKind::Tar | ArchiveKind::TarGz => {
            if meta.len() > TAR_LIST_CAP {
                return Err(std::io::Error::other(format!(
                    "tar archive too large to list ({} bytes)",
                    meta.len()
                )));
            }
            let f = std::fs::File::open(path)?;
            let read: Box<dyn std::io::Read> = if kind == ArchiveKind::TarGz {
                // Cap the DECODED bytes too: a small .tar.gz can expand
                // far past the source-size gate above (#198 review).
                Box::new(CappedRead {
                    inner: flate2::read::GzDecoder::new(f),
                    remaining: TAR_LIST_CAP,
                })
            } else {
                Box::new(f)
            };
            let mut a = tar::Archive::new(read);
            for entry in a.entries()? {
                let e = entry?;
                let hdr = e.header();
                entries.push(ArchiveEntry {
                    path: e.path()?.to_string_lossy().into_owned(),
                    size: hdr.size().unwrap_or(0),
                    dir: hdr.entry_type().is_dir(),
                });
            }
        }
    }
    entries.sort_by(|a, b| a.path.cmp(&b.path));
    Ok(ArchiveView {
        kind,
        source_byte_size: meta.len(),
        entries,
        selected: 0,
        scroll: 0,
        rows_top: 0,
        rows_visible: 0,
    })
}

/// Extract one member into `dest_dir` (created on demand), returning
/// the extracted file's path. Containment-checked: the joined path must
/// stay inside `dest_dir` after component normalisation, so `../` and
/// absolute member names are refused, never written.
pub fn extract_member(
    path: &Path,
    kind: ArchiveKind,
    member: &str,
    dest_dir: &Path,
) -> std::io::Result<PathBuf> {
    let dest = contained_join(dest_dir, member)
        .ok_or_else(|| std::io::Error::other(format!("refusing unsafe member path: {member}")))?;
    std::fs::create_dir_all(dest_dir)?;
    prepare_no_follow(dest_dir, &dest)?;
    match kind {
        ArchiveKind::Zip => {
            let f = std::fs::File::open(path)?;
            let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
            let mut e = z.by_name(member).map_err(std::io::Error::other)?;
            if e.size() > MEMBER_CAP {
                return Err(std::io::Error::other(format!(
                    "member too large ({} bytes)",
                    e.size()
                )));
            }
            write_out(&dest, &mut e)?;
        }
        ArchiveKind::Tar | ArchiveKind::TarGz => {
            let f = std::fs::File::open(path)?;
            let read: Box<dyn std::io::Read> = if kind == ArchiveKind::TarGz {
                Box::new(flate2::read::GzDecoder::new(f))
            } else {
                Box::new(f)
            };
            let mut a = tar::Archive::new(read);
            for entry in a.entries()? {
                let mut e = entry?;
                if e.path()?.to_string_lossy() == member {
                    if e.header().size().unwrap_or(0) > MEMBER_CAP {
                        return Err(std::io::Error::other("member too large"));
                    }
                    write_out(&dest, &mut e)?;
                    return Ok(dest);
                }
            }
            return Err(std::io::Error::other(format!("no such member: {member}")));
        }
    }
    Ok(dest)
}

fn write_out(dest: &Path, read: &mut dyn std::io::Read) -> std::io::Result<()> {
    write_out_capped(dest, read, MEMBER_CAP)
}

/// Create the intermediate directories of `dest` below `root`,
/// REFUSING any component that exists as a symlink (#198 review:
/// create_dir_all + File::create follow existing links, so a planted
/// `dest/nested -> /elsewhere` redirected the write outside the root).
/// The leaf is created with create_new after removing only a REGULAR
/// file, so an existing symlink leaf is refused too.
fn prepare_no_follow(root: &Path, dest: &Path) -> std::io::Result<()> {
    let rel = dest.strip_prefix(root).map_err(std::io::Error::other)?;
    let mut cur = root.to_path_buf();
    let parts: Vec<_> = rel.components().collect();
    for c in &parts[..parts.len().saturating_sub(1)] {
        cur.push(c);
        match std::fs::symlink_metadata(&cur) {
            Ok(m) if m.file_type().is_symlink() => {
                return Err(std::io::Error::other(format!(
                    "refusing to traverse a symlink inside the extraction root: {}",
                    cur.display()
                )));
            }
            Ok(m) if m.is_dir() => {}
            Ok(_) => {
                return Err(std::io::Error::other(format!(
                    "a member path component is a file: {}",
                    cur.display()
                )));
            }
            Err(_) => std::fs::create_dir(&cur)?,
        }
    }
    match std::fs::symlink_metadata(dest) {
        Ok(m) if m.file_type().is_symlink() => Err(std::io::Error::other(
            "refusing to overwrite a symlink at the extraction target",
        )),
        Ok(m) if m.is_file() => {
            std::fs::remove_file(dest)?;
            Ok(())
        }
        Ok(_) => Err(std::io::Error::other("extraction target is a directory")),
        Err(_) => Ok(()),
    }
}

/// Copy at most `cap` bytes; a stream that keeps going past it (a
/// member whose DECLARED size lied - deflate does not bound its output
/// to the metadata, #198 review) fails and the partial file is removed.
fn write_out_capped(dest: &Path, read: &mut dyn std::io::Read, cap: u64) -> std::io::Result<()> {
    let mut out = std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(dest)?;
    let mut limited = std::io::Read::take(read, cap + 1);
    let copied = std::io::copy(&mut limited, &mut out)?;
    if copied > cap {
        drop(out);
        let _ = std::fs::remove_file(dest);
        return Err(std::io::Error::other(
            "member stream exceeded the size cap; partial output removed",
        ));
    }
    Ok(())
}

/// Join `member` under `dir`, refusing absolute paths, drive prefixes,
/// and any `..` traversal. Pure lexical containment: nothing is written
/// while deciding.
pub fn contained_join(dir: &Path, member: &str) -> Option<PathBuf> {
    use std::path::Component;
    let rel = Path::new(member);
    let mut out = dir.to_path_buf();
    let mut depth = 0usize;
    for c in rel.components() {
        match c {
            Component::Normal(seg) => {
                out.push(seg);
                depth += 1;
            }
            Component::CurDir => {}
            Component::ParentDir => {
                if depth == 0 {
                    return None;
                }
                out.pop();
                depth -= 1;
            }
            Component::RootDir | Component::Prefix(_) => return None,
        }
    }
    (depth > 0).then_some(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write as _;

    fn make_zip(p: &Path) {
        let f = std::fs::File::create(p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let opts = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.add_directory("docs/", opts).unwrap();
        z.start_file("docs/readme.txt", opts).unwrap();
        z.write_all(b"hello from zip").unwrap();
        z.start_file("evil.txt", opts).unwrap();
        z.write_all(b"ok").unwrap();
        z.finish().unwrap();
    }

    fn make_tar_gz(p: &Path) {
        let f = std::fs::File::create(p).unwrap();
        let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
        let mut t = tar::Builder::new(gz);
        let data = b"tar payload";
        let mut hdr = tar::Header::new_gnu();
        hdr.set_size(data.len() as u64);
        hdr.set_mode(0o644);
        hdr.set_cksum();
        t.append_data(&mut hdr, "inner/file.bin", &data[..])
            .unwrap();
        t.into_inner().unwrap().finish().unwrap();
    }

    #[test]
    fn lists_zip_and_targz_members_sorted_without_payload_reads() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let v = list(&zp, ArchiveKind::Zip).unwrap();
        let names: Vec<&str> = v.entries.iter().map(|e| e.path.as_str()).collect();
        assert_eq!(names, vec!["docs/", "docs/readme.txt", "evil.txt"]);
        assert!(v.entries[0].dir);
        assert_eq!(v.entries[1].size, 14);

        let tp = tmp.path().join("a.tar.gz");
        make_tar_gz(&tp);
        let v = list(&tp, ArchiveKind::TarGz).unwrap();
        assert_eq!(v.entries.len(), 1);
        assert_eq!(v.entries[0].path, "inner/file.bin");
        assert_eq!(v.entries[0].size, 11);
    }

    #[test]
    fn extracts_a_member_and_refuses_traversal() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let dest = tmp.path().join("out");
        let got = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap();
        assert_eq!(std::fs::read_to_string(&got).unwrap(), "hello from zip");
        assert!(got.starts_with(&dest));

        let tp = tmp.path().join("a.tar.gz");
        make_tar_gz(&tp);
        let got = extract_member(&tp, ArchiveKind::TarGz, "inner/file.bin", &dest).unwrap();
        assert_eq!(std::fs::read(&got).unwrap(), b"tar payload");

        assert!(extract_member(&zp, ArchiveKind::Zip, "../escape.txt", &dest).is_err());
        assert!(extract_member(&zp, ArchiveKind::Zip, "/abs.txt", &dest).is_err());
        assert!(!dest.join("..").join("escape.txt").exists());
    }

    #[test]
    fn contained_join_is_strictly_lexical() {
        let d = Path::new("/safe/dir");
        assert_eq!(
            contained_join(d, "a/b.txt").unwrap(),
            Path::new("/safe/dir/a/b.txt")
        );
        assert_eq!(
            contained_join(d, "a/../b.txt").unwrap(),
            Path::new("/safe/dir/b.txt")
        );
        assert!(contained_join(d, "../evil").is_none());
        assert!(contained_join(d, "a/../../evil").is_none());
        assert!(contained_join(d, "/etc/passwd").is_none());
        assert!(contained_join(d, ".").is_none(), "no file at all");
    }

    #[test]
    fn write_out_caps_the_actual_stream_not_the_declared_size() {
        let tmp = tempfile::tempdir().unwrap();
        let dest = tmp.path().join("x.bin");
        let mut endless = std::io::repeat(7u8);
        let err = write_out_capped(&dest, &mut endless, 1024).unwrap_err();
        assert!(err.to_string().contains("size cap"));
        assert!(!dest.exists(), "partial output removed");
        let mut small = &b"ok"[..];
        write_out_capped(&dest, &mut small, 1024).unwrap();
        assert_eq!(std::fs::read(&dest).unwrap(), b"ok");
    }

    #[test]
    fn oversized_tar_refuses_to_list() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("big.tar");
        let f = std::fs::File::create(&p).unwrap();
        f.set_len(TAR_LIST_CAP + 1).unwrap();
        drop(f);
        let err = list(&p, ArchiveKind::Tar).unwrap_err();
        assert!(err.to_string().contains("too large to list"));
    }

    #[test]
    fn targz_expanding_past_the_cap_is_refused_at_list() {
        // A tiny compressed file whose DECODED stream blows the cap.
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("bomb.tar.gz");
        let f = std::fs::File::create(&p).unwrap();
        let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
        let mut t = tar::Builder::new(gz);
        let zeros = vec![0u8; (TAR_LIST_CAP / 4) as usize];
        for i in 0..5 {
            let mut hdr = tar::Header::new_gnu();
            hdr.set_size(zeros.len() as u64);
            hdr.set_cksum();
            t.append_data(&mut hdr, format!("z{i}.bin"), &zeros[..])
                .unwrap();
        }
        t.into_inner().unwrap().finish().unwrap();
        assert!(
            std::fs::metadata(&p).unwrap().len() < TAR_LIST_CAP,
            "the compressed file itself is small"
        );
        let err = list(&p, ArchiveKind::TarGz).unwrap_err();
        assert!(err.to_string().contains("listing cap"), "{err}");
    }

    #[cfg(unix)]
    #[test]
    fn extraction_refuses_symlinked_components_and_targets() {
        let tmp = tempfile::tempdir().unwrap();
        let zp = tmp.path().join("a.zip");
        make_zip(&zp);
        let dest = tmp.path().join("out");
        let outside = tmp.path().join("outside");
        std::fs::create_dir_all(&dest).unwrap();
        std::fs::create_dir_all(&outside).unwrap();
        // dest/docs -> outside: the member docs/readme.txt must refuse.
        std::os::unix::fs::symlink(&outside, dest.join("docs")).unwrap();
        let err = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap_err();
        assert!(err.to_string().contains("symlink"), "{err}");
        assert!(!outside.join("readme.txt").exists(), "nothing escaped");
        // A symlink LEAF is refused too.
        std::fs::remove_file(dest.join("docs")).unwrap();
        std::os::unix::fs::symlink(outside.join("evil.txt"), dest.join("evil.txt")).unwrap();
        let err = extract_member(&zp, ArchiveKind::Zip, "evil.txt", &dest).unwrap_err();
        assert!(err.to_string().contains("symlink"), "{err}");
        assert!(!outside.join("evil.txt").exists());
    }

    #[test]
    fn kind_detection_covers_compound_extensions() {
        assert_eq!(
            kind_from_ext(Path::new("a.tar.gz")),
            Some(ArchiveKind::TarGz)
        );
        assert_eq!(kind_from_ext(Path::new("a.tgz")), Some(ArchiveKind::TarGz));
        assert_eq!(kind_from_ext(Path::new("a.tar")), Some(ArchiveKind::Tar));
        assert_eq!(kind_from_ext(Path::new("a.zip")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("lib.jar")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("pkg.whl")), Some(ArchiveKind::Zip));
        assert_eq!(kind_from_ext(Path::new("a.txt")), None);
    }
}