Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/Discussion Permit ignoring entry permissions #207

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct ArchiveInner<R: ?Sized> {
unpack_xattrs: bool,
preserve_permissions: bool,
preserve_mtime: bool,
ignore_permissions: bool,
ignore_zeros: bool,
obj: RefCell<R>,
}
Expand All @@ -47,6 +48,7 @@ impl<R: Read> Archive<R> {
unpack_xattrs: false,
preserve_permissions: false,
preserve_mtime: true,
ignore_permissions: false,
ignore_zeros: false,
obj: RefCell::new(obj),
pos: Cell::new(0),
Expand Down Expand Up @@ -125,6 +127,15 @@ impl<R: Read> Archive<R> {
self.inner.preserve_mtime = preserve;
}

/// Indicate whether any permissions (like exec/readonly/suid on Unix) are set
/// when unpacking this entry.
///
/// This flag is disabled by default. When enabled all files will be unpacked
/// and the final chmod after extracting content entirely skipped.
pub fn set_ignore_permissions(&mut self, ignore: bool) {
self.inner.ignore_permissions = ignore;
}

/// Ignore zeroed headers, which would otherwise indicate to the archive that it has no more
/// entries.
///
Expand Down Expand Up @@ -255,6 +266,8 @@ impl<'a> EntriesFields<'a> {
unpack_xattrs: self.archive.inner.unpack_xattrs,
preserve_permissions: self.archive.inner.preserve_permissions,
preserve_mtime: self.archive.inner.preserve_mtime,
ignore_permissions: self.archive.inner.ignore_permissions,
mode: None,
};

// Store where the next entry is, rounding up by 512 bytes (the size of
Expand Down
54 changes: 48 additions & 6 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct EntryFields<'a> {
pub unpack_xattrs: bool,
pub preserve_permissions: bool,
pub preserve_mtime: bool,
pub ignore_permissions: bool,
pub mode: Option<u32>,
}

pub enum EntryIo<'a> {
Expand Down Expand Up @@ -249,6 +251,20 @@ impl<'a, R: Read> Entry<'a, R> {
pub fn set_preserve_mtime(&mut self, preserve: bool) {
self.fields.preserve_mtime = preserve;
}

/// Indicate whether any permissions (like exec/readonly/suid on Unix) are set
/// when unpacking this entry.
///
/// This flag is disabled by default. When enabled all files will be unpacked
/// and the final chmod after extracting content entirely skipped.
pub fn set_ignore_permissions(&mut self, ignore: bool) {
self.fields.ignore_permissions = ignore;
}

/// Use a custom mode when unpacking the entry.
pub fn set_mode(&mut self, mode: Option<u32>) {
self.fields.mode = mode;
}
}

impl<'a, R: Read> Read for Entry<'a, R> {
Expand Down Expand Up @@ -424,14 +440,24 @@ impl<'a> EntryFields<'a> {
})
}

fn get_mode(&mut self) -> Option<u32> {
self.mode.or_else(|| self.header.mode().ok())
}

