exarch-core 0.10.0

Memory-safe archive extraction library with security validation
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
//! What extraction does with a path that is already occupied.
//!
//! One decision point for every site that meets an occupant: the policy maps
//! to [`OccupantAction`], and the only way to obtain the permission to replace
//! is [`ExistingFiles::on_occupant`] under
//! [`ExistingFiles::Overwrite`]. A site that destroys an occupant takes a
//! [`ReplacePermit`], so a missed `Refuse` arm cannot reach an overwrite.
//!
//! [`is_run_duplicate`] is the one rule for which occupants a run may skip as
//! duplicates under `Refuse`. Extraction on the host, the in-memory sink and
//! the `list` model ([`ArchiveManifest::dispositions`]) all apply it, so they
//! cannot disagree about an archive that names a path twice.
//!
//! [`ArchiveManifest::dispositions`]: crate::ArchiveManifest::dispositions

use std::path::Path;

use crate::ArchiveError;
use crate::ExistingFiles;
use crate::OccupiedBy;
use crate::Result;
use crate::Violation;
use crate::security::fs_view::Occupant;
use crate::security::fs_view::normalized_display;
use crate::selection::EntryShape;

/// Proof that the overwrite policy allows destroying an occupant.
///
/// Neither `Clone` nor `Copy`, and constructible only by
/// [`ExistingFiles::on_occupant`], so each replacement consumes its own
/// decision.
#[derive(Debug)]
pub(crate) struct ReplacePermit(());

impl ReplacePermit {
    #[cfg(test)]
    pub(crate) const fn for_test() -> Self {
        Self(())
    }
}

/// What to do with an occupied path.
#[derive(Debug)]
pub(crate) enum OccupantAction {
    /// Leave the occupant in place and count a skip.
    Skip,
    /// Replace the occupant; the permit proves the policy allows it.
    Replace(ReplacePermit),
}

/// Whether `arriving`, finding `occupant` at its path, is a duplicate of an
/// earlier entry that a run under [`ExistingFiles::Refuse`] skips.
///
/// Only a file or a link over a file or a symlink: those are the entries a
/// repeated name replaces. A directory occupant is never a duplicate of a
/// non-directory, whoever created it, so the run refuses instead of hiding a
/// member behind a directory.
pub(crate) const fn is_run_duplicate(arriving: EntryShape, occupant: Occupant) -> bool {
    matches!(
        (arriving, occupant),
        (
            EntryShape::File | EntryShape::Link(_),
            Occupant::File | Occupant::Symlink
        )
    )
}

impl ExistingFiles {
    /// Decides what to do with an occupied path.
    ///
    /// Under [`Refuse`](Self::Refuse) the occupant is skipped only when
    /// `occupant` finds one that [`is_run_duplicate`] of `arriving` and
    /// `created_here` confirms the run made it; any other outcome, including
    /// an occupant that has vanished since the create failed, is refused with
    /// the error `refusal` builds, which then says whether the run made the
    /// occupant ([`OccupiedBy`]). The caller never retries the create.
    /// `occupant` runs only under `Refuse`, `created_here` after it and only
    /// for an occupant that exists.
    ///
    /// # Errors
    ///
    /// The error built by `refusal`, and the I/O error of `occupant`.
    pub(crate) fn on_occupant(
        self,
        arriving: EntryShape,
        occupant: impl FnOnce() -> std::io::Result<Option<Occupant>>,
        created_here: impl FnOnce() -> bool,
        refusal: impl FnOnce() -> ArchiveError,
    ) -> Result<OccupantAction> {
        match self {
            Self::Skip => Ok(OccupantAction::Skip),
            Self::Refuse => refuse_unless_duplicate(arriving, occupant()?, created_here, refusal)
                .map(|RunDuplicate(())| OccupantAction::Skip),
            Self::Overwrite => Ok(OccupantAction::Replace(ReplacePermit(()))),
        }
    }
}

/// Proof that an occupant is a repeat of an entry the run made, which a run
/// under [`ExistingFiles::Refuse`] skips.
#[derive(Debug)]
pub(crate) struct RunDuplicate(());

/// Who made the entry that `found` reports at a path: the run itself when
/// `created_here` confirms it, otherwise an entry that was there before, or
/// whose maker is unknown.
///
/// The one rule for [`OccupiedBy`]; `created_here` runs only for an occupant
/// that exists.
pub(crate) fn occupied_by(
    found: Option<Occupant>,
    created_here: impl FnOnce() -> bool,
) -> OccupiedBy {
    if found.is_some() && created_here() {
        OccupiedBy::Archive
    } else {
        OccupiedBy::Existing
    }
}

