exarch-core 0.8.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
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
506
507
508
509
510
//! FFI error message formatting.
//!
//! The single source of binding-facing error data: a stable [`ErrorCode`], a
//! typed [`FfiKind`] with the payload each code carries, a redacted
//! description and the fate of partial output. Python, Node.js and the CLI
//! consume [`FfiErrorMessage`] instead of re-matching [`ArchiveError`].

use super::redaction::sanitize_io_error_for_error;
use super::types::ArchiveError;
use super::types::PartialWrites;
use super::types::PolicySetting;
use super::types::ReaderFormatError;
use crate::ExtractionReport;
use crate::closed_enum::closed_enum;

closed_enum! {
    /// Stable machine-readable identifier of an [`ArchiveError`] variant.
    ///
    /// The spellings are part of every binding's observable contract; the
    /// bindings take them from [`ErrorCode::as_str`].
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::ZipBomb.as_str(), "ZIP_BOMB");
    /// assert!(ErrorCode::ALL.contains(&ErrorCode::PathTraversal));
    /// ```
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    #[non_exhaustive]
    pub enum ErrorCode {
        /// [`ArchiveError::PathTraversal`].
        PathTraversal => "PATH_TRAVERSAL",
        /// [`ArchiveError::SymlinkEscape`].
        SymlinkEscape => "SYMLINK_ESCAPE",
        /// [`ArchiveError::HardlinkEscape`].
        HardlinkEscape => "HARDLINK_ESCAPE",
        /// [`ArchiveError::ZipBomb`].
        ZipBomb => "ZIP_BOMB",
        /// [`ArchiveError::ZipInconsistency`].
        ZipInconsistency => "ZIP_INCONSISTENCY",
        /// [`ArchiveError::QuotaExceeded`].
        QuotaExceeded => "QUOTA_EXCEEDED",
        /// [`ArchiveError::SecurityViolation`].
        SecurityViolation => "SECURITY_VIOLATION",
        /// [`ArchiveError::InvalidArchive`].
        InvalidArchive => "INVALID_ARCHIVE",
        /// [`ArchiveError::Io`].
        IoError => "IO_ERROR",
        /// [`ArchiveError::InvalidPermissions`].
        InvalidPermissions => "INVALID_PERMISSIONS",
        /// [`ArchiveError::SourceNotFound`].
        SourceNotFound => "SOURCE_NOT_FOUND",
        /// [`ArchiveError::SourceNotAccessible`].
        SourceNotAccessible => "SOURCE_NOT_ACCESSIBLE",
        /// [`ArchiveError::OutputExists`].
        OutputExists => "OUTPUT_EXISTS",
        /// [`ArchiveError::InvalidCompressionLevel`].
        InvalidCompressionLevel => "INVALID_COMPRESSION_LEVEL",
        /// [`ArchiveError::UnknownFormat`].
        UnknownFormat => "UNKNOWN_FORMAT",
        /// [`ArchiveError::InvalidConfiguration`].
        InvalidConfiguration => "INVALID_CONFIGURATION",
    }
}

/// The error class of an [`FfiErrorMessage`], with the payload that class
/// carries.
///
/// One variant per [`ErrorCode`]; [`FfiKind::code`] is the bijection.
/// Payload lives inside the variant so a binding cannot read a field that
/// does not exist for the class.
///
/// # Examples
///
/// ```
/// use exarch_core::ArchiveError;
/// use exarch_core::ErrorCode;
/// use exarch_core::FfiKind;
/// use std::assert_matches;
///
/// let msg = ArchiveError::violation("nope".into()).into_ffi_message();
/// assert_matches!(msg.kind, FfiKind::SecurityViolation { relaxable_by: None });
/// assert_eq!(msg.kind.code(), ErrorCode::SecurityViolation);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FfiKind {
    /// Entry path escapes the destination.
    PathTraversal,
    /// Symlink target escapes the destination.
    SymlinkEscape,
    /// Hardlink target escapes the destination.
    HardlinkEscape,
    /// Compression ratio limit exceeded.
    ZipBomb,
    /// ZIP structure is ambiguous or self-contradictory.
    ZipInconsistency,
    /// A quota was exceeded.
    QuotaExceeded,
    /// Denied by security policy.
    SecurityViolation {
        /// The `SecurityConfig` setting whose current value refused the
        /// entry; see [`ArchiveError::relaxable_by`].
        relaxable_by: Option<PolicySetting>,
    },
    /// Archive is corrupted or malformed.
    InvalidArchive,
    /// I/O failure of the given kind.
    Io(std::io::ErrorKind),
    /// Invalid permission bits.
    InvalidPermissions,
    /// Creation source does not exist.
    SourceNotFound,
    /// Creation source cannot be read.
    SourceNotAccessible,
    /// Output file already exists.
    OutputExists,
    /// Compression level out of range.
    InvalidCompressionLevel,
    /// Archive format could not be determined.
    UnknownFormat,
    /// Configuration is invalid.
    InvalidConfiguration,
}

