pub mod support;
#[cfg(unix)]
use std::{io, os::unix::net::UnixListener, path::Path};
use support::{ArchiveBuilder, single_pax_member};
#[cfg(unix)]
use support::{pax_record, symlink_dir, symlink_file};
#[cfg(not(unix))]
use tar_codec::ExtractPolicyViolation;
use tar_codec::{
Archive as _, ExtractError, TarArchive,
extract::{ExtractPolicy, LinkPolicy},
};
#[cfg(unix)]
use tar_framing::PaxKeyword;
use tempfile::tempdir;
#[cfg(unix)]
#[tokio::test]
async fn preserves_safe_symlink_chains_and_forward_references() {
let temp = tempdir().unwrap();
let destination = temp.path().join("out");
let mut archive = ArchiveBuilder::new();
archive
.ustar("dir/file", b'0', b"ok", "", 0o644)
.ustar("dir/one", b'2', b"", "file", 0o644)
.ustar("dir/exact", b'2', b"", "./file", 0o644)
.ustar("two", b'2', b"", "dir/one", 0o644)
.ustar("forward", b'2', b"", "later", 0o644)
.ustar("later", b'0', b"later", "", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
for (path, target) in [
("dir/one", "file"),
("dir/exact", "./file"),
("two", "dir/one"),
("forward", "later"),
] {
assert_eq!(
std::fs::read_link(destination.join(path)).unwrap(),
Path::new(target)
);
}
assert_eq!(std::fs::read(destination.join("two")).unwrap(), b"ok");
assert_eq!(
std::fs::read(destination.join("forward")).unwrap(),
b"later"
);
std::fs::write(destination.join("dir/file"), b"changed").unwrap();
assert_eq!(std::fs::read(destination.join("two")).unwrap(), b"changed");
}
#[cfg(unix)]
#[tokio::test]
async fn preserves_dangling_symlink_chains_by_default() {
let temp = tempdir().unwrap();
let destination = temp.path().join("dangling");
std::fs::create_dir_all(destination.join("ambient")).unwrap();
let bytes = single_pax_member("link", b'2', b"", "ambient/missing", 0o644);
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("link")).unwrap(),
Path::new("ambient/missing")
);
let destination = temp.path().join("dangling-chain");
let mut archive = ArchiveBuilder::new();
archive
.ustar("one", b'2', b"", "two", 0o644)
.ustar("two", b'2', b"", "missing", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("one")).unwrap(),
Path::new("two")
);
assert_eq!(
std::fs::read_link(destination.join("two")).unwrap(),
Path::new("missing")
);
}
#[cfg(unix)]
#[tokio::test]
async fn preserves_directory_required_symlink_targets_and_rejects_file_targets() {
let temp = tempdir().expect("temporary directory should be created");
let policy = ExtractPolicy::default();
for (case, target) in [("separator", "target/"), ("dot", "target/.")] {
let destination = temp.path().join(format!("directory-{case}"));
let mut archive = ArchiveBuilder::new();
archive
.ustar("target", b'5', b"", "", 0o755)
.pax(b'x', &pax_record(PaxKeyword::LinkPath, target))
.ustar("link", b'2', b"", "ignored", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.expect("directory-required target should resolve to a directory");
assert_eq!(
std::fs::read_link(destination.join("link"))
.expect("symbolic link should be readable")
.as_os_str(),
Path::new(target).as_os_str()
);
let destination = temp.path().join(format!("file-{case}"));
let mut archive = ArchiveBuilder::new();
archive
.ustar("target", b'0', b"contents", "", 0o644)
.pax(b'x', &pax_record(PaxKeyword::LinkPath, target))
.ustar("link", b'2', b"", "ignored", 0o644);
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await,
Err(ExtractError::InvalidLink {
reason: "target path suffix requires a directory",
..
})
));
assert!(!destination.join("link").exists());
assert_eq!(
std::fs::read(destination.join("target"))
.expect("previous target payload should remain"),
b"contents"
);
}
}
#[cfg(unix)]
#[tokio::test]
async fn rejects_ambiguous_parent_directory_traversal_in_pax_symlink_targets() {
let temp = tempdir().expect("temporary directory should be created");
let destination = temp.path().join("out");
let mut archive = ArchiveBuilder::new();
archive
.ustar("regular", b'0', b"regular", "", 0o644)
.ustar("secret", b'0', b"secret", "", 0o600)
.pax(b'x', &pax_record(PaxKeyword::LinkPath, "regular/../secret"))
.ustar("link", b'2', b"", "ignored", 0o644);
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::UnsafePath {
context: "symbolic-link target",
reason: "contains ambiguous parent-directory traversal",
..
})
));
assert!(!destination.join("link").exists());
}
#[cfg(unix)]
#[tokio::test]
async fn default_preserves_links_to_missing_root_and_directory_targets() {
let temp = tempdir().unwrap();
for (case, target, directory_target) in [
("missing", "missing", false),
("root", ".", false),
("directory", "directory", true),
] {
let destination = temp.path().join(case);
let bytes = if directory_target {
let mut archive = ArchiveBuilder::new();
archive
.ustar("directory", b'5', b"", "", 0o755)
.ustar("link", b'2', b"", target, 0o644);
archive.finish()
} else {
single_pax_member("link", b'2', b"", target, 0o644)
};
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("link")).unwrap(),
Path::new(target)
);
}
let destination = temp.path().join("directory-required-file");
let mut archive = ArchiveBuilder::new();
archive
.ustar("file", b'0', b"contents", "", 0o644)
.ustar("link", b'2', b"", "file/", 0o644);
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::InvalidLink {
reason: "target path suffix requires a directory",
..
})
));
}
#[cfg(unix)]
#[tokio::test]
async fn targets_blocked_by_archive_files_are_dangling() {
let temp = tempdir().unwrap();
let mut archive = ArchiveBuilder::new();
archive.ustar("file", b'0', b"contents", "", 0o644).ustar(
"link",
b'2',
b"",
"file/child",
0o644,
);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(temp.path().join("allow"), ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_link(temp.path().join("allow/link")).unwrap(),
Path::new("file/child")
);
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(
temp.path().join("deny"),
ExtractPolicy::default()
.link_policy(LinkPolicy::default().allow_missing_targets(false)),
)
.await,
Err(ExtractError::InvalidLink {
reason: "target does not exist",
..
})
));
}
#[cfg(unix)]
#[tokio::test]
async fn missing_targets_can_be_forbidden() {
let temp = tempdir().expect("temporary directory should be created");
let destination = temp.path();
std::fs::write(destination.join("file"), b"ambient").expect("file should be created");
std::fs::create_dir(destination.join("directory")).expect("directory should be created");
let existing_links = [
("dangling-link", "missing"),
("directory-link", "directory"),
("file-link", "file"),
];
for (path, target) in existing_links {
symlink_file(target, destination.join(path)).expect("ambient link should be created");
}
let default_policy = ExtractPolicy::default();
let require_target =
default_policy.link_policy(LinkPolicy::default().allow_missing_targets(false));
let bytes = single_pax_member("link", b'2', b"", "file", 0o644);
TarArchive::new(bytes.as_slice())
.extract_in(destination, require_target)
.await
.expect("the existing file should satisfy the target requirement");
assert_eq!(
std::fs::read_link(destination.join("link")).expect("link should be readable"),
Path::new("file")
);
for (index, target) in [
"missing",
"file/child",
"dangling-link",
"directory-link/missing",
"file-link/child",
]
.into_iter()
.enumerate()
{
let link = format!("dangling-{index}");
let bytes = single_pax_member(&link, b'2', b"", target, 0o644);
let path = destination.join(link);
assert!(
matches!(
TarArchive::new(bytes.as_slice())
.extract_in(destination, require_target)
.await,
Err(ExtractError::InvalidLink {
reason: "target does not exist",
..
})
),
"{target}"
);
assert!(
matches!(
std::fs::symlink_metadata(&path),
Err(error) if error.kind() == io::ErrorKind::NotFound
),
"{target}"
);
TarArchive::new(bytes.as_slice())
.extract_in(destination, default_policy)
.await
.expect("default policy should preserve the dangling link");
assert_eq!(
std::fs::read_link(path).expect("link should be readable"),
Path::new(target)
);
}
for (path, target) in existing_links {
assert_eq!(
std::fs::read_link(destination.join(path)).expect("ambient link should be readable"),
Path::new(target)
);
}
}
#[cfg(not(unix))]
#[tokio::test]
async fn symlink_members_fail_by_default_on_unsupported_platforms() {
let temp = tempdir().unwrap();
let regular = single_pax_member("file", b'0', b"contents", "", 0o644);
TarArchive::new(regular.as_slice())
.extract_in(temp.path().join("regular"), ExtractPolicy::default())
.await
.unwrap();
let mut archive = ArchiveBuilder::new();
archive
.ustar("target", b'0', b"contents", "", 0o644)
.ustar("link", b'2', b"", "target", 0o644);
let bytes = archive.finish();
let destination = temp.path().join("link");
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::PolicyViolation {
position: 1024,
violation: ExtractPolicyViolation::NativeSymlinkCreationUnsupported,
})
));
assert_eq!(
std::fs::read(destination.join("target")).unwrap(),
b"contents"
);
assert!(!destination.join("link").exists());
}
#[cfg(unix)]
#[tokio::test]
async fn symlink_graph_resolution_budget_is_shared_across_links() {
let temp = tempdir().unwrap();
let destination = temp.path().join("out");
let mut archive = ArchiveBuilder::new();
archive
.pax(
b'x',
&pax_record(
PaxKeyword::LinkPath,
&format!("missing/{}leaf", "x/".repeat(300)),
),
)
.ustar("chain", b'2', b"", "fallback", 0o644);
for index in 0..128 {
archive.ustar(&format!("alias-{index}"), b'2', b"", "chain", 0o644);
}
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::InvalidLink {
reason: "symbolic-link target resolution work limit exceeded",
..
})
));
assert!(!destination.join("chain").exists());
assert!(!destination.join("alias-0").exists());
}
#[cfg(unix)]
#[tokio::test]
async fn preserves_links_to_pre_existing_files_and_directories_by_default() {
let temp = tempdir().expect("temporary directory should be created");
std::fs::write(temp.path().join("file"), b"ambient").expect("file should be created");
std::fs::create_dir(temp.path().join("dir")).expect("directory should be created");
let mut archive = ArchiveBuilder::new();
archive
.ustar("file-link", b'2', b"", "file", 0o644)
.ustar("dir-link", b'2', b"", "dir", 0o644);
TarArchive::new(archive.finish().as_slice())
.extract_in(temp.path(), ExtractPolicy::default())
.await
.expect("links to existing entries should be preserved");
assert_eq!(
std::fs::read(temp.path().join("file-link")).expect("file should be readable"),
b"ambient"
);
assert!(temp.path().join("dir-link").is_dir());
}
#[cfg(unix)]
#[tokio::test]
async fn native_links_allow_ambient_non_regular_targets() {
let temp = tempdir().unwrap();
let destination = temp.path().join("out");
std::fs::create_dir(&destination).unwrap();
let _socket = UnixListener::bind(destination.join("socket")).unwrap();
let bytes = single_pax_member("link", b'2', b"", "socket", 0o644);
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("link")).unwrap(),
Path::new("socket")
);
}
#[cfg(unix)]
#[tokio::test]
async fn ambient_link_components_must_resolve_beneath_the_root() {
let temp = tempdir().unwrap();
let policy = ExtractPolicy::default();
let destination = temp.path().join("contained-leaf");
std::fs::create_dir(&destination).unwrap();
std::fs::write(destination.join("target"), b"inside").unwrap();
symlink_file(Path::new("target"), destination.join("ambient-link")).unwrap();
let bytes = single_pax_member("alias", b'2', b"", "ambient-link", 0o644);
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("alias")).unwrap(),
Path::new("ambient-link")
);
let destination = temp.path().join("contained-intermediate");
std::fs::create_dir_all(destination.join("target")).unwrap();
std::fs::write(destination.join("target/file"), b"inside").unwrap();
symlink_dir(Path::new("target"), destination.join("ambient-link")).unwrap();
let bytes = single_pax_member("alias", b'2', b"", "ambient-link/file", 0o644);
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.unwrap();
assert_eq!(
std::fs::read_link(destination.join("alias")).unwrap(),
Path::new("ambient-link/file")
);
let destination = temp.path().join("leaf");
let outside = temp.path().join("outside-file");
std::fs::create_dir(&destination).unwrap();
std::fs::write(&outside, b"outside").unwrap();
symlink_file(&outside, destination.join("ambient-link")).unwrap();
let mut archive = ArchiveBuilder::new();
archive
.ustar("safe", b'0', b"safe", "", 0o644)
.pax(b'x', &pax_record(PaxKeyword::LinkPath, "ambient-link"))
.ustar("alias", b'2', b"", "safe", 0o644);
let bytes = archive.finish();
assert!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.is_err()
);
assert!(!destination.join("alias").exists());
assert_eq!(std::fs::read(&outside).unwrap(), b"outside");
let destination = temp.path().join("intermediate");
let outside = temp.path().join("outside-directory");
std::fs::create_dir(&destination).unwrap();
std::fs::create_dir(&outside).unwrap();
std::fs::write(outside.join("file"), b"outside").unwrap();
symlink_dir(&outside, destination.join("ambient-link")).unwrap();
let bytes = single_pax_member("alias", b'2', b"", "ambient-link/file", 0o644);
assert!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.is_err()
);
assert!(!destination.join("alias").exists());
let destination = temp.path().join("cycle");
std::fs::create_dir(&destination).expect("destination should be created");
symlink_file("alias", destination.join("ambient-link"))
.expect("ambient link should be created");
let bytes = single_pax_member("alias", b'2', b"", "ambient-link", 0o644);
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await,
Err(ExtractError::Filesystem { operation: "resolve extracted symbolic link", path, .. })
if path == Path::new("alias")
));
}
#[cfg(unix)]
#[tokio::test]
async fn preserves_links_to_reused_directories_and_their_extracted_children() {
let temp = tempdir().expect("temporary directory should be created");
let destination = temp.path();
for directory in ["declared", "implicit"] {
std::fs::create_dir(destination.join(directory)).expect("directory should be created");
}
let mut archive = ArchiveBuilder::new();
archive.ustar("declared", b'5', b"", "", 0o755);
archive.ustar("directory-link", b'2', b"", "declared", 0o644);
archive
.ustar("implicit/file", b'0', b"archive", "", 0o644)
.ustar("file-link", b'2', b"", "implicit/file", 0o644);
TarArchive::new(archive.finish().as_slice())
.extract_in(destination, ExtractPolicy::default())
.await
.expect("links to the reused directory and extracted file should be preserved");
assert!(destination.join("directory-link").is_dir());
assert_eq!(
std::fs::read_link(destination.join("file-link")).expect("link should be readable"),
Path::new("implicit/file")
);
}
#[cfg(unix)]
#[tokio::test]
async fn symlink_graphs_allow_finite_expansion_and_reject_cycles_and_escapes() {
let temp = tempdir().unwrap();
let destination = temp.path().join("finite");
let mut archive = ArchiveBuilder::new();
archive
.ustar("file", b'0', b"ok", "", 0o644)
.ustar("a", b'2', b"", "file", 0o644)
.ustar("b", b'2', b"", "a", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read_to_string(destination.join("b")).unwrap(),
"ok"
);
assert_eq!(
std::fs::read_link(destination.join("a")).unwrap(),
Path::new("file")
);
assert_eq!(
std::fs::read_link(destination.join("b")).unwrap(),
Path::new("a")
);
for (case, first_target, second_target, expansion_limit) in [
("cycle", "b", "a", false),
("growing-cycle", "b/x", "a/y", true),
] {
let destination = temp.path().join(case);
let mut archive = ArchiveBuilder::new();
archive.ustar("a", b'2', b"", first_target, 0o644).ustar(
"b",
b'2',
b"",
second_target,
0o644,
);
let bytes = archive.finish();
let error = TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap_err();
assert!(matches!(error, ExtractError::InvalidLink { .. }));
if expansion_limit {
assert!(matches!(
error,
ExtractError::InvalidLink {
reason: "symbolic-link target expansion limit exceeded",
..
}
));
}
assert!(!destination.join("a").exists());
assert!(!destination.join("b").exists());
}
let destination = temp.path().join("growing-cycle-work-limit");
let suffix = "x".repeat(512);
let mut archive = ArchiveBuilder::new();
archive
.pax(
b'x',
&pax_record(PaxKeyword::LinkPath, &format!("b/{suffix}")),
)
.ustar("a", b'2', b"", "fallback", 0o644)
.pax(
b'x',
&pax_record(PaxKeyword::LinkPath, &format!("a/{suffix}")),
)
.ustar("b", b'2', b"", "fallback", 0o644);
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::InvalidLink {
reason: "symbolic-link target resolution work limit exceeded",
..
})
));
assert!(!destination.join("a").exists());
assert!(!destination.join("b").exists());
let destination = temp.path().join("escape");
let bytes = single_pax_member("link", b'2', b"", "../outside", 0o644);
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::UnsafePath { .. })
));
assert!(!destination.join("link").exists());
}
#[cfg(unix)]
#[tokio::test]
async fn overwritten_pending_symlinks_do_not_affect_installation_or_resolution() {
let temp = tempdir().unwrap();
let destination = temp.path().join("out");
let mut archive = ArchiveBuilder::new();
archive
.ustar("obsolete", b'2', b"", "missing", 0o644)
.ustar("obsolete", b'0', b"file", "", 0o644)
.ustar("alias", b'2', b"", "target", 0o644)
.ustar("target", b'2', b"", "missing", 0o644)
.ustar("target", b'0', b"target", "", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.unwrap();
assert_eq!(
std::fs::read(destination.join("obsolete")).unwrap(),
b"file"
);
assert_eq!(std::fs::read(destination.join("alias")).unwrap(), b"target");
assert_eq!(
std::fs::read_link(destination.join("alias")).unwrap(),
Path::new("target")
);
}
#[cfg(unix)]
#[tokio::test]
async fn later_link_entries_replace_links_of_the_same_kind() {
let temp = tempdir().unwrap();
for (case, typeflag) in [("symbolic-link", b'2'), ("hard-link", b'1')] {
let destination = temp.path().join(case);
let mut archive = ArchiveBuilder::new();
archive
.ustar("first", b'0', b"first", "", 0o644)
.ustar("second", b'0', b"second", "", 0o644)
.ustar("same", typeflag, b"", "first", 0o644)
.ustar("same", typeflag, b"", "second", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(
&destination,
ExtractPolicy::default().link_policy(LinkPolicy::default().allow_hard_links(true)),
)
.await
.unwrap();
assert_eq!(std::fs::read(destination.join("same")).unwrap(), b"second");
if typeflag == b'2' {
assert_eq!(
std::fs::read_link(destination.join("same")).unwrap(),
Path::new("second")
);
}
}
}
#[cfg(unix)]
fn is_case_insensitive(directory: &Path) -> bool {
std::fs::write(directory.join("case-fold-probe-a"), b"").expect("probe should be created");
directory.join("case-fold-probe-A").exists()
}
#[cfg(unix)]
#[tokio::test]
async fn case_folded_deferred_symlinks_honor_overwrite_policy() {
let temp = tempdir().expect("temporary directory should be created");
if !is_case_insensitive(temp.path()) {
return;
}
let mut archive = ArchiveBuilder::new();
archive
.ustar("first", b'0', b"first", "", 0o644)
.ustar("second", b'0', b"second", "", 0o644)
.ustar("2621A", b'2', b"", "first", 0o644)
.ustar("2621a", b'2', b"", "second", 0o644);
let bytes = archive.finish();
let destination = temp.path().join("overwrites");
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.expect("later case-folded link should replace the earlier link");
assert_eq!(
std::fs::read_link(destination.join("2621A"))
.expect("case-folded symbolic link should be readable"),
Path::new("second")
);
assert_eq!(
std::fs::read(destination.join("2621a"))
.expect("case-folded symbolic link target should be readable"),
b"second"
);
let destination = temp.path().join("no-overwrites");
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(
&destination,
ExtractPolicy::default().allow_overwrites(false),
)
.await,
Err(ExtractError::PathCollision { path }) if path == Path::new("2621a")
));
assert_eq!(
std::fs::read_link(destination.join("2621A"))
.expect("first symbolic link should remain after the collision"),
Path::new("first")
);
let mut archive = ArchiveBuilder::new();
archive
.ustar("first", b'0', b"first", "", 0o644)
.ustar("file-A", b'2', b"", "first", 0o644)
.ustar("file-a", b'0', b"later", "", 0o644)
.ustar("directory-A", b'2', b"", "first", 0o644)
.ustar("directory-a", b'5', b"", "", 0o755);
let bytes = archive.finish();
let destination = temp.path().join("later-non-links");
TarArchive::new(bytes.as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await
.expect("later non-link aliases should retain precedence");
assert_eq!(
std::fs::read(destination.join("file-A"))
.expect("later case-folded file should be readable"),
b"later"
);
assert!(destination.join("directory-A").is_dir());
let destination = temp.path().join("no-cross-kind-overwrites");
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(
&destination,
ExtractPolicy::default().allow_overwrites(false),
)
.await,
Err(ExtractError::PathCollision { path }) if path == Path::new("file-A")
));
assert_eq!(
std::fs::read(destination.join("file-a"))
.expect("later file should remain after the collision"),
b"later"
);
}
#[cfg(unix)]
#[tokio::test]
async fn case_folded_symlink_parents_and_targets_cannot_escape() {
let temp = tempdir().expect("temporary directory should be created");
if !is_case_insensitive(temp.path()) {
return;
}
let mut archive = ArchiveBuilder::new();
archive
.ustar("nest/MAP", b'2', b"", ".", 0o644)
.ustar("nest/map/door", b'2', b"", "../..", 0o644)
.ustar("NEST/mAp", b'2', b"", "..", 0o644);
let destination = temp.path().join("aliased-parent");
assert!(matches!(
TarArchive::new(archive.finish().as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::Filesystem { path, .. }) if path == Path::new("nest/map")
));
assert!(!destination.join("nest/door").is_symlink());
let destination = temp.path().join("aliased-target");
std::fs::create_dir(&destination).expect("destination should be created");
symlink_file("..", destination.join("up")).expect("ambient link should be created");
let mut archive = ArchiveBuilder::new();
archive
.ustar("door", b'2', b"", "map/up", 0o644)
.ustar("MAP", b'2', b"", ".", 0o644);
assert!(matches!(
TarArchive::new(archive.finish().as_slice())
.extract_in(&destination, ExtractPolicy::default())
.await,
Err(ExtractError::Filesystem { path, .. }) if path == Path::new("door")
));
}
#[tokio::test]
async fn hard_links_require_prior_archive_targets() {
let temp = tempdir().unwrap();
let policy = ExtractPolicy::default().link_policy(LinkPolicy::default().allow_hard_links(true));
let unresolved = single_pax_member("b", b'1', b"", "a", 0o644);
assert!(matches!(
TarArchive::new(unresolved.as_slice())
.extract_in(temp.path().join("forward"), policy)
.await,
Err(ExtractError::InvalidLink { .. })
));
let destination = temp.path().join("ambient");
std::fs::create_dir(&destination).unwrap();
std::fs::write(destination.join("a"), b"ambient").unwrap();
assert!(matches!(
TarArchive::new(unresolved.as_slice())
.extract_in(&destination, policy)
.await,
Err(ExtractError::InvalidLink { .. })
));
assert_eq!(std::fs::read(destination.join("a")).unwrap(), b"ambient");
assert!(!destination.join("b").exists());
let destination = temp.path().join("different-mode");
let mut archive = ArchiveBuilder::new();
archive
.ustar("a", b'0', b"keep", "", 0o644)
.ustar("b", b'1', b"", "a", 0o755);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(&destination, policy)
.await
.unwrap();
assert_eq!(
std::fs::read(destination.join("b")).expect("hard link should be readable"),
b"keep"
);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(destination.join("a"))
.unwrap()
.permissions()
.mode()
& 0o111,
0
);
}
}
#[tokio::test]
async fn hard_links_cannot_replace_their_targets() {
let temp = tempdir().unwrap();
for (case, path) in [("self", "target"), ("ancestor", "target/link")] {
let destination = temp.path().join(case);
let mut archive = ArchiveBuilder::new();
archive
.ustar("target", b'0', b"keep", "", 0o644)
.ustar(path, b'1', b"", "target", 0o644);
let bytes = archive.finish();
assert!(matches!(
TarArchive::new(bytes.as_slice())
.extract_in(
&destination,
ExtractPolicy::default()
.link_policy(LinkPolicy::default().allow_hard_links(true)),
)
.await,
Err(ExtractError::InvalidLink { .. })
));
assert_eq!(std::fs::read(destination.join("target")).unwrap(), b"keep");
}
}
#[cfg(unix)]
#[tokio::test]
async fn native_symlink_and_hard_link_builders_compose() {
let temp = tempdir().unwrap();
let mut archive = ArchiveBuilder::new();
archive
.ustar("target", b'0', b"contents", "", 0o644)
.ustar("symbolic", b'2', b"", "target", 0o644)
.ustar("hard", b'1', b"", "target", 0o644);
let bytes = archive.finish();
TarArchive::new(bytes.as_slice())
.extract_in(
temp.path(),
ExtractPolicy::default().link_policy(LinkPolicy::default().allow_hard_links(true)),
)
.await
.unwrap();
assert_eq!(
std::fs::read_link(temp.path().join("symbolic")).unwrap(),
Path::new("target")
);
std::fs::write(temp.path().join("target"), b"updated").unwrap();
assert_eq!(
std::fs::read(temp.path().join("symbolic")).unwrap(),
b"updated"
);
assert_eq!(std::fs::read(temp.path().join("hard")).unwrap(), b"updated");
}