use std::{
collections::BTreeSet,
io::{Cursor, Read},
};
use thiserror::Error;
use zip::ZipArchive;
use crate::device::{DeviceCatalog, DeviceKey};
pub const MAXIMUM_PACKAGE_ARCHIVE_BYTES: usize = 256 * 1024 * 1024;
pub const MAXIMUM_PACKAGE_FILES: usize = 65_536;
pub const MAXIMUM_PACKAGE_FILE_NAME_BYTES: usize = 16 * 1024;
pub const MAXIMUM_PACKAGE_UNCOMPRESSED_BYTES: u64 = 1024 * 1024 * 1024;
pub const MAXIMUM_PACKAGE_FILE_BYTES: u64 = 64 * 1024 * 1024;
#[derive(Debug, Clone, Copy)]
pub struct PackageLimits {
pub archive_bytes: usize,
pub files: usize,
pub file_name_bytes: usize,
pub uncompressed_bytes: u64,
pub file_bytes: u64,
}
impl Default for PackageLimits {
fn default() -> Self {
Self {
archive_bytes: 64 * 1024 * 1024,
files: 1024,
file_name_bytes: 1024,
uncompressed_bytes: 64 * 1024 * 1024,
file_bytes: 8 * 1024 * 1024,
}
}
}
impl PackageLimits {
pub const fn validate(self) -> Result<(), PackageError> {
if self.archive_bytes == 0
|| self.files == 0
|| self.file_name_bytes == 0
|| self.uncompressed_bytes == 0
|| self.file_bytes == 0
{
return Err(PackageError::ZeroLimit);
}
if self.archive_bytes > MAXIMUM_PACKAGE_ARCHIVE_BYTES {
return Err(PackageError::LimitTooLarge("archive_bytes"));
}
if self.files > MAXIMUM_PACKAGE_FILES {
return Err(PackageError::LimitTooLarge("files"));
}
if self.file_name_bytes > MAXIMUM_PACKAGE_FILE_NAME_BYTES {
return Err(PackageError::LimitTooLarge("file_name_bytes"));
}
if self.uncompressed_bytes > MAXIMUM_PACKAGE_UNCOMPRESSED_BYTES {
return Err(PackageError::LimitTooLarge("uncompressed_bytes"));
}
if self.file_bytes > MAXIMUM_PACKAGE_FILE_BYTES {
return Err(PackageError::LimitTooLarge("file_bytes"));
}
Ok(())
}
pub const fn with_archive_bytes(mut self, archive_bytes: usize) -> Self {
self.archive_bytes = archive_bytes;
self
}
pub const fn with_files(mut self, files: usize) -> Self {
self.files = files;
self
}
pub const fn with_file_name_bytes(mut self, file_name_bytes: usize) -> Self {
self.file_name_bytes = file_name_bytes;
self
}
pub const fn with_uncompressed_bytes(mut self, uncompressed_bytes: u64) -> Self {
self.uncompressed_bytes = uncompressed_bytes;
self
}
pub const fn with_file_bytes(mut self, file_bytes: u64) -> Self {
self.file_bytes = file_bytes;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageReport {
pub devices: Vec<DeviceKey>,
pub inspected_files: usize,
pub skipped_json: Vec<String>,
}
#[derive(Debug, Error)]
pub enum PackageError {
#[error("package limits must be greater than zero")]
ZeroLimit,
#[error("package limit {0} exceeds the supported maximum")]
LimitTooLarge(&'static str),
#[error("package contains {actual} bytes and exceeds the {maximum}-byte archive limit")]
ArchiveSize {
actual: usize,
maximum: usize,
},
#[error("ZIP error: {0}")]
Zip(#[from] zip::result::ZipError),
#[error("read error: {0}")]
Io(#[from] std::io::Error),
#[error("archive contains more entries than allowed")]
FileCount,
#[error("archive entry name contains {0} UTF-8 bytes")]
FileNameSize(usize),
#[error("file {0} exceeds the configured limit")]
FileSize(String),
#[error("total uncompressed size exceeds the configured limit")]
TotalSize,
#[error("unsafe archive path: {0}")]
UnsafePath(String),
#[error("DeviceInfo error: {0}")]
Device(#[from] crate::device::CatalogLoadError),
#[error("package contains duplicate device type {expanded_type} revision {device_revision}")]
DuplicateDevice {
expanded_type: u16,
device_revision: u8,
},
}
pub fn import_package(
bytes: &[u8],
catalog: &mut DeviceCatalog,
limits: PackageLimits,
) -> Result<PackageReport, PackageError> {
limits.validate()?;
if bytes.len() > limits.archive_bytes {
return Err(PackageError::ArchiveSize {
actual: bytes.len(),
maximum: limits.archive_bytes,
});
}
let mut archive = ZipArchive::new(Cursor::new(bytes))?;
if archive.len() > limits.files {
return Err(PackageError::FileCount);
}
let mut total = 0u64;
let mut actual_json_bytes = 0u64;
let mut devices = Vec::new();
let mut skipped_json = Vec::new();
let mut package_keys = BTreeSet::new();
let mut staging = catalog.clone();
for index in 0..archive.len() {
let file = archive.by_index(index)?;
if file.name().len() > limits.file_name_bytes {
return Err(PackageError::FileNameSize(file.name().len()));
}
let name = file.name().to_owned();
if file.enclosed_name().is_none() {
return Err(PackageError::UnsafePath(name));
}
if file.size() > limits.file_bytes {
return Err(PackageError::FileSize(name));
}
total = total
.checked_add(file.size())
.ok_or(PackageError::TotalSize)?;
if total > limits.uncompressed_bytes {
return Err(PackageError::TotalSize);
}
if file.is_file() && name.to_ascii_lowercase().ends_with(".json") {
let capacity = usize::try_from(file.size().min(limits.file_bytes)).unwrap_or(0);
let mut content = Vec::with_capacity(capacity);
let read_limit = limits.file_bytes.saturating_add(1);
file.take(read_limit).read_to_end(&mut content)?;
if u64::try_from(content.len()).unwrap_or(u64::MAX) > limits.file_bytes {
return Err(PackageError::FileSize(name));
}
actual_json_bytes = actual_json_bytes
.checked_add(u64::try_from(content.len()).unwrap_or(u64::MAX))
.ok_or(PackageError::TotalSize)?;
if actual_json_bytes > limits.uncompressed_bytes {
return Err(PackageError::TotalSize);
}
match staging.load_json(&content) {
Ok(key) if package_keys.insert(key) => devices.push(key),
Ok(key) => {
return Err(PackageError::DuplicateDevice {
expanded_type: key.expanded_type,
device_revision: key.device_revision,
});
}
Err(
error @ (crate::device::CatalogLoadError::JsonLimit { .. }
| crate::device::CatalogLoadError::Schema(
crate::device::SchemaError::CatalogProfileLimit(_),
)),
) => return Err(PackageError::Device(error)),
Err(_) => skipped_json.push(name),
}
}
}
*catalog = staging;
Ok(PackageReport {
devices,
inspected_files: archive.len(),
skipped_json,
})
}