88 lines
2.7 KiB
Rust
88 lines
2.7 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! Archive entries (hostile even when signed: a release key can sign a mistake): what may be
|
|
//! unpacked, and where.
|
|
|
|
/// Entries in one archive at most.
|
|
pub const MAX_ARCHIVE_ENTRIES: usize = 100_000;
|
|
/// Bytes unpacked from one archive at most (4 GiB).
|
|
pub const MAX_ARCHIVE_BYTES: u64 = 4 * 1024 * 1024 * 1024;
|
|
|
|
/// The kind of an archive entry.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum EntryKind {
|
|
/// A regular file.
|
|
File,
|
|
/// A directory.
|
|
Dir,
|
|
/// A symbolic link (refused).
|
|
Symlink,
|
|
/// A hard link (refused).
|
|
Hardlink,
|
|
/// Devices, FIFOs and anything else (refused).
|
|
Other,
|
|
}
|
|
|
|
/// Why an archive is refused.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
|
pub enum ArchiveError {
|
|
/// Links, devices and other special entries are not unpacked.
|
|
#[error("the archive holds a link or a special file")]
|
|
Kind,
|
|
/// A path that is absolute, climbs out, or is not plain.
|
|
#[error("the archive holds a path outside the release")]
|
|
Path,
|
|
/// More bytes than allowed.
|
|
#[error("the archive is too large")]
|
|
TooLarge,
|
|
/// More entries than allowed.
|
|
#[error("the archive has too many entries")]
|
|
TooMany,
|
|
/// Not a readable archive.
|
|
#[error("the archive cannot be read")]
|
|
Corrupt,
|
|
}
|
|
|
|
impl ArchiveError {
|
|
/// The stable code used in reports.
|
|
pub fn code(&self) -> &'static str {
|
|
"bad_archive"
|
|
}
|
|
}
|
|
|
|
/// Check one entry and return its path as plain components (no `.`; empty only for the root dir).
|
|
pub fn check_entry(kind: EntryKind, path: &str, size: u64) -> Result<Vec<String>, ArchiveError> {
|
|
match kind {
|
|
EntryKind::File | EntryKind::Dir => {}
|
|
_ => return Err(ArchiveError::Kind),
|
|
}
|
|
if size > MAX_ARCHIVE_BYTES {
|
|
return Err(ArchiveError::TooLarge);
|
|
}
|
|
if path.is_empty()
|
|
|| path.len() > 4096
|
|
|| path.starts_with('/')
|
|
|| path
|
|
.bytes()
|
|
.any(|b| b == 0 || b == b'\\' || b == b':' || b.is_ascii_control())
|
|
{
|
|
return Err(ArchiveError::Path);
|
|
}
|
|
let mut parts = Vec::new();
|
|
for c in path.split('/') {
|
|
match c {
|
|
"" | "." => {}
|
|
".." => return Err(ArchiveError::Path),
|
|
c => parts.push(c.to_owned()),
|
|
}
|
|
}
|
|
// The release's own root (`./`, as `tar -C dir -czf x .` writes it) names nothing: allowed for a
|
|
// directory, refused for anything else.
|
|
if parts.is_empty() && kind != EntryKind::Dir {
|
|
return Err(ArchiveError::Path);
|
|
}
|
|
Ok(parts)
|
|
}
|