exarch-core 0.12.0

Memory-safe archive extraction library with security validation
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
//! An [`EntrySink`] that hands every verified entry to a closure.

use std::error::Error;
use std::fmt;
use std::io;

use crate::ArchiveError;
use crate::ArchiveReader;
use crate::ExtractionReport;
use crate::ProgressCallback;
use crate::security::fs_view::PathModel;
use crate::security::permissions::SanitizedMode;
use crate::security::quota::AdmittedSize;
use crate::sink::EntrySink;
use crate::sink::MemoryEntry;
use crate::sink::MemoryEntryKind;
use crate::sink::MemoryExtractRequest;
use crate::sink::MemoryFile;
use crate::types::CheckedLinkTarget;
use crate::types::SafePath;

/// An entry delivered by a [`CallbackSink`].
///
/// Everything a [`MemorySink`](crate::MemorySink) can hold is
/// [`Stored`](Self::Stored); a hardlink is delivered by reference instead,
/// because the sink keeps no bytes to copy.
///
/// # Examples
///
/// ```
/// use exarch_core::CallbackEntry;
/// use exarch_core::MemoryEntry;
/// use exarch_core::MemoryEntryKind;
///
/// let entry = CallbackEntry::from(MemoryEntry::Directory);
/// assert_eq!(entry.kind(), MemoryEntryKind::Directory);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CallbackEntry {
    /// A file, directory or symlink, as a [`MemorySink`](crate::MemorySink)
    /// would store it.
    Stored(MemoryEntry),
    /// A hard link to an entry that was already delivered, by reference.
    Hardlink {
        /// The validated, root-relative path of an already delivered entry: a
        /// file, or an earlier hardlink (a chain is delivered link by link).
        target: SafePath,
    },
}

impl CallbackEntry {
    /// What the entry holds.
    #[must_use]
    pub const fn kind(&self) -> MemoryEntryKind {
        match self {
            Self::Stored(entry) => entry.kind(),
            Self::Hardlink { .. } => MemoryEntryKind::Hardlink,
        }
    }
}

impl From<MemoryEntry> for CallbackEntry {
    fn from(entry: MemoryEntry) -> Self {
        Self::Stored(entry)
    }
}

/// An [`EntrySink`] that delivers each entry to a closure as it is verified.
///
/// The closure receives the entry's validated path and a [`CallbackEntry`]. A
/// file is delivered whole, after all of its bytes were written and the stream
/// delivered exactly the size the archive declares. The memory held at any
/// moment is one file, bounded by the configuration's `max_file_size`; the sink
/// keeps nothing after delivery.
///
/// # Entries are provisional
///
/// An entry is delivered as soon as it is verified, before the rest of the
/// archive is read. A later failure (a corrupt or refused member further on)
/// ends the extraction after earlier entries were delivered, so treat what the
/// closure received as final only once the extraction returned `Ok`. A
/// stream-level checksum is checked last: the gzip trailer CRC of a `.tar.gz`
/// is verified after the end-of-archive marker, so a corrupt compressed stream
/// ends the extraction with an error only after its entries were delivered.
///
/// An error from the closure ends the extraction like any other sink error
/// (see [`EntrySink`]).
///
/// # What `target` means
///
/// - A [`MemoryEntry::Symlink`] target is spelled as the archive stores it and
///   is relative to the directory of the link.
/// - A [`CallbackEntry::Hardlink`] target is the validated, root-relative,
///   resolved [`SafePath`] of an entry that was already delivered: a file, or
///   an earlier hardlink, so a chain of links is delivered link by link. The
///   sink holds no bytes, so the closure resolves the reference against what it
///   kept.
///
/// # Delivery rules
///
/// - Under overwrite (`ExistingFiles::Overwrite`) one path can be delivered
///   more than once, and a hardlink refers to the most recent delivery of its
///   target.
/// - Implicit parent directories are not delivered; only directories the
///   archive names are.
/// - A hardlink is delivered after every other entry. Its bytes stay charged to
///   the extraction quota although they are not copied.
///
/// # Examples
///
/// ```
/// use std::io;
///
/// use exarch_core::ArchiveReader;
/// use exarch_core::CallbackEntry;
/// use exarch_core::CallbackSink;
/// use exarch_core::MemoryEntry;
/// use exarch_core::SecurityConfig;
/// use exarch_core::SinkExtractRequest;
/// use exarch_core::SinkOptions;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut builder = tar::Builder::new(Vec::new());
/// let mut header = tar::Header::new_gnu();
/// header.set_size(5);
/// header.set_mode(0o644);
/// builder.append_data(&mut header, "docs/hello.txt", &b"hello"[..])?;
/// let archive = builder.into_inner()?;
///
/// let mut seen = Vec::new();
/// let mut sink = CallbackSink::new(|path, entry| {
///     if let CallbackEntry::Stored(MemoryEntry::File { data, .. }) = entry {
///         seen.push((path.as_path().to_path_buf(), data));
///     }
///     Ok(())
/// });
/// let reader = ArchiveReader::tar(io::Cursor::new(archive), None);
/// let config = SecurityConfig::default();
/// SinkExtractRequest::new(&config)
///     .with_options(SinkOptions::default())
///     .extract(reader, &mut sink)?;
/// assert_eq!(seen.len(), 1);
/// assert_eq!(seen[0].1, b"hello");
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct CallbackSink<F> {
    callback: F,
    path_model: PathModel,
}

