walkkit 0.1.3

Parallel filesystem walker with ignore-aware traversal and bounded work queues
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
494
495
496
497
498
499
500
501
502
503
504
505
use super::entry::{ArchiveEntry, MAX_ENTRIES, MAX_ENTRY_BYTES};
use super::error::ArchiveError;
use std::iter::FusedIterator;
use std::str;

/// Stateless ustar tar reader (in-memory slice).
///
/// # Example
///
/// Build tar bytes (or read from disk), then iterate:
///
/// ```no_run
/// use walkkit::archive::TarReader;
/// # let raw: &[u8] = &[];
/// let _entries: Vec<_> = TarReader::entries(raw).collect::<Result<Vec<_>, _>>().unwrap();
/// ```
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TarReader;

impl TarReader {
    /// Walk `data` as a tar stream and yield regular files (`typeflag` `0` or `'\0'`).
    ///
    /// Directories, hard links, symlinks, and special entries are skipped (consumed but not yielded).
    /// Symlink and hard-link entries still validate the `linkname` field for path traversal.
    /// End of archive requires **two** consecutive all-zero 512-byte blocks (POSIX); a single
    /// zero block with trailing data is rejected as corrupt, and a lone zero block at EOF is
    /// [`ArchiveError::Truncated`].
    #[must_use]
    pub fn entries(data: &[u8]) -> TarEntriesIter<'_> {
        TarEntriesIter {
            data,
            offset: 0,
            finished: false,
            entry_count: 0,
        }
    }
}

/// Iterator over tar regular files, yielding [`ArchiveEntry`] or [`ArchiveError`].
///
/// Stops after the first unrecoverable error. After two zero blocks (end-of-archive), iteration ends.
#[derive(Clone, Debug)]
pub struct TarEntriesIter<'a> {
    data: &'a [u8],
    offset: usize,
    finished: bool,
    entry_count: usize,
}

/// Checks if a path contains traversal patterns that could escape the extraction directory.
///
/// Returns `Err` if the path:
/// - Starts with '/' (absolute path)
/// - Contains '..' as a path component (parent directory traversal)
/// - Contains percent-encoded or unicode dot variants
/// # Errors
/// Returns `ArchiveError::PathTraversal` if the path contains `..`, encoded variants, or is absolute.
pub fn validate_path_traversal(name: &str) -> Result<(), ArchiveError> {
    // Reject paths with unicode dots that can be used for normalization bypass
    if name.chars().any(|c| {
        matches!(
            c,
            '\u{2024}' | '\u{2025}' | '\u{FE52}' | '\u{FF0E}' | '\u{3002}'
        )
    }) {
        return Err(ArchiveError::PathTraversal {
            name: name.to_string(),
        });
    }

    // Reject absolute paths
    if name.starts_with('/')
        || name.starts_with('\\')
        || name.starts_with('\u{FF0F}')
        || name.starts_with('\u{FF3C}')
    {
        return Err(ArchiveError::PathTraversal {
            name: name.to_string(),
        });
    }

    // Reject percent-encoded dot patterns that decode to ".."
    let lower = name.to_lowercase();
    let encoded_dotdot = ["%2e%2e", "%252e%252e", "%2e.", ".%2e", "%252e.", ".%252e"];
    if encoded_dotdot.iter().any(|pat| lower.contains(pat)) {
        return Err(ArchiveError::PathTraversal {
            name: name.to_string(),
        });
    }

    // Reject paths containing '..' as a normalized component
    for component in name.split(['/', '\\', '\u{FF0F}', '\u{FF3C}']) {
        let normalized = component.trim();
        if normalized == ".." {
            return Err(ArchiveError::PathTraversal {
                name: name.to_string(),
            });
        }
    }
    Ok(())
}

