mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
atomic: a write-only file's unreadable attributes are dropped, as documented and tested (review)
On macOS a mode-0200 file's owner can list none of its attribute names either, so they are dropped, as before attributes were kept. An ACL that denies the data but lets the names be listed still refuses the save. Both are now tested on macOS. Also: the pages key doc gives the macOS exception for Cmd+Home/End.
This commit is contained in:
@@ -548,9 +548,10 @@ const MAX_XATTR_TOTAL_BYTES: usize = 64 * 1024 * 1024;
|
||||
/// binding, quarantine, resource forks) to the stage. The attributes are as untrusted as the
|
||||
/// bytes, so the reads are bounded; one that cannot be read or written whole, or passes a bound,
|
||||
/// refuses the save rather than dropping the user's metadata unnoticed. Only a volume without
|
||||
/// extended attributes (`ENOTSUP`) is skipped, and so is a file whose data the user may not read
|
||||
/// (mode 0200) when it has no attribute names the user can list either; one whose names can be
|
||||
/// listed refuses the save, as its attributes can't be copied without its data.
|
||||
/// extended attributes (`ENOTSUP`) is skipped. So is a file whose data the user may not read when
|
||||
/// its attribute names can't be listed either, as with mode 0200: its attributes are dropped, as
|
||||
/// before attributes were kept, since the user can't read them (or its bytes) at all. One whose
|
||||
/// data is denied but whose names can be listed (an ACL that denies reading) refuses the save.
|
||||
/// `com.apple.provenance` belongs to macOS, which gives the new file its own. Compression
|
||||
/// bookkeeping stays hidden (no `XATTR_SHOWCOMPRESSION`).
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -599,10 +600,10 @@ fn copy_xattrs(target: &Path, stage: &File) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// For a target whose data can't be opened for reading: `Ok` when it has no extended attribute the
|
||||
/// user can list (or the volume has none), so the save loses nothing; otherwise refuse the save, as
|
||||
/// the attributes can't be copied without opening the file. The names are read without following a
|
||||
/// link, by path.
|
||||
/// For a target whose data can't be opened for reading: `Ok` when the user can list no extended
|
||||
/// attribute names on it (none, only `com.apple.provenance`, none supported, or listing is denied too,
|
||||
/// as for mode 0200, whose attributes are then dropped); otherwise refuse the save, as the attributes
|
||||
/// can't be copied without opening the file. The names are read without following a link, by path.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn unreadable_has_no_attributes(target: &Path) -> io::Result<()> {
|
||||
use rustix::fs::llistxattr;
|
||||
|
||||
@@ -99,3 +99,49 @@ fn private_staging_preserves_existing_modes_after_callback() -> io::Result<()> {
|
||||
assert_no_temp(&d.0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Extended attribute names on `path`, read by path.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn xattr_names(path: &Path) -> Vec<Vec<u8>> {
|
||||
let mut names = vec![0u8; 64 * 1024];
|
||||
let len = rustix::fs::listxattr(path, names.as_mut_slice()).unwrap_or(0);
|
||||
names.truncate(len);
|
||||
names.split(|b| *b == 0).filter(|n| !n.is_empty()).map(<[u8]>::to_vec).collect()
|
||||
}
|
||||
|
||||
/// A write-only file (mode 0200) saves: macOS lets its owner neither read its data nor list its
|
||||
/// attributes, so its attributes are dropped, as before attributes were kept.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn a_write_only_file_saves_and_drops_the_attributes_it_cannot_read() -> io::Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let d = TempDir::new("write_only_xattrs");
|
||||
let p = d.0.join("doc.craft");
|
||||
std::fs::write(&p, ORIGINAL)?;
|
||||
rustix::fs::setxattr(&p, "com.craft.t", b"tag", rustix::fs::XattrFlags::empty())?;
|
||||
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o200))?;
|
||||
atomic_write(&p, b"new")?;
|
||||
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o600))?;
|
||||
assert_eq!(std::fs::read(&p)?, b"new");
|
||||
assert!(!xattr_names(&p).iter().any(|n| n == b"com.craft.t"), "the unreadable attribute is dropped");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A file whose data an ACL denies but whose attribute names can be listed is not replaced: its
|
||||
/// attributes can't be copied without its data, and the save refuses rather than drop them.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn an_unreadable_file_with_listable_attributes_is_not_replaced() -> io::Result<()> {
|
||||
let d = TempDir::new("acl_denied_xattrs");
|
||||
let p = d.0.join("doc.craft");
|
||||
std::fs::write(&p, ORIGINAL)?;
|
||||
rustix::fs::setxattr(&p, "com.craft.t", b"tag", rustix::fs::XattrFlags::empty())?;
|
||||
let denied = std::process::Command::new("/bin/chmod").arg("+a").arg("everyone deny read").arg(&p).status()?;
|
||||
assert!(denied.success(), "the ACL is set");
|
||||
let result = atomic_write(&p, b"new");
|
||||
let _ = std::process::Command::new("/bin/chmod").arg("-N").arg(&p).status();
|
||||
assert!(result.is_err(), "the save is refused");
|
||||
assert_eq!(std::fs::read(&p)?, ORIGINAL, "the original is unchanged");
|
||||
assert!(xattr_names(&p).iter().any(|n| n == b"com.craft.t"), "and keeps its attribute");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1544,7 +1544,8 @@ impl KeyMoves {
|
||||
/// Page Up and Down a screen less a key line (a row when not continuous, and with Cmd), Home and End the first
|
||||
/// and last page, Up and Down a key line (not continuous, a turn once the row is at its edge that way), Left and
|
||||
/// Right a key line sideways while the current page or spread has more to show that way, else a turn of a page or
|
||||
/// spread; Cmd with Left or Right turns at once. Home and End with Shift or Cmd, and Page Up, Page Down, Up and
|
||||
/// spread; Cmd with Left or Right turns at once. Home and End with Shift, or with Cmd off macOS (which gives Cmd
|
||||
/// with them no move), and Page Up, Page Down, Up and
|
||||
/// Down with Shift and Cmd together, go to the first or last page; Alt, and Shift alone with the other keys,
|
||||
/// leave them to nothing.
|
||||
fn of(
|
||||
|
||||
Reference in New Issue
Block a user