impl FfiKind {
    /// The stable [`ErrorCode`] of this class.
    #[must_use]
    pub const fn code(&self) -> ErrorCode {
        match self {
            Self::PathTraversal => ErrorCode::PathTraversal,
            Self::SymlinkEscape => ErrorCode::SymlinkEscape,
            Self::HardlinkEscape => ErrorCode::HardlinkEscape,
            Self::ZipBomb => ErrorCode::ZipBomb,
            Self::ZipInconsistency => ErrorCode::ZipInconsistency,
            Self::QuotaExceeded => ErrorCode::QuotaExceeded,
            Self::SecurityViolation { .. } => ErrorCode::SecurityViolation,
            Self::InvalidArchive => ErrorCode::InvalidArchive,
            Self::Io(_) => ErrorCode::IoError,
            Self::InvalidPermissions => ErrorCode::InvalidPermissions,
            Self::SourceNotFound => ErrorCode::SourceNotFound,
            Self::SourceNotAccessible => ErrorCode::SourceNotAccessible,
            Self::OutputExists => ErrorCode::OutputExists,
            Self::InvalidCompressionLevel => ErrorCode::InvalidCompressionLevel,
            Self::UnknownFormat => ErrorCode::UnknownFormat,
            Self::InvalidConfiguration => ErrorCode::InvalidConfiguration,
        }
    }
}

/// What happened to the output of an extraction that stopped part-way.
///
/// # Examples
///
/// ```
/// use exarch_core::PartialOutcome;
/// use exarch_core::PartialWrites;
///
/// assert_eq!(
///     PartialOutcome::RolledBack.writes(),
///     PartialWrites::RolledBack
/// );
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum PartialOutcome {
    /// The written files remain in the destination; the report counts them.
    Kept(ExtractionReport),
    /// Atomic extraction discarded the written files; no counters are
    /// meaningful.
    RolledBack,
    /// An in-memory extraction dropped what it collected; no counters are
    /// meaningful.
    Discarded,
}

impl PartialOutcome {
    /// The [`PartialWrites`] state matching this outcome.
    #[must_use]
    pub const fn writes(&self) -> PartialWrites {
        match self {
            Self::Kept(_) => PartialWrites::Kept,
            Self::RolledBack => PartialWrites::RolledBack,
            Self::Discarded => PartialWrites::Discarded,
        }
    }
}

/// Error data for FFI consumption.
///
/// `#[non_exhaustive]` and output-only: bindings read it, only
/// [`ArchiveError::into_ffi_message`] builds it.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FfiErrorMessage {
    /// Error class and its class-specific payload.
    pub kind: FfiKind,

    /// Human-readable, redacted description.
    pub description: String,

    /// Set when the error came from a partially completed extraction.
    pub partial: Option<PartialOutcome>,
}

impl FfiErrorMessage {
    const fn new(kind: FfiKind, description: String) -> Self {
        Self {
            kind,
            description,
            partial: None,
        }
    }
}

impl ArchiveError {
    /// Converts the error into the data every binding consumes.
    ///
    /// Path and I/O-error text is redacted per the shared policy in
    /// [`super::redaction`] — see [`Self::redacted_path`] and
    /// [`sanitize_io_error_for_error`]. A [`Self::PartialExtraction`] yields
    /// its source's message with [`FfiErrorMessage::partial`] set.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::ArchiveError;
    /// use exarch_core::ErrorCode;
    /// use std::path::PathBuf;
    ///
    /// let error = ArchiveError::PathTraversal {
    ///     path: PathBuf::from("../../etc/passwd"),
    /// };
    ///
    /// let msg = error.into_ffi_message();
    /// assert_eq!(msg.kind.code(), ErrorCode::PathTraversal);
    /// // Archive-relative, attacker-authored path: never redacted (#462).
    /// assert!(msg.description.contains("../../etc/passwd"));
    /// ```
    #[must_use]
    pub fn into_ffi_message(self) -> FfiErrorMessage {
        if let Self::PartialExtraction {
            source,
            report,
            writes,
        } = self
        {
            let mut msg = source.into_ffi_message();
            msg.partial = Some(match writes {
                PartialWrites::Kept => PartialOutcome::Kept(report),
                PartialWrites::RolledBack => PartialOutcome::RolledBack,
                PartialWrites::Discarded => PartialOutcome::Discarded,
            });
            return msg;
        }
        FfiErrorMessage::new(self.ffi_kind(), self.ffi_description())
    }