impl Iterator for TarEntriesIter<'_> {
    type Item = Result<ArchiveEntry, ArchiveError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }
        loop {
            if self.offset >= self.data.len() {
                self.finished = true;
                return None;
            }
            let header_off = self.offset;
            if self.data.len() - self.offset < 512 {
                self.finished = true;
                return Some(Err(ArchiveError::Truncated {
                    offset: header_off,
                    need: 512,
                    have: self.data.len() - header_off,
                }));
            }
            let header = &self.data[header_off..header_off + 512];
            if header.iter().all(|&b| b == 0) {
                // POSIX end-of-archive is two consecutive 512-byte zero blocks  -  not one.
                let Some(second_off) = header_off.checked_add(512) else {
                    self.finished = true;
                    return Some(Err(ArchiveError::InvalidHeader {
                        offset: header_off,
                        message:
                            "offset overflow after zero block. Fix: reject corrupted tar streams."
                                .into(),
                    }));
                };
                if second_off > self.data.len() {
                    self.finished = true;
                    return Some(Err(ArchiveError::Truncated {
                        offset: second_off,
                        need: 512,
                        have: 0,
                    }));
                }
                if self.data.len() - second_off < 512 {
                    self.finished = true;
                    return Some(Err(ArchiveError::Truncated {
                        offset: second_off,
                        need: 512,
                        have: self.data.len() - second_off,
                    }));
                }
                let second = &self.data[second_off..second_off + 512];
                if second.iter().all(|&b| b == 0) {
                    self.finished = true;
                    self.offset = second_off + 512;
                    return None;
                }
                self.finished = true;
                return Some(Err(ArchiveError::InvalidHeader {
                    offset: header_off,
                    message: "single zero block is not a valid ustar header (POSIX requires two zero blocks to end an archive). Fix: reject truncated or corrupted tar streams.".into(),
                }));
            }
            let parsed = match parse_header(header, header_off) {
                Ok(p) => p,
                Err(e) => {
                    self.finished = true;
                    return Some(Err(e));
                }
            };

            // Check for path traversal in the entry name
            if let Err(e) = validate_path_traversal(&parsed.full_name) {
                self.finished = true;
                return Some(Err(e));
            }

            // Check MAX_ENTRIES limit before processing this entry
            self.entry_count = self.entry_count.saturating_add(1);
            if self.entry_count > MAX_ENTRIES {
                self.finished = true;
                return Some(Err(ArchiveError::TooManyEntries {
                    count: self.entry_count,
                    max: MAX_ENTRIES,
                }));
            }

            let block_after_header = header_off + 512;

            // Check size against MAX_ENTRY_BYTES using checked arithmetic
            let size_usize = match parsed.size.try_into() {
                Ok(s) if s <= MAX_ENTRY_BYTES => s,
                _ => {
                    self.finished = true;
                    return Some(Err(ArchiveError::EntryTooLarge {
                        size: parsed.size,
                        max: MAX_ENTRY_BYTES,
                    }));
                }
            };

            // Compute padding using checked arithmetic
            let Some(pad) = padding_checked(parsed.size) else {
                self.finished = true;
                return Some(Err(ArchiveError::InvalidHeader {
                    offset: header_off,
                    message: "size overflow when computing padding. Fix: reject corrupted tar headers with implausibly large sizes.".into(),
                }));
            };

            let total_after = block_after_header
                .checked_add(size_usize)
                .and_then(|x| x.checked_add(pad));
            let Some(end) = total_after else {
                self.finished = true;
                return Some(Err(ArchiveError::InvalidHeader {
                    offset: header_off,
                    message: "size overflow when computing extent. Fix: reject corrupted tar headers with implausibly large sizes.".into(),
                }));
            };
            if end > self.data.len() {
                self.finished = true;
                return Some(Err(ArchiveError::Truncated {
                    offset: block_after_header,
                    need: size_usize + pad,
                    have: self.data.len().saturating_sub(block_after_header),
                }));
            }
            self.offset = end;
            if !parsed.is_regular_file {
                continue;
            }
            let payload = std::sync::Arc::from(
                &self.data[block_after_header..block_after_header + size_usize],
            );
            return Some(Ok(ArchiveEntry {
                name: parsed.full_name,
                size: parsed.size,
                data: payload,
            }));
        }
    }
}

impl FusedIterator for TarEntriesIter<'_> {}

struct ParsedHeader {
    full_name: String,
    size: u64,
    is_regular_file: bool,
}

