mod sparse;
mod window;
use window::WindowWalk;
use std::collections::HashSet;
use std::io::Read;
use std::time::Duration;
use std::time::SystemTime;
use tar::Entry;
use tar::EntryType;
use tar::Header;
use tar::PaxExtensions;
use crate::ArchiveError;
use crate::Malformation;
use crate::Result;
use crate::TarAmbiguity;
use crate::TarExtensionKind;
use crate::TarHeaderField;
use crate::TarNonRegularKind;
use crate::TarPathKey;
use crate::TarSparsePolicy;
use crate::formats::tar_metadata_limit::MetadataWindow;
const PAX_PATH: &[u8] = b"path";
const PAX_LINKPATH: &[u8] = b"linkpath";
const PAX_SIZE: &[u8] = b"size";
const PAX_MTIME: &[u8] = b"mtime";
const PAX_SPARSE_PREFIX: &[u8] = b"GNU.sparse.";
const PAX_REALSIZE: &[u8] = b"SCHILY.realsize";
const PAX_HOLESDATA: &[u8] = b"SUN.holesdata";
pub(crate) const MAX_PAX_RECORD_BYTES: usize = 999_999;
const MAX_ECHOED_BYTES: usize = 256;
const SIZE_FIELD: std::ops::Range<usize> = 124..136;
const CHECKSUM_FIELD: std::ops::Range<usize> = 148..156;
const PREFIX_OFFSET: usize = 345;
const BASE256_MARKER: u8 = 0x80;
const BASE256_VALUE: std::ops::Range<usize> = 4..12;
const BLOCK: usize = 512;
#[derive(Clone, Copy, PartialEq, Eq)]
enum PaxScope {
Local,
Global,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum PrecedingEntry {
#[default]
Other,
GlobalPax,
}
#[derive(Default)]
struct LocalOverrides {
path: Option<Vec<u8>>,
linkpath: Option<Vec<u8>>,
mtime: Option<SystemTime>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) enum SkipPlan {
#[default]
Drain,
TarSkip {
end: u64,
},
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct CheckedHeaders {
pub(crate) preceding: PrecedingEntry,
pub(crate) mtime: Option<SystemTime>,
pub(crate) skip: SkipPlan,
}
fn parse_pax_mtime(value: &[u8]) -> Option<SystemTime> {
let (negative, unsigned) = value
.strip_prefix(b"-")
.map_or((false, value), |rest| (true, rest));
let (whole, fraction) = match unsigned.iter().position(|&b| b == b'.') {
Some(dot) => {
let (whole, rest) = unsigned.split_at(dot);
(whole, Some(rest.get(1..)?))
}
None => (unsigned, None),
};
let digits_only = |part: &[u8]| !part.is_empty() && part.iter().all(u8::is_ascii_digit);
if !digits_only(whole) || fraction.is_some_and(|f| !digits_only(f)) {
return None;
}
let seconds: u64 = std::str::from_utf8(whole).ok()?.parse().ok()?;
let nanos: u32 = match fraction {
Some(f) => {
let nine: Vec<u8> = f
.iter()
.copied()
.chain(std::iter::repeat(b'0'))
.take(9)
.collect();
std::str::from_utf8(&nine).ok()?.parse().ok()?
}
None => 0,
};
let offset = Duration::new(seconds, nanos);
if negative {
SystemTime::UNIX_EPOCH.checked_sub(offset)
} else {
SystemTime::UNIX_EPOCH.checked_add(offset)
}
}
fn lossy(bytes: &[u8]) -> String {
let shown = bytes.get(..MAX_ECHOED_BYTES).unwrap_or(bytes);
let mut text = crate::bytes_text(shown);
if shown.len() < bytes.len() {
text.push_str("...");
}
text
}
fn record_len(key: &[u8], value: &[u8]) -> usize {
let base = key.len().saturating_add(value.len()).saturating_add(3);
let mut digits = 1;
loop {
let total = base.saturating_add(digits);
let needed = total.checked_ilog10().map_or(1, |d| d as usize + 1);
if needed <= digits {
return total;
}
digits = needed;
}
}
#[derive(Clone, Copy)]
struct PaxRecord<'a> {
key: &'a [u8],
value: &'a [u8],
}
const TAR_READ_KEYS: [&[u8]; 5] = [PAX_PATH, PAX_LINKPATH, PAX_SIZE, b"uid", b"gid"];
const TAR_STOPPING_KEYS: [&[u8]; 3] = [PAX_SIZE, b"uid", b"gid"];
struct ParsedPax<'w> {
body: &'w [u8],
records: Vec<PaxRecord<'w>>,
declares_size: bool,
}
impl<'w> ParsedPax<'w> {
fn parse(body: &'w [u8]) -> Result<Self> {
let records = parse_records(body)?;
let declares_size = records.iter().any(|record| record.key == PAX_SIZE);
Ok(Self {
body,
records,
declares_size,
})
}
}
fn parse_records(body: &[u8]) -> Result<Vec<PaxRecord<'_>>> {
split_records(body)?
.into_iter()
.map(|record| {
let space = record
.iter()
.position(|&b| b == b' ')
.ok_or(TarAmbiguity::MalformedRecord)?;
let text = record
.get(space.saturating_add(1)..)
.and_then(|rest| rest.strip_suffix(b"\n"))
.ok_or(TarAmbiguity::MalformedRecord)?;
let equals = text
.iter()
.position(|&b| b == b'=')
.ok_or(TarAmbiguity::MalformedRecord)?;
let (key, tail) = text.split_at(equals);
if key.contains(&b'\n') {
return Err(TarAmbiguity::MalformedRecord.into());
}
Ok(PaxRecord {
key,
value: tail.get(1..).unwrap_or_default(),
})
})
.collect()
}
struct CrateRecord {
key_offset: usize,
after_unparsed: bool,
}
fn offset_in(body: &[u8], part: &[u8]) -> Option<usize> {
body.subslice_range(part).map(|range| range.start)
}
fn crate_records(body: &[u8]) -> Result<Vec<CrateRecord>> {
let mut after_unparsed = false;
let mut yielded = Vec::new();
for item in PaxExtensions::new(body) {
match item {
Ok(record) => yielded.push(CrateRecord {
key_offset: offset_in(body, record.key_bytes())
.ok_or(TarAmbiguity::MalformedRecord)?,
after_unparsed,
}),
Err(_) => after_unparsed = true,
}
}
Ok(yielded)
}
fn check_crate_view(pax: &ParsedPax<'_>, header_size: Option<u64>) -> Result<()> {
let body = pax.body;
let yielded = crate_records(body)?;
let mut seen = yielded.iter().peekable();
for record in &pax.records {
let key_offset = offset_in(body, record.key).ok_or(TarAmbiguity::MalformedRecord)?;
let matched = seen.next_if(|y| y.key_offset == key_offset);
if TAR_READ_KEYS.contains(&record.key) {
let stops = TAR_STOPPING_KEYS.contains(&record.key);
let hidden = matched.is_none_or(|y| stops && y.after_unparsed);
let equals_header = record.key == PAX_SIZE
&& header_size.is_some_and(|size| canonical_u64(record.value) == Some(size));
if hidden && !equals_header {
return Err(TarAmbiguity::MalformedRecord.into());
}
}
}
if seen.next().is_some() {
return Err(TarAmbiguity::MalformedRecord.into());
}
Ok(())
}
fn scan_records(
pax: &ParsedPax<'_>,
scope: PaxScope,
header_size: Option<u64>,
) -> Result<LocalOverrides> {
check_crate_view(pax, header_size)?;
let mut seen: HashSet<&[u8]> = HashSet::new();
let mut overrides = LocalOverrides::default();
for &PaxRecord { key, value } in &pax.records {
if key.is_empty() {
return Err(TarAmbiguity::EmptyKey.into());
}
if record_len(key, value) > MAX_PAX_RECORD_BYTES {
return Err(TarAmbiguity::OversizedRecord { key: lossy(key) }.into());
}
if !seen.insert(key) {
return Err(TarAmbiguity::DuplicateRecord { key: lossy(key) }.into());
}
if key.starts_with(PAX_SPARSE_PREFIX) || key == PAX_REALSIZE || key == PAX_HOLESDATA {
return Err(TarAmbiguity::UnsupportedRecord { key: lossy(key) }.into());
}
let overrides_entry = [PAX_PATH, PAX_LINKPATH, PAX_SIZE].contains(&key);
if scope == PaxScope::Global && overrides_entry {
return Err(TarAmbiguity::GlobalOverride { key: lossy(key) }.into());
}
match key {
PAX_PATH => overrides.path = Some(value.to_vec()),
PAX_LINKPATH => overrides.linkpath = Some(value.to_vec()),
PAX_MTIME => overrides.mtime = parse_pax_mtime(value),
PAX_SIZE if !is_decimal(value) => {
return Err(TarAmbiguity::InvalidSize.into());
}
_ => {}
}
}
Ok(overrides)
}
fn canonical_u64(value: &[u8]) -> Option<u64> {
let canonical =
value.iter().all(u8::is_ascii_digit) && (value.len() == 1 || value.first() != Some(&b'0'));
canonical
.then(|| std::str::from_utf8(value).ok()?.parse().ok())
.flatten()
}
fn is_decimal(value: &[u8]) -> bool {
std::str::from_utf8(value)
.is_ok_and(|text| text.bytes().all(|b| b.is_ascii_digit()) && text.parse::<u64>().is_ok())
}
fn split_records(body: &[u8]) -> Result<Vec<&[u8]>> {
let mut records = Vec::new();
let mut rest = body;
while !rest.is_empty() {
let space = rest
.iter()
.position(|&b| b == b' ')
.ok_or(TarAmbiguity::MalformedRecord)?;
let (digits, _) = rest
.split_at_checked(space)
.ok_or(TarAmbiguity::MalformedRecord)?;
let len = canonical_u64(digits)
.and_then(|len| usize::try_from(len).ok())
.filter(|len| *len >= 1)
.ok_or(TarAmbiguity::MalformedRecord)?;
let (record, tail) = rest
.split_at_checked(len)
.ok_or(TarAmbiguity::MalformedRecord)?;
if record.last() != Some(&b'\n') {
return Err(TarAmbiguity::MalformedRecord.into());
}
records.push(record);
rest = tail;
}
Ok(records)
}
fn is_plain_octal(field: &[u8]) -> bool {
let leading_spaces = field.iter().take_while(|&&b| b == b' ').count();
let Some(trimmed) = field.get(leading_spaces..) else {
return false;
};
let digits = trimmed
.iter()
.take_while(|b| (b'0'..=b'7').contains(b))
.count();
digits > 0
&& trimmed
.get(digits..)
.is_some_and(|rest| rest.iter().all(|&b| b == b' ' || b == 0))
}
fn is_plain_base256(field: &[u8]) -> bool {
field.first() == Some(&BASE256_MARKER)
&& field
.get(1..4)
.is_some_and(|zeros| zeros.iter().all(|&b| b == 0))
}
fn octal_value(field: &[u8]) -> Option<u64> {
field
.iter()
.skip_while(|&&b| b == b' ')
.take_while(|b| (b'0'..=b'7').contains(b))
.try_fold(0u64, |acc, &b| {
acc.checked_mul(8)?
.checked_add(u64::from(b.checked_sub(b'0')?))
})
}
fn numeric_value(field: &[u8]) -> Option<u64> {
if is_plain_base256(field) {
field
.get(BASE256_VALUE)
.and_then(|value| value.as_array::<8>())
.copied()
.map(u64::from_be_bytes)
} else if is_plain_octal(field) {
octal_value(field)
} else {
None
}
}
fn check_header_fields(header: &Header) -> Result<()> {
check_header_block(header.as_bytes(), header.as_ustar().is_some())
}
fn check_header_block(bytes: &[u8; 512], has_ustar_magic: bool) -> Result<()> {
if !is_plain_octal(&bytes[SIZE_FIELD]) && !is_plain_base256(&bytes[SIZE_FIELD]) {
return Err(TarAmbiguity::NonCanonicalNumber {
field: TarHeaderField::Size,
}
.into());
}
if !is_plain_octal(&bytes[CHECKSUM_FIELD]) {
return Err(TarAmbiguity::NonCanonicalNumber {
field: TarHeaderField::Checksum,
}
.into());
}
if !has_ustar_magic && bytes[PREFIX_OFFSET] != 0 {
return Err(TarAmbiguity::PrefixWithoutUstar.into());
}
Ok(())
}
pub(crate) fn check_trailing_headers(
window: &MetadataWindow<'_>,
preceding: PrecedingEntry,
) -> Result<()> {
match preceding {
PrecedingEntry::GlobalPax => window::check_trailing_global_padding(window),
PrecedingEntry::Other => Ok(()),
}
}
pub(crate) fn check_entry_headers<R: Read>(
entry: &mut Entry<'_, R>,
window: &MetadataWindow<'_>,
metadata_limit: u64,
sparse: TarSparsePolicy,
preceding: PrecedingEntry,
) -> Result<CheckedHeaders> {
let walk = window::walk_window(
window,
preceding,
entry.raw_header_position(),
entry.raw_file_position(),
sparse,
);
let mtime = check_parsed_headers(entry, metadata_limit, sparse, &walk)?;
walk.outcome?;
let preceding = if entry.header().entry_type() == EntryType::XGlobalHeader {
PrecedingEntry::GlobalPax
} else {
PrecedingEntry::Other
};
let skip = skip_plan(entry, window)?;
Ok(CheckedHeaders {
preceding,
mtime,
skip,
})
}
fn skip_plan<R: Read>(entry: &Entry<'_, R>, window: &MetadataWindow<'_>) -> Result<SkipPlan> {
if entry.header().entry_type() != EntryType::GNUSparse {
return Ok(SkipPlan::Drain);
}
let archived = entry
.header()
.entry_size()
.map_err(|_| TarAmbiguity::NonCanonicalNumber {
field: TarHeaderField::Size,
})?;
let end = archived
.checked_next_multiple_of(BLOCK as u64)
.and_then(|padded| window.end().checked_add(padded))
.ok_or(TarAmbiguity::WindowMismatch)?;
Ok(SkipPlan::TarSkip { end })
}
fn check_parsed_headers<R: Read>(
entry: &mut Entry<'_, R>,
metadata_limit: u64,
sparse: TarSparsePolicy,
walk: &WindowWalk<'_>,
) -> Result<Option<SystemTime>> {
check_header_fields(entry.header())?;
match entry.header().entry_type() {
EntryType::XHeader => Err(unrecognized(TarExtensionKind::Pax)),
EntryType::GNULongName => Err(unrecognized(TarExtensionKind::LongName)),
EntryType::GNULongLink => Err(unrecognized(TarExtensionKind::LongLink)),
EntryType::GNUSparse => match sparse {
TarSparsePolicy::Refuse => Err(TarAmbiguity::SparseEntry.into()),
TarSparsePolicy::Expand => check_sparse_headers(entry, walk),
},
EntryType::XGlobalHeader => check_global_header(entry, metadata_limit).map(|()| None),
kind if kind.as_byte() == b'V' => check_volume_label(entry, walk).map(|()| None),
_ => check_local_headers(entry, walk),
}
}
fn unrecognized(kind: TarExtensionKind) -> ArchiveError {
TarAmbiguity::UnrecognizedHeader { kind }.into()
}
fn pax_header_unreadable(error: &std::io::Error) -> ArchiveError {
ArchiveError::InvalidArchive(Malformation::TarPaxHeaderUnreadable {
detail: error.to_string().into(),
})
}
fn check_volume_label<R: Read>(entry: &Entry<'_, R>, walk: &WindowWalk<'_>) -> Result<()> {
if let Some(&kind) = walk.extensions.first() {
return Err(TarAmbiguity::ExtensionBeforeLabel { kind }.into());
}
let size = entry
.header()
.entry_size()
.map_err(|_| TarAmbiguity::NonCanonicalNumber {
field: TarHeaderField::Size,
})?;
if size == 0 {
return Ok(());
}
Err(TarAmbiguity::NonRegularPayload {
kind: TarNonRegularKind::VolumeLabel,
size,
path: crate::bytes_text(&entry.path_bytes()),
}
.into())
}
fn check_sparse_headers<R: Read>(
entry: &mut Entry<'_, R>,
walk: &WindowWalk<'_>,
) -> Result<Option<SystemTime>> {
if walk.pax.as_ref().is_some_and(|pax| pax.declares_size) {
return Err(TarAmbiguity::UnsupportedRecord {
key: lossy(PAX_SIZE),
}
.into());
}
check_local_headers(entry, walk)
}
fn check_local_headers<R: Read>(
entry: &mut Entry<'_, R>,
walk: &WindowWalk<'_>,
) -> Result<Option<SystemTime>> {
let tar_sees_pax = entry
.pax_extensions()
.map_err(|e| pax_header_unreadable(&e))?
.is_some();
let overrides = match (walk.pax.as_ref(), tar_sees_pax) {
(Some(pax), true) => scan_records(pax, PaxScope::Local, entry.header().size().ok())?,
(None, false) => return Ok(None),
(None, true) if walk.outcome.is_err() => return Ok(None),
_ => return Err(TarAmbiguity::WindowMismatch.into()),
};
if let Some(pax_path) = overrides.path
&& *entry.path_bytes() != *pax_path
{
return Err(TarAmbiguity::StackedLongRecord {
key: TarPathKey::Path,
}
.into());
}
if let Some(pax_linkpath) = overrides.linkpath
&& entry.link_name_bytes().as_deref() != Some(pax_linkpath.as_slice())
{
return Err(TarAmbiguity::StackedLongRecord {
key: TarPathKey::LinkPath,
}
.into());
}
Ok(overrides.mtime)
}
fn check_global_header<R: Read>(entry: &mut Entry<'_, R>, metadata_limit: u64) -> Result<()> {
let size = entry.size();
if size > metadata_limit {
return Err(TarAmbiguity::GlobalTooLarge {
size,
limit: metadata_limit,
}
.into());
}
let mut body = Vec::new();
entry
.by_ref()
.take(metadata_limit)
.read_to_end(&mut body)
.map_err(|e| pax_header_unreadable(&e))?;
let follows_local = entry
.pax_extensions()
.map_err(|e| pax_header_unreadable(&e))?
.is_some_and(|mut records| records.next().is_some());
if follows_local {
return Err(TarAmbiguity::LocalBeforeGlobal.into());
}
let header = entry.header();
if *entry.path_bytes() != *header.path_bytes()
|| entry.link_name_bytes() != header.link_name_bytes()
{
return Err(TarAmbiguity::LongRecordBeforeGlobal.into());
}
scan_records(&ParsedPax::parse(&body)?, PaxScope::Global, None).map(drop)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_tar_crate_yields_slices_of_the_body() {
let body = b"12 path=a.t\n14 mtime=1700\n";
let offsets: Vec<usize> = crate_records(body)
.unwrap()
.iter()
.map(|r| r.key_offset)
.collect();
assert_eq!(offsets, [3, 15]);
for record in PaxExtensions::new(body).flatten() {
let value_start = offset_in(body, record.value_bytes()).unwrap();
let value_end = value_start + record.value_bytes().len();
assert_eq!(body.get(value_start..value_end), Some(record.value_bytes()));
}
}
#[test]
fn a_slice_outside_the_body_has_no_offset() {
let body = b"12 path=a.t\n";
let copy = body.to_vec();
assert_eq!(offset_in(body, &body[3..7]), Some(3));
assert_eq!(offset_in(body, ©[3..7]), None);
}
#[test]
fn a_record_length_with_a_leading_zero_or_sign_is_refused() {
for body in [&b"012 path=a.t\n"[..], b"+12 path=a.t\n", b"0 path=a.t\n"] {
assert!(split_records(body).is_err(), "{body:?}");
}
assert_eq!(split_records(b"12 path=a.t\n").unwrap().len(), 1);
}
}