impl<F> CallbackSink<F>
where
    F: FnMut(&SafePath, CallbackEntry) -> io::Result<()>,
{
    /// A sink that calls `callback` for every entry, validating against the
    /// host's path rules.
    #[must_use]
    pub const fn new(callback: F) -> Self {
        Self {
            callback,
            path_model: PathModel::Host,
        }
    }

    /// Validates against `path_model` instead of the host's rules.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io;
    ///
    /// use exarch_core::CallbackSink;
    /// use exarch_core::EntrySink as _;
    /// use exarch_core::PathModel;
    ///
    /// let sink = CallbackSink::new(|_, _| Ok(())).with_path_model(PathModel::Portable);
    /// assert_eq!(sink.path_model(), PathModel::Portable);
    /// ```
    #[must_use]
    pub const fn with_path_model(mut self, path_model: PathModel) -> Self {
        self.path_model = path_model;
        self
    }
}

impl<F> EntrySink for CallbackSink<F>
where
    F: FnMut(&SafePath, CallbackEntry) -> io::Result<()>,
{
    type File = MemoryFile;

    fn path_model(&self) -> PathModel {
        self.path_model
    }

    fn create_directory(&mut self, path: &SafePath) -> io::Result<()> {
        (self.callback)(path, MemoryEntry::Directory.into())
    }

    fn create_file(
        &mut self,
        _path: &SafePath,
        mode: Option<SanitizedMode>,
        size: AdmittedSize,
    ) -> io::Result<MemoryFile> {
        Ok(MemoryFile::with_size(size.get(), mode))
    }

    fn finish_file(&mut self, path: &SafePath, file: MemoryFile) -> io::Result<()> {
        (self.callback)(path, file.into_entry().into())
    }

    fn create_symlink(&mut self, link: &SafePath, target: CheckedLinkTarget<'_>) -> io::Result<()> {
        (self.callback)(
            link,
            MemoryEntry::Symlink {
                target: target.as_path().to_path_buf(),
            }
            .into(),
        )
    }

    fn create_hardlink(&mut self, link: &SafePath, target: &SafePath) -> io::Result<()> {
        (self.callback)(
            link,
            CallbackEntry::Hardlink {
                target: target.clone(),
            },
        )
    }
}

/// Why [`extract_reader_to_callback`] failed.
///
/// The first error the callback returned is kept as `E`, so the caller keeps
/// its own error type instead of recovering it from a side slot.
///
/// Closed by design: a failure is either the caller's own or the archive's, so
/// a `match` over both variants needs no wildcard arm and the compiler flags
/// any variant a future change adds.
///
/// # Examples
///
/// ```
/// use std::error::Error as _;
///
/// use exarch_core::CallbackError;
///
/// let err = CallbackError::Callback(std::io::Error::other("stop"));
/// assert_eq!(err.to_string(), "entry callback failed");
/// assert_eq!(
///     err.source().map(ToString::to_string).as_deref(),
///     Some("stop")
/// );
/// ```
#[derive(Debug)]
pub enum CallbackError<E> {
    /// The callback returned this error for an entry, which ended the
    /// extraction. It takes precedence over the archive error the aborted
    /// extraction reports.
    Callback(E),
    /// The archive was refused or could not be read.
    Archive(ArchiveError),
}

/// `Callback` names the failure and chains to the callback's error through
/// `source()`; `Archive` is transparent, so an error chain shows each message
/// once.
impl<E> fmt::Display for CallbackError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Callback(_) => f.write_str("entry callback failed"),
            Self::Archive(err) => err.fmt(f),
        }
    }
}

impl<E: Error + 'static> Error for CallbackError<E> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Callback(err) => Some(err),
            Self::Archive(err) => err.source(),
        }
    }
}