/// The `Refuse` rule for one occupied path: a [`RunDuplicate`] when
/// `occupant` is one that [`is_run_duplicate`] of `arriving` and `created_here`
/// confirms the run made it, otherwise the error `refusal` builds, saying who
/// made the occupant ([`OccupiedBy`]).
///
/// `created_here` runs only for an occupant that exists.
///
/// # Errors
///
/// The error built by `refusal`.
pub(crate) fn refuse_unless_duplicate(
    arriving: EntryShape,
    occupant: Option<Occupant>,
    created_here: impl FnOnce() -> bool,
    refusal: impl FnOnce() -> ArchiveError,
) -> Result<RunDuplicate> {
    let by = occupied_by(occupant, created_here);
    match occupant {
        Some(found) if by == OccupiedBy::Archive && is_run_duplicate(arriving, found) => {
            Ok(RunDuplicate(()))
        }
        _ => Err(refusal().occupied_by(by)),
    }
}

/// The occupants a write does not replace, even under
/// [`ExistingFiles::Overwrite`]: a file is replaced, so it cannot reach
/// [`kept_occupant_refusal`]. The one exception is a 7z extraction on a
/// Windows host, which replaces a symlink like a regular file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeptOccupant {
    /// A directory, which only a directory entry may share.
    Directory,
    /// A symlink, which a file entry does not replace (see the type doc for 7z
    /// on Windows).
    Symlink,
}

impl KeptOccupant {
    /// The kept occupant `occupant` is, or `None` for a file.
    pub(crate) const fn of(occupant: Occupant) -> Option<Self> {
        match occupant {
            Occupant::Directory => Some(Self::Directory),
            Occupant::Symlink => Some(Self::Symlink),
            Occupant::File => None,
        }
    }
}

/// The security refusal of a path that holds a symlink no write replaces,
/// naming the entry `rel`.
///
/// The one constructor of [`Violation::ReplacesSymlink`] for an occupant, so
/// the write phase, the in-memory sink and the destination pre-check read
/// alike.
pub(crate) fn replaces_symlink(rel: &Path) -> ArchiveError {
    Violation::ReplacesSymlink {
        rel: normalized_display(rel).into(),
    }
    .into()
}

/// The typed refusal of a path whose occupant no write replaces: a symlink
/// gives [`replaces_symlink`], a directory the [`ArchiveError`] built by
/// `occupied` with the maker ([`OccupiedBy`]) `created_here` decides.
///
/// Shared by the write phase, the in-memory sink and the destination
/// pre-check, so they report one error class for one occupant.
pub(crate) fn kept_occupant_refusal(
    kept: KeptOccupant,
    rel: &Path,
    created_here: impl FnOnce() -> bool,
    occupied: impl FnOnce() -> ArchiveError,
) -> ArchiveError {
    match kept {
        KeptOccupant::Symlink => replaces_symlink(rel),
        KeptOccupant::Directory => {
            occupied().occupied_by(occupied_by(Some(Occupant::Directory), created_here))
        }
    }
}

/// The refusal of a hardlink entry whose path is occupied under
/// [`ExistingFiles::Overwrite`].
///
/// A hardlink never replaces an occupant. A file gives the typed
/// [`ArchiveError::DestinationOccupied`] built by `occupied`, naming the maker
/// `created_here` decides; a directory or symlink gives the same
/// [`kept_occupant_refusal`] a file or symlink entry gets there. An occupant
/// that vanished since the create failed counts as an existing file. The
/// permit proves the policy was `Overwrite`.
pub(crate) fn refuse_hardlink_replace(
    _permit: ReplacePermit,
    found: Option<Occupant>,
    rel: &Path,
    created_here: impl FnOnce() -> bool,
    occupied: impl FnOnce() -> ArchiveError,
) -> ArchiveError {
    match found.and_then(KeptOccupant::of) {
        Some(kept) => kept_occupant_refusal(kept, rel, created_here, occupied),
        None => occupied().occupied_by(occupied_by(found, created_here)),
    }
}

#[cfg(test)]
mod tests {
    use std::assert_matches;
    use std::cell::Cell;
    use std::path::Path;

    use super::KeptOccupant;
    use super::OccupantAction;
    use super::ReplacePermit;
    use super::is_run_duplicate;
    use super::kept_occupant_refusal;
    use super::refuse_hardlink_replace;
    use crate::ArchiveError;
    use crate::ExistingFiles;
    use crate::OccupiedBy;
    use crate::security::fs_view::LinkKind;
    use crate::security::fs_view::Occupant;
    use crate::selection::EntryShape;

    fn refusal() -> ArchiveError {
        ArchiveError::Cancelled
    }

    const FILE: EntryShape = EntryShape::File;

    #[test]
    fn skip_and_overwrite_never_consult_the_occupant_or_the_run_record() {
        let asked = Cell::new(false);
        let occupant = || {
            asked.set(true);
            Ok(Some(Occupant::File))
        };
        let created_here = || {
            asked.set(true);
            true
        };

        let skip = ExistingFiles::Skip.on_occupant(FILE, occupant, created_here, refusal);
        let overwrite = ExistingFiles::Overwrite.on_occupant(FILE, occupant, created_here, refusal);

        assert_matches!(skip, Ok(OccupantAction::Skip));
        assert_matches!(overwrite, Ok(OccupantAction::Replace(_)));
        assert!(!asked.get());
    }