fn parse_header(block: &[u8], offset: usize) -> Result<ParsedHeader, ArchiveError> {
    if block.len() != 512 {
        return Err(ArchiveError::InvalidHeader {
            offset,
            message: "internal header slice length must be 512. Fix: report this as a library bug; the iterator should always pass 512-byte blocks.".into(),
        });
    }
    if !checksum_valid(block) {
        return Err(ArchiveError::InvalidHeader {
            offset,
            message: "checksum mismatch. Fix: reject corrupted or modified tar archives.".into(),
        });
    }
    let name = field_str(&block[0..100]);
    let prefix = field_str(&block[345..500]);
    let full_name = join_path(prefix, name);
    let size = parse_octal_field(&block[124..136], "size", offset)?;
    let typeflag = block[156];
    let linkname = field_str(&block[157..257]);
    if typeflag == b'1' || typeflag == b'2' {
        if linkname.is_empty() {
            return Err(ArchiveError::InvalidHeader {
                offset,
                message: "hard link or symlink entry has an empty link path. Fix: reject corrupted tar headers.".into(),
            });
        }
        validate_path_traversal(&linkname)?;
    }
    let is_regular_file = typeflag == b'0' || typeflag == 0;
    Ok(ParsedHeader {
        full_name,
        size,
        is_regular_file,
    })
}

fn field_str(bytes: &[u8]) -> String {
    let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    let slice = &bytes[..end];
    String::from_utf8_lossy(trim_trailing_space(slice)).into_owned()
}

fn trim_trailing_space(s: &[u8]) -> &[u8] {
    let end = s
        .iter()
        .rposition(|b| *b != b' ' && *b != 0)
        .map_or(0, |i| i + 1);
    &s[..end]
}

fn join_path(prefix: String, name: String) -> String {
    if prefix.is_empty() {
        return name;
    }
    if name.is_empty() {
        return prefix;
    }
    let prefix_trim = prefix.trim_end_matches('/');
    let name_trim = name.trim_start_matches('/');
    format!("{prefix_trim}/{name_trim}")
}

fn parse_octal_field(field: &[u8], label: &str, offset: usize) -> Result<u64, ArchiveError> {
    // GNU/POSIX base-256 (binary) encoding: the high bit of the first byte is
    // set. Used for size/offset fields that exceed the 8 GiB octal ceiling.
    if field.first().is_some_and(|b| b & 0x80 != 0) {
        return parse_base256_field(field, label, offset);
    }
    let raw = field
        .iter()
        .copied()
        .take_while(|b| *b != 0 && *b != b' ')
        .collect::<Vec<u8>>();
    let s = str::from_utf8(&raw).map_err(|_| ArchiveError::InvalidHeader {
        offset,
        message: format!("{label} field is not valid UTF-8 for octal digits. Fix: ensure archive uses standard octal values."),
    })?;
    let s = s.trim();
    if s.is_empty() {
        return Ok(0);
    }
    u64::from_str_radix(s, 8).map_err(|err| match err.kind() {
        std::num::IntErrorKind::PosOverflow | std::num::IntErrorKind::NegOverflow => ArchiveError::InvalidHeader {
            offset,
            message: format!("{label} octal value {s:?} overflows u64. Fix: reject corrupted archives with oversized fields."),
        },
        _ => ArchiveError::InvalidHeader {
            offset,
            message: format!("{label} is not valid octal: {s:?}. Fix: check if the archive is corrupted or uses base-256 extensions."),
        }
    })
}

/// Big-endian base-256 (binary) numeric field, GNU/POSIX star extension.
///
/// The high bit of the first byte flags base-256. `0x80` in the first byte is a
/// positive sentinel (masked off); `0xff` flags a negative value, which is not a
/// valid size/offset and is rejected rather than silently wrapped.
fn parse_base256_field(field: &[u8], label: &str, offset: usize) -> Result<u64, ArchiveError> {
    let first = field[0];
    if first == 0xff {
        return Err(ArchiveError::InvalidHeader {
            offset,
            message: format!(
                "{label} uses negative base-256 encoding, which is not a valid size. Fix: reject corrupted archives with negative numeric fields."
            ),
        });
    }
    let mut value: u64 = u64::from(first & 0x7f);
    for &b in &field[1..] {
        value = value
            .checked_mul(256)
            .and_then(|v| v.checked_add(u64::from(b)))
            .ok_or_else(|| ArchiveError::InvalidHeader {
                offset,
                message: format!(
                    "{label} base-256 value overflows u64. Fix: reject corrupted archives with oversized fields."
                ),
            })?;
    }
    Ok(value)
}