impl<E> From<ArchiveError> for CallbackError<E> {
    fn from(err: ArchiveError) -> Self {
        Self::Archive(err)
    }
}

/// Extracts the archive in `reader`, handing every verified entry to
/// `on_entry`.
///
/// Validates with the config, options and path model of `request`; its memory
/// limit is ignored, since nothing is kept. Entries are provisional and
/// delivered as described at [`CallbackSink`]. The first error `on_entry`
/// returns ends the extraction and is returned as
/// [`CallbackError::Callback`], ahead of any archive error.
///
/// # Errors
///
/// [`CallbackError::Callback`] with the first error of `on_entry`, else
/// [`CallbackError::Archive`] with the errors of
/// [`SinkExtractRequest::extract`](crate::SinkExtractRequest::extract).
///
/// # Examples
///
/// ```
/// use std::assert_matches;
///
/// use exarch_core::ArchiveReader;
/// use exarch_core::CallbackError;
/// use exarch_core::MemoryExtractRequest;
/// use exarch_core::NoopProgress;
/// use exarch_core::extract_reader_to_callback;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut builder = tar::Builder::new(Vec::new());
/// let mut header = tar::Header::new_gnu();
/// header.set_size(2);
/// header.set_mode(0o644);
/// builder.append_data(&mut header, "a.txt", &b"hi"[..])?;
/// let archive = builder.into_inner()?;
///
/// let mut paths = Vec::new();
/// let reader = ArchiveReader::from_bytes(archive.clone(), None)?;
/// extract_reader_to_callback(
///     reader,
///     &MemoryExtractRequest::default(),
///     |path, _entry| {
///         paths.push(path.as_path().to_path_buf());
///         Ok::<(), std::io::Error>(())
///     },
///     &mut NoopProgress,
/// )?;
/// assert_eq!(paths.len(), 1);
///
/// let reader = ArchiveReader::from_bytes(archive, None)?;
/// let failed = extract_reader_to_callback(
///     reader,
///     &MemoryExtractRequest::default(),
///     |_, _| Err(std::io::Error::other("stop")),
///     &mut NoopProgress,
/// );
/// assert_matches!(failed, Err(CallbackError::Callback(err)) if err.to_string() == "stop");
/// # Ok(())
/// # }
/// ```
pub fn extract_reader_to_callback<F, E>(
    reader: ArchiveReader<'_>,
    request: &MemoryExtractRequest,
    mut on_entry: F,
    progress: &mut dyn ProgressCallback,
) -> Result<ExtractionReport, CallbackError<E>>
where
    F: FnMut(&SafePath, CallbackEntry) -> Result<(), E>,
{
    let mut failure = None;
    let mut sink = CallbackSink::new(|path: &SafePath, entry| {
        on_entry(path, entry).map_err(|err| {
            failure.get_or_insert(err);
            io::Error::other("entry callback failed")
        })
    })
    .with_path_model(request.model);
    let extract = crate::SinkExtractRequest::new(&request.config)
        .with_options(request.options)
        .with_progress(progress);
    #[cfg(feature = "encryption")]
    let extract = extract.with_password(request.password().cloned());
    let result = extract.extract(reader, &mut sink);
    match (failure, result) {
        (Some(err), _) => Err(CallbackError::Callback(err)),
        (None, Ok(report)) => Ok(report),
        (None, Err(err)) => Err(CallbackError::Archive(err)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::SecurityConfig;

    fn hardlink_target() -> SafePath {
        SafePath::validate(
            std::path::Path::new("target.txt"),
            &crate::DestDir::new(std::env::temp_dir()).unwrap(),
            &SecurityConfig::default(),
        )
        .unwrap()
    }

    #[test]
    fn every_callback_entry_maps_to_a_memory_entry_kind() {
        let entries = [
            CallbackEntry::Stored(MemoryEntry::Directory),
            CallbackEntry::Hardlink {
                target: hardlink_target(),
            },
        ];
        let mut seen = Vec::new();
        for entry in &entries {
            // A wildcard-free match: a new variant fails to compile here.
            let expected = match entry {
                CallbackEntry::Stored(stored) => stored.kind(),
                CallbackEntry::Hardlink { .. } => MemoryEntryKind::Hardlink,
            };
            assert_eq!(entry.kind(), expected);
            seen.push(entry.kind());
        }
        assert!(seen.contains(&MemoryEntryKind::Directory));
        assert!(seen.contains(&MemoryEntryKind::Hardlink));
    }
}