    #[test]
    fn refuse_skips_a_duplicate_of_the_run_and_refuses_a_foreign_occupant() {
        let found = || Ok(Some(Occupant::File));

        let duplicate = ExistingFiles::Refuse.on_occupant(FILE, found, || true, refusal);
        let foreign = ExistingFiles::Refuse.on_occupant(FILE, found, || false, refusal);

        assert_matches!(duplicate, Ok(OccupantAction::Skip));
        assert_matches!(foreign, Err(ArchiveError::Cancelled));
    }

    #[test]
    fn refuse_refuses_a_run_directory_whatever_the_record_says() {
        for made_here in [true, false] {
            let outcome = ExistingFiles::Refuse.on_occupant(
                FILE,
                || Ok(Some(Occupant::Directory)),
                || made_here,
                refusal,
            );

            assert_matches!(outcome, Err(ArchiveError::Cancelled));
        }
    }

    #[test]
    fn refuse_refuses_a_vanished_occupant_and_propagates_a_probe_error() {
        let vanished = ExistingFiles::Refuse.on_occupant(FILE, || Ok(None), || true, refusal);
        let failed = ExistingFiles::Refuse.on_occupant(
            FILE,
            || Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied)),
            || true,
            refusal,
        );

        assert_matches!(vanished, Err(ArchiveError::Cancelled));
        assert_matches!(failed, Err(ArchiveError::Io(_)));
    }

    #[test]
    fn a_refusal_says_whether_the_run_made_the_occupant() {
        let by = |outcome: Result<OccupantAction, ArchiveError>| {
            let Err(ArchiveError::DestinationOccupied { by, .. }) = outcome else {
                panic!("expected a refusal: {outcome:?}");
            };
            by
        };
        let occupied =
            || crate::formats::common::destination_occupied(Path::new("d"), Path::new("x"));
        let dir = || Ok(Some(Occupant::Directory));

        let made = ExistingFiles::Refuse.on_occupant(FILE, dir, || true, occupied);
        let foreign = ExistingFiles::Refuse.on_occupant(FILE, dir, || false, occupied);
        let vanished = ExistingFiles::Refuse.on_occupant(FILE, || Ok(None), || true, occupied);

        assert_eq!(by(made), OccupiedBy::Archive);
        assert_eq!(by(foreign), OccupiedBy::Existing);
        assert_eq!(by(vanished), OccupiedBy::Existing);
    }

    #[test]
    fn a_kept_directory_is_a_typed_conflict_and_a_kept_symlink_a_violation() {
        let occupied = || crate::formats::common::occupied_without_destination(Path::new("d"));

        let made =
            kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || true, occupied);
        let foreign =
            kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || false, occupied);
        let link =
            kept_occupant_refusal(KeptOccupant::Symlink, Path::new("a//l"), || true, occupied);

        assert_matches!(
            made,
            ArchiveError::DestinationOccupied {
                by: OccupiedBy::Archive,
                ..
            }
        );
        assert_matches!(
            foreign,
            ArchiveError::DestinationOccupied {
                by: OccupiedBy::Existing,
                ..
            }
        );
        assert_matches!(
            link,
            ArchiveError::SecurityViolation {
                kind: crate::Violation::ReplacesSymlink { rel },
                ..
            } if &*rel == "a/l"
        );
    }

    #[test]
    fn a_hardlink_over_a_kept_occupant_gets_the_kept_refusal() {
        let refusal = |found| {
            refuse_hardlink_replace(
                ReplacePermit::for_test(),
                found,
                Path::new("h"),
                || true,
                || crate::formats::common::occupied_without_destination(Path::new("h")),
            )
        };

        assert_matches!(
            refusal(Some(Occupant::Directory)),
            ArchiveError::DestinationOccupied {
                by: OccupiedBy::Archive,
                ..
            }
        );
        assert_matches!(
            refusal(Some(Occupant::Symlink)),
            ArchiveError::SecurityViolation { .. }
        );
        assert_matches!(
            refusal(Some(Occupant::File)),
            ArchiveError::DestinationOccupied {
                by: OccupiedBy::Archive,
                ..
            }
        );
        assert_matches!(
            refusal(None),
            ArchiveError::DestinationOccupied {
                by: OccupiedBy::Existing,
                ..
            }
        );
    }

    #[test]
    fn only_a_file_or_link_over_a_file_or_symlink_is_a_duplicate() {
        let shapes = [
            (EntryShape::Directory, false, false),
            (EntryShape::File, true, true),
            (EntryShape::Link(LinkKind::Symlink), true, true),
            (EntryShape::Link(LinkKind::Hardlink), true, true),
        ];
        for (shape, over_file, over_symlink) in shapes {
            assert_eq!(
                is_run_duplicate(shape, Occupant::File),
                over_file,
                "{shape:?}"
            );
            assert_eq!(
                is_run_duplicate(shape, Occupant::Symlink),
                over_symlink,
                "{shape:?}"
            );
            assert!(!is_run_duplicate(shape, Occupant::Directory), "{shape:?}");
        }
    }
}