fn checksum_valid(block: &[u8]) -> bool {
    // Historic tar writers computed the header checksum treating bytes as either
    // unsigned or signed chars. Accept either so archives written by both
    // conventions (signed-char sums differ once any field byte has the high bit
    // set, e.g. non-ASCII names or base-256 fields) validate.
    let mut unsigned_sum: u32 = 0;
    let mut signed_sum: i32 = 0;
    for (i, &b) in block.iter().enumerate() {
        let val = if (148..156).contains(&i) { b' ' } else { b };
        unsigned_sum += u32::from(val);
        signed_sum += i32::from(val as i8);
    }
    let Ok(stored) = parse_octal_field(&block[148..156], "checksum", 0) else {
        return false;
    };
    let Ok(stored) = i64::try_from(stored) else {
        return false;
    };
    i64::from(unsigned_sum) == stored || i64::from(signed_sum) == stored
}

/// Computes padding with checked arithmetic to prevent 32-bit truncation.
/// Returns `None` if the calculation would overflow.
#[must_use]
pub fn padding_checked(size: u64) -> Option<usize> {
    let remainder = size % 512;
    let r: usize = remainder.try_into().ok()?;
    if r == 0 {
        Some(0)
    } else {
        Some(512 - r)
    }
}

#[cfg(test)]
mod tar_tests {
    use super::*;

    #[test]
    fn path_traversal_rejects_dotdot() {
        assert!(validate_path_traversal("../etc/passwd").is_err());
        assert!(validate_path_traversal("../../root/.ssh/id_rsa").is_err());
    }

    #[test]
    fn path_traversal_rejects_absolute() {
        assert!(validate_path_traversal("/etc/passwd").is_err());
    }

    #[test]
    fn path_traversal_accepts_normal() {
        assert!(validate_path_traversal("package/index.js").is_ok());
        assert!(validate_path_traversal("src/lib.rs").is_ok());
    }

    #[test]
    fn path_traversal_accepts_dotfile() {
        assert!(validate_path_traversal(".gitignore").is_ok());
        assert!(validate_path_traversal("src/.hidden").is_ok());
    }

    #[test]
    fn tar_entries_empty_data() {
        let entries: Vec<_> = TarReader::entries(&[]).collect();
        assert!(entries.is_empty());
    }

    #[test]
    fn tar_entries_short_data_no_panic() {
        // Previously this was a FINDING  -  TarReader panicked on short data.
        // After investigation, the iterator returns Err(Truncated) which is correct.
        let results: Vec<_> = TarReader::entries(&[0u8; 100]).collect();
        // Should return 1 error (Truncated), not panic
        assert_eq!(
            results.len(),
            1,
            "short data should produce exactly 1 Truncated error"
        );
        assert!(results[0].is_err(), "result should be an error");
    }

    #[test]
    fn parse_octal_field_reads_base256_size() {
        // 12-byte size field, base-256 encoding of 10 GiB (0x2_8000_0000),
        // which is beyond the 8 GiB octal ceiling. First byte 0x80 = positive.
        let value: u64 = 10 * 1024 * 1024 * 1024;
        let mut field = [0u8; 12];
        field[0] = 0x80;
        // big-endian bytes of `value` in the low 8 bytes of the 12-byte field.
        field[4..12].copy_from_slice(&value.to_be_bytes());
        let parsed = parse_octal_field(&field, "size", 0).expect("base-256 size parses");
        assert_eq!(parsed, value);
    }

    #[test]
    fn parse_octal_field_rejects_negative_base256() {
        let mut field = [0u8; 12];
        field[0] = 0xff; // negative sentinel
        assert!(parse_octal_field(&field, "size", 0).is_err());
    }

    #[test]
    fn parse_base256_field_overflow_fails_closed() {
        // 12 payload bytes all 0xff after a 0x80 lead would exceed u64.
        let field = [0x80u8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
        assert!(parse_octal_field(&field, "size", 0).is_err());
    }

    #[test]
    fn checksum_valid_accepts_signed_char_convention() {
        // Build a block whose name contains a high-bit byte so signed and
        // unsigned sums diverge, then store the SIGNED sum. checksum_valid must
        // still accept it via the signed path.
        let mut block = [0u8; 512];
        block[0] = 0xC3; // non-ASCII name byte (high bit set)
        // Compute the signed-char checksum with the checksum field as spaces.
        let mut signed_sum: i32 = 0;
        for (i, &b) in block.iter().enumerate() {
            let val = if (148..156).contains(&i) { b' ' } else { b };
            signed_sum += i32::from(val as i8);
        }
        // Store as 6 octal digits + NUL + space per tar convention.
        let octal = format!("{signed_sum:06o}\0 ");
        block[148..156].copy_from_slice(octal.as_bytes());
        assert!(
            checksum_valid(&block),
            "signed-char checksum convention must validate"
        );
    }
}