    /// The only variant-to-[`FfiKind`] table; [`Self::error_code`] derives
    /// from it.
    fn ffi_kind(&self) -> FfiKind {
        match self {
            Self::PathTraversal { .. } => FfiKind::PathTraversal,
            Self::SymlinkEscape { .. } => FfiKind::SymlinkEscape,
            Self::HardlinkEscape { .. } => FfiKind::HardlinkEscape,
            Self::ZipBomb(_) => FfiKind::ZipBomb,
            Self::ZipInconsistency(_) => FfiKind::ZipInconsistency,
            Self::QuotaExceeded { .. } => FfiKind::QuotaExceeded,
            Self::SecurityViolation { relaxable_by, .. } => FfiKind::SecurityViolation {
                relaxable_by: *relaxable_by,
            },
            Self::InvalidArchive(_) => FfiKind::InvalidArchive,
            Self::Io(io_err) => FfiKind::Io(io_err.kind()),
            Self::InvalidPermissions { .. } => FfiKind::InvalidPermissions,
            Self::SourceNotFound { .. } => FfiKind::SourceNotFound,
            Self::SourceNotAccessible { .. } => FfiKind::SourceNotAccessible,
            Self::OutputExists { .. } => FfiKind::OutputExists,
            Self::InvalidCompressionLevel { .. } => FfiKind::InvalidCompressionLevel,
            Self::UnknownFormat { .. } | Self::ReaderFormat(ReaderFormatError::Unrecognized) => {
                FfiKind::UnknownFormat
            }
            Self::InvalidConfiguration { .. } | Self::ReaderFormat(_) => {
                FfiKind::InvalidConfiguration
            }
            Self::PartialExtraction { source, .. } => source.ffi_kind(),
        }
    }

    /// The redacted, binding-facing description.
    fn ffi_description(&self) -> String {
        let path = self
            .redacted_path()
            .unwrap_or_else(|| "<unknown>".to_string());
        match self {
            Self::PathTraversal { .. } => format!("path traversal detected: {path}"),
            Self::SymlinkEscape { .. } => {
                format!("symlink target outside extraction directory: {path}")
            }
            Self::HardlinkEscape { .. } => {
                format!("hardlink target outside extraction directory: {path}")
            }
            Self::ZipBomb(_) | Self::ZipInconsistency(_) => self.to_string(),
            Self::QuotaExceeded { resource } => resource.to_string(),
            Self::SecurityViolation { reason, .. } => {
                format!("operation denied by security policy: {reason}")
            }
            Self::InvalidArchive(reason) => format!("invalid archive: {reason}"),
            Self::Io(io_err) => sanitize_io_error_for_error(io_err),
            Self::InvalidPermissions { mode, .. } => {
                format!("invalid permissions for {path}: {mode:#o}")
            }
            Self::SourceNotFound { .. } => format!("source path not found: {path}"),
            Self::SourceNotAccessible { .. } => {
                format!("source path is not accessible: {path}")
            }
            Self::OutputExists { .. } => format!("output file already exists: {path}"),
            Self::InvalidCompressionLevel { level } => {
                format!("invalid compression level {level}, must be 1-9")
            }
            Self::UnknownFormat { .. } => {
                format!("cannot determine archive format from: {path}")
            }
            Self::InvalidConfiguration { reason } => format!("invalid configuration: {reason}"),
            Self::ReaderFormat(reason) => reason.to_string(),
            Self::PartialExtraction { source, .. } => source.ffi_description(),
        }
    }

    /// Returns the stable [`ErrorCode`] of this error.
    ///
    /// Useful for matching on error types without full message formatting.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::ArchiveError;
    /// use exarch_core::ErrorCode;
    ///
    /// let err = ArchiveError::violation("nope".into());
    /// assert_eq!(err.error_code(), ErrorCode::SecurityViolation);
    /// assert_eq!(err.error_code().as_str(), "SECURITY_VIOLATION");
    /// ```
    #[must_use]
    pub fn error_code(&self) -> ErrorCode {
        self.ffi_kind().code()
    }
}

#[cfg(test)]
mod tests {
    use super::super::types::QuotaResource;
    use super::super::types::ZipBombDetails;
    use super::super::types::ZipBombScope;
    use super::*;
    use std::assert_matches;
    use std::path::PathBuf;

    /// Regression test for #462: `PathTraversal` carries an
    /// archive-relative, attacker-authored path and must never be redacted,
    /// unlike a genuinely host-derived path variant (see the test below).
    #[test]
    fn test_attacker_path_never_redacted() {
        let error = ArchiveError::PathTraversal {
            path: PathBuf::from("../../etc/passwd"),
        };
        let msg = error.into_ffi_message();
        assert!(msg.description.contains("../../etc/passwd"));
    }

