exarch-core 0.11.0

Memory-safe archive extraction library with security validation
//! Zip bomb detection.

use crate::ArchiveError;
use crate::CompressionRatio;
use crate::Malformation;
use crate::Result;
use crate::SecurityConfig;
use crate::SolidLimits;
use crate::ZipBombDetails;
use crate::ZipBombScope;
use crate::inspection::EntryCompression;
use crate::inspection::SolidBlock;

/// A byte count as stored in the archive.
///
/// Distinct from [`Uncompressed`] so the two sides of the compression ratio
/// cannot be swapped at a call site.
///
/// # Examples
///
/// ```
/// use exarch_core::Compressed;
///
/// assert_eq!(Compressed(512).0, 512);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Compressed(pub u64);

/// A byte count after decoding.
///
/// # Examples
///
/// ```
/// use exarch_core::Uncompressed;
///
/// assert_eq!(Uncompressed(1024).0, 1024);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Uncompressed(pub u64);

/// Validates compression ratio to detect potential zip bombs.
///
/// # Errors
///
/// Returns an error if the compression ratio exceeds the configured maximum.
#[cfg_attr(not(feature = "testing"), allow(unreachable_pub))]
pub fn validate_compression_ratio(
    compressed: Compressed,
    uncompressed: Uncompressed,
    config: &SecurityConfig,
) -> Result<()> {
    check_ratio(
        ZipBombScope::Entry,
        compressed,
        uncompressed,
        config.max_compression_ratio.get(),
    )
}

/// Validates the compression ratio of a whole solid block against
/// [`SolidLimits::max_ratio`](crate::SolidLimits::max_ratio).
///
/// Under [`SolidArchives::Refuse`](crate::SolidArchives::Refuse), which
/// refuses solid archives before any data is read, the default limit applies
/// to the block sizes a listing reports.
///
/// A block without data (`uncompressed == 0`) is accepted.
///
/// # Errors
///
/// Returns [`ArchiveError::ZipBomb`] when the block claims data but no
/// compressed bytes, or when the ratio exceeds the configured maximum.
pub(crate) fn validate_solid_block_ratio(
    block: &SolidBlock,
    config: &SecurityConfig,
) -> Result<()> {
    check_ratio(
        ZipBombScope::SolidBlock,
        block.compressed,
        block.uncompressed,
        config
            .solid_archives
            .limits()
            .unwrap_or(SolidLimits::DEFAULT)
            .max_ratio
            .get(),
    )
}

/// Applies the ratio check that fits how `compression` attributes data to an
/// entry of `uncompressed_size` bytes: per file for
/// [`EntryCompression::Entry`], per block for a solid block start, none
/// otherwise.
///
/// # Errors
///
/// Returns the error of [`validate_compression_ratio`] or
/// [`validate_solid_block_ratio`].
pub(crate) fn validate_entry_compression(
    compression: &EntryCompression,
    uncompressed_size: Uncompressed,
    config: &SecurityConfig,
) -> Result<()> {
    match compression {
        EntryCompression::Entry(compressed) => {
            validate_compression_ratio(*compressed, uncompressed_size, config)
        }
        EntryCompression::SolidBlockStart(block) => validate_solid_block_ratio(block, config),
        EntryCompression::Unknown | EntryCompression::SolidBlockMember => Ok(()),
    }
}

/// Whether an entry of `uncompressed_size` bytes from `compressed_size` bytes
/// is within `max_ratio`, by the same rule as [`validate_compression_ratio`].
pub(crate) fn ratio_admitted(
    compressed: Compressed,
    uncompressed: Uncompressed,
    max_ratio: f64,
) -> bool {
    check_ratio(ZipBombScope::Entry, compressed, uncompressed, max_ratio).is_ok()
}

fn check_ratio(
    scope: ZipBombScope,
    compressed: Compressed,
    uncompressed: Uncompressed,
    max_ratio: f64,
) -> Result<()> {
    let details = ZipBombDetails::new(scope, compressed, uncompressed);
    match details.ratio() {
        // HIGH-001: data claimed from zero compressed bytes bypasses any finite
        // limit
        CompressionRatio::Unbounded => Err(ArchiveError::ZipBomb(details)),
        CompressionRatio::Finite(ratio) if ratio > max_ratio => Err(ArchiveError::ZipBomb(details)),
        CompressionRatio::Finite(_) => Ok(()),
    }
}