/// Returns access to the header of this entry in the archive.
fn unpack(&mut self, target_base: Option<&Path>, dst: &Path) -> io::Result<Unpacked> {
let kind = self.header.entry_type();

if kind.is_dir() {
self.unpack_dir(dst)?;
if let Ok(mode) = self.header.mode() {
set_perms(dst, None, mode, self.preserve_permissions)?;
if let Some(mode) = self.get_mode() {
set_perms(
dst,
None,
mode,
self.preserve_permissions,
self.ignore_permissions,
)?;
}
return Ok(Unpacked::__Nonexhaustive);
} else if kind.is_hard_link() || kind.is_symlink() {
Expand Down Expand Up @@ -526,8 +552,14 @@ impl<'a> EntryFields<'a> {
// Only applies to old headers.
if self.header.as_ustar().is_none() && self.path_bytes().ends_with(b"/") {
self.unpack_dir(dst)?;
if let Ok(mode) = self.header.mode() {
set_perms(dst, None, mode, self.preserve_permissions)?;
if let Some(mode) = self.get_mode() {
set_perms(
dst,
None,
mode,
self.preserve_permissions,
self.ignore_permissions,
)?;
}
return Ok(Unpacked::__Nonexhaustive);
}
Expand Down Expand Up @@ -596,8 +628,14 @@ impl<'a> EntryFields<'a> {
})?;
}
}
if let Ok(mode) = self.header.mode() {
set_perms(dst, Some(&mut f), mode, self.preserve_permissions)?;
if let Some(mode) = self.get_mode() {
set_perms(
dst,
Some(&mut f),
mode,
self.preserve_permissions,
self.ignore_permissions,
)?;
}
if self.unpack_xattrs {
set_xattrs(self, dst)?;
Expand All @@ -609,7 +647,11 @@ impl<'a> EntryFields<'a> {
f: Option<&mut std::fs::File>,
mode: u32,
preserve: bool,
ignore: bool,
) -> Result<(), TarError> {
if ignore {
return Ok(());
}
_set_perms(dst, f, mode, preserve).map_err(|e| {
TarError::new(
&format!(
Expand Down
37 changes: 37 additions & 0 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,43 @@ fn extract_sparse() {
assert!(s[0x2fa0 + 6..0x4000].chars().all(|x| x == '\u{0}'));
}

#[test]
fn extracting_readonly_file_ignore_permissions() -> io::Result<()> {
let td = t!(TempDir::new("tar-rs"));

let mut tar = Vec::new();

{
let mut a = Builder::new(&mut tar);
let path = "file.txt";

let mut header = Header::new_gnu();
header.set_path(path)?;
{
let h = header.as_gnu_mut().unwrap();
for (a, b) in h.name.iter_mut().zip(path.as_bytes()) {
*a = *b;
}
}
header.set_size(1);
header.set_mtime(2);
header.set_mode(0o400);
header.set_cksum();
t!(a.append(&header, io::repeat(1).take(1)));
}

let mut ar = Archive::new(&tar[..]);
ar.set_ignore_permissions(true);
ar.unpack(td.path())?;

let path = td.path().join("file.txt");
let m = fs::metadata(&path)?;
assert!(!m.permissions().readonly());
assert_eq!(1, fs::read(&path)?[0]);

Ok(())
}

#[test]
fn path_separators() {
let mut ar = Builder::new(Vec::new());
Expand Down
57 changes: 55 additions & 2 deletions tests/entry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extern crate tar;
extern crate tempdir;

use std::fs::File;
use std::io::Read;
#[allow(unused_imports)]
use std::fs::{self, File};
#[allow(unused_imports)]
use std::io::{self, Read};

use tempdir::TempDir;

Expand Down Expand Up @@ -346,3 +348,54 @@ fn modify_symlink_just_created() {
t!(t!(File::open(&test)).read_to_end(&mut contents));
assert_eq!(contents.len(), 0);
}

#[test]
#[cfg(unix)]
fn modify_mode_during_unpack() -> io::Result<()> {
use ::std::os::unix::fs::PermissionsExt;
let td = t!(TempDir::new("tar-rs"));

let mut tar = Vec::new();

{
let mut a = tar::Builder::new(&mut tar);
let path = "file.txt";

let mut header = tar::Header::new_gnu();
header.set_path(path)?;
{
let h = header.as_gnu_mut().unwrap();
for (a, b) in h.name.iter_mut().zip(path.as_bytes()) {
*a = *b;
}
}
header.set_size(1);
header.set_mtime(2);
header.set_mode(0o400); // no exec, no write
header.set_cksum();
t!(a.append(&header, io::repeat(1).take(1)));
}

let mut ar = tar::Archive::new(&tar[..]);
let entries = ar.entries()?;
for entry in entries {
let mut entry = entry?;
let full_path = td.path().join(entry.path()?.into_owned());
entry.set_preserve_mtime(false);
entry.set_mode(Some(0o500)); // exec, no write
entry.unpack(&full_path).and_then(|unpacked| {
match unpacked {
tar::Unpacked::File(f) => {
assert_eq!(f.metadata()?.permissions().mode() & 0o777, 0o500);
}
_ => {}
};
Ok(())
})?;
}

let path = td.path().join("file.txt");
assert_eq!(1, fs::read(&path)?[0]);

Ok(())
}