    /// Regression test for #453: `SourceNotFound` carries a host filesystem
    /// path; in release builds it must be redacted to filename-only. This
    /// only exercises the release-build branch — the debug-build behavior
    /// is covered directly in `super::redaction`.
    #[test]
    #[cfg(not(debug_assertions))]
    fn test_host_path_redacted_in_release() {
        let error = ArchiveError::SourceNotFound {
            path: PathBuf::from("/srv/secret/app/x.txt"),
        };
        let msg = error.into_ffi_message();
        assert!(msg.description.contains("x.txt"));
        assert!(!msg.description.contains("/srv/secret"));
    }

    /// Regression test for #453: `Io` messages must not leak a host path
    /// embedded in the free-form message text in release builds.
    #[test]
    #[cfg(not(debug_assertions))]
    fn test_io_error_redacted_in_release() {
        let error = ArchiveError::Io(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            "directory is not writable: /srv/secret/app/private-output",
        ));
        let msg = error.into_ffi_message();
        assert!(!msg.description.contains("/srv/secret"));
    }

    #[test]
    fn test_error_codes_match() {
        let test_cases = vec![
            (
                ArchiveError::PathTraversal {
                    path: PathBuf::from("test"),
                },
                ErrorCode::PathTraversal,
            ),
            (
                ArchiveError::SymlinkEscape {
                    path: PathBuf::from("test"),
                },
                ErrorCode::SymlinkEscape,
            ),
            (
                ArchiveError::ZipBomb(ZipBombDetails::new(ZipBombScope::Entry, 100, 10000)),
                ErrorCode::ZipBomb,
            ),
        ];

        for (error, expected_code) in test_cases {
            assert_eq!(error.error_code(), expected_code);
            assert_eq!(error.into_ffi_message().kind.code(), expected_code);
        }
    }

    fn one_error_per_variant() -> Vec<ArchiveError> {
        vec![
            ArchiveError::PathTraversal {
                path: PathBuf::from("test"),
            },
            ArchiveError::SymlinkEscape {
                path: PathBuf::from("test"),
            },
            ArchiveError::HardlinkEscape {
                path: PathBuf::from("test"),
            },
            ArchiveError::ZipBomb(ZipBombDetails::new(ZipBombScope::Entry, 100, 10000)),
            ArchiveError::ZipInconsistency(
                super::super::zip_inconsistency::ZipInconsistency::OverlappingEntries,
            ),
            ArchiveError::QuotaExceeded {
                resource: QuotaResource::IntegerOverflow,
            },
            ArchiveError::violation("test".into()),
            ArchiveError::InvalidArchive("test".into()),
            ArchiveError::Io(std::io::Error::other("test")),
            ArchiveError::InvalidPermissions {
                path: PathBuf::from("test"),
                mode: 0o777,
            },
            ArchiveError::SourceNotFound {
                path: PathBuf::from("test"),
            },
            ArchiveError::SourceNotAccessible {
                path: PathBuf::from("test"),
            },
            ArchiveError::OutputExists {
                path: PathBuf::from("test"),
            },
            ArchiveError::InvalidCompressionLevel { level: 10 },
            ArchiveError::UnknownFormat {
                path: PathBuf::from("test"),
            },
            ArchiveError::InvalidConfiguration {
                reason: "test".into(),
            },
        ]
    }

    #[test]
    fn test_all_error_variants_have_codes() {
        for error in one_error_per_variant() {
            let code = error.error_code();
            let msg = error.into_ffi_message();
            assert_eq!(msg.kind.code(), code);
            assert!(!msg.description.is_empty());
            assert!(msg.partial.is_none());
        }
    }

    #[test]
    fn test_kind_code_covers_error_code_all_exactly_once() {
        let mut seen: Vec<ErrorCode> = one_error_per_variant()
            .into_iter()
            .map(|e| e.into_ffi_message().kind.code())
            .collect();
        seen.sort_by_key(|c| c.as_str());
        let mut expected = ErrorCode::ALL.to_vec();
        expected.sort_by_key(|c| c.as_str());
        assert_eq!(seen, expected);
    }

    #[test]
    fn test_partial_extraction_sets_partial() {
        let wrap = |writes| ArchiveError::PartialExtraction {
            source: Box::new(ArchiveError::violation("x".into())),
            report: ExtractionReport::default(),
            writes,
        };
        let kept = wrap(PartialWrites::Kept).into_ffi_message();
        assert_matches!(kept.partial, Some(PartialOutcome::Kept(_)));
        assert_eq!(kept.kind.code(), ErrorCode::SecurityViolation);
        let rolled = wrap(PartialWrites::RolledBack).into_ffi_message();
        assert_matches!(rolled.partial, Some(PartialOutcome::RolledBack));
        let discarded = wrap(PartialWrites::Discarded).into_ffi_message();
        assert_matches!(discarded.partial, Some(PartialOutcome::Discarded));
        assert_eq!(PartialOutcome::Discarded.writes(), PartialWrites::Discarded);
    }
}