/// Bounds the sum of declared compressed sizes by the archive's data region.
///
/// Each entry's declared compressed size is charged against the budget, so
/// the sizes of all entries together never exceed the space the archive can
/// actually hold. Entries that declare more data than remains are rejected,
/// which keeps per-entry compression-ratio checks anchored to real archive
/// bytes.
#[derive(Debug)]
pub(crate) struct DeclaredCompressedBudget {
    remaining: u64,
}

impl DeclaredCompressedBudget {
    /// Creates a budget of `data_region` bytes, the archive space that can
    /// hold compressed entry data.
    pub(crate) const fn new(data_region: u64) -> Self {
        Self {
            remaining: data_region,
        }
    }

    /// Charges one declared compressed size against the budget.
    ///
    /// # Errors
    ///
    /// Returns [`ArchiveError::InvalidArchive`] when `declared` exceeds what
    /// is left of the budget.
    pub(crate) fn charge(&mut self, declared: u64) -> Result<()> {
        self.remaining = self
            .remaining
            .checked_sub(declared)
            .ok_or_else(|| ArchiveError::from(Malformation::DeclaredCompressedExceedsArchive))?;
        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use std::assert_matches;

    #[test]
    fn test_validate_compression_ratio_safe() {
        let config = SecurityConfig::default();
        let result = validate_compression_ratio(Compressed(1000), Uncompressed(10_000), &config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_compression_ratio_bomb() {
        let config = SecurityConfig::default();
        let result = validate_compression_ratio(Compressed(1000), Uncompressed(1_000_000), &config);
        assert_matches!(result, Err(ArchiveError::ZipBomb(_)));
    }

    #[test]
    fn test_validate_compression_ratio_zero_compressed() {
        let config = SecurityConfig::default();

        // HIGH-001: Zero compressed with non-zero uncompressed is an unbounded
        // ratio
        let result = validate_compression_ratio(Compressed(0), Uncompressed(1000), &config);
        assert_matches!(
            result,
            Err(ArchiveError::ZipBomb(details))
                if details.ratio() == CompressionRatio::Unbounded
                    && details.scope() == ZipBombScope::Entry,
            "zero compressed with non-zero uncompressed should be rejected"
        );
    }

    #[test]
    fn test_solid_block_ratio_uses_solid_limit() {
        let config = SecurityConfig::default();
        // 138x passes the solid limit (1000) though it exceeds the per-file one
        // (100).
        assert!(
            validate_solid_block_ratio(
                &SolidBlock::new(Compressed(1000), Uncompressed(138_000)),
                &config
            )
            .is_ok()
        );
        assert_matches!(
            validate_solid_block_ratio(
                &SolidBlock::new(Compressed(1000), Uncompressed(1_500_000)),
                &config
            ),
            Err(ArchiveError::ZipBomb(_))
        );
    }

    #[test]
    fn test_ratio_equal_to_limit_is_accepted_and_just_above_is_rejected() {
        let config = SecurityConfig::default();
        assert!(
            validate_solid_block_ratio(
                &SolidBlock::new(Compressed(10), Uncompressed(10_000)),
                &config
            )
            .is_ok()
        );
        assert_matches!(
            validate_solid_block_ratio(
                &SolidBlock::new(Compressed(10), Uncompressed(10_001)),
                &config
            ),
            Err(ArchiveError::ZipBomb(_))
        );
        assert!(validate_compression_ratio(Compressed(10), Uncompressed(1000), &config).is_ok());
        assert_matches!(
            validate_compression_ratio(Compressed(10), Uncompressed(1001), &config),
            Err(ArchiveError::ZipBomb(_))
        );
    }

    #[test]
    fn test_solid_block_ratio_empty_and_zero_compressed() {
        let config = SecurityConfig::default();
        assert!(
            validate_solid_block_ratio(&SolidBlock::new(Compressed(0), Uncompressed(0)), &config)
                .is_ok()
        );
        assert_matches!(
            validate_solid_block_ratio(&SolidBlock::new(Compressed(0), Uncompressed(10)), &config),
            Err(ArchiveError::ZipBomb(details))
                if details.ratio() == CompressionRatio::Unbounded
                    && details.scope() == ZipBombScope::SolidBlock
        );
    }

    #[test]
    fn test_entry_compression_dispatch() {
        let config = SecurityConfig::default();
        let block = EntryCompression::SolidBlockStart(SolidBlock::new(
            Compressed(1000),
            Uncompressed(138_000),
        ));
        assert!(validate_entry_compression(&block, Uncompressed(1), &config).is_ok());
        assert!(
            validate_entry_compression(
                &EntryCompression::SolidBlockMember,
                Uncompressed(u64::MAX),
                &config
            )
            .is_ok()
        );
        assert!(
            validate_entry_compression(&EntryCompression::Unknown, Uncompressed(u64::MAX), &config)
                .is_ok()
        );
        assert_matches!(
            validate_entry_compression(
                &EntryCompression::Entry(Compressed(1000)),
                Uncompressed(138_000),
                &config
            ),
            Err(ArchiveError::ZipBomb(_))
        );
    }

    #[test]
    fn test_declared_budget_accepts_sizes_within_region() {
        let mut budget = DeclaredCompressedBudget::new(100);
        budget.charge(60).expect("fits");
        budget.charge(40).expect("fits exactly");
        budget.charge(0).expect("zero is free");
    }

    #[test]
    fn test_declared_budget_rejects_cumulative_overrun() {
        let mut budget = DeclaredCompressedBudget::new(100);
        budget.charge(60).expect("fits");
        assert_matches!(budget.charge(41), Err(ArchiveError::InvalidArchive(_)));
    }

    #[test]
    fn test_declared_budget_rejects_u64_max() {
        let mut budget = DeclaredCompressedBudget::new(u64::MAX - 1);
        assert_matches!(
            budget.charge(u64::MAX),
            Err(ArchiveError::InvalidArchive(_))
        );
    }

    // H-TEST-3: Division by zero edge cases test
    #[test]
    fn test_compressed_size_zero_with_uncompressed_zero() {
        let config = SecurityConfig::default();

        // Both zero - should be OK (empty file)
        let result = validate_compression_ratio(Compressed(0), Uncompressed(0), &config);
        assert!(result.is_ok(), "0/0 should be handled gracefully");
    }

    #[test]
    fn test_compressed_size_zero_with_large_uncompressed() {
        let config = SecurityConfig::default();

        // HIGH-001: Compressed size zero with uncompressed > 0 is INVALID
        // This prevents zip bomb bypass for stored compression
        let result = validate_compression_ratio(Compressed(0), Uncompressed(1_000_000), &config);
        assert_matches!(
            result,
            Err(ArchiveError::ZipBomb(details))
                if details.ratio() == CompressionRatio::Unbounded,
            "compressed_size == 0 but uncompressed_size > 0 should be rejected (HIGH-001 fix)"
        );
    }

    #[test]
    fn test_very_small_compressed_large_uncompressed() {
        let config = SecurityConfig::default();

        // Very small compressed size (1 byte) with large uncompressed
        // This should trigger zip bomb detection
        let result = validate_compression_ratio(Compressed(1), Uncompressed(1_000_000), &config);
        assert_matches!(
            result,
            Err(ArchiveError::ZipBomb(_)),
            "extremely high compression ratio should be detected as zip bomb"
        );
    }

    #[test]
    fn test_equal_sizes() {
        let config = SecurityConfig::default();

        // No compression (ratio = 1.0)
        let result = validate_compression_ratio(Compressed(1000), Uncompressed(1000), &config);
        assert!(result.is_ok(), "ratio of 1.0 should be safe");
    }

    #[test]
    fn test_negative_compression() {
        let config = SecurityConfig::default();

        // Compressed larger than uncompressed (poor compression)
        let result = validate_compression_ratio(Compressed(2000), Uncompressed(1000), &config);
        assert!(
            result.is_ok(),
            "ratio < 1.0 should be safe (negative compression)"
        );
    }
}