diff --git a/docs/audits/2026-11-17-capstd-experiment.adoc b/docs/audits/2026-11-17-capstd-experiment.adoc new file mode 100644 index 00000000..01e70613 --- /dev/null +++ b/docs/audits/2026-11-17-capstd-experiment.adoc @@ -0,0 +1,279 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 +// Copyright (c) Jonathan D.A. Jewell += Containment experiment: capability handles (`cap-std`), 2026-11-17 +:toc: preamble + +The route plan asks for a one-day experiment before the sandbox escapes are +fixed (`docs/plans/VSH-ROUTE-PLAN-2026-11-04.adoc`, Phase 1; step B1 of the +2026-11-17 amendment): route a few commands through a directory handle from +the `cap-std` crate, and decide from the result how the shell is contained. + +*Result: go.* The mechanism closes the symlink and `..` escapes for the +commands ported, with a small change and no test regressions. It does not +cover hard links, which need their own check. + +== The question + +Today the shell contains paths lexically: it joins the path to the root and +removes `..` components. It never asks where the path leads on disk, so a +symlink inside the root reaches outside it (holes j, k, l, m), and `cd` +moves the root itself (holes f, g). + +Three ways to fix that were rated in the plan: capability handles (elegance +9 / dependability 8), a hand-written `openat2` and `O_NOFOLLOW` walk (6 / 7), +and more lexical checks (3 / 3, which is what fails today). + +A capability handle is an open directory. Every path given to it is relative, +and the operating system resolves it beneath that directory (`openat2` with +`RESOLVE_BENEATH` on Linux 5.6 and later), so a path cannot name anything +outside, whatever symlinks it passes through. + +== Part 1: the crate alone + +A stand-alone program opened a handle on a root directory and tried each +escape (Measured, Linux 6.18, `cap-std` 4.0.3): + +[cols="3,2",options="header"] +|=== +| Attempt | Result + +| Write through a symlink to a file outside (absolute or relative target) | Refused; the outside file is unchanged +| Create a file or directory under a symlink to an outside directory | Refused +| Rename a file into a symlink to an outside directory | Refused +| `../escaped`, `sub/../../escaped`, and opening `..` | Refused +| An absolute path | Refused (paths must be relative to the handle) +| `sub/../inside` (stays in the root) | Allowed +| Write through a symlink whose target is inside the root | Allowed +| Open for writing a hard link whose other name is outside the root | *Allowed.* The link count (2) is visible on the open file +|=== + +Cost of 20,000 file creations, one run each, release build: at the top of the +root, 0.93 s through the handle against 0.23 s through `std::fs`; six +directories deep, 0.18 s against 0.25 s. The two runs disagree on which is +faster, so the only safe reading is that the cost is tens of microseconds per +operation at most, which does not matter for a shell. + +== Part 2: in the shell + +`ShellState` gained a handle on the session root, opened once at start, and a +function that turns a typed path into a path relative to the session root (or +refuses it if it lies outside). Four commands were moved onto the handle: +`touch`, `mkdir`, `mv`, and output redirection (`>` and `>>`, both the +built-in and the external-command paths). + +[cols="2,3",options="header"] +|=== +| Measure | Value + +| Source change | 3 files under `src/`, 101 lines added, 21 removed +| New crates in `Cargo.lock` | 11 (304 to 315): `cap-std`, `cap-primitives`, `io-lifetimes`, `io-extras`, `ambient-authority`, `fs-set-times`, `maybe-owned`, `ipnet`, `rustix-linux-procfs`, and `winx` (Windows only) +| Licences of those crates | `Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT`, or `MIT OR Apache-2.0` +| Debug binary size | 61.9 MB to 64.3 MB +| `cargo test --no-fail-fast` | 843 passed, 0 failed, as before the change +| Hole tests (`tests/regressions_parsed_path.rs`) | j, k and l no longer reproduce and left the open list +|=== + +Hole g (`cd ..; touch escaped`) also stopped reproducing for the four ported +commands. That was too narrow a test: `cd ..; rm bystander` still removed a +file outside the root, because `rm` was not ported and `cd` still moved the +root. The test for g now covers `touch`, `mkdir`, `>` and `rm`, and at this +point of the experiment g stayed on the open list. + +NOTE: This part and the next describe the shell as it was when the experiment +ended. Later work on the same day changed some of it: `rm` is ported, `cd` +moves a separate working directory, and hole g is closed. <> gives the +state at the end of the day. + +== What it does not do + +* *Hard links* (hole n). A hard link inside the root to a file outside it is + an ordinary file as far as path resolution goes. Content operations must + check the link count on the open file and refuse a count above one. +* *Commands not yet ported.* About 200 other filesystem calls in the shell + still use `std::fs` with a lexically resolved path. Containment holds only + where the handle is used. A lint that bans `std::fs` outside one module is + needed to keep it that way. +* *`cd`.* The working directory was still stored by overwriting the root. + Fixing f and g needed a session root that never moves and a separate + working directory. (Done later the same day: see <>.) +* *External commands.* A program the shell runs is not confined by the + handle; it runs as the user. +* *Other platforms.* Not run on macOS or Windows. On systems without + `openat2`, `cap-std` resolves paths itself, one component at a time; that + path was not exercised here. + +== Follow-up the same day: obliterate + +`obliterate` (file and directory forms) and `secure_delete` now work through +the handle, and close holes c, i, m and n: + +* A symbolic link is refused, wherever it points. The request named the link, + and the data belongs to its target. +* A file with more than one hard link is refused, and so is a directory that + contains one. The check is made on the open file, which is opened without + following a link, so the name cannot be swapped in between. +* A directory is walked first, without following links, and refused whole if + anything in it cannot be obliterated safely. Links inside it are removed as + links. +* `obliterate` is refused inside a transaction (ruling OD-3a). +* An audit entry is written before anything is destroyed, and its failure + stops the command. A second entry records the outcome. + +Output redirection also refuses a file with more than one hard link. + +One fault was found on the way: a directory handle from `open_dir` cannot be +synced (`EBADF`), so the first version destroyed the file and then reported +failure. The directory is now opened as a file for syncing. The existing +tests did not catch this; the hole test for d did, because its set-up runs +`obliterate`. + +== Follow-up the same day: one open per redirection + +Review found that a redirection's target was opened more than once: by the +set-up (beneath the root, with the check on hard links), and then again by +whatever wrote to it. For a built-in the second open was beneath the root, +without the check. For an external command it was an ordinary `File::create` +on the name. A name given to another file between the two opens was +followed: a hard link to a file outside the root was truncated and written, +and for an external command so was the target of a symbolic link. + +A redirection's target is now opened once: + +* It is opened beneath the root without truncating it. The file type and the + link count are read from the open file. Only then is it truncated. +* A file that does not exist is created exclusively. A file that another + program makes at that moment is not taken for a new one. One consequence: + a redirection to a symbolic link whose target does not exist is refused + (bash creates the target). +* The bytes saved for undo are read through the same handle, opened with + `O_NOATIME` where the system has it. +* Built-ins and external commands write through duplicates of that handle. +* The history records the file that was changed, with the links in the path + resolved, so `echo x > alias` is recorded under the file `alias` points + to. `obliterate` of that file then finds the record. + +Input redirection is still opened by an ordinary path (once). Undoing an +append or the creation of a file, and redoing the creation of a file, still +go by an ordinary path. + +== Follow-up the same day: redo, and a gap in the scrub + +Review found that a removal which is redone lost the file. The record made by +`redo` held no content, and undo of a removal with no content wrote an empty +file, readable by its owner only, and reported success: `rm f; undo; redo; +undo` left `f` empty. A redone truncation (`echo x > f; undo; redo; undo`) +lost the content in the same way. + +* `redo` of a removal now removes the file as `rm` does: beneath the root, + keeping the content and the file's permissions, owner and modification + time. `redo` of a truncation empties the file as the redirection does: one + open, the same checks, the content read from the file that is emptied. + What is kept is what the file holds at that moment, which is not always + what the first run saved. +* Undo of a removal or of a truncation whose record holds no content now + fails and writes nothing. Such a record can come from `redo` in an earlier + build. +* The audit entry of a directory obliteration that fails part of the way + counts a file as soon as its name is removed. It was counted after the + directory was synced, so a failed sync reported one file too few. + +One gap in the scrub of obliterated content was found while this was +checked, and is not closed. The scrub finds the records of a file by path: +the path as recorded, and where that path leads now. Content that was saved +while the file had another name is not found: + +* `echo secret > a; echo x > a; mv a b; obliterate b` leaves `secret` in the + saved history, under the name `a`; +* so does content saved through a symbolic link that has been removed + since, in a history written before redirections recorded the file they + changed. + +It is listed as hole (p) in `impl/rust-cli/tests/regressions_parsed_path.rs`. +That test fails if the hole closes and the list is not updated. Closing it +needs a decision on how a record identifies its file: by the names the +history gives the file over time, by the device and inode read when the +content is saved, or both. The decision is open. + +== Follow-up the same day: what a failed command leaves behind + +Review found four more faults, and checking them found others of the same +kind. + +* *A `>` target was emptied before the command was known to start.* Each + target was emptied as it was set up. If a later redirection of the same + command was then refused (`echo hi > out 2> dangling`), `out` was already + empty and the record of what it had held was dropped. The same happened + when the command itself failed after the set-up: a built-in that returned + an error, a command that was not found, a command that was interrupted, + and every background command. In each case the file was emptied and + nothing was recorded, so undo could not restore it. Now no target is + emptied until every redirection is set up, and what the set-up changed is + recorded whatever becomes of the command. One function does the set-up, + runs the command and makes the record, so a caller cannot leave the record + out. +* *The shell did not start in its session root.* Started with `-r` from + another directory, the process stayed in that directory until the first + `cd`. Built-ins worked in the session root, but the commands the shell + started, and `$PWD`, used the other directory, outside the root. The shell + now enters the session root when it starts. +* *Undo wrote to whatever had the recorded name.* Undo of `rm` wrote over a + file that another program had made under the name since. Undo of a + truncation opened the name with truncation, unchecked, so a name that had + become a hard link to a file outside the root was emptied and written. + Undo of `rm` now creates the file only if nothing has the name. Undo of a + truncation opens the existing file without truncating it and makes the + checks a redirection makes (a regular file with no other name) before it + writes. It no longer creates a file that has been removed since. +* *`obliterate` used what it had read before the confirmation.* The length + and the link count were read when the file was opened, before the wait + for the answer. Bytes that another program appended during the wait were + not overwritten, and a hard link made during the wait was not seen. The + link count is now read again after the answer, before anything is + recorded. The length is read where the overwrite starts, and a file that + is longer after the overwrite than the length overwritten is an error: + the name is left in place. + +Not changed: a file that an earlier redirection created stays, empty, when a +later one is refused (as in bash), and it is not recorded. The checks are +made on the open file, but another program can still change the file +between a check and the write that follows it; the gap is short, not +closed. + +[[now]] +== Where this stands at the end of the day + +Through the handle on the session root: `touch`, `mkdir`, `mv`, `rm`, +`obliterate`, output redirection (the set-up, and the writes of built-ins +and of commands), redo of a removal and of a truncation, and undo of a +removal and of a truncation. + +Still by an ordinary path: `cp`, `rmdir`, `ln -s`, `chmod`, `chown`, input +redirection, `ls`, the test built-in, undo and redo of the other operations, +and rollback of them. + +The session root is fixed. `cd` moves a separate working directory, within +the root, and the process starts in the root. History paths are relative to +the session root. + +Holes open in `impl/rust-cli/tests/regressions_parsed_path.rs`: b, o and p. +Holes a, c to n are closed. + +== Decision and next steps + +Capability handles are adopted for containment (route plan step B2). The +steps decided at the end of the experiment, with their state at the end of +the day: + +. Port the remaining commands, `rm` first. `rm` and `obliterate` are done; + the commands listed in <> are not. +. Give the shell a fixed session root and a separate working directory; + record history paths relative to the session root. Done. +. Refuse content operations on files with more than one hard link, and + refuse `obliterate` on a symlink. Done for `>`, `>>`, `obliterate`, redo + and undo of a truncation. +. Add the lint, so that a new `std::fs` call outside the filesystem module + fails the build. Not done. + +The same handle answers the question of where the shell's own state lives: a +second handle on a state directory outside the session root, which no +root-relative path can reach. diff --git a/impl/rust-cli/Cargo.lock b/impl/rust-cli/Cargo.lock index af880d97..a50362b7 100644 --- a/impl/rust-cli/Cargo.lock +++ b/impl/rust-cli/Cargo.lock @@ -17,6 +17,12 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "ambient-authority" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d4ee0d472d1cd2e28c97dfa124b3d8d992e10eb0a035f33f5d12e3a177ba3b" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -190,6 +196,48 @@ version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" +[[package]] +name = "cap-fs-ext" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ff379b70af8e08307a8f65e7040c7301cb4a572538ade16b4984f0da77847f" +dependencies = [ + "cap-primitives", + "cap-std", + "io-lifetimes 3.0.1", + "windows-sys 0.61.2", +] + +[[package]] +name = "cap-primitives" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b5f74729fd2f44701d1a8eb47e906cdb3ccd9ec0f02baad85a744b791940b18" +dependencies = [ + "ambient-authority", + "fs-set-times", + "io-extras", + "io-lifetimes 3.0.1", + "ipnet", + "maybe-owned", + "rustix 1.1.4", + "rustix-linux-procfs", + "windows-sys 0.61.2", + "winx", +] + +[[package]] +name = "cap-std" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ec78e242cfa2cfe276807ac2ecc00315a6c97786977414bcd1c3963b6c91b8" +dependencies = [ + "cap-primitives", + "io-extras", + "io-lifetimes 3.0.1", + "rustix 1.1.4", +] + [[package]] name = "cast" version = "0.3.0" @@ -738,6 +786,17 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" +[[package]] +name = "fs-set-times" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94e7099f6313ecacbe1256e8ff9d617b75d1bcb16a6fddef94866d225a01a14a" +dependencies = [ + "io-lifetimes 2.0.4", + "rustix 1.1.4", + "windows-sys 0.59.0", +] + [[package]] name = "futures-core" version = "0.3.32" @@ -958,6 +1017,34 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "io-extras" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20fd6de4ccfcc187e38bc21cfa543cb5a302cb86a8b114eb7f0bf0dc9f8ac00f" +dependencies = [ + "io-lifetimes 3.0.1", + "windows-sys 0.59.0", +] + +[[package]] +name = "io-lifetimes" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06432fb54d3be7964ecd3649233cddf80db2832f47fec34c01f65b3d9d774983" + +[[package]] +name = "io-lifetimes" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0fb0570afe1fed943c5c3d4102d5358592d8625fda6a0007fdbe65a92fba96" + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + [[package]] name = "is-terminal" version = "0.4.17" @@ -1125,6 +1212,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "maybe-owned" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" + [[package]] name = "memchr" version = "2.8.1" @@ -1791,6 +1884,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "rustix-linux-procfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fc84bf7e9aa16c4f2c758f27412dc9841341e16aa682d9c7ac308fe3ee12056" +dependencies = [ + "once_cell", + "rustix 1.1.4", +] + [[package]] name = "rustversion" version = "1.0.22" @@ -2308,6 +2411,8 @@ name = "vsh" version = "0.9.0" dependencies = [ "anyhow", + "cap-fs-ext", + "cap-std", "chrono", "clap", "colored", @@ -2749,6 +2854,16 @@ version = "0.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" +[[package]] +name = "winx" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f3fd376f71958b862e7afb20cfe5a22830e1963462f3a17f49d82a6c1d1f42d" +dependencies = [ + "bitflags 2.11.1", + "windows-sys 0.59.0", +] + [[package]] name = "wit-bindgen" version = "0.51.0" diff --git a/impl/rust-cli/Cargo.toml b/impl/rust-cli/Cargo.toml index d0a8569c..ddc16372 100644 --- a/impl/rust-cli/Cargo.toml +++ b/impl/rust-cli/Cargo.toml @@ -64,6 +64,11 @@ libc = "0.2" # Safe syscall wrappers (access(2) for the precondition checks) nix = { version = "0.31", default-features = false, features = ["fs", "user"] } +# Capability-based filesystem access: every path is resolved beneath an open +# directory handle, so it cannot name anything outside the session root. +cap-std = "4.0" +cap-fs-ext = "4.0" + # Glob pattern matching glob = "0.3" diff --git a/impl/rust-cli/src/arith.rs b/impl/rust-cli/src/arith.rs index 875f83d0..8021aa1a 100644 --- a/impl/rust-cli/src/arith.rs +++ b/impl/rust-cli/src/arith.rs @@ -604,7 +604,9 @@ pub fn eval_arith(expr: &ArithExpr, state: &ShellState) -> Result { let val = eval_arith(expr, state)?; match op { ArithOp::UnaryPlus => Ok(val), - ArithOp::UnaryMinus => Ok(-val), + // Wraps at the edge of i64, as addition does and as bash does: + // the negation of the smallest value is itself. + ArithOp::UnaryMinus => Ok(val.wrapping_neg()), ArithOp::LogNot => Ok(if val == 0 { 1 } else { 0 }), ArithOp::BitNot => Ok(!val), _ => Err(anyhow!("Invalid unary operator")), @@ -643,14 +645,17 @@ pub fn eval_arith(expr: &ArithExpr, state: &ShellState) -> Result { if rval == 0 { Err(anyhow!("Division by zero")) } else { - Ok(lval / rval) + // `i64::MIN / -1` does not fit in an i64. It wraps to + // `i64::MIN`, as bash gives, instead of panicking. + Ok(lval.wrapping_div(rval)) } } ArithOp::Mod => { if rval == 0 { Err(anyhow!("Modulo by zero")) } else { - Ok(lval % rval) + // `i64::MIN % -1` is 0; plain `%` panics on it. + Ok(lval.wrapping_rem(rval)) } } ArithOp::Pow => { diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 4f91625d..80102c95 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -14,6 +14,34 @@ use crate::proof_refs; use crate::state::{Operation, OperationType, ShellState}; use crate::verification; +/// The target of a symbolic link as the bytes an operation record stores. +/// +/// On Unix a link target is a string of bytes and need not be text, so the +/// bytes are kept as they are. Turning them into text first replaced what +/// was not text, and undo then made a link to a different path. +#[cfg(unix)] +fn link_target_bytes(target: &std::path::Path) -> Vec { + use std::os::unix::ffi::OsStrExt; + target.as_os_str().as_bytes().to_vec() +} + +#[cfg(not(unix))] +fn link_target_bytes(target: &std::path::Path) -> Vec { + target.to_string_lossy().into_owned().into_bytes() +} + +/// The link target an operation record stores, as a path. +#[cfg(unix)] +fn link_target_from_bytes(bytes: &[u8]) -> std::path::PathBuf { + use std::os::unix::ffi::OsStrExt; + std::path::PathBuf::from(std::ffi::OsStr::from_bytes(bytes)) +} + +#[cfg(not(unix))] +fn link_target_from_bytes(bytes: &[u8]) -> std::path::PathBuf { + std::path::PathBuf::from(String::from_utf8_lossy(bytes).into_owned()) +} + // Secure deletion (RMO - Remove-Match-Obliterate) pub mod secure_deletion; @@ -79,7 +107,7 @@ pub fn mkdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_mkdir(state.root(), path)?; + verification::verify_mkdir(state.root(), &state.rooted(path))?; // Check preconditions (matching Coq) if full_path.exists() { @@ -91,8 +119,12 @@ pub fn mkdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { anyhow::bail!("Parent directory does not exist (ENOENT)"); } - // Execute operation - fs::create_dir(&full_path).context("mkdir failed")?; + // Execute operation, beneath the session root. + let contained = state.contained(path)?; + state + .root_dir() + .create_dir(&contained) + .context("mkdir failed")?; // Record in history let op = Operation::new(OperationType::Mkdir, path.to_string(), None); @@ -146,7 +178,7 @@ pub fn rmdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_rmdir(state.root(), path)?; + verification::verify_rmdir(state.root(), &state.rooted(path))?; // Check preconditions if !full_path.exists() { @@ -210,7 +242,7 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_create_file(state.root(), path)?; + verification::verify_create_file(state.root(), &state.rooted(path))?; if full_path.exists() { anyhow::bail!("Path already exists (EEXIST)"); @@ -221,7 +253,16 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { anyhow::bail!("Parent directory does not exist (ENOENT)"); } - fs::write(&full_path, "").context("touch failed")?; + // Create beneath the session root. `create_new` also closes the gap + // between the existence check above and the creation. + let contained = state.contained(path)?; + state + .root_dir() + .open_with( + &contained, + cap_std::fs::OpenOptions::new().write(true).create_new(true), + ) + .context("touch failed")?; let op = Operation::new(OperationType::CreateFile, path.to_string(), None); let op_id = op.id; @@ -243,6 +284,138 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { Ok(()) } +/// The permissions, owner and modification time in `meta`. +#[cfg(unix)] +fn file_meta(meta: &cap_std::fs::Metadata) -> Option { + use cap_std::fs::MetadataExt; + Some(crate::state::FileMeta { + mode: meta.mode() & 0o7777, + uid: meta.uid(), + gid: meta.gid(), + mtime: meta.mtime(), + mtime_nsec: meta.mtime_nsec(), + }) +} + +#[cfg(not(unix))] +fn file_meta(_meta: &cap_std::fs::Metadata) -> Option { + None +} + +/// Put back a file that was removed: its content, and what it was if +/// `meta` is known. +/// +/// `recorded` is a path from the history, relative to the session root. The +/// file is created beneath the session root, and only if nothing has the +/// name. Whatever has taken the name since the removal is left as it is: +/// writing over it replaced a file that another program had made. The file +/// is created readable by its owner only, and gets its recorded +/// permissions once the content is in place, so its content is never +/// exposed under looser permissions than it had. +fn restore_removed_file( + state: &ShellState, + recorded: &str, + content: &[u8], + meta: Option<&crate::state::FileMeta>, +) -> Result<()> { + use std::io::Write; + + let contained = state.contained_abs(&state.resolve_op_path(recorded))?; + let mut options = cap_std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use cap_std::fs::OpenOptionsExt; + options.mode(0o600); + } + let mut file = match state.root_dir().open_with(&contained, &options) { + Ok(file) => file.into_std(), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => anyhow::bail!( + "something else has the name {recorded} now, and it was left as it is; \ + move it away to restore the removed file" + ), + Err(e) => return Err(e.into()), + }; + file.write_all(content)?; + + #[cfg(unix)] + if let Some(meta) = meta { + use std::os::unix::fs::PermissionsExt; + // The owner first: changing it clears set-ID bits. It needs + // privilege when the owner differs, so a failure is not an error. + let _ = std::os::unix::fs::fchown(&file, Some(meta.uid), Some(meta.gid)); + file.set_permissions(fs::Permissions::from_mode(meta.mode & 0o7777))?; + if let Some(modified) = modification_time(meta) { + file.set_modified(modified)?; + } + } + #[cfg(not(unix))] + let _ = meta; + + Ok(()) +} + +/// Put back the content of a file that a redirection emptied. +/// +/// `recorded` is a path from the history, relative to the session root. The +/// file must still be there. It is opened beneath the session root without +/// truncating it and checked while open, as a redirection's target is: a +/// regular file with no other name. Only then is it emptied and written. +/// Opening it with truncation emptied whatever had the name by then, a +/// hard link to a file outside the session root included. +fn restore_content(state: &ShellState, recorded: &str, content: &[u8]) -> Result<()> { + use std::io::Write; + + let path = state.resolve_op_path(recorded); + let contained = state.contained_abs(&path)?; + let mut file = + crate::redirection::open_checked_target(state.root_dir(), &contained, &path)?.into_std(); + file.set_len(0)?; + file.write_all(content)?; + Ok(()) +} + +/// The recorded modification time as a `SystemTime`, if it is representable. +#[cfg(unix)] +fn modification_time(meta: &crate::state::FileMeta) -> Option { + let nanos = u32::try_from(meta.mtime_nsec).ok()?; + if meta.mtime >= 0 { + std::time::UNIX_EPOCH.checked_add(std::time::Duration::new(meta.mtime as u64, nanos)) + } else { + std::time::UNIX_EPOCH + .checked_sub(std::time::Duration::from_secs(meta.mtime.unsigned_abs()))? + .checked_add(std::time::Duration::from_nanos(u64::from(nanos))) + } +} + +/// Remove the regular file at `contained` (beneath the session root), which +/// `named` describes, and return what undo needs to put it back: its content +/// and what the file was. `command` names the caller in the error. +/// +/// A file that cannot be read cannot be restored, so it is not removed: +/// saving it as empty would lose it on undo. +fn remove_keeping( + state: &ShellState, + contained: &std::path::Path, + named: &cap_std::fs::Metadata, + command: &str, +) -> Result<(Vec, Option)> { + // Only a regular file is read. Opening a FIFO would wait for a writer, + // and a device can be read without end. + if !named.is_file() { + anyhow::bail!("{command} refused: not a regular file"); + } + let content = state.root_dir().read(contained).with_context(|| { + format!("{command} refused: the file cannot be read, so it could not be restored by undo") + })?; + let meta = file_meta(named); + state + .root_dir() + .remove_file(contained) + .with_context(|| format!("{command} failed"))?; + Ok((content, meta)) +} + /// Remove a file at the specified path. /// /// The file's content is preserved for undo. This operation is reversible via [`undo`] @@ -270,26 +443,44 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn rm(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { - let full_path = state.resolve_path(path); - // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_delete_file(state.root(), path)?; - - if !full_path.exists() { - anyhow::bail!("Path does not exist (ENOENT)"); - } - if full_path.is_dir() { + verification::verify_delete_file(state.root(), &state.rooted(path))?; + + // Look at the name itself, beneath the session root. + let contained = state.contained(path)?; + let named = match state.root_dir().symlink_metadata(&contained) { + Ok(meta) => meta, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + anyhow::bail!("Path does not exist (ENOENT)") + } + Err(e) => return Err(e).context("rm failed"), + }; + if named.is_dir() { anyhow::bail!("Path is a directory - use rmdir (EISDIR)"); } - // Store content for undo (pure read of the soon-to-be-deleted file; - // routed through fs_pure to honour the noatime discipline). - let content = crate::fs_pure::read_to_end(&full_path).unwrap_or_default(); - - fs::remove_file(&full_path).context("rm failed")?; - - let op = - Operation::new(OperationType::DeleteFile, path.to_string(), None).with_undo_data(content); + let op = if named.is_symlink() { + // Removing a link removes the link, not what it points to. Undo + // puts the link back. + let target = state + .root_dir() + .read_link_contents(&contained) + .context("rm failed: cannot read the link")?; + state + .root_dir() + .remove_file(&contained) + .context("rm failed")?; + Operation::new(OperationType::Unlink, path.to_string(), None) + .with_undo_data(link_target_bytes(&target)) + } else { + let (content, meta) = remove_keeping(state, &contained, &named, "rm")?; + let op = Operation::new(OperationType::DeleteFile, path.to_string(), None) + .with_undo_data(content); + match meta { + Some(meta) => op.with_undo_meta(meta), + None => op, + } + }; let op_id = op.id; state.record_operation(op); @@ -341,7 +532,7 @@ pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result let dst_path = state.resolve_path(dst); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_copy_file(state.root(), src, dst)?; + verification::verify_copy_file(state.root(), &state.rooted(src), &state.rooted(dst))?; // Check preconditions (matching Lean 4 copyFilePrecondition) if !src_path.exists() { @@ -363,8 +554,12 @@ pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result // Record in history: path = dst, undo_data = src path for reference // Undo = delete the destination file + // + // The source is stored relative to the session root, as the destination + // is. Stored as typed, a copy made in `sub` was redone from the root + // with the wrong source: another file of that name, or none. let op = Operation::new(OperationType::CopyFile, dst.to_string(), None) - .with_undo_data(src.as_bytes().to_vec()); + .with_undo_data(state.rooted(src).into_bytes()); let op_id = op.id; state.record_operation(op); @@ -418,7 +613,7 @@ pub fn mv(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result let dst_path = state.resolve_path(dst); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_move(state.root(), src, dst)?; + verification::verify_move(state.root(), &state.rooted(src), &state.rooted(dst))?; // Check preconditions (matching Lean 4 movePrecondition) if !src_path.exists() { @@ -439,8 +634,12 @@ pub fn mv(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result anyhow::bail!("Parent of destination does not exist (ENOENT)"); } - // Execute operation - fs::rename(&src_path, &dst_path).context("mv failed")?; + // Execute operation, beneath the session root. + let (from, to) = (state.contained(src)?, state.contained(dst)?); + state + .root_dir() + .rename(&from, state.root_dir(), &to) + .context("mv failed")?; // Record: store both paths null-separated for undo // path = "src\0dst" so undo can move dst back to src @@ -496,7 +695,7 @@ pub fn symlink(state: &mut ShellState, target: &str, link: &str, verbose: bool) let link_path = state.resolve_path(link); // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_symlink(state.root(), target, link)?; + verification::verify_symlink(state.root(), target, &state.rooted(link))?; // Check preconditions (matching Lean 4 SymlinkPrecondition) if link_path.exists() || link_path.symlink_metadata().is_ok() { @@ -846,7 +1045,7 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { continue; }; - let path = state.resolve_path(&op.path); + let path = state.resolve_op_path(&op.path); // Execute inverse operation match inverse_type { @@ -860,12 +1059,21 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { fs::remove_file(&path).context("Undo touch failed")?; } OperationType::CreateFile => { - let content = op.undo_data.as_ref().cloned().unwrap_or_default(); - fs::write(&path, content).context("Undo rm failed")?; + // A record with no content cannot put the file back. An + // empty file in its place would look like a restore. + let content = op + .undo_data + .as_deref() + .context("Undo rm failed: the record holds no content")?; + restore_removed_file(state, &op.path, content, op.undo_meta.as_ref()) + .context("Undo rm failed")?; } OperationType::WriteFile => { - let content = op.undo_data.as_ref().cloned().unwrap_or_default(); - fs::write(&path, content).context("Undo write failed")?; + let content = op + .undo_data + .as_deref() + .context("Undo write failed: the record holds no content")?; + restore_content(state, &op.path, content).context("Undo write failed")?; } OperationType::FileAppended => { // Undo append: truncate file to original size @@ -890,8 +1098,8 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { if parts.len() != 2 { anyhow::bail!("Invalid move operation record"); } - let src_path = state.resolve_path(parts[0]); - let dst_path = state.resolve_path(parts[1]); + let src_path = state.resolve_op_path(parts[0]); + let dst_path = state.resolve_op_path(parts[1]); fs::rename(&dst_path, &src_path).context("Undo mv failed")?; } OperationType::Unlink => { @@ -903,7 +1111,7 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { let target = op .undo_data .as_ref() - .map(|d| String::from_utf8_lossy(d).to_string()) + .map(|d| link_target_from_bytes(d)) .context("Missing symlink target for undo")?; #[cfg(unix)] std::os::unix::fs::symlink(&target, &path) @@ -1047,7 +1255,12 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { } }; - let path = state.resolve_path(&op.path); + let path = state.resolve_op_path(&op.path); + + // What a removal or a truncation destroys this time: the content, + // and for a removal what the file was. The new record carries it, + // so that it can be undone like the first one. + let mut kept: Option<(Vec, Option)> = None; // Re-execute the original operation match op.op_type { @@ -1061,15 +1274,28 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { fs::write(&path, "").context("Redo touch failed")?; } OperationType::DeleteFile => { - fs::remove_file(&path).context("Redo rm failed")?; + // As `rm` does it: beneath the session root, and keeping + // the content. Without it, `rm f; undo; redo; undo` put an + // empty file in place of `f`. + let contained = state.contained_abs(&path)?; + let named = state + .root_dir() + .symlink_metadata(&contained) + .context("Redo rm failed")?; + kept = Some(remove_keeping(state, &contained, &named, "Redo rm")?); } OperationType::WriteFile => { // Would need new content anyhow::bail!("WriteFile redo not yet implemented"); } OperationType::FileTruncated => { - // Redo truncate: truncate file (original undo_data was the old content) - fs::write(&path, "").context("Redo truncate failed")?; + // Empty the file again, keeping what it holds now, as the + // redirection did. + let contained = state.contained_abs(&path)?; + let content = + crate::redirection::truncate_keeping(state.root_dir(), &contained, &path) + .context("Redo truncate failed")?; + kept = Some((content, None)); } OperationType::FileAppended => { // Cannot redo append without knowing what was appended @@ -1084,7 +1310,8 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { .as_ref() .map(|d| String::from_utf8_lossy(d).to_string()) .context("Missing source path for redo cp")?; - let src_path = state.resolve_path(&src); + // The source is recorded relative to the session root. + let src_path = state.resolve_op_path(&src); fs::copy(&src_path, &path).context("Redo cp failed")?; } OperationType::Move => { @@ -1093,8 +1320,8 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { if parts.len() != 2 { anyhow::bail!("Invalid move operation record for redo"); } - let src_path = state.resolve_path(parts[0]); - let dst_path = state.resolve_path(parts[1]); + let src_path = state.resolve_op_path(parts[0]); + let dst_path = state.resolve_op_path(parts[1]); fs::rename(&src_path, &dst_path).context("Redo mv failed")?; } OperationType::Symlink => { @@ -1102,7 +1329,7 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { let target = op .undo_data .as_ref() - .map(|d| String::from_utf8_lossy(d).to_string()) + .map(|d| link_target_from_bytes(d)) .context("Missing symlink target for redo")?; #[cfg(unix)] std::os::unix::fs::symlink(&target, &path).context("Redo ln -s failed")?; @@ -1152,7 +1379,22 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { } } - let new_op = Operation::new(op.op_type, op.path.clone(), None); + let mut new_op = Operation::new(op.op_type, op.path.clone(), None); + if let Some((content, meta)) = kept { + new_op.undo_data = Some(content); + new_op.undo_meta = meta; + } else if matches!( + op.op_type, + OperationType::CopyFile | OperationType::Symlink | OperationType::Unlink + ) { + // A copy, a link and the removal of a link are redone from what + // the first record carried (the source, the link's target), and + // undoing or redoing the new record needs it again. Without it, + // `rm link; undo; redo; undo` failed with "Missing symlink + // target for undo". + new_op.undo_data = op.undo_data.clone(); + new_op.undo_meta = op.undo_meta; + } let new_id = new_op.id; state.record_redo_operation(new_op); @@ -1355,7 +1597,7 @@ pub fn rollback_transaction(state: &mut ShellState) -> Result<()> { // Undo all operations in reverse order for op in ops.iter().rev() { if let Some(inverse_type) = op.op_type.inverse() { - let path = state.resolve_path(&op.path); + let path = state.resolve_op_path(&op.path); let result = match inverse_type { OperationType::Rmdir => fs::remove_dir(&path).context("Failed to remove directory"), @@ -1363,14 +1605,22 @@ pub fn rollback_transaction(state: &mut ShellState) -> Result<()> { OperationType::DeleteFile => { fs::remove_file(&path).context("Failed to remove file") } - OperationType::CreateFile => { - let content = op.undo_data.as_ref().cloned().unwrap_or_default(); - fs::write(&path, content).context("Failed to restore file") - } - OperationType::WriteFile => { - let content = op.undo_data.as_ref().cloned().unwrap_or_default(); - fs::write(&path, content).context("Failed to restore file content") - } + OperationType::CreateFile => op + .undo_data + .as_deref() + .context("Failed to restore file: the record holds no content") + .and_then(|content| { + restore_removed_file(state, &op.path, content, op.undo_meta.as_ref()) + .context("Failed to restore file") + }), + OperationType::WriteFile => op + .undo_data + .as_deref() + .context("Failed to restore file content: the record holds no content") + .and_then(|content| { + restore_content(state, &op.path, content) + .context("Failed to restore file content") + }), OperationType::FileAppended => { // Rollback append: truncate file to original size if let Some(size_bytes) = op.undo_data.as_ref() { @@ -1393,8 +1643,8 @@ pub fn rollback_transaction(state: &mut ShellState) -> Result<()> { // Rollback move: move it back (dst -> src) let parts: Vec<&str> = op.path.splitn(2, '\0').collect(); if parts.len() == 2 { - let src_path = state.resolve_path(parts[0]); - let dst_path = state.resolve_path(parts[1]); + let src_path = state.resolve_op_path(parts[0]); + let dst_path = state.resolve_op_path(parts[1]); fs::rename(&dst_path, &src_path).context("Failed to undo move") } else { Err(anyhow::anyhow!("Invalid move operation record")) @@ -1407,7 +1657,7 @@ pub fn rollback_transaction(state: &mut ShellState) -> Result<()> { OperationType::Symlink => { // Rollback unlink: re-create the symlink if let Some(target_bytes) = op.undo_data.as_ref() { - let target = String::from_utf8_lossy(target_bytes).to_string(); + let target = link_target_from_bytes(target_bytes); #[cfg(unix)] { std::os::unix::fs::symlink(&target, &path) diff --git a/impl/rust-cli/src/commands/secure_deletion.rs b/impl/rust-cli/src/commands/secure_deletion.rs index 37373908..edc61a94 100644 --- a/impl/rust-cli/src/commands/secure_deletion.rs +++ b/impl/rust-cli/src/commands/secure_deletion.rs @@ -35,46 +35,230 @@ //! operations use [`crate::secure_erase`] and validate the result independently. use anyhow::{bail, Context, Result}; +use cap_fs_ext::{FollowSymlinks, MetadataExt, OpenOptionsFollowExt}; +use cap_std::fs::Dir; use colored::Colorize; -use std::fs::{self, OpenOptions}; use std::io::{BufRead, Read, Seek, SeekFrom, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::state::{Operation, OperationType, ShellState}; /// Dispatch `obliterate` to the file or directory variant by path type. /// This is the entry point wired into the shell parser/executor. +/// +/// The type is read from the name itself, without following a symbolic +/// link: a link to a directory is a link, and is refused like any other. pub fn obliterate_path( state: &mut ShellState, path: &str, verbose: bool, force: bool, ) -> Result<()> { - if state.resolve_path(path).is_dir() { + let contained = state.contained(path)?; + let is_dir = state + .root_dir() + .symlink_metadata(&contained) + .map(|meta| meta.is_dir()) + .unwrap_or(false); + if is_dir { obliterate_dir(state, path, verbose, force) } else { obliterate(state, path, verbose, force) } } -/// Append an RMO audit **residue**: a best-effort record that the command -/// completed its local overwrite-and-unlink sequence. It is evidence of the -/// command outcome, not proof of physical erasure or legal compliance. Written -/// within the shell's sandbox root -/// (`/.vsh-audit.log`) so it is session-isolated and testable. -/// Best-effort: obliteration still succeeds if the residue cannot be written, -/// but a warning is emitted so the failure is never silent. -fn append_rmo_residue(state: &ShellState, op: &Operation, outcome: &str) { - let log_path = state.root.join(".vsh-audit.log"); - match crate::audit_log::AuditLog::new(log_path, None) { - Ok(log) => { - let entry = crate::audit_log::AuditEntry::from_operation(op, outcome, None); - if let Err(e) = log.append(&entry) { - eprintln!("warning: RMO audit residue not written: {e}"); - } +/// Obliteration cannot be undone, so it has no place in a transaction, +/// whose promise is that it can be rolled back (owner ruling OD-3a). +fn refuse_inside_transaction(state: &ShellState) -> Result<()> { + if state.active_transaction.is_some() { + bail!( + "obliterate cannot be undone, so it is refused inside a transaction. \ + Commit or roll back the transaction first." + ); + } + Ok(()) +} + +/// Append an RMO audit entry for `op` to the audit log in the session root +/// (`/.vsh-audit.log`). It is evidence of what the command +/// did, not proof of physical erasure or legal compliance. +/// +/// The `…-intent` entry is written *before* anything is destroyed, and its +/// failure stops the command: nothing is obliterated that the log does not +/// know about. The entry written afterwards records the outcome; if that +/// one fails, the destruction has already happened, so the caller only +/// warns. +fn append_rmo_residue(state: &ShellState, op: &Operation, outcome: &str) -> Result<()> { + append_rmo_entry(state, op, outcome, None) +} + +/// As [`append_rmo_residue`], with the text of the error that ended the +/// obliteration, if one did. +fn append_rmo_entry( + state: &ShellState, + op: &Operation, + outcome: &str, + error: Option, +) -> Result<()> { + let log_path = state.session_root().join(".vsh-audit.log"); + let log = + crate::audit_log::AuditLog::new(log_path, None).context("RMO audit log unavailable")?; + let entry = crate::audit_log::AuditEntry::from_operation(op, outcome, error); + log.append(&entry).context("RMO audit entry not written") +} + +/// Record an obliteration that began and then failed. +/// +/// By this point the audit log holds the intent entry, and the history has +/// been scrubbed and saved: its earlier operations on the path are marked +/// as undone by this one. Returning the error alone left the log with an +/// intent and no outcome, and the history without the operation, although +/// some or all of the data may be gone. So the outcome is written (with +/// the error), and the operation is recorded, before the error is returned. +fn record_failed_obliteration( + state: &mut ShellState, + op: Operation, + outcome: &str, + error: &anyhow::Error, +) { + if let Err(e) = append_rmo_entry(state, &op, outcome, Some(format!("{error:#}"))) { + eprintln!("warning: RMO audit outcome not written: {e:#}"); + } + state.record_rooted_operation(op); +} + +/// The regular file at `contained`, opened for overwriting, and its size. +/// +/// Refuses anything that would destroy data reachable some other way: +/// +/// * a symbolic link, because the data belongs to the link's target, which +/// may lie outside the session root; +/// * a file with more than one hard link, because its other names, which +/// may lie outside the session root, would lose their contents. +/// +/// The file is opened without following a link, and the checks are made on +/// the open file, so the name cannot be swapped for a link in between. +fn open_for_obliteration(dir: &Dir, contained: &Path) -> Result<(cap_std::fs::File, u64)> { + let named = match dir.symlink_metadata(contained) { + Ok(meta) => meta, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + bail!("Path does not exist (ENOENT)") } - Err(e) => eprintln!("warning: RMO audit log unavailable: {e}"), + Err(e) => return Err(e).context("Failed to stat target"), + }; + if named.is_symlink() { + bail!( + "Path is a symbolic link. obliterate does not follow links: remove the link \ + with rm, or obliterate its target by the target's own path." + ); + } + if named.is_dir() { + bail!("Path is a directory (EISDIR)"); + } + if !named.is_file() { + bail!("Path is not a regular file"); + } + + let mut options = cap_std::fs::OpenOptions::new(); + options.write(true).follow(FollowSymlinks::No); + let file = dir + .open_with(contained, &options) + .context("Failed to open file for secure overwrite")?; + let opened = file.metadata().context("Failed to stat opened file")?; + if !opened.is_file() || opened.dev() != named.dev() || opened.ino() != named.ino() { + bail!("Path changed while it was being opened; nothing was overwritten"); } + refuse_other_names(&opened)?; + Ok((file, opened.len())) +} + +/// Refuse a file that has more than one name. `opened` is the metadata of +/// the open file. +fn refuse_other_names(opened: &cap_std::fs::Metadata) -> Result<()> { + if opened.nlink() > 1 { + bail!( + "File has {} hard links. Obliterating it would destroy the data under its \ + other names too, so it is refused.", + opened.nlink() + ); + } + Ok(()) +} + +/// Overwrite the open `file` three times, remove its name, and make the +/// removal durable in its directory. +fn overwrite_and_unlink(dir: &Dir, contained: &Path, file: cap_std::fs::File) -> Result<()> { + overwrite_and_remove_name(dir, contained, file)?; + sync_directory(dir, contained.parent()) +} + +/// Overwrite the open `file` three times, then remove its name. +/// +/// The name is removed only if it still refers to the file that was +/// overwritten. The removal is not yet durable: the caller syncs the +/// directory. +fn overwrite_and_remove_name(dir: &Dir, contained: &Path, file: cap_std::fs::File) -> Result<()> { + overwrite_and_remove_name_with(dir, contained, file, overwrite_3pass) +} + +/// [`overwrite_and_remove_name`], with the overwrite passed in as +/// `overwrite` (given the file and its length), so that a test can add to +/// the file while it is being overwritten. +/// +/// The length is read here, from the open file, at the moment the +/// overwrite starts. A length read earlier (when the file was opened, +/// before the wait for a confirmation) misses what another program +/// appended in between: those bytes were left as they were, and the name +/// was removed all the same. If the file is longer after the overwrite +/// than the length that was overwritten, it grew during the overwrite; +/// that is an error, and the name is left in place. +fn overwrite_and_remove_name_with( + dir: &Dir, + contained: &Path, + file: cap_std::fs::File, + overwrite: impl FnOnce(&mut std::fs::File, u64) -> Result<()>, +) -> Result<()> { + let identity = file.metadata().context("Failed to stat opened file")?; + let file_size = identity.len(); + let mut file = file.into_std(); + overwrite(&mut file, file_size)?; + let length_now = file + .metadata() + .context("Failed to stat the file after the overwrite")? + .len(); + if length_now > file_size { + bail!( + "The file grew from {file_size} to {length_now} bytes while it was being \ + overwritten. The added bytes were not overwritten, and the name was left in place." + ); + } + drop(file); + + let named = dir + .symlink_metadata(contained) + .context("Failed to stat target after overwrite")?; + if named.dev() != identity.dev() || named.ino() != identity.ino() { + bail!("Path was replaced during the overwrite; the name was left in place"); + } + dir.remove_file(contained) + .context("Failed to delete file after overwrite") +} + +/// Make a removal durable in its directory. +fn sync_directory(dir: &Dir, parent: Option<&Path>) -> Result<()> { + let parent = parent + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + // Opened as a file, read-only: a directory handle from `open_dir` may be + // path-only, which cannot be synced. + #[cfg(unix)] + dir.open(parent) + .with_context(|| format!("Failed to open directory {}", parent.display()))? + .sync_all() + .with_context(|| format!("Failed to sync directory {}", parent.display()))?; + #[cfg(not(unix))] + let _ = (dir, parent); + Ok(()) } /// Simple yes/no confirmation prompt @@ -172,39 +356,14 @@ fn overwrite_pass(file: &mut std::fs::File, file_size: u64, pattern: Pattern) -> /// next begins. /// /// See module-level docs for filesystem limitations (CoW / FTL remapping). -fn secure_overwrite_3pass(file_path: &std::path::Path, file_size: u64) -> Result<()> { - let mut file = OpenOptions::new() - .write(true) - .open(file_path) - .context("Failed to open file for secure overwrite")?; - - overwrite_pass(&mut file, file_size, Pattern::Random)?; - overwrite_pass(&mut file, file_size, Pattern::Fixed(0x00))?; - overwrite_pass(&mut file, file_size, Pattern::Fixed(0xFF))?; +fn overwrite_3pass(file: &mut std::fs::File, file_size: u64) -> Result<()> { + overwrite_pass(file, file_size, Pattern::Random)?; + overwrite_pass(file, file_size, Pattern::Fixed(0x00))?; + overwrite_pass(file, file_size, Pattern::Fixed(0xFF))?; Ok(()) } -/// Make an unlink durable in the containing directory where the platform -/// exposes directory fsync. This closes the common crash window between a -/// successful `unlink(2)` and persistence of that directory entry update. -#[cfg(unix)] -fn sync_parent_directory(path: &Path) -> Result<()> { - let parent = path - .parent() - .filter(|p| !p.as_os_str().is_empty()) - .unwrap_or(Path::new(".")); - std::fs::File::open(parent) - .with_context(|| format!("Failed to open parent directory {}", parent.display()))? - .sync_all() - .with_context(|| format!("Failed to sync parent directory {}", parent.display())) -} - -#[cfg(not(unix))] -fn sync_parent_directory(_path: &Path) -> Result<()> { - Ok(()) -} - /// Securely delete a single regular file by 3-pass overwrite + unlink. /// /// This is the low-level primitive used by [`obliterate`]; exposed publicly so @@ -233,18 +392,21 @@ fn sync_parent_directory(_path: &Path) -> Result<()> { /// remapping, and snapshot-bearing volumes defeat in-place overwrite. For /// hardware-level guarantees on SSDs use [`crate::secure_erase`]. pub fn secure_delete(path: &Path) -> Result<()> { - if !path.exists() { - bail!("Path does not exist (ENOENT): {}", path.display()); - } - let meta = fs::metadata(path).context("Failed to stat target")?; - if meta.is_dir() { - bail!("Path is a directory (EISDIR): {}", path.display()); - } - let file_size = meta.len(); - secure_overwrite_3pass(path, file_size)?; - fs::remove_file(path).context("Failed to unlink after overwrite")?; - sync_parent_directory(path)?; - Ok(()) + // Open the containing directory, then treat the file as a name beneath + // it. This gives the same refusals as `obliterate`: no symbolic link, + // no file with more than one hard link. + let parent = path + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + let name = path + .file_name() + .with_context(|| format!("Path has no file name: {}", path.display()))?; + let dir = Dir::open_ambient_dir(parent, cap_std::ambient_authority()) + .with_context(|| format!("Failed to open directory {}", parent.display()))?; + let (file, _) = open_for_obliteration(&dir, Path::new(name)) + .with_context(|| format!("Cannot securely delete {}", path.display()))?; + overwrite_and_unlink(&dir, Path::new(name), file) } /// Obliterate a file using best-effort overwrite and unlink @@ -281,22 +443,11 @@ pub fn secure_delete(path: &Path) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn obliterate(state: &mut ShellState, path: &str, verbose: bool, force: bool) -> Result<()> { - let full_path = state.resolve_path(path); - - // Precondition checks - if !full_path.exists() { - bail!("Path does not exist (ENOENT)"); - } - if full_path.is_dir() { - bail!("Path is a directory - secure directory deletion not yet implemented (EISDIR)"); - } - - // Get file size before deletion - let metadata = fs::metadata(&full_path)?; - let file_size = metadata.len(); - - // CRITICAL: Require confirmation for irreversible operation - if !force { + obliterate_confirmed_by(state, path, verbose, |full_path, file_size| { + // CRITICAL: Require confirmation for irreversible operation + if force { + return Ok(true); + } eprintln!(); eprintln!( "{}", @@ -316,28 +467,61 @@ pub fn obliterate(state: &mut ShellState, path: &str, verbose: bool, force: bool eprintln!("{}", "This operation CANNOT be undone.".bright_red().bold()); eprintln!(); - if !ask_confirmation("Are you sure you want to obliterate this file?")? { - println!("{}", "Operation cancelled.".bright_yellow()); - return Ok(()); - } + ask_confirmation("Are you sure you want to obliterate this file?") + }) +} + +/// [`obliterate`], with the step that asks for confirmation passed in as +/// `confirm` (given the file's path and its size), so that a test can +/// change the file while the answer is awaited. +fn obliterate_confirmed_by( + state: &mut ShellState, + path: &str, + verbose: bool, + confirm: impl FnOnce(&Path, u64) -> Result, +) -> Result<()> { + refuse_inside_transaction(state)?; + let full_path = state.resolve_path(path); + let contained = state.contained(path)?; + + // Precondition checks, on the file itself rather than on its name. + let (file, file_size) = open_for_obliteration(state.root_dir(), &contained)?; + + if !confirm(&full_path, file_size)? { + println!("{}", "Operation cancelled.".bright_yellow()); + return Ok(()); } - // Step 1: Secure overwrite + // The answer can be a long time coming, and the file can be given + // another name meanwhile. The overwrite would then destroy the data + // under that name too, so the link count is read again from the open + // file, before anything is recorded or destroyed. (The length is read + // again where the overwrite starts.) + refuse_other_names(&file.metadata().context("Failed to stat opened file")?)?; + + // Steps 1 and 2: the audit entry, then the history. Nothing is + // destroyed before both are done. The operation names the path as the + // history stores it, relative to the session root, so that the audit + // entries and the history record name the same file whatever the + // working directory is. + let op = Operation::new(OperationType::Obliterate, state.rooted(path), None); + let op_id = op.id; + prepare_obliteration(state, &op, "obliterate-intent")?; + + // Step 3: Secure overwrite, then delete the name. if verbose { println!("🔥 3-pass overwrite (random / zeros / 0xFF) + unlink..."); } - secure_overwrite_3pass(&full_path, file_size)?; - - // Step 2: Delete file - fs::remove_file(&full_path).context("Failed to delete file after overwrite")?; - sync_parent_directory(&full_path)?; + if let Err(e) = overwrite_and_unlink(state.root_dir(), &contained, file) { + record_failed_obliteration(state, op, "obliterate-failed", &e); + return Err(e); + } - // Step 3: Record as irreversible operation (NO undo_data stored) - let op = Operation::new(OperationType::Obliterate, path.to_string(), None); - let op_id = op.id; - // RMO residue: persist the append-only attestation BEFORE moving `op`. - append_rmo_residue(state, &op, "obliterated"); - state.record_operation(op); + // Step 4: Record the outcome, and the irreversible operation (NO undo_data). + if let Err(e) = append_rmo_residue(state, &op, "obliterated") { + eprintln!("warning: RMO audit outcome not written: {e:#}"); + } + state.record_rooted_operation(op); // Output println!( @@ -366,6 +550,86 @@ pub fn obliterate(state: &mut ShellState, path: &str, verbose: bool, force: bool Ok(()) } +/// What must be done before an obliteration destroys anything. +/// +/// First the audit log is told what is about to happen. Then the history +/// forgets what it holds about `path` (and about anything beneath it, if it +/// is a directory), and that is saved. Earlier operations may hold the +/// content as undo data; if the history could not be saved without it, the +/// content would outlive the file in the saved state. If either step fails, +/// nothing is obliterated. +fn prepare_obliteration(state: &mut ShellState, op: &Operation, intent: &str) -> Result<()> { + append_rmo_residue(state, op, intent) + .context("Nothing was obliterated, because the audit entry could not be written")?; + // The archive of operations that aged out of the history can hold the + // content too. + state.scrub_archive(&op.path).context( + "Nothing was obliterated, because the history archive could not be rewritten without \ + the earlier contents of what was to be obliterated", + )?; + let scrubbed = state.scrub_obliterated(&op.path, op.id); + if let Err(e) = state.save_history() { + // The file stays, so the history must still be able to undo what + // was done to it. + state.restore_scrubbed(scrubbed); + return Err(e).context( + "Nothing was obliterated, because the history could not be saved without the \ + earlier contents of what was to be obliterated", + ); + } + Ok(()) +} + +/// Destroy what [`plan_dir`] found: the files, the links, the directories +/// deepest first, and then the directory `contained` itself. `obliterated` +/// counts the files destroyed, and holds the count reached if this fails +/// part of the way. +fn destroy_planned( + root: &Dir, + plan: &DirPlan, + contained: &Path, + verbose: bool, + obliterated: &mut usize, +) -> Result<()> { + destroy_planned_with(root, plan, contained, verbose, obliterated, sync_directory) +} + +/// [`destroy_planned`], with the step that makes a removal durable passed +/// in as `durable`, so that a test can make that step fail. +fn destroy_planned_with( + root: &Dir, + plan: &DirPlan, + contained: &Path, + verbose: bool, + obliterated: &mut usize, + durable: impl Fn(&Dir, Option<&Path>) -> Result<()>, +) -> Result<()> { + for file_path in &plan.files { + let (file, _) = open_for_obliteration(root, file_path)?; + overwrite_and_remove_name(root, file_path, file)?; + // Counted as soon as the name is gone. Making the removal durable + // can still fail, and the audit entry must then not report one + // file fewer than were destroyed. + *obliterated += 1; + durable(root, file_path.parent())?; + if verbose { + println!(" 🔥 {}", file_path.display()); + } + } + for link in &plan.links { + // Removing a link removes only the link; its target is not touched. + root.remove_file(link)?; + } + for dir_path in &plan.dirs { + root.remove_dir(dir_path)?; + durable(root, dir_path.parent())?; + } + + // Remove root directory + root.remove_dir(contained)?; + durable(root, contained.parent()) +} + /// Obliterate a directory and its contents (recursive) /// /// This is an **irreversible** operation that securely deletes a directory tree. @@ -391,28 +655,34 @@ pub fn obliterate_dir( verbose: bool, force: bool, ) -> Result<()> { + refuse_inside_transaction(state)?; let full_path = state.resolve_path(path); + let contained = state.contained(path)?; - if !full_path.exists() { - bail!("Path does not exist (ENOENT)"); - } - if !full_path.is_dir() { - bail!("Path is not a directory (ENOTDIR)"); + if contained == Path::new(".") { + bail!("Refusing to obliterate the session root itself"); } - - // Count files for confirmation message - let mut file_count = 0usize; - let mut total_size = 0u64; - for entry in walkdir::WalkDir::new(&full_path) { - let entry = entry?; - if entry.file_type().is_file() { - file_count += 1; - if let Ok(metadata) = entry.metadata() { - total_size += metadata.len(); - } + match state.root_dir().symlink_metadata(&contained) { + Ok(meta) if meta.is_dir() => {} + Ok(meta) if meta.is_symlink() => bail!( + "Path is a symbolic link. obliterate does not follow links: remove the link \ + with rm, or obliterate its target by the target's own path." + ), + Ok(_) => bail!("Path is not a directory (ENOTDIR)"), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + bail!("Path does not exist (ENOENT)") } + Err(e) => return Err(e).context("Failed to stat target"), } + // Walk the tree first, without following any link, and refuse the whole + // operation if anything in it cannot be obliterated safely. Nothing is + // destroyed until the whole tree has been checked. + let mut plan = DirPlan::default(); + plan_dir(state.root_dir(), &contained, &mut plan)?; + let file_count = plan.files.len(); + let total_size = plan.total_size; + // CRITICAL: Require confirmation if !force { eprintln!(); @@ -442,36 +712,31 @@ pub fn obliterate_dir( } } - // Obliterate all files in directory tree - let mut obliterated = 0usize; - for entry in walkdir::WalkDir::new(&full_path).contents_first(true) - // Process files before directories - { - let entry = entry?; - let entry_path = entry.path(); + // The audit entry, then the history, before anything is destroyed. + let op = Operation::new(OperationType::Obliterate, state.rooted(path), None); + let op_id = op.id; + prepare_obliteration(state, &op, "obliterate-dir-intent")?; - if entry.file_type().is_file() { - secure_delete(entry_path)?; - obliterated += 1; - if verbose { - println!(" 🔥 {}", entry_path.display()); - } - } else if entry.file_type().is_dir() && entry_path != full_path { - fs::remove_dir(entry_path)?; - sync_parent_directory(entry_path)?; - } + // Obliterate the files, remove the links, then the directories, deepest + // first. + let mut obliterated = 0usize; + if let Err(e) = destroy_planned( + state.root_dir(), + &plan, + &contained, + verbose, + &mut obliterated, + ) { + let outcome = format!("obliterate-dir-failed-after-{obliterated}-of-{file_count}-files"); + record_failed_obliteration(state, op, &outcome, &e); + return Err(e); } - // Remove root directory - fs::remove_dir(&full_path)?; - sync_parent_directory(&full_path)?; - - // Record operation - let op = Operation::new(OperationType::Obliterate, path.to_string(), None); - let op_id = op.id; - // RMO residue: persist the append-only attestation BEFORE moving `op`. - append_rmo_residue(state, &op, "obliterated-dir"); - state.record_operation(op); + // Record the outcome and the operation. + if let Err(e) = append_rmo_residue(state, &op, "obliterated-dir") { + eprintln!("warning: RMO audit outcome not written: {e:#}"); + } + state.record_rooted_operation(op); println!( "{} {} {}", @@ -491,10 +756,64 @@ pub fn obliterate_dir( Ok(()) } +/// What obliterating a directory will do, worked out before anything is +/// destroyed. Paths are relative to the session root. +#[derive(Default)] +struct DirPlan { + /// Regular files to overwrite and remove. + files: Vec, + /// Symbolic links to remove (the link only). + links: Vec, + /// Sub-directories to remove, deepest first. + dirs: Vec, + total_size: u64, +} + +/// Walk the directory at `contained` without following links, filling +/// `plan`. Fails, with nothing destroyed, on a file with more than one hard +/// link or on anything that is not a regular file, a link or a directory. +fn plan_dir(dir: &Dir, contained: &Path, plan: &mut DirPlan) -> Result<()> { + for entry in dir + .read_dir(contained) + .with_context(|| format!("Failed to read directory {}", contained.display()))? + { + let entry = entry?; + let child = contained.join(entry.file_name()); + let meta = dir + .symlink_metadata(&child) + .with_context(|| format!("Failed to stat {}", child.display()))?; + if meta.is_symlink() { + plan.links.push(child); + } else if meta.is_dir() { + plan_dir(dir, &child, plan)?; + plan.dirs.push(child); + } else if meta.is_file() { + if meta.nlink() > 1 { + bail!( + "{} has {} hard links. Obliterating it would destroy the data under \ + its other names too, so the whole directory is refused.", + child.display(), + meta.nlink() + ); + } + plan.total_size += meta.len(); + plan.files.push(child); + } else { + bail!( + "{} is not a regular file, a link or a directory; the whole directory \ + is refused.", + child.display() + ); + } + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; + use std::fs; use tempfile::TempDir; #[test] @@ -507,7 +826,9 @@ mod tests { fs::write(&file_path, original).unwrap(); let file_size = fs::metadata(&file_path).unwrap().len(); - secure_overwrite_3pass(&file_path, file_size).unwrap(); + let mut file = fs::OpenOptions::new().write(true).open(&file_path).unwrap(); + overwrite_3pass(&mut file, file_size).unwrap(); + drop(file); // Verify content is overwritten (not original data) let after = fs::read(&file_path).unwrap(); @@ -515,6 +836,123 @@ mod tests { assert_ne!(&after[..], original); } + /// A file whose name is gone has been destroyed, whether or not its + /// directory could then be synced. Counting it only after the sync left + /// the audit entry one file short ("failed after 0 of 2 files" with one + /// file gone). + #[test] + fn test_a_file_is_counted_once_its_name_is_gone() { + let temp_dir = TempDir::new().unwrap(); + fs::create_dir(temp_dir.path().join("d")).unwrap(); + fs::write(temp_dir.path().join("d/one"), b"first").unwrap(); + fs::write(temp_dir.path().join("d/two"), b"second").unwrap(); + let root = Dir::open_ambient_dir(temp_dir.path(), cap_std::ambient_authority()).unwrap(); + let mut plan = DirPlan::default(); + plan_dir(&root, Path::new("d"), &mut plan).unwrap(); + assert_eq!(plan.files.len(), 2); + + let mut destroyed = 0usize; + let result = destroy_planned_with( + &root, + &plan, + Path::new("d"), + false, + &mut destroyed, + |_, _| bail!("the directory could not be synced"), + ); + + assert!( + result.is_err(), + "the failed sync must stop the obliteration" + ); + let left = fs::read_dir(temp_dir.path().join("d")).unwrap().count(); + assert_eq!(left, 1, "one file was destroyed before the sync failed"); + assert_eq!(destroyed, 1, "the destroyed file must be counted"); + } + + /// What another program appends to the file while the confirmation is + /// awaited is overwritten too. The length was read before the wait, so + /// the added bytes were left as they were and the name was removed. + #[cfg(unix)] + #[test] + fn test_bytes_appended_during_the_confirmation_are_overwritten() { + let temp_dir = TempDir::new().unwrap(); + let mut state = ShellState::new(temp_dir.path().to_str().unwrap()).unwrap(); + let path = temp_dir.path().join("f"); + fs::write(&path, b"first").unwrap(); + // A descriptor of the test's own, which outlives the name. + let mut kept = fs::File::open(&path).unwrap(); + + obliterate_confirmed_by(&mut state, "f", false, |_, _| { + fs::OpenOptions::new() + .append(true) + .open(&path) + .unwrap() + .write_all(b"SECRET") + .unwrap(); + Ok(true) + }) + .unwrap(); + + assert!(!path.exists()); + let mut left = Vec::new(); + kept.read_to_end(&mut left).unwrap(); + assert_eq!(left.len(), b"firstSECRET".len()); + assert!( + left.iter().all(|byte| *byte == 0xFF), + "bytes were left as they were: {left:?}" + ); + } + + /// A hard link made while the confirmation is awaited is seen, and the + /// file is refused before anything is recorded or overwritten. The + /// link count was read only before the wait. + #[cfg(unix)] + #[test] + fn test_a_hard_link_made_during_the_confirmation_is_refused() { + let temp_dir = TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + fs::create_dir(&root).unwrap(); + let mut state = ShellState::new(root.to_str().unwrap()).unwrap(); + let path = root.join("f"); + let other_name = temp_dir.path().join("other-name"); + fs::write(&path, b"first").unwrap(); + + let refused = obliterate_confirmed_by(&mut state, "f", false, |_, _| { + fs::hard_link(&path, &other_name).unwrap(); + Ok(true) + }) + .unwrap_err(); + + assert!(format!("{refused:#}").contains("hard links"), "{refused:#}"); + assert_eq!(fs::read(&other_name).unwrap(), b"first"); + assert_eq!(fs::read(&path).unwrap(), b"first"); + let audit = fs::read_to_string(root.join(".vsh-audit.log")).unwrap_or_default(); + assert!(!audit.contains("obliterate"), "{audit}"); + } + + /// A file that grows while it is being overwritten is not removed, and + /// that is an error: the added bytes were not overwritten. + #[test] + fn test_a_file_that_grows_during_the_overwrite_keeps_its_name() { + let temp_dir = TempDir::new().unwrap(); + fs::write(temp_dir.path().join("f"), b"first").unwrap(); + let root = Dir::open_ambient_dir(temp_dir.path(), cap_std::ambient_authority()).unwrap(); + let (file, _) = open_for_obliteration(&root, Path::new("f")).unwrap(); + + let refused = + overwrite_and_remove_name_with(&root, Path::new("f"), file, |file, length| { + overwrite_3pass(file, length)?; + file.seek(SeekFrom::End(0))?; + file.write_all(b"added meanwhile")?; + Ok(()) + }) + .unwrap_err(); + + assert!(format!("{refused:#}").contains("grew"), "{refused:#}"); + assert!(temp_dir.path().join("f").exists()); + } + #[test] #[ignore] // Requires user confirmation fn test_obliterate() { diff --git a/impl/rust-cli/src/executable.rs b/impl/rust-cli/src/executable.rs index aeb72a25..51b94493 100644 --- a/impl/rust-cli/src/executable.rs +++ b/impl/rust-cli/src/executable.rs @@ -295,7 +295,7 @@ impl ExecutableCommand for Command { let target = if let Some(ref p) = expanded_path { state.resolve_path(p) } else { - state.root.clone() + state.cwd().to_path_buf() }; if !target.is_dir() { @@ -321,7 +321,7 @@ impl ExecutableCommand for Command { let target = if let Some(ref p) = expanded_path { s.resolve_path(p) } else { - s.root.clone() + s.cwd().to_path_buf() }; if !target.is_dir() { @@ -347,10 +347,10 @@ impl ExecutableCommand for Command { Command::Pwd { redirects } => { if redirects.is_empty() { - println!("{}", state.root.display()); + println!("{}", state.cwd().display()); } else { redirection::capture_and_redirect(redirects, state, |s| { - println!("{}", s.root.display()); + println!("{}", s.cwd().display()); Ok(()) })?; } @@ -379,18 +379,29 @@ impl ExecutableCommand for Command { } } } else if p.starts_with('/') { - // Absolute path (also handles expanded ~/... since - // tilde expansion in expand_variables already turned - // ~/path into /home/user/path) - PathBuf::from(p) + // A path that starts with `/`. If it names a real + // directory inside the session root (as `pwd` prints, + // or as `~/...` expands to), use it. Otherwise read it + // as relative to the session root, as every other + // command does. + let literal = PathBuf::from(p); + let inside = std::fs::canonicalize(&literal) + .map(|real| real.starts_with(state.session_root())) + .unwrap_or(false); + if inside { + literal + } else { + state.resolve_path(p) + } } else { - // Relative to current directory - state.root.join(p) + // Relative to the working directory + state.resolve_path(p) } } else { - // cd with no args goes to home - dirs::home_dir() - .ok_or_else(|| anyhow::anyhow!("Could not find home directory"))? + // `cd` with no argument goes to the session root: the + // shell is confined to it, and the home directory is + // normally outside. + state.session_root().to_path_buf() }; if !target.exists() { @@ -401,14 +412,19 @@ impl ExecutableCommand for Command { anyhow::bail!("cd: {}: Not a directory", target.display()); } - // Store current directory as previous before changing - state.previous_dir = Some(state.root.clone()); - - // Change the shell's working directory - std::env::set_current_dir(&target)?; - - // Update state root to track current directory - state.root = std::fs::canonicalize(target)?; + // Confine the working directory to the session root. This + // resolves symbolic links first, so a link that leads out of + // the root is refused too. + let previous = state.cwd().to_path_buf(); + state.set_cwd(&target)?; + + // Change the process's working directory, for external + // commands. If that fails, the shell's own stays put. + if let Err(e) = std::env::set_current_dir(state.cwd()) { + state.set_cwd(&previous)?; + return Err(e.into()); + } + state.previous_dir = Some(previous); Ok(ExecutionResult::Success) } diff --git a/impl/rust-cli/src/external.rs b/impl/rust-cli/src/external.rs index 4dbc9f94..a1cd03be 100644 --- a/impl/rust-cli/src/external.rs +++ b/impl/rust-cli/src/external.rs @@ -6,7 +6,7 @@ //! and manages stdio. use anyhow::{Context, Result}; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::path::PathBuf; use std::process::{Command, Stdio}; use std::time::Duration; @@ -30,12 +30,25 @@ pub fn execute_external_with_redirects( redirects: &[Redirection], state: &mut ShellState, ) -> Result { - // Setup redirections (validates, opens files, saves state) - let redirect_setup = RedirectSetup::setup(redirects, state)?; + // Set the redirections up (validate, open the files, save their + // state), run the command, and record what the set-up changed whatever + // becomes of the command: found or not, finished or interrupted. + RedirectSetup::run(redirects, state, |redirect_setup, state| { + run_with_redirects(program, args, redirects, redirect_setup, state) + }) +} +/// Run `program` with its redirections already set up as `redirect_setup`. +fn run_with_redirects( + program: &str, + args: &[String], + redirects: &[Redirection], + redirect_setup: &RedirectSetup, + state: &mut ShellState, +) -> Result { // Configure stdio from redirections let (stdin_cfg, stdout_cfg, stderr_cfg) = - stdio_config_from_redirects(redirects, &redirect_setup, state)?; + stdio_config_from_redirects(redirects, redirect_setup, state)?; // Expand glob patterns in arguments (Phase 6 M12) let expanded_args = expand_glob_args(args, state)?; @@ -67,8 +80,7 @@ pub fn execute_external_with_redirects( loop { match child.try_wait().context("Failed to wait for child")? { Some(status) => { - // Child exited - record modifications and return - redirect_setup.record_for_undo(state)?; + // Child exited. The caller records the modifications. return handle_exit_status(status); } None => { @@ -81,7 +93,9 @@ pub fn execute_external_with_redirects( // Wait for child to actually terminate child.wait().context("Failed to wait for killed child")?; - // Don't record modifications - operation interrupted + // The modifications are recorded all the same (by the + // caller): the targets were emptied and may have been + // written to. return Ok(130); // 128 + SIGINT(2) } @@ -139,13 +153,24 @@ pub fn execute_pipeline( return execute_external_with_redirects(&stages[0].0, &stages[0].1, redirects, state); } - // Setup final redirection output files - let redirect_setup = if !redirects.is_empty() { - Some(RedirectSetup::setup(redirects, state)?) - } else { - None - }; + if redirects.is_empty() { + return run_pipeline(stages, redirects, None, state); + } + // Set up the final redirection, run the stages, and record what the + // set-up changed whatever becomes of them. + RedirectSetup::run(redirects, state, |redirect_setup, state| { + run_pipeline(stages, redirects, Some(redirect_setup), state) + }) +} +/// Run the stages of a pipeline. `redirect_setup` is the set-up of the +/// redirections of the last stage, if it has any. +fn run_pipeline( + stages: &[(String, Vec)], + redirects: &[Redirection], + redirect_setup: Option<&RedirectSetup>, + state: &mut ShellState, +) -> Result { // Configure stdio for stages and spawn children let mut children: Vec = Vec::new(); let mut prev_stdout: Option = None; @@ -179,7 +204,7 @@ pub fn execute_pipeline( // Option both removes a duplicated emptiness check and eliminates // a previous panic site. let stdout_cfg = if is_last { - match &redirect_setup { + match redirect_setup { Some(setup) => stdio_config_from_redirects(redirects, setup, state)?.1, None => Stdio::inherit(), } @@ -189,7 +214,7 @@ pub fn execute_pipeline( }; // Configure stderr (inherit unless redirected in last stage). - let stderr_cfg = match (is_last, &redirect_setup) { + let stderr_cfg = match (is_last, redirect_setup) { (true, Some(setup)) => stdio_config_from_redirects(redirects, setup, state)?.2, _ => Stdio::inherit(), }; @@ -242,7 +267,8 @@ pub fn execute_pipeline( signals::clear_interrupt(); child.wait().context("Failed to wait for killed child")?; - // Don't record modifications - operation interrupted + // The caller records the modifications all the + // same. return Ok(130); // 128 + SIGINT } std::thread::sleep(Duration::from_millis(50)); @@ -251,11 +277,6 @@ pub fn execute_pipeline( } } - // Record undo for final redirection (if any) - if let Some(setup) = redirect_setup { - setup.record_for_undo(state)?; - } - Ok(final_exit_code) } @@ -330,9 +351,16 @@ pub fn execute_external(program: &str, args: &[String]) -> Result { } /// Convert redirections to stdio configuration +/// +/// The child is given the files that `setup` opened. They were opened once, +/// beneath the session root, and checked while open (see +/// [`RedirectSetup::handle`]). Before, each name was opened a second time +/// here with an ordinary path: a name given to a link in between was +/// followed out of the session root, and a file was truncated without the +/// checks. fn stdio_config_from_redirects( redirects: &[Redirection], - _setup: &RedirectSetup, + setup: &RedirectSetup, state: &mut ShellState, ) -> Result<(Stdio, Stdio, Stdio)> { let mut stdin_cfg = Stdio::inherit(); @@ -342,12 +370,10 @@ fn stdio_config_from_redirects( // Track stdout's file handle for 2>&1 fd duplication let mut stdout_file_dup: Option = None; - for redirect in redirects { + for (index, redirect) in redirects.iter().enumerate() { match redirect { - Redirection::Output { file } => { - let target = state.resolve_path(file); - let file_handle = File::create(&target) - .with_context(|| format!("Failed to open output file: {}", target.display()))?; + Redirection::Output { .. } | Redirection::Append { .. } => { + let file_handle = setup.handle(index)?; // Keep a clone for potential 2>&1 duplication stdout_file_dup = Some( file_handle @@ -357,51 +383,18 @@ fn stdio_config_from_redirects( stdout_cfg = Stdio::from(file_handle); } - Redirection::Append { file } => { - let target = state.resolve_path(file); - let file_handle = OpenOptions::new() - .create(true) - .append(true) - .open(&target) - .with_context(|| { - format!( - "Failed to open output file for append: {}", - target.display() - ) - })?; - stdout_file_dup = Some( - file_handle - .try_clone() - .context("Failed to duplicate stdout file handle for 2>&1 tracking")?, - ); - stdout_cfg = Stdio::from(file_handle); + Redirection::Input { .. } => { + stdin_cfg = Stdio::from(setup.handle(index)?); } - Redirection::Input { file } => { - let target = state.resolve_path(file); - let file_handle = crate::fs_pure::open_pure(&target) - .with_context(|| format!("Failed to open input file: {}", target.display()))?; - stdin_cfg = Stdio::from(file_handle); + Redirection::ErrorOutput { .. } | Redirection::ErrorAppend { .. } => { + stderr_cfg = Stdio::from(setup.handle(index)?); } - Redirection::ErrorOutput { file } | Redirection::ErrorAppend { file } => { - let target = state.resolve_path(file); - let file_handle = if matches!(redirect, Redirection::ErrorAppend { .. }) { - OpenOptions::new().create(true).append(true).open(&target) - } else { - File::create(&target) - } - .with_context(|| format!("Failed to open error file: {}", target.display()))?; - - stderr_cfg = Stdio::from(file_handle); - } - - Redirection::BothOutput { file } => { - let target = state.resolve_path(file); - let file_handle = File::create(&target) - .with_context(|| format!("Failed to open output file: {}", target.display()))?; - - // Clone the file handle for both stdout and stderr + Redirection::BothOutput { .. } => { + // One open file for both streams, so that they share one + // position in it. + let file_handle = setup.handle(index)?; let file_handle2 = file_handle .try_clone() .context("Failed to duplicate file handle")?; @@ -617,12 +610,26 @@ pub fn execute_external_background( redirects: &[Redirection], state: &mut ShellState, ) -> Result { - // Setup redirections if any - let redirect_setup = RedirectSetup::setup(redirects, state)?; + // Set the redirections up, start the job, and record what the set-up + // changed. A background job's redirection was never recorded, so a + // file it emptied could not be restored by undo. + RedirectSetup::run(redirects, state, |redirect_setup, state| { + start_background(program, args, redirects, redirect_setup, state) + }) +} +/// Start `program` as a background job, with its redirections already set +/// up as `redirect_setup`. +fn start_background( + program: &str, + args: &[String], + redirects: &[Redirection], + redirect_setup: &RedirectSetup, + state: &mut ShellState, +) -> Result { // Configure stdio from redirections let (stdin_cfg, stdout_cfg, stderr_cfg) = - stdio_config_from_redirects(redirects, &redirect_setup, state)?; + stdio_config_from_redirects(redirects, redirect_setup, state)?; // PATH lookup let executable = find_in_path(program)?; @@ -674,6 +681,103 @@ pub fn execute_external_background( mod tests { use super::*; + /// A command's output goes to the file the set-up opened and checked, + /// whatever the name refers to by the time the command starts. The + /// name used to be opened a second time, with an ordinary path: a name + /// given to a file outside the session root in between was followed, + /// and that file was truncated and written. + #[cfg(unix)] + #[test] + fn test_a_command_writes_to_the_file_that_was_opened_and_checked() { + use std::fs; + + for as_hard_link in [true, false] { + let temp = tempfile::TempDir::new().unwrap(); + let root = temp.path().join("root"); + let victim = temp.path().join("victim"); + fs::create_dir(&root).unwrap(); + fs::write(&victim, "PRECIOUS").unwrap(); + let mut state = ShellState::new(root.to_str().unwrap()).unwrap(); + let redirects = vec![Redirection::Output { + file: "out".to_string(), + }]; + let setup = RedirectSetup::setup(&redirects, &state).unwrap(); + + // Another program gives the name to the file outside the root. + fs::remove_file(root.join("out")).unwrap(); + if as_hard_link { + fs::hard_link(&victim, root.join("out")).unwrap(); + } else { + std::os::unix::fs::symlink(&victim, root.join("out")).unwrap(); + } + + let (_, stdout, _) = + stdio_config_from_redirects(&redirects, &setup, &mut state).unwrap(); + let status = Command::new("sh") + .arg("-c") + .arg("echo written") + .stdout(stdout) + .status() + .unwrap(); + + assert!(status.success()); + assert_eq!( + fs::read_to_string(&victim).unwrap(), + "PRECIOUS", + "as a hard link: {as_hard_link}" + ); + } + } + + /// A command that cannot be found has had its `>` target emptied by + /// then, as in bash. That is recorded all the same, so undo restores + /// the file. It was recorded only when the command had run. + #[test] + fn test_a_command_that_is_not_found_leaves_its_redirect_undoable() { + use std::fs; + + let temp = tempfile::TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let mut state = ShellState::new(root.to_str().unwrap()).unwrap(); + fs::write(root.join("out"), "ORIGINAL").unwrap(); + let redirects = vec![Redirection::Output { + file: "out".to_string(), + }]; + + let outcome = execute_external_with_redirects( + "no-such-command-for-this-test", + &[], + &redirects, + &mut state, + ); + + assert!(outcome.is_err()); + assert_eq!(fs::read(root.join("out")).unwrap(), b""); + crate::commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("out")).unwrap(), b"ORIGINAL"); + } + + /// A background command's redirection is recorded too. It never was, + /// so the file it emptied could not be restored. + #[cfg(unix)] + #[test] + fn test_a_background_command_leaves_its_redirect_undoable() { + use std::fs; + + let temp = tempfile::TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let mut state = ShellState::new(root.to_str().unwrap()).unwrap(); + fs::write(root.join("out"), "ORIGINAL").unwrap(); + let redirects = vec![Redirection::Output { + file: "out".to_string(), + }]; + + execute_external_background("true", &[], &redirects, &mut state).unwrap(); + + crate::commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("out")).unwrap(), b"ORIGINAL"); + } + #[test] fn test_find_ls() { let ls = find_in_path("ls"); diff --git a/impl/rust-cli/src/main.rs b/impl/rust-cli/src/main.rs index ebb288ac..cbd48697 100644 --- a/impl/rust-cli/src/main.rs +++ b/impl/rust-cli/src/main.rs @@ -149,6 +149,21 @@ fn main() -> Result<()> { let mut state = state::ShellState::new(&root)?; + // A script named on the command line is found from the directory the + // shell was started in, so it is located before that directory is left. + let script = cli.script.as_ref().map(|path| { + std::env::current_dir() + .map(|started_in| started_in.join(path)) + .unwrap_or_else(|_| std::path::PathBuf::from(path)) + }); + + // Enter the session root. The shell's own working directory starts + // there, and the process must be there too: a command that the shell + // starts inherits the directory of the process, and `$PWD` reports it. + // Started with `-r` from somewhere else, the shell ran its first + // commands in that other directory, outside the session root. + std::env::set_current_dir(state.cwd()).context("Failed to enter the session root")?; + // Mode 1: Execute inline command string (-c "command") if let Some(ref cmd_string) = cli.command_string { // Set positional parameters: $0 = "vsh", $1.. = script_args @@ -157,9 +172,9 @@ fn main() -> Result<()> { } // Mode 2: Execute a script file (vsh script.sh [args...]) - if let Some(ref script_path) = cli.script { + if let (Some(script_path), Some(script_file)) = (cli.script.as_ref(), script.as_ref()) { // Check for shebang or just execute - let script_content = vsh::fs_pure::read_to_string(std::path::Path::new(script_path)) + let script_content = vsh::fs_pure::read_to_string(script_file) .context(format!("Cannot read script: {}", script_path))?; // Set positional parameters: $0 = script_path, $1.. = script_args diff --git a/impl/rust-cli/src/parser.rs b/impl/rust-cli/src/parser.rs index da677ce3..79035a05 100644 --- a/impl/rust-cli/src/parser.rs +++ b/impl/rust-cli/src/parser.rs @@ -2364,22 +2364,30 @@ fn apply_substring(value: &str, offset: i32, length: Option) -> String { /// Expands an unquoted leading `~` in a word: /// /// - `~` or `~/path` → `$HOME` / `$HOME/path` -/// - `~+` or `~+/...` → `$PWD` / `$PWD/...` -/// - `~-` or `~-/...` → `$OLDPWD` / `$OLDPWD/...` +/// - `~+` or `~+/...` → the working directory +/// - `~-` or `~-/...` → the directory the shell was in before the last `cd` +/// +/// The working directory and the one before it are the shell's own. The +/// process's `PWD` and `OLDPWD` are what the shell was started with, and `cd` +/// does not change them, so reading them gave stale answers: after +/// `cd sub`, `cd ~-` went wherever the shell's parent had last been. Before +/// the first `cd` there is no previous directory of the shell's own, and +/// `~-` falls back to the `OLDPWD` it was started with. /// /// `~user` is not yet supported (requires getpwnam). An escaped `\~` /// (produced by `quoted_word_to_string` for quoted `~`) stays literal. -fn expand_tilde(input: &str) -> String { +/// +/// Returns the text the tilde stands for, and the rest of the word. The +/// first is a directory name and is used as it is; only the second is read +/// for variables. +fn expand_tilde<'a>(input: &'a str, state: &crate::state::ShellState) -> (String, &'a str) { // An escaped tilde `\~` means the tilde was inside quotes — leave it. if let Some(stripped) = input.strip_prefix("\\~") { - let mut s = String::with_capacity(input.len()); - s.push('~'); - s.push_str(stripped); - return s; + return ("~".to_string(), stripped); } if !input.starts_with('~') { - return input.to_string(); + return (String::new(), input); } // After the `~`, the "prefix" extends until the first `/` or end-of-string. @@ -2391,17 +2399,21 @@ fn expand_tilde(input: &str) -> String { let replacement = match prefix { "" => std::env::var("HOME").ok(), - "+" => std::env::var("PWD").ok(), - "-" => std::env::var("OLDPWD").ok(), + "+" => Some(state.cwd().to_string_lossy().into_owned()), + "-" => state + .previous_dir + .as_ref() + .map(|dir| dir.to_string_lossy().into_owned()) + .or_else(|| std::env::var("OLDPWD").ok()), _ => { // ~user — not implemented yet; leave the tilde literal. - return input.to_string(); + return (String::new(), input); } }; match replacement { - Some(home) => format!("{}{}", home, suffix), - None => input.to_string(), + Some(directory) => (directory, suffix), + None => (String::new(), input), } } @@ -2410,9 +2422,13 @@ pub fn expand_variables(input: &str, state: &crate::state::ShellState) -> String // and only at the start of a word (the word has already been split by // the tokeniser). An escaped `\~` (produced by quoted_word_to_string // for `'~'` or `"~"`) is left literal. - let input = expand_tilde(input); + // + // The directory a tilde stands for is taken as it is. Only the rest of + // the word is read for `$`: a working directory named `cash$NAME` + // must not become `cashvalue`. + let (directory, input) = expand_tilde(input, state); - let mut result = String::new(); + let mut result = directory; let mut chars = input.chars().peekable(); while let Some(ch) = chars.next() { diff --git a/impl/rust-cli/src/redirection.rs b/impl/rust-cli/src/redirection.rs index 8c8c1558..0e338af6 100644 --- a/impl/rust-cli/src/redirection.rs +++ b/impl/rust-cli/src/redirection.rs @@ -8,7 +8,7 @@ use anyhow::{Context, Result}; use std::collections::HashSet; use std::fs::{self, File, OpenOptions}; -use std::io::{Read, Write}; +use std::io::{Read, Seek, Write}; use std::path::{Path, PathBuf}; use crate::state::ShellState; @@ -36,64 +36,49 @@ where // Resolve redirect file paths before capturing stdout // (state.resolve_path needs immutable access) // IMPORTANT: Expand variables in file paths first (fixes M11 edge case) - let resolved_redirects: Vec<(Redirection, PathBuf)> = redirects + let resolved_redirects: Vec<(usize, PathBuf)> = redirects .iter() - .filter_map(|r| match r { - Redirection::Output { file } => { - let expanded = crate::parser::expand_variables(file, state); - Some((r.clone(), state.resolve_path(&expanded))) - } - Redirection::Append { file } => { - let expanded = crate::parser::expand_variables(file, state); - Some((r.clone(), state.resolve_path(&expanded))) - } - Redirection::BothOutput { file } => { + .enumerate() + .filter_map(|(index, r)| match r { + Redirection::Output { file } + | Redirection::Append { file } + | Redirection::BothOutput { file } => { let expanded = crate::parser::expand_variables(file, state); - Some((r.clone(), state.resolve_path(&expanded))) + Some((index, state.resolve_path(&expanded))) } _ => None, }) .collect(); - // Setup redirections (validates, opens files, records original state) - let redirect_setup = RedirectSetup::setup(redirects, state)?; - - // Capture stdout - let mut buffer = gag::BufferRedirect::stdout()?; - - // Execute command with mutable state - f(state)?; - - // Get captured output - let mut output = String::new(); - buffer.read_to_string(&mut output)?; - drop(buffer); // Release stdout - - // Write to redirect targets - for (redirect, path) in resolved_redirects { - match redirect { - Redirection::Output { .. } | Redirection::BothOutput { .. } => { - fs::write(&path, &output) - .with_context(|| format!("Failed to write to: {}", path.display()))?; - } - - Redirection::Append { .. } => { - let mut f = OpenOptions::new() - .create(true) - .append(true) - .open(&path) - .with_context(|| format!("Failed to open for append: {}", path.display()))?; - f.write_all(output.as_bytes())?; - } - - _ => {} // Filtered out above + // Set the redirections up (validate, open the files, save their + // original state), run the command, and record what the set-up + // changed, whatever becomes of the command. + RedirectSetup::run(redirects, state, |redirect_setup, state| { + // Capture stdout + let mut buffer = gag::BufferRedirect::stdout()?; + + // Execute command with mutable state + f(state)?; + + // Get captured output + let mut output = String::new(); + buffer.read_to_string(&mut output)?; + drop(buffer); // Release stdout + + // Write to the redirect targets, through the files the set-up + // opened and checked. Opening the names a second time here went + // round that check: a name given to another file in between (a + // hard link to a file outside the root, say) was opened and + // overwritten. + for (index, path) in resolved_redirects { + redirect_setup + .handle(index)? + .write_all(output.as_bytes()) + .with_context(|| format!("Failed to write to: {}", path.display()))?; } - } - - // Record modifications for undo - redirect_setup.record_for_undo(state)?; - Ok(()) + Ok(()) + }) } /// I/O redirection operator for commands. @@ -239,6 +224,195 @@ pub struct RedirectSetup { /// File modifications made (for undo) pub modifications: Vec, + + /// For each redirection given to [`Self::setup`], in order, the place + /// in `opened_files` of the file opened for it, if it names a file. + targets: Vec>, + + /// The `>` targets that are opened and saved but not emptied yet: the + /// place of each in `opened_files` and of its record in + /// `modifications`. See [`Self::truncate_pending`]. + pending_truncations: Vec<(usize, usize)>, +} + +/// `path`, an absolute path inside the session root, as the root-relative +/// form the history stores. Recording the absolute path made undo resolve +/// it under the root a second time and fail. +fn rooted_path(state: &ShellState, path: &Path) -> String { + match state.contained_abs(path) { + Ok(relative) => relative.to_string_lossy().into_owned(), + Err(_) => path.to_string_lossy().into_owned(), + } +} + +/// The file a redirection writes to, opened beneath the session root +/// without truncating it, and whether this call created it. +/// +/// An existing file is opened first. Only if there is none is one created, +/// and then exclusively, so a file that another program makes in between +/// is never taken for a new one (its content would be lost, and `undo` +/// would delete it). +fn open_redirect_target( + root: &cap_std::fs::Dir, + contained: &Path, + append: bool, +) -> std::io::Result<(cap_std::fs::File, bool)> { + use std::io::ErrorKind; + + match open_existing_target(root, contained, append) { + Ok(file) => Ok((file, false)), + Err(e) if e.kind() == ErrorKind::NotFound => { + let mut options = cap_std::fs::OpenOptions::new(); + if append { + options.append(true); + } else { + options.write(true); + } + options.create_new(true); + match root.open_with(contained, &options) { + Ok(file) => Ok((file, true)), + Err(e) if e.kind() == ErrorKind::AlreadyExists => Err(std::io::Error::new( + ErrorKind::AlreadyExists, + "the name is a symbolic link to a file that does not exist, or another \ + program created the file at the same moment; nothing was written", + )), + Err(e) => Err(e), + } + } + Err(e) => Err(e), + } +} + +/// An existing redirect target, opened without truncating it. +/// +/// For `>` it is opened for reading too, so that the bytes saved for undo +/// are read from the very file that is then truncated, and, where the +/// system has it, with `O_NOATIME`, so that saving them does not change the +/// file's access time (see [`crate::fs_pure`]). +fn open_existing_target( + root: &cap_std::fs::Dir, + contained: &Path, + append: bool, +) -> std::io::Result { + let mut options = cap_std::fs::OpenOptions::new(); + if append { + options.append(true); + return root.open_with(contained, &options); + } + options.read(true).write(true); + #[cfg(target_os = "linux")] + { + use cap_std::fs::OpenOptionsExt; + let mut pure = options.clone(); + pure.custom_flags(libc::O_NOATIME); + match root.open_with(contained, &pure) { + // Not the owner: the kernel refuses the flag. Open without it. + Err(e) if e.raw_os_error() == Some(libc::EPERM) => {} + other => return other, + } + } + root.open_with(contained, &options) +} + +/// Refuse an open redirect target that must not be written to. `shown` is +/// its path, for the message. +fn check_writable_target(meta: &cap_std::fs::Metadata, shown: &Path) -> Result<()> { + if !meta.is_file() { + anyhow::bail!( + "Output redirection target is not a file: {}", + shown.display() + ); + } + // A file with more than one hard link has other names, which may lie + // outside the session root. Writing to it would change what those + // names hold, so it is refused. + let links = cap_fs_ext::MetadataExt::nlink(meta); + if links > 1 { + anyhow::bail!( + "Cannot redirect to {}: the file has {} hard links, and writing to it \ + would change the data under its other names too", + shown.display(), + links + ) + } + Ok(()) +} + +/// The existing file at `contained` (beneath the session root `root`), +/// opened for reading and writing without truncating it, and checked while +/// open as a redirection's target is: it must be a regular file with no +/// other name. `shown` is its path, for the messages. +pub(crate) fn open_checked_target( + root: &cap_std::fs::Dir, + contained: &Path, + shown: &Path, +) -> Result { + let target = open_existing_target(root, contained, false) + .with_context(|| format!("Failed to open file for writing: {}", shown.display()))?; + let meta = target + .metadata() + .with_context(|| format!("Failed to stat file: {}", shown.display()))?; + check_writable_target(&meta, shown)?; + Ok(target) +} + +/// Empty the file at `contained` (beneath the session root `root`) and +/// return what it held, for redoing a `>` redirection. `shown` is its path, +/// for the messages. +/// +/// As for the redirection itself, the file is opened once, checked, read +/// and then truncated through that one handle, so the bytes returned are +/// those of the file that was emptied. +pub(crate) fn truncate_keeping( + root: &cap_std::fs::Dir, + contained: &Path, + shown: &Path, +) -> Result> { + let mut target = open_checked_target(root, contained, shown)?; + let mut original_content = Vec::new(); + target + .read_to_end(&mut original_content) + .with_context(|| format!("Failed to read file for backup: {}", shown.display()))?; + target + .set_len(0) + .with_context(|| format!("Failed to truncate: {}", shown.display()))?; + Ok(original_content) +} + +/// The absolute path of the file that `contained` names once the links in +/// it are followed, checked to be the file that was opened (`opened` is the +/// open file's metadata). +/// +/// # Errors +/// +/// Fails if the name leads to another file than the one opened (it was +/// changed in between), or if the real path is not valid UTF-8, which the +/// history cannot store. +fn changed_file( + state: &ShellState, + contained: &Path, + opened: &cap_std::fs::Metadata, +) -> Result { + use cap_fs_ext::MetadataExt; + + let root = state.root_dir(); + let real = root + .canonicalize(contained) + .context("Failed to resolve the path of the opened file")?; + let named = root + .symlink_metadata(&real) + .context("Failed to stat the opened file by its path")?; + if named.dev() != opened.dev() || named.ino() != opened.ino() { + anyhow::bail!("the name was given to another file while it was being opened"); + } + if real.to_str().is_none() { + anyhow::bail!( + "its real path ({}) is not valid UTF-8, so it could not be recorded for undo; \ + nothing was written", + real.display() + ); + } + Ok(state.session_root().join(real)) } impl Default for RedirectSetup { @@ -253,13 +427,72 @@ impl RedirectSetup { Self { opened_files: Vec::new(), modifications: Vec::new(), + targets: Vec::new(), + pending_truncations: Vec::new(), } } + /// Set up `redirects`, run `body` with them, and record what the + /// set-up changed, whatever becomes of `body`. + /// + /// A `>` target is emptied when it is set up. That used to be recorded + /// only when the command went well: a built-in that failed, a command + /// that could not be found or was interrupted, and every background + /// command left the file emptied with nothing for `undo` to restore. + /// Going through this function, a caller cannot leave the record out. + /// + /// # Errors + /// + /// Fails if the set-up fails (then nothing was changed and `body` does + /// not run), if `body` fails, or if the record cannot be made. + pub fn run( + redirects: &[Redirection], + state: &mut ShellState, + body: impl FnOnce(&Self, &mut ShellState) -> Result, + ) -> Result { + let setup = Self::setup(redirects, state)?; + let outcome = body(&setup, state); + let recorded = setup.record_for_undo(state); + let value = outcome?; + recorded?; + Ok(value) + } + + /// A second handle on the file opened for the redirection at `index` + /// of the list given to [`Self::setup`]. + /// + /// Whatever writes or reads the redirected stream uses this handle. The + /// file was opened once, beneath the session root, and checked while + /// open. Opening its name again would go round both: the name can be + /// given to another file in between. + /// + /// # Errors + /// + /// Fails if that redirection does not name a file, or the handle cannot + /// be duplicated. + pub fn handle(&self, index: usize) -> Result { + let file = self + .targets + .get(index) + .copied() + .flatten() + .and_then(|place| self.opened_files.get(place)) + .context("No file was opened for this redirection")?; + file.try_clone() + .context("Failed to duplicate the handle of a redirection target") + } + /// Prepare file redirections before command execution /// /// Validates all redirections, opens files, and records original state for undo. - /// Fails atomically if any redirection is invalid. + /// + /// No file is emptied until every redirection has been set up. If one + /// of them is refused, the targets of the others still hold what they + /// held. A file that an earlier redirection created stays, empty, as + /// it does in bash. + /// + /// Prefer [`Self::run`], which also makes the record for undo on every + /// path. pub fn setup(redirects: &[Redirection], state: &ShellState) -> Result { // Validate all redirections first (fail fast) validate_redirections(redirects, state)?; @@ -267,6 +500,7 @@ impl RedirectSetup { let mut setup = Self::new(); for redirect in redirects { + let opened_before = setup.opened_files.len(); match redirect { Redirection::Output { file } => { setup.setup_output_redirect(file, state, false)?; @@ -300,11 +534,80 @@ impl RedirectSetup { // Handled by external command execution (creates temp file) } } + let opened = setup.opened_files.len() > opened_before; + setup.targets.push(opened.then_some(opened_before)); } + setup.truncate_pending()?; + Ok(setup) } + /// Empty the files that `>` redirections opened, once every + /// redirection of the command has been set up. + /// + /// Emptying each file as it was set up lost its content when a later + /// redirection was then refused: `echo hi > out 2> dangling` emptied + /// `out`, failed on `dangling`, and dropped the record that held what + /// `out` had contained. + /// + /// If a file cannot be emptied, those emptied before it get their + /// content back, so the targets are either all emptied or all as they + /// were. + fn truncate_pending(&mut self) -> Result<()> { + self.truncate_pending_with(|file| file.set_len(0)) + } + + /// [`Self::truncate_pending`], with the step that empties a file passed + /// in as `empty`, so that a test can make that step fail. + fn truncate_pending_with( + &mut self, + empty: impl Fn(&File) -> std::io::Result<()>, + ) -> Result<()> { + let pending = std::mem::take(&mut self.pending_truncations); + for (done, &(place, record)) in pending.iter().enumerate() { + let mut file = &self.opened_files[place]; + if let Err(e) = empty(file).and_then(|()| file.rewind()) { + let not_restored = self.write_back(&pending[..done]); + let path = self.modifications[record].path().display().to_string(); + let failed = anyhow::Error::new(e).context(format!("Failed to truncate: {path}")); + if not_restored.is_empty() { + return Err(failed); + } + return Err(failed.context(format!( + "The earlier content could not be written back to: {}", + not_restored.join(", ") + ))); + } + } + Ok(()) + } + + /// Write the saved content back to the files of `emptied` (places in + /// `opened_files` and `modifications`). Returns the paths of those + /// that could not be written. + fn write_back(&self, emptied: &[(usize, usize)]) -> Vec { + let mut not_restored = Vec::new(); + for &(place, record) in emptied { + let FileModification::Truncated { + path, + original_content, + } = &self.modifications[record] + else { + continue; + }; + let mut file = &self.opened_files[place]; + let written = file + .rewind() + .and_then(|()| file.write_all(original_content)) + .and_then(|()| file.rewind()); + if written.is_err() { + not_restored.push(path.display().to_string()); + } + } + not_restored + } + /// Setup output redirection (> or >>) fn setup_output_redirect( &mut self, @@ -316,55 +619,63 @@ impl RedirectSetup { let expanded = crate::parser::expand_variables(file, state); let path = state.resolve_path(&expanded); - // Record original state for undo - if path.exists() { - if append { - let metadata = fs::metadata(&path) - .with_context(|| format!("Failed to stat file: {}", path.display()))?; - - self.modifications.push(FileModification::Appended { - path: path.clone(), - original_size: metadata.len(), - }); - } else { - // Truncate - save original content (pure read for undo backup; - // routed through fs_pure to honour the noatime discipline). - let original_content = crate::fs_pure::read_to_end(&path).with_context(|| { - format!("Failed to read file for backup: {}", path.display()) - })?; + // The target is opened beneath the session root, once, and without + // truncating it. Everything else is decided from the open file. A + // check made on the name before the open can be overtaken: between + // the two, the name can be given to another file (a hard link to a + // file outside the root, say), and the open would then truncate + // that one. A path that leaves the root through a symlink is + // refused by the open itself. + let contained = state.contained_abs(&path)?; + let (mut target, created) = open_redirect_target(state.root_dir(), &contained, append) + .with_context(|| format!("Failed to open file for writing: {}", path.display()))?; + let meta = target + .metadata() + .with_context(|| format!("Failed to stat file: {}", path.display()))?; + check_writable_target(&meta, &path)?; + + // The record names the file that is changed. Through a link + // (`echo x > alias`) that is the link's target: the bytes saved for + // undo are the target's, and an `obliterate` of the target must be + // able to find them. + let changed = changed_file(state, &contained, &meta) + .with_context(|| format!("Cannot redirect to: {}", path.display()))?; - // Warn if file is large - if original_content.len() > 10 * 1024 * 1024 { - eprintln!( - "Warning: Saving {:.1}MB file for undo. This may use significant memory.", - original_content.len() as f64 / (1024.0 * 1024.0) - ); - } - - self.modifications.push(FileModification::Truncated { - path: path.clone(), - original_content, - }); - } - } else { - // File doesn't exist - will be created + // Record original state for undo + if created { + // File did not exist - it was created by the open self.modifications - .push(FileModification::Created { path: path.clone() }); - } - - // Open file for writing - let file = if append { - OpenOptions::new() - .create(true) - .append(true) - .open(&path) - .with_context(|| format!("Failed to open file for append: {}", path.display()))? + .push(FileModification::Created { path: changed }); + } else if append { + self.modifications.push(FileModification::Appended { + path: changed, + original_size: meta.len(), + }); } else { - File::create(&path) - .with_context(|| format!("Failed to create file: {}", path.display()))? - }; + // Save the original content, read from the file that will be + // truncated. It is not truncated here: see `truncate_pending`. + let mut original_content = Vec::new(); + target + .read_to_end(&mut original_content) + .with_context(|| format!("Failed to read file for backup: {}", path.display()))?; + + // Warn if file is large + if original_content.len() > 10 * 1024 * 1024 { + eprintln!( + "Warning: Saving {:.1}MB file for undo. This may use significant memory.", + original_content.len() as f64 / (1024.0 * 1024.0) + ); + } - self.opened_files.push(file); + self.pending_truncations + .push((self.opened_files.len(), self.modifications.len())); + self.modifications.push(FileModification::Truncated { + path: changed, + original_content, + }); + } + + self.opened_files.push(target.into_std()); Ok(()) } @@ -403,7 +714,7 @@ impl RedirectSetup { let (op_type, path, undo_data) = match modification { FileModification::Created { path } => { // File was created by redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = rooted_path(state, &path); (OperationType::CreateFile, path_str, None) } @@ -412,7 +723,7 @@ impl RedirectSetup { original_content, } => { // File was truncated by > redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = rooted_path(state, &path); ( OperationType::FileTruncated, path_str, @@ -425,7 +736,7 @@ impl RedirectSetup { original_size, } => { // File was appended to by >> redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = rooted_path(state, &path); // Encode size as bytes let size_bytes = original_size.to_le_bytes().to_vec(); (OperationType::FileAppended, path_str, Some(size_bytes)) @@ -441,7 +752,10 @@ impl RedirectSetup { op = op.with_undo_data(data); } - state.record_operation(op); + // `path` is relative to the session root already (see + // `rooted_path`), so it must not be read against the working + // directory a second time. + state.record_rooted_operation(op); } Ok(()) @@ -907,4 +1221,260 @@ mod tests { let restored = fs::read_to_string(&file_path).unwrap(); assert_eq!(restored, "original"); } + + /// The file a test's redirection was opened on: its device and inode. + #[cfg(unix)] + fn identity(file: &File) -> (u64, u64) { + use std::os::unix::fs::MetadataExt; + let meta = file.metadata().unwrap(); + (meta.dev(), meta.ino()) + } + + /// Each redirection is handed the file that was opened for it, and a + /// redirection that names no file has none. + #[cfg(unix)] + #[test] + fn test_each_redirection_gets_the_handle_of_its_own_file() { + let temp = TempDir::new().unwrap(); + let state = test_state(temp.path()); + fs::write(temp.path().join("in"), "input").unwrap(); + let redirects = vec![ + Redirection::Input { + file: "in".to_string(), + }, + Redirection::HereString { + content: "text".to_string(), + expand: false, + }, + Redirection::Output { + file: "out".to_string(), + }, + Redirection::ErrorAppend { + file: "err".to_string(), + }, + ]; + + let setup = RedirectSetup::setup(&redirects, &state).unwrap(); + + for (index, name) in [(0, "in"), (2, "out"), (3, "err")] { + let by_name = File::open(temp.path().join(name)).unwrap(); + assert_eq!( + identity(&setup.handle(index).unwrap()), + identity(&by_name), + "{name}" + ); + } + assert!(setup.handle(1).is_err(), "a here-string names no file"); + assert!(setup.handle(4).is_err(), "there is no fifth redirection"); + } + + /// A write through a link is recorded under the file it changed, not + /// under the link: the bytes saved for undo are that file's. + #[cfg(unix)] + #[test] + fn test_a_write_through_a_link_is_recorded_under_the_file_it_changed() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let state = test_state(&root); + fs::create_dir(root.join("dir")).unwrap(); + fs::write(root.join("dir/real"), "ORIGINAL").unwrap(); + std::os::unix::fs::symlink("dir/real", root.join("alias")).unwrap(); + std::os::unix::fs::symlink("dir", root.join("way")).unwrap(); + + for (name, append) in [("alias", false), ("way/real", true)] { + let mut setup = RedirectSetup::new(); + setup.setup_output_redirect(name, &state, append).unwrap(); + setup.truncate_pending().unwrap(); + assert_eq!( + setup.modifications[0].path(), + root.join("dir/real"), + "{name}" + ); + } + // The first one truncated the file, and kept what it held. + assert_eq!(fs::read(root.join("dir/real")).unwrap(), b""); + } + + /// No target is emptied until every redirection of the command is set + /// up. `echo hi > out 2> dangling` emptied `out`, was then refused on + /// `dangling`, and dropped the record of what `out` had held. + #[cfg(unix)] + #[test] + fn test_no_target_is_emptied_when_a_later_redirection_is_refused() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let state = test_state(&root); + fs::write(root.join("out"), "ORIGINAL").unwrap(); + std::os::unix::fs::symlink("nowhere", root.join("dangling")).unwrap(); + let redirects = vec![ + Redirection::Output { + file: "out".to_string(), + }, + Redirection::ErrorOutput { + file: "dangling".to_string(), + }, + ]; + + assert!(RedirectSetup::setup(&redirects, &state).is_err()); + + assert_eq!(fs::read(root.join("out")).unwrap(), b"ORIGINAL"); + } + + /// If a target cannot be emptied, those emptied before it get their + /// content back. + #[test] + fn test_targets_get_their_content_back_when_one_cannot_be_emptied() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let state = test_state(&root); + fs::write(root.join("one"), "FIRST").unwrap(); + fs::write(root.join("two"), "SECOND").unwrap(); + let mut setup = RedirectSetup::new(); + setup.setup_output_redirect("one", &state, false).unwrap(); + setup.setup_output_redirect("two", &state, false).unwrap(); + + let emptied = std::cell::Cell::new(0); + let refused = setup.truncate_pending_with(|file| { + emptied.set(emptied.get() + 1); + if emptied.get() == 2 { + return Err(std::io::Error::other("the second file cannot be emptied")); + } + file.set_len(0) + }); + + assert!(refused.is_err()); + assert_eq!(fs::read(root.join("one")).unwrap(), b"FIRST"); + assert_eq!(fs::read(root.join("two")).unwrap(), b"SECOND"); + } + + /// What the set-up changed is recorded whatever becomes of the command. + /// A command that failed after its `>` target was emptied left nothing + /// for undo to restore. + #[test] + fn test_run_records_the_set_up_when_the_command_fails() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let mut state = test_state(&root); + fs::write(root.join("f"), "ORIGINAL").unwrap(); + let redirects = vec![Redirection::Output { + file: "f".to_string(), + }]; + + let outcome: Result<()> = RedirectSetup::run(&redirects, &mut state, |_, _| { + anyhow::bail!("the command failed") + }); + + assert!(outcome.is_err()); + assert_eq!(fs::read(root.join("f")).unwrap(), b""); + crate::commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("f")).unwrap(), b"ORIGINAL"); + } + + /// A truncation that is redone empties the file a second time, and its + /// record must hold the content again. It held none, so `echo x > f; + /// undo; redo; undo` left `f` empty. + #[cfg(unix)] + #[test] + fn test_a_redone_truncation_can_be_undone_again() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let mut state = test_state(&root); + fs::write(root.join("f"), "ORIGINAL").unwrap(); + + let redirects = vec![Redirection::Output { + file: "f".to_string(), + }]; + RedirectSetup::run(&redirects, &mut state, |_, _| Ok(())).unwrap(); + assert_eq!(fs::read(root.join("f")).unwrap(), b""); + + crate::commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("f")).unwrap(), b"ORIGINAL"); + crate::commands::redo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("f")).unwrap(), b""); + crate::commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(root.join("f")).unwrap(), b"ORIGINAL"); + } + + /// Redoing a truncation makes the checks the redirection made: a file + /// that has gained another name since is not emptied. + #[cfg(unix)] + #[test] + fn test_a_truncation_is_not_redone_on_a_file_with_another_name() { + let temp = TempDir::new().unwrap(); + let root = fs::canonicalize(temp.path()).unwrap(); + let mut state = test_state(&root); + fs::write(root.join("f"), "ORIGINAL").unwrap(); + + let redirects = vec![Redirection::Output { + file: "f".to_string(), + }]; + RedirectSetup::run(&redirects, &mut state, |_, _| Ok(())).unwrap(); + crate::commands::undo(&mut state, 1, false).unwrap(); + fs::hard_link(root.join("f"), root.join("other-name")).unwrap(); + + let refused = crate::commands::redo(&mut state, 1, false).unwrap_err(); + + assert!(format!("{refused:#}").contains("hard links"), "{refused:#}"); + assert_eq!(fs::read(root.join("other-name")).unwrap(), b"ORIGINAL"); + } + + /// A link whose target does not exist is refused, and nothing is made. + /// The file would have to be created through the link, and a file that + /// another program makes at that moment cannot be told from it. + #[cfg(unix)] + #[test] + fn test_a_redirect_to_a_link_with_no_target_is_refused_and_makes_nothing() { + let temp = TempDir::new().unwrap(); + let state = test_state(temp.path()); + std::os::unix::fs::symlink("nowhere", temp.path().join("dangling")).unwrap(); + + let mut setup = RedirectSetup::new(); + let refused = setup + .setup_output_redirect("dangling", &state, false) + .unwrap_err(); + + assert!( + format!("{refused:#}").contains("symbolic link"), + "{refused:#}" + ); + assert!(!temp.path().join("nowhere").exists()); + assert!(setup.modifications.is_empty()); + assert!(setup.opened_files.is_empty()); + } + + /// Saving the old content for undo is a read, and it must not change + /// the file's access time (see `fs_pure`). The file is read through + /// the handle that then truncates it. + #[cfg(target_os = "linux")] + #[test] + fn test_saving_the_old_content_does_not_change_the_access_time() { + use std::time::{Duration, UNIX_EPOCH}; + + let temp = TempDir::new().unwrap(); + let state = test_state(temp.path()); + let path = temp.path().join("f"); + fs::write(&path, "OLD CONTENT").unwrap(); + // An access time older than the modification time: the one case in + // which a read moves it under `relatime`. + let accessed = UNIX_EPOCH + Duration::from_secs(1_000_000_000); + let modified = UNIX_EPOCH + Duration::from_secs(1_100_000_000); + let times = fs::FileTimes::new() + .set_accessed(accessed) + .set_modified(modified); + File::options() + .write(true) + .open(&path) + .unwrap() + .set_times(times) + .unwrap(); + + let mut setup = RedirectSetup::new(); + setup.setup_output_redirect("f", &state, false).unwrap(); + + assert!(matches!( + &setup.modifications[0], + FileModification::Truncated { original_content, .. } if original_content == b"OLD CONTENT" + )); + assert_eq!(fs::metadata(&path).unwrap().accessed().unwrap(), accessed); + } } diff --git a/impl/rust-cli/src/repl.rs b/impl/rust-cli/src/repl.rs index c861e84e..b65f5aad 100644 --- a/impl/rust-cli/src/repl.rs +++ b/impl/rust-cli/src/repl.rs @@ -300,14 +300,29 @@ fn print_help() { ); } +/// The session root and the working directory, as `status` shows them. The +/// two differ after `cd`; the sandbox boundary is the first. +fn location_lines(state: &ShellState) -> [String; 2] { + [ + format!( + " {}: {}", + "Sandbox root".bright_black(), + state.session_root().display() + ), + format!( + " {}: {}", + "Working directory".bright_black(), + state.cwd().display() + ), + ] +} + fn print_status(state: &ShellState) { println!("{}", "═══ Shell Status ═══".bright_blue().bold()); println!(); - println!( - " {}: {}", - "Sandbox root".bright_black(), - state.root.display() - ); + for line in location_lines(state) { + println!("{line}"); + } println!( " {}: {}", "Total operations".bright_black(), @@ -340,3 +355,30 @@ fn print_status(state: &ShellState) { state.transactions.len() ); } + +#[cfg(test)] +mod status_display_tests { + use super::*; + + /// After `cd`, `status` still names the session root as the sandbox + /// root, and shows the working directory beside it. It used to show the + /// working directory under the label "Sandbox root". + #[test] + fn status_shows_the_session_root_and_the_working_directory() { + let temp = tempfile::tempdir().unwrap(); + let root = std::fs::canonicalize(temp.path()).unwrap(); + std::fs::create_dir(root.join("sub")).unwrap(); + let mut state = ShellState::new(root.to_str().unwrap()).unwrap(); + state.set_cwd(&root.join("sub")).unwrap(); + + let [sandbox, working] = location_lines(&state); + + assert!(sandbox.contains("Sandbox root")); + assert!(sandbox.ends_with(&root.display().to_string()), "{sandbox}"); + assert!(working.contains("Working directory")); + assert!( + working.ends_with(&root.join("sub").display().to_string()), + "{working}" + ); + } +} diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 7f75389b..fd99793f 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -157,6 +157,19 @@ impl std::fmt::Display for OperationType { } } +/// What a file was, apart from its content: permission bits, owner and +/// modification time. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct FileMeta { + /// Permission and set-ID bits (the low 12 bits of the mode). + pub mode: u32, + pub uid: u32, + pub gid: u32, + /// Modification time: seconds and nanoseconds since the Unix epoch. + pub mtime: i64, + pub mtime_nsec: i64, +} + /// A single filesystem operation with complete undo/redo information. /// /// Each operation is assigned a unique UUID and records all data needed for reversal. @@ -190,6 +203,13 @@ pub struct Operation { /// Data needed for undo (e.g., file contents before write) pub undo_data: Option>, + /// The removed file's permissions, owner and modification time, so + /// that undo restores the file as it was and not merely its content. + /// Absent on operations that removed nothing and on records written + /// before this field existed. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub undo_meta: Option, + /// Whether this operation has been undone pub undone: bool, @@ -206,6 +226,7 @@ impl Operation { timestamp: Utc::now(), transaction_id, undo_data: None, + undo_meta: None, undone: false, undone_by: None, } @@ -215,6 +236,11 @@ impl Operation { self.undo_data = Some(data); self } + + pub fn with_undo_meta(mut self, meta: FileMeta) -> Self { + self.undo_meta = Some(meta); + self + } } /// Transaction group for atomic multi-operation rollback. @@ -243,6 +269,15 @@ pub struct Transaction { pub committed: bool, } +/// What [`ShellState::scrub_obliterated`] changed: the operations as they +/// were, with their places in the history, and the redo stack as it was if +/// anything was taken off it. +#[derive(Debug)] +pub struct Scrubbed { + operations: Vec<(usize, Operation)>, + redo_stack: Option>, +} + /// Main shell state managing operation history, undo/redo stacks, and transactions. /// /// The state is persisted to `~/.cache/vsh/state.json` and restored on shell startup. @@ -263,9 +298,22 @@ pub struct Transaction { /// # Ok::<(), anyhow::Error>(()) /// ``` pub struct ShellState { - /// Sandbox root path + /// The session root: the directory the shell was started on. It is + /// fixed for the life of the session, and nothing the shell does may + /// reach outside it. `cd` moves [`Self::cwd`], not this. pub root: PathBuf, + /// The working directory. Always the session root or a directory + /// beneath it. Paths a user types are relative to it; paths in the + /// history are relative to the session root, so they mean the same + /// thing after a `cd`. + cwd: PathBuf, + + /// An open handle on the session root. Paths given to it are relative + /// and are resolved beneath it by the operating system, so no symlink + /// and no `..` can make them name anything outside the session root. + root_dir: cap_std::fs::Dir, + /// Operation history (append-only) pub history: Vec, @@ -338,7 +386,12 @@ impl ShellState { fs::canonicalize(&root_path).context("Failed to canonicalize sandbox root")?; let state_file = root_path.join(".vsh_state.json"); + let root_dir = cap_std::fs::Dir::open_ambient_dir(&root_path, cap_std::ambient_authority()) + .context("Failed to open sandbox root")?; + let mut state = Self { + cwd: root_path.clone(), + root_dir, root: root_path, history: Vec::new(), redo_stack: VecDeque::new(), @@ -368,6 +421,18 @@ impl ShellState { /// Record an operation pub fn record_operation(&mut self, mut op: Operation) { + self.root_operation_path(&mut op); + self.record_rooted_operation(op); + } + + /// Record an operation whose path is already relative to the session + /// root. + /// + /// [`Self::record_operation`] takes a path as it was typed and makes it + /// relative to the root, reading it against the working directory. A + /// path that is relative to the root already must not go through that + /// again: from `sub`, `sub/f` would become `sub/sub/f`. + pub fn record_rooted_operation(&mut self, mut op: Operation) { // Associate with active transaction if any if let Some(ref mut txn) = self.active_transaction { op.transaction_id = Some(txn.id); @@ -393,6 +458,11 @@ impl ShellState { /// /// Unlike [`Self::record_operation`], this preserves remaining redo entries /// so that multiple sequential redos work correctly. + /// + /// The path of a redone operation comes from the history, so it is + /// relative to the session root already and is recorded as it is. + /// Reading it against the working directory again recorded `sub/f` for + /// `f` when the redo was done from `sub`. pub fn record_redo_operation(&mut self, mut op: Operation) { if let Some(ref mut txn) = self.active_transaction { op.transaction_id = Some(txn.id); @@ -558,10 +628,361 @@ impl ShellState { } } + /// The directory the session was started on. It does not move with + /// `cd`. + pub fn session_root(&self) -> &Path { + &self.root + } + + /// The working directory: the session root or a directory beneath it. + pub fn cwd(&self) -> &Path { + &self.cwd + } + + /// Change the working directory to `target`, which must be a directory + /// inside the session root once every symbolic link in it is resolved. + /// + /// # Errors + /// + /// Fails if `target` is not a directory or lies outside the session + /// root. The working directory is then unchanged. + pub fn set_cwd(&mut self, target: &Path) -> Result<()> { + let resolved = fs::canonicalize(target) + .with_context(|| format!("cd: {}: No such file or directory", target.display()))?; + if !resolved.is_dir() { + anyhow::bail!("cd: {}: Not a directory", target.display()); + } + if !resolved.starts_with(&self.root) { + anyhow::bail!( + "cd: {}: outside the session root {}", + target.display(), + self.root.display() + ); + } + // The history stores paths as text. In a directory whose name is not + // text, every operation would be recorded under a different path, + // and undo would act on that one. The shell does not enter a + // directory it could not name in its history. (A name typed at the + // prompt is always text; this is reached through a symbolic link.) + if resolved.to_str().is_none() { + anyhow::bail!( + "cd: {}: the directory's name is not valid UTF-8, so operations there \ + could not be recorded for undo", + target.display() + ); + } + self.cwd = resolved; + Ok(()) + } + + /// `typed`, a path relative to the working directory, as a path + /// relative to the session root. This is the form the history stores. + /// A path that does not lie inside the session root is returned as it + /// was typed. + pub fn rooted(&self, typed: &str) -> String { + match self.contained(typed) { + Ok(relative) if relative.as_os_str() == "." => String::new(), + Ok(relative) => relative.to_string_lossy().into_owned(), + Err(_) => typed.to_string(), + } + } + + /// The location of a path recorded in the history. Recorded paths are + /// relative to the session root, whatever the working directory is now. + pub fn resolve_op_path(&self, recorded: &str) -> PathBuf { + resolve_under_root(&self.root, recorded) + } + + /// Forget what the history holds about a path that has been + /// obliterated: `rooted` itself, or anything beneath it if it was a + /// directory. + /// + /// Earlier operations on the path keep their place in the history, but + /// lose their undo data (which may be the very content that was + /// obliterated) and are marked undone by the obliteration `by`, so that + /// no later `undo` or `redo` can bring any of it back. The caller + /// records the obliteration next, which saves the scrubbed history. + /// + /// A recorded path concerns the obliterated one if it is that path or + /// lies beneath it, as written or once the symbolic links in its + /// directories are resolved. The same file can be reached under two + /// names when a link inside the session root points at its directory; + /// comparing the names as written left the other name's records, and + /// the content in them, in place. + /// + /// Returns what was changed, so that [`Self::restore_scrubbed`] can put + /// it back if the scrubbed history then cannot be saved. + pub fn scrub_obliterated(&mut self, rooted: &str, by: Uuid) -> Scrubbed { + let mut scrubbed = Scrubbed { + operations: Vec::new(), + redo_stack: None, + }; + if rooted.is_empty() { + return scrubbed; + } + let target = self.real_location(rooted); + // By path component, so that it holds whatever separator the + // platform writes: `dir` contains `dir/f`, and not `directory`. + let concerned: Vec = self + .history + .iter() + .map(|op| self.concerns(op, rooted, &target)) + .collect(); + for (index, op) in self.history.iter_mut().enumerate() { + if !concerned[index] { + continue; + } + scrubbed.operations.push((index, op.clone())); + op.undo_data = None; + op.undo_meta = None; + if !op.undone { + op.undone = true; + op.undone_by = Some(by); + } + } + let kept: VecDeque = self + .redo_stack + .iter() + .filter(|op| !self.concerns(op, rooted, &target)) + .cloned() + .collect(); + if kept.len() != self.redo_stack.len() { + scrubbed.redo_stack = Some(std::mem::replace(&mut self.redo_stack, kept)); + } + scrubbed + } + + /// Put back what [`Self::scrub_obliterated`] changed. For an + /// obliteration that did not go ahead: the file is still there, so the + /// history must still be able to undo what was done to it. + pub fn restore_scrubbed(&mut self, scrubbed: Scrubbed) { + for (index, op) in scrubbed.operations { + if let Some(slot) = self.history.get_mut(index) { + *slot = op; + } + } + if let Some(redo_stack) = scrubbed.redo_stack { + self.redo_stack = redo_stack; + } + } + + /// Whether a recorded operation is about `rooted`, whose real location + /// is `target`, or about something beneath it. + fn concerns(&self, op: &Operation, rooted: &str, target: &Path) -> bool { + if matches!( + op.op_type, + OperationType::SetVariable + | OperationType::UnsetVariable + | OperationType::HardwareErase + ) { + return false; + } + // An operation on a link itself (making it, removing it, moving + // it) is about the link. Any other operation went through the link + // to the file it points at: `echo x > alias` saved the bytes of the + // target under the name `alias`. Such a record is also compared by + // where its path leads now, with the last link followed too. + let on_the_link = matches!( + op.op_type, + OperationType::Symlink | OperationType::Unlink | OperationType::Move + ); + op.path.split('\0').any(|recorded| { + Path::new(recorded).starts_with(Path::new(rooted)) + || self.real_location(recorded).starts_with(target) + || (!on_the_link + && fs::canonicalize(self.resolve_op_path(recorded)) + .is_ok_and(|real| real.starts_with(target))) + }) + } + + /// Take out of the history archive what it holds about a path that is + /// about to be obliterated: the undo data of every archived operation + /// that [`Self::scrub_obliterated`] would have scrubbed in the history. + /// + /// Operations that age out of the history are written to the archive + /// with their undo data, which can be the content that is being + /// obliterated. Scrubbing only the history left that copy on disk. + /// + /// The archive is rewritten through a temporary file and renamed into + /// place, so it is either the old archive or the scrubbed one. A line + /// that cannot be read as an operation is kept as it is. Nothing is + /// done when no archive is configured or none has been written yet. + /// + /// # Errors + /// + /// Fails if the archive cannot be read or rewritten. The caller must + /// then not obliterate. + pub(crate) fn scrub_archive(&self, rooted: &str) -> Result<()> { + use std::io::Write; + + let Some(archive_path) = self.history_archive_path.as_ref() else { + return Ok(()); + }; + if rooted.is_empty() { + return Ok(()); + } + let archived = match fs::read_to_string(archive_path) { + Ok(text) => text, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(e) => { + return Err(e).with_context(|| { + format!("Failed to read history archive {}", archive_path.display()) + }) + } + }; + + let target = self.real_location(rooted); + let mut changed = false; + let mut scrubbed = String::with_capacity(archived.len()); + for line in archived.lines() { + match serde_json::from_str::(line) { + Ok(mut op) + if (op.undo_data.is_some() || op.undo_meta.is_some()) + && self.concerns(&op, rooted, &target) => + { + op.undo_data = None; + op.undo_meta = None; + scrubbed.push_str(&serde_json::to_string(&op)?); + changed = true; + } + _ => scrubbed.push_str(line), + } + scrubbed.push('\n'); + } + if !changed { + return Ok(()); + } + + let mut temporary = archive_path.clone().into_os_string(); + temporary.push(".scrub"); + let temporary = PathBuf::from(temporary); + let written = (|| -> std::io::Result<()> { + let mut file = fs::File::create(&temporary)?; + file.write_all(scrubbed.as_bytes())?; + file.sync_all()?; + fs::rename(&temporary, archive_path)?; + if let Some(parent) = archive_path.parent().filter(|p| !p.as_os_str().is_empty()) { + fs::File::open(parent)?.sync_all()?; + } + Ok(()) + })(); + written.with_context(|| { + format!( + "Failed to rewrite history archive {}", + archive_path.display() + ) + }) + } + + /// Where a recorded path is now, with the symbolic links in its + /// directories resolved as far as they still exist. The last component + /// is kept as written: a record about a link is about the link, not + /// about what it points to. + fn real_location(&self, recorded: &str) -> PathBuf { + let path = self.resolve_op_path(recorded); + let Some(name) = path.file_name().map(std::ffi::OsStr::to_os_string) else { + return path; + }; + let mut missing = Vec::new(); + let mut directory = path.parent(); + while let Some(current) = directory { + if let Ok(real) = fs::canonicalize(current) { + let mut location = real; + location.extend(missing.iter().rev()); + location.push(&name); + return location; + } + match current.file_name() { + Some(part) => missing.push(part.to_os_string()), + None => break, + } + directory = current.parent(); + } + path + } + + /// Rewrite the path of an operation about to be recorded so that it is + /// relative to the session root. Variable operations carry a name, not + /// a path, and are left alone. A move carries two paths. + fn root_operation_path(&self, op: &mut Operation) { + match op.op_type { + OperationType::SetVariable | OperationType::UnsetVariable => {} + // A hardware erase names a device (`/dev/nvme0n1`), not a file + // in the session root. The record must keep that name. + OperationType::HardwareErase => {} + OperationType::Move => { + let rooted: Vec = + op.path.split('\0').map(|part| self.rooted(part)).collect(); + op.path = rooted.join("\0"); + } + _ => op.path = self.rooted(&op.path), + } + } + + /// The handle on the session root. Use it, with a path from + /// [`Self::contained`], for anything that creates, changes or removes a + /// file. + pub fn root_dir(&self) -> &cap_std::fs::Dir { + &self.root_dir + } + + /// `path`, as typed relative to the working directory, as a path + /// relative to the session root, for use with [`Self::root_dir`]. + /// + /// # Errors + /// + /// Fails if the path lies outside the session root. + pub fn contained(&self, path: &str) -> Result { + self.contained_abs(&self.resolve_path(path)) + } + + /// As [`Self::contained`], for a path already resolved by + /// [`Self::resolve_path`]. + /// + /// # Errors + /// + /// Fails if the path lies outside the session root, or is not valid + /// UTF-8. The history stores paths as text. A path that is not text + /// would be stored as a different path, and undo would then act on + /// that one, so the operation is refused before anything is done. + pub fn contained_abs(&self, full: &Path) -> Result { + let relative = full + .strip_prefix(&self.root) + .map_err(|_| anyhow::anyhow!("{} is outside the session root", full.display()))?; + if relative.to_str().is_none() { + anyhow::bail!( + "{} is not valid UTF-8, so it could not be recorded for undo; nothing was done", + full.display() + ); + } + Ok(if relative.as_os_str().is_empty() { + PathBuf::from(".") + } else { + relative.to_path_buf() + }) + } + /// Resolve a path relative to sandbox root /// Prevents path traversal attacks via `..` components + /// + /// A path that starts with `/` is read from the session root, with one + /// exception: a real path that lies inside the session root is that + /// path. `pwd`, `$PWD`, `~+` and `~-` all give real paths, and reading + /// them from the session root added the root a second time, so + /// `touch ~+/f` failed in every directory. `cd` already read them this + /// way. The result is inside the session root either way. pub fn resolve_path(&self, path: &str) -> PathBuf { - resolve_under_root(&self.root, path) + if path.starts_with('/') && Path::new(path).starts_with(&self.root) { + return normalise_within(&self.root, Path::new(path)); + } + if path.starts_with('/') || self.cwd == self.root { + return resolve_under_root(&self.root, path); + } + // Relative to the working directory, which is inside the root. The + // working directory is used as the path it is: its name may hold + // bytes that are not text, and writing it out as text and reading + // it back would name another directory. + resolve_under_base(&self.root, &self.cwd, path) } /// Get root path as string (for Lean FFI) @@ -585,6 +1006,13 @@ impl ShellState { } /// Save state to file + /// Save the history now, and report a failure. The recording functions + /// only warn when a save fails; a caller that must not go on without + /// the save uses this. + pub(crate) fn save_history(&self) -> Result<()> { + self.save() + } + fn save(&self) -> Result<()> { let state = SerializableState { history: self.history.clone(), @@ -930,7 +1358,21 @@ pub fn resolve_under_root(root: &Path, path: &str) -> PathBuf { } else { root.join(path) }; + normalise_within(root, &raw) +} +/// Resolve the relative `path` against `base`, a directory inside the +/// sandbox `root`, under the same rule as [`resolve_under_root`]. +/// +/// `base` is joined as the path it is. Its name may hold bytes that are not +/// text, which writing it out as text and reading it back would change. +pub fn resolve_under_base(root: &Path, base: &Path, path: &str) -> PathBuf { + normalise_within(root, &base.join(path)) +} + +/// Drop `.` components and apply `..` components of `raw`, never going above +/// `root`. +fn normalise_within(root: &Path, raw: &Path) -> PathBuf { // Normalize path components to prevent traversal via .. let mut normalized = PathBuf::new(); for component in raw.components() { @@ -963,6 +1405,78 @@ pub fn resolve_under_root(root: &Path, path: &str) -> PathBuf { mod tests { use super::*; + /// A hardware erase names a device, not a file under the session root. + /// Its record keeps that name. It used to be rewritten as `dev/nvme0n1`. + #[test] + fn test_a_hardware_erase_record_keeps_its_device_name() { + let root = tempfile::tempdir().unwrap(); + let mut state = ShellState::new(root.path().to_str().unwrap()).unwrap(); + + state.record_operation(Operation::new( + OperationType::HardwareErase, + "/dev/nvme0n1".to_string(), + None, + )); + + assert_eq!(state.history.last().unwrap().path, "/dev/nvme0n1"); + } + + /// Scrubbing an obliterated directory reaches what is inside it and + /// nothing else: not a neighbour whose name only starts the same way. + #[test] + fn test_scrub_reaches_what_is_inside_the_directory_and_nothing_else() { + let root = tempfile::tempdir().unwrap(); + let mut state = ShellState::new(root.path().to_str().unwrap()).unwrap(); + for path in ["dir", "dir/f", "dir/deep/g", "directory/h", "dir2"] { + state.record_rooted_operation( + Operation::new(OperationType::DeleteFile, path.to_string(), None) + .with_undo_data(b"content".to_vec()), + ); + } + + state.scrub_obliterated("dir", Uuid::new_v4()); + + let kept: Vec<&str> = state + .history + .iter() + .filter(|op| op.undo_data.is_some()) + .map(|op| op.path.as_str()) + .collect(); + assert_eq!(kept, ["directory/h", "dir2"]); + } + + /// A record of a write made through a link holds the bytes of the + /// link's target, under the link's name. Obliterating the target must + /// reach it. A record about the link itself (making it, removing it) + /// is about the link, and stays. + #[cfg(unix)] + #[test] + fn test_scrub_reaches_content_recorded_under_a_link_to_the_file() { + let root = tempfile::tempdir().unwrap(); + let mut state = ShellState::new(root.path().to_str().unwrap()).unwrap(); + fs::write(root.path().join("real"), "now").unwrap(); + std::os::unix::fs::symlink("real", root.path().join("alias")).unwrap(); + for op_type in [ + OperationType::FileTruncated, + OperationType::Symlink, + OperationType::Unlink, + ] { + state.record_rooted_operation( + Operation::new(op_type, "alias".to_string(), None).with_undo_data(b"data".to_vec()), + ); + } + + state.scrub_obliterated("real", Uuid::new_v4()); + + let kept: Vec = state + .history + .iter() + .filter(|op| op.undo_data.is_some()) + .map(|op| op.op_type) + .collect(); + assert_eq!(kept, [OperationType::Symlink, OperationType::Unlink]); + } + #[test] fn test_operation_inverse() { assert_eq!(OperationType::Mkdir.inverse(), Some(OperationType::Rmdir)); diff --git a/impl/rust-cli/tests/fixtures/mod.rs b/impl/rust-cli/tests/fixtures/mod.rs index b3f9ced7..d459c9a0 100644 --- a/impl/rust-cli/tests/fixtures/mod.rs +++ b/impl/rust-cli/tests/fixtures/mod.rs @@ -57,3 +57,78 @@ pub fn test_sandbox_path(test_name: &str) -> (TempDir, PathBuf) { let path = temp.path().to_path_buf(); (temp, path) } + +/// A session root for a shell, and a directory beside it that a test can +/// check was never touched. +#[allow(dead_code)] +pub struct Sandbox { + _temp: TempDir, + pub root: PathBuf, + pub outside: PathBuf, +} + +/// A fresh [`Sandbox`] and a shell state rooted in it. Both paths are in +/// canonical form, so they compare equal to what the shell reports. +#[allow(dead_code)] +pub fn sandbox() -> (Sandbox, vsh::state::ShellState) { + let temp = TempDir::new().expect("temporary directory"); + let root = temp.path().join("root"); + let outside = temp.path().join("outside"); + fs::create_dir(&root).expect("create the session root"); + fs::create_dir(&outside).expect("create the directory outside it"); + let root = fs::canonicalize(root).expect("canonical session root"); + let outside = fs::canonicalize(outside).expect("canonical outside directory"); + let state = vsh::state::ShellState::new(root.to_str().expect("a UTF-8 path")) + .expect("open the shell state"); + ( + Sandbox { + _temp: temp, + root, + outside, + }, + state, + ) +} + +/// Holds the working directory of the test process still for one test. +/// +/// `cd` moves the working directory of the whole process, as a shell must: +/// the commands it starts inherit it. A test that runs `cd` in the test +/// process therefore moves every other test in the same binary, and when +/// its temporary directory is removed the process is left in a directory +/// that no longer exists. Take this in such a test, before its first `cd`. +/// It lets one such test run at a time, and puts the directory back when +/// the test ends, whether it passes or fails. +/// +/// Declare it *after* the temporary directory the test works in. Locals +/// are dropped in the reverse of the order they are declared in, so the +/// working directory is then put back before the temporary directory is +/// removed, and the process is never left in a removed directory. +#[allow(dead_code)] +pub struct ProcessDirectory { + saved: PathBuf, + _one_at_a_time: std::sync::MutexGuard<'static, ()>, +} + +/// See [`ProcessDirectory`]. +#[allow(dead_code)] +pub fn hold_process_directory() -> ProcessDirectory { + static ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(()); + // A test that failed while holding the lock leaves it poisoned. The + // directory was put back all the same, so the lock is still good. + let guard = ONE_AT_A_TIME + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + ProcessDirectory { + saved: std::env::current_dir().unwrap_or_else(|_| std::env::temp_dir()), + _one_at_a_time: guard, + } +} + +impl Drop for ProcessDirectory { + fn drop(&mut self) { + // Nothing useful can be done if this fails, and a panic in a + // destructor would hide the test's own result. + let _ = std::env::set_current_dir(&self.saved); + } +} diff --git a/impl/rust-cli/tests/obliterate_containment_tests.rs b/impl/rust-cli/tests/obliterate_containment_tests.rs new file mode 100644 index 00000000..359acca5 --- /dev/null +++ b/impl/rust-cli/tests/obliterate_containment_tests.rs @@ -0,0 +1,528 @@ +// SPDX-License-Identifier: MPL-2.0 +//! `obliterate` destroys only what it was pointed at. +//! +//! It must never follow a symbolic link, never overwrite a file that has +//! another name, never run inside a transaction, and never destroy anything +//! before its audit entry is written. When it refuses, nothing is changed. + +#![cfg(unix)] + +mod fixtures; + +use fixtures::{sandbox, Sandbox}; +use std::fs; +use std::os::unix::fs::symlink; +use vsh::commands::secure_deletion::{obliterate_path, secure_delete}; +use vsh::state::ShellState; + +fn audit_log(sandbox: &Sandbox) -> String { + fs::read_to_string(sandbox.root.join(".vsh-audit.log")).unwrap_or_default() +} + +#[test] +fn a_plain_file_is_obliterated_and_audited_before_and_after() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.root.join("secret"), "SECRET").unwrap(); + + obliterate_path(&mut state, "secret", false, true).unwrap(); + + assert!(!sandbox.root.join("secret").exists()); + let log = audit_log(&sandbox); + let intent = log.find("obliterate-intent").expect("intent entry"); + let outcome = log.find("\"obliterated\"").expect("outcome entry"); + assert!(intent < outcome, "the intent entry must come first"); +} + +#[test] +fn a_symlink_is_refused_and_its_target_is_untouched() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + fs::write(sandbox.root.join("inside"), "INSIDE").unwrap(); + symlink(sandbox.outside.join("victim"), sandbox.root.join("out")).unwrap(); + symlink("inside", sandbox.root.join("in")).unwrap(); + + // Even a link to a file inside the root is refused: the request named + // the link, not the file. + for link in ["out", "in"] { + let refused = obliterate_path(&mut state, link, false, true).unwrap_err(); + assert!(refused.to_string().contains("symbolic link"), "{refused}"); + assert!(sandbox.root.join(link).is_symlink(), "{link} must remain"); + } + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert_eq!(fs::read(sandbox.root.join("inside")).unwrap(), b"INSIDE"); + assert!(audit_log(&sandbox).is_empty(), "a refusal writes no entry"); +} + +#[test] +fn a_symlink_to_a_directory_is_refused() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + symlink(&sandbox.outside, sandbox.root.join("dirlink")).unwrap(); + + assert!(obliterate_path(&mut state, "dirlink", false, true).is_err()); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); +} + +#[test] +fn a_hard_linked_file_is_refused() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + fs::hard_link(sandbox.outside.join("victim"), sandbox.root.join("hl")).unwrap(); + + let refused = obliterate_path(&mut state, "hl", false, true).unwrap_err(); + assert!(refused.to_string().contains("hard links"), "{refused}"); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert!(sandbox.root.join("hl").exists()); +} + +#[test] +fn obliterate_is_refused_inside_a_transaction() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.root.join("f"), "DATA").unwrap(); + fs::create_dir(sandbox.root.join("d")).unwrap(); + state.begin_transaction("t").unwrap(); + + for target in ["f", "d"] { + let refused = obliterate_path(&mut state, target, false, true).unwrap_err(); + assert!(refused.to_string().contains("transaction"), "{refused}"); + } + assert_eq!(fs::read(sandbox.root.join("f")).unwrap(), b"DATA"); + assert!(sandbox.root.join("d").is_dir()); +} + +#[test] +fn nothing_is_destroyed_when_the_audit_entry_cannot_be_written() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.root.join("f"), "DATA").unwrap(); + fs::create_dir(sandbox.root.join("d")).unwrap(); + fs::write(sandbox.root.join("d/inner"), "INNER").unwrap(); + // A directory where the audit log should be: no entry can be written. + fs::create_dir(sandbox.root.join(".vsh-audit.log")).unwrap(); + + for target in ["f", "d"] { + let refused = obliterate_path(&mut state, target, false, true).unwrap_err(); + assert!(format!("{refused:#}").contains("audit"), "{refused:#}"); + } + assert_eq!(fs::read(sandbox.root.join("f")).unwrap(), b"DATA"); + assert_eq!(fs::read(sandbox.root.join("d/inner")).unwrap(), b"INNER"); +} + +#[test] +fn a_directory_is_obliterated_without_following_the_links_in_it() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + fs::create_dir_all(sandbox.root.join("d/sub")).unwrap(); + fs::write(sandbox.root.join("d/a"), "A").unwrap(); + fs::write(sandbox.root.join("d/sub/b"), "B").unwrap(); + symlink(sandbox.outside.join("victim"), sandbox.root.join("d/out")).unwrap(); + symlink(&sandbox.outside, sandbox.root.join("d/sub/outdir")).unwrap(); + + obliterate_path(&mut state, "d", false, true).unwrap(); + + assert!(!sandbox.root.join("d").exists()); + // The links are gone; what they pointed to is not. + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert!(sandbox.outside.is_dir()); +} + +#[test] +fn a_directory_holding_a_hard_linked_file_is_refused_whole() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + fs::create_dir(sandbox.root.join("d")).unwrap(); + fs::write(sandbox.root.join("d/a"), "A").unwrap(); + fs::hard_link(sandbox.outside.join("victim"), sandbox.root.join("d/hl")).unwrap(); + + let refused = obliterate_path(&mut state, "d", false, true).unwrap_err(); + assert!(refused.to_string().contains("hard links"), "{refused}"); + + // Nothing in the directory was touched, not even the ordinary file. + assert_eq!(fs::read(sandbox.root.join("d/a")).unwrap(), b"A"); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert!(audit_log(&sandbox).is_empty()); +} + +#[test] +fn secure_delete_refuses_links_too() { + let (sandbox, _state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + fs::write(sandbox.outside.join("other"), "OTHER").unwrap(); + symlink(sandbox.outside.join("victim"), sandbox.root.join("sl")).unwrap(); + fs::hard_link(sandbox.outside.join("other"), sandbox.root.join("hl")).unwrap(); + fs::write(sandbox.root.join("plain"), "PLAIN").unwrap(); + + assert!(secure_delete(&sandbox.root.join("sl")).is_err()); + assert!(secure_delete(&sandbox.root.join("hl")).is_err()); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert_eq!(fs::read(sandbox.outside.join("other")).unwrap(), b"OTHER"); + + secure_delete(&sandbox.root.join("plain")).unwrap(); + assert!(!sandbox.root.join("plain").exists()); +} + +/// After obliteration the history holds none of the file's earlier content, +/// and no earlier operation on it can be undone or redone. +#[test] +fn obliterate_scrubs_the_history_of_the_path() { + use vsh::parser::parse_command; + use vsh::ExecutableCommand; + + let (sandbox, mut state) = sandbox(); + let run = |state: &mut ShellState, command: &str| { + parse_command(command).unwrap().execute(state).unwrap(); + }; + fs::write(sandbox.root.join("other"), "OTHER").unwrap(); + fs::write(sandbox.root.join("f"), "SECRETA").unwrap(); + // `rm` then `undo` puts the content into the history twice over: as the + // undo data of the removal, and in the redo stack. + run(&mut state, "rm f"); + run(&mut state, "undo"); + run(&mut state, "rm other"); + + obliterate_path(&mut state, "f", false, true).unwrap(); + + for op in &state.history { + let holds = op + .undo_data + .as_deref() + .is_some_and(|data| data.windows(7).any(|w| w == b"SECRETA")); + assert!( + !holds, + "{:?} on {} still holds the content", + op.op_type, op.path + ); + // Every earlier operation on `f` is closed off. The obliteration + // itself stays as the record of what happened. + if op.path == "f" && op.op_type != vsh::state::OperationType::Obliterate { + assert!(op.undone, "{:?} on f can still be undone", op.op_type); + } + } + assert!(state.redo_stack.iter().all(|op| op.path != "f")); + let saved = fs::read_to_string(sandbox.root.join(".vsh_state.json")).unwrap(); + let compact: String = saved.chars().filter(|c| !c.is_whitespace()).collect(); + assert!( + !compact.contains("83,69,67,82,69,84,65"), + "SECRETA is still saved" + ); + + // Operations on other paths are untouched: undo restores `other`. The + // newest operation is the obliteration, which undo passes over, so two + // steps are asked for. + vsh::commands::undo(&mut state, 2, false).unwrap(); + assert_eq!(fs::read(sandbox.root.join("other")).unwrap(), b"OTHER"); + assert!(!sandbox.root.join("f").exists()); +} + +/// Obliterating a directory scrubs the history of everything in it. +#[test] +fn obliterating_a_directory_scrubs_what_was_in_it() { + let (sandbox, mut state) = sandbox(); + fs::create_dir(sandbox.root.join("d")).unwrap(); + fs::write(sandbox.root.join("d/inner"), "SECRETB").unwrap(); + fs::write(sandbox.root.join("dx"), "NOTINDIR").unwrap(); + vsh::commands::rm(&mut state, "d/inner", false).unwrap(); + vsh::commands::rm(&mut state, "dx", false).unwrap(); + + obliterate_path(&mut state, "d", false, true).unwrap(); + + let inner = state + .history + .iter() + .find(|op| op.path == "d/inner") + .unwrap(); + assert!(inner.undo_data.is_none() && inner.undone); + // `dx` only shares a prefix with `d`; it is not inside it. + let sibling = state.history.iter().find(|op| op.path == "dx").unwrap(); + assert_eq!(sibling.undo_data.as_deref(), Some(&b"NOTINDIR"[..])); + assert!(!sibling.undone); +} + +#[test] +fn the_session_root_itself_cannot_be_obliterated() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.root.join("f"), "DATA").unwrap(); + assert!(obliterate_path(&mut state, ".", false, true).is_err()); + assert_eq!(fs::read(sandbox.root.join("f")).unwrap(), b"DATA"); +} + +/// The history may hold a file's earlier content as undo data. Obliterate +/// forgets it and saves the history before it destroys anything. If that +/// save fails, the content would outlive the file in the saved state, so +/// nothing is obliterated. Before, the file was destroyed first and a failed +/// save was only a warning. +#[test] +fn nothing_is_destroyed_when_the_scrubbed_history_cannot_be_saved() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.root.join("f"), "SECRET").unwrap(); + fs::create_dir(sandbox.root.join("d")).unwrap(); + fs::write(sandbox.root.join("d/inner"), "INNER").unwrap(); + // The history takes a copy of each file's content, as it would have + // from earlier work on them. + for name in ["f", "d/inner"] { + vsh::commands::rm(&mut state, name, false).unwrap(); + vsh::commands::undo(&mut state, 1, false).unwrap(); + } + let holds = |state: &ShellState, content: &[u8]| { + state + .history + .iter() + .any(|op| op.undo_data.as_deref() == Some(content)) + }; + let redo_entries = state.get_redo_stack().len(); + // A directory where the saved state should be: it cannot be written. + let saved = sandbox.root.join(".vsh_state.json"); + let _ = fs::remove_file(&saved); + fs::create_dir(&saved).unwrap(); + + for target in ["f", "d"] { + let refused = obliterate_path(&mut state, target, false, true).unwrap_err(); + assert!( + format!("{refused:#}").contains("history could not be saved"), + "{refused:#}" + ); + } + assert_eq!(fs::read(sandbox.root.join("f")).unwrap(), b"SECRET"); + assert_eq!(fs::read(sandbox.root.join("d/inner")).unwrap(), b"INNER"); + // The files are still there, so the history must still be able to undo + // what was done to them: the scrub is put back when the save fails. + assert!(holds(&state, b"SECRET")); + assert!(holds(&state, b"INNER")); + assert_eq!(state.get_redo_stack().len(), redo_entries); +} + +/// One file can have two names when a link inside the session root points +/// at its directory. Obliterating it under one name removes what the +/// history holds about it under the other. Comparing the names as written +/// left the other name's record, and the content in it, in place. +#[test] +fn obliterate_scrubs_what_the_history_holds_under_another_name() { + let (sandbox, mut state) = sandbox(); + fs::create_dir(sandbox.root.join("real")).unwrap(); + fs::write(sandbox.root.join("real/secret"), "SECRET-CONTENT").unwrap(); + // The history takes a copy of the content under the name `real/secret`. + vsh::commands::rm(&mut state, "real/secret", false).unwrap(); + vsh::commands::undo(&mut state, 1, false).unwrap(); + symlink("real", sandbox.root.join("alias")).unwrap(); + + obliterate_path(&mut state, "alias/secret", false, true).unwrap(); + + assert!(!sandbox.root.join("real/secret").exists()); + let holds_it = + |op: &vsh::state::Operation| op.undo_data.as_deref() == Some(b"SECRET-CONTENT".as_slice()); + assert!(!state.history.iter().any(holds_it), "in the history"); + assert!( + !state.get_redo_stack().iter().any(holds_it), + "in the redo stack" + ); + let reloaded = ShellState::new(sandbox.root.to_str().unwrap()).unwrap(); + assert!(!reloaded.history.iter().any(holds_it), "in the saved state"); +} + +/// Start the built shell in `root`. A redirection changes where the +/// process writes, so a case with one needs a process of its own. +fn shell(root: &std::path::Path, script: &str) -> std::process::Output { + std::process::Command::new(env!("CARGO_BIN_EXE_vsh")) + .arg("-r") + .arg(root) + .arg("-c") + .arg(script) + .current_dir(root) + .env("NO_COLOR", "1") + .env_remove("RUST_BACKTRACE") + .stdin(std::process::Stdio::null()) + .output() + .expect("start the built shell") +} + +/// Whether an operation's undo data holds `content`. +fn holds(op: &vsh::state::Operation, content: &[u8]) -> bool { + op.undo_data + .as_deref() + .is_some_and(|data| data.windows(content.len()).any(|part| part == content)) +} + +/// A write through a link saves the bytes of the link's target. The record +/// was kept under the link's name, so obliterating the target left its +/// earlier content in the saved history, and `undo` could bring it back. +/// The record is now kept under the file that was changed, so it is found +/// even when the link has been removed since. +#[test] +fn obliterate_scrubs_content_that_was_saved_through_a_link() { + for remove_the_link_first in [false, true] { + let (sandbox, _state) = sandbox(); + fs::write(sandbox.root.join("real"), "SECRET-THROUGH-LINK").unwrap(); + symlink("real", sandbox.root.join("alias")).unwrap(); + let mut script = String::from("echo replacement > alias; "); + if remove_the_link_first { + script.push_str("rm alias; "); + } + script.push_str("obliterate real --force"); + + let output = shell(&sandbox.root, &script); + + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(!sandbox.root.join("real").exists()); + let reloaded = ShellState::new(sandbox.root.to_str().unwrap()).unwrap(); + assert!( + !reloaded + .history + .iter() + .any(|op| holds(op, b"SECRET-THROUGH-LINK")), + "the saved history still holds the content (link removed first: \ + {remove_the_link_first})" + ); + } +} + +/// Operations that age out of the history are written to the archive with +/// their undo data. Obliterate scrubbed the history and left that copy. +#[test] +fn obliterate_scrubs_the_history_archive_too() { + let (sandbox, mut state) = sandbox(); + let archive = sandbox.outside.join("history-archive.jsonl"); + state.max_history_size = Some(1); + state.history_archive_path = Some(archive.clone()); + fs::write(sandbox.root.join("f"), "ARCHIVED-SECRET").unwrap(); + fs::write(sandbox.root.join("other"), "OTHER-CONTENT").unwrap(); + // The removal of `f` holds its content. Undoing it puts `f` back, and + // the next operation pushes that record out of the history, into the + // archive. + vsh::commands::rm(&mut state, "f", false).unwrap(); + vsh::commands::undo(&mut state, 1, false).unwrap(); + vsh::commands::rm(&mut state, "other", false).unwrap(); + let archived = |archive: &std::path::Path| -> Vec { + fs::read_to_string(archive) + .unwrap() + .lines() + .map(|line| serde_json::from_str(line).unwrap()) + .collect() + }; + assert!( + archived(&archive) + .iter() + .any(|op| holds(op, b"ARCHIVED-SECRET")), + "the set-up did not put the content into the archive" + ); + + obliterate_path(&mut state, "f", false, true).unwrap(); + + let after = archived(&archive); + assert!( + !after.iter().any(|op| holds(op, b"ARCHIVED-SECRET")), + "the archive still holds the content" + ); + // The record of `f` is still there, without the content, and what the + // archive holds about another path is untouched. + assert!(after.iter().any(|op| op.path == "f")); + assert!(after.iter().any(|op| holds(op, b"OTHER-CONTENT"))); +} + +/// The audit entries name the path as the history records it, relative to +/// the session root. They used to carry the path as it was typed, so an +/// obliteration made in `sub` was audited as `f` and recorded as `sub/f`. +#[test] +fn an_obliteration_below_the_root_is_audited_under_the_path_the_history_records() { + let (sandbox, mut state) = sandbox(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/f"), "CONTENT").unwrap(); + state.set_cwd(&sandbox.root.join("sub")).unwrap(); + + obliterate_path(&mut state, "f", false, true).unwrap(); + + assert_eq!(state.history.last().unwrap().path, "sub/f"); + let log = audit_log(&sandbox); + let paths: Vec = log + .lines() + .map(|line| serde_json::from_str::(line).unwrap()) + .map(|entry| entry["path"].as_str().unwrap().to_string()) + .collect(); + assert_eq!(paths, ["sub/f", "sub/f"], "{log}"); +} + +/// An obliteration that fails after it has begun is recorded as failed. +/// By then the intent is in the audit log and the history has been +/// scrubbed. The error used to be returned with nothing more written: an +/// intent with no outcome, and no record in the history, although data +/// was gone. +/// +/// The shell walks the directory, then asks. While it waits, a file is put +/// where the walk did not see one, so the directory cannot be removed +/// after its files have been destroyed. +#[test] +fn an_obliteration_that_fails_part_way_is_recorded_as_failed() { + use std::io::{Read, Write}; + use std::process::{Command, Stdio}; + + let (sandbox, _state) = sandbox(); + fs::create_dir_all(sandbox.root.join("dir/inner")).unwrap(); + fs::write(sandbox.root.join("dir/a"), "CONTENT").unwrap(); + + let mut child = Command::new(env!("CARGO_BIN_EXE_vsh")) + .arg("-r") + .arg(&sandbox.root) + .arg("-c") + .arg("obliterate dir") + .current_dir(&sandbox.root) + .env("NO_COLOR", "1") + .env_remove("RUST_BACKTRACE") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .expect("start the built shell"); + let mut stdout = child.stdout.take().unwrap(); + let mut seen = Vec::new(); + let mut byte = [0u8; 1]; + while !seen.ends_with(b"[y/N] ") { + assert_eq!( + stdout.read(&mut byte).unwrap(), + 1, + "the shell ended before it asked: {}", + String::from_utf8_lossy(&seen) + ); + seen.push(byte[0]); + } + fs::write(sandbox.root.join("dir/inner/late"), "LATE").unwrap(); + child.stdin.take().unwrap().write_all(b"y\n").unwrap(); + let status = child.wait().unwrap(); + + assert!(!status.success(), "the command must report the failure"); + assert!(!sandbox.root.join("dir/a").exists(), "`a` was destroyed"); + assert!(sandbox.root.join("dir/inner/late").exists()); + let log = audit_log(&sandbox); + assert!(log.contains("obliterate-dir-intent"), "{log}"); + assert!( + log.contains("obliterate-dir-failed-after-1-of-1-files"), + "{log}" + ); + let reloaded = ShellState::new(sandbox.root.to_str().unwrap()).unwrap(); + assert!( + reloaded + .history + .iter() + .any(|op| op.op_type == vsh::state::OperationType::Obliterate && op.path == "dir"), + "the history has no record of the obliteration" + ); +} diff --git a/impl/rust-cli/tests/property_tests.rs b/impl/rust-cli/tests/property_tests.rs index 6639789b..189dcde2 100644 --- a/impl/rust-cli/tests/property_tests.rs +++ b/impl/rust-cli/tests/property_tests.rs @@ -1086,3 +1086,95 @@ fn prop_glob_deterministic() { prop_assert_eq!(expansion1, expansion2, "Glob expansion should be deterministic"); }); } + +// ============================================================ +// Arithmetic never panics +// ============================================================ + +const ARITH_BINARY_OPS: &[&str] = &[ + "+", "-", "*", "/", "%", "**", "<<", ">>", "&", "|", "^", "<", "<=", ">", ">=", "==", "!=", + "&&", "||", +]; +const ARITH_UNARY_OPS: &[&str] = &["", "-", "+", "!", "~"]; + +/// Operands at and around the edges of i64, where `/`, `%` and negation used +/// to panic. +const ARITH_EDGES: &[i64] = &[ + i64::MIN, + i64::MIN + 1, + -2, + -1, + 0, + 1, + 2, + 63, + 64, + i64::MAX - 1, + i64::MAX, +]; + +/// Evaluate every shape of expression over `a` and `b`. Returning at all is +/// the property: a panic fails the test. +fn evaluate_all_shapes(state: &mut vsh::state::ShellState, a: i64, b: i64, op: &str, unary: &str) { + // Operands go in through variables: the smallest i64 cannot be written + // as a literal. + state.set_variable("a", a.to_string()); + state.set_variable("b", b.to_string()); + for expression in [ + format!("a {op} b"), + format!("{unary}a {op} {unary}b"), + format!("{unary}(a {op} b)"), + ] { + // Every expression here is in the supported syntax, so it must + // parse. Skipping one that did not would let a parser fault pass. + // The evaluation may still fail: a zero divisor, a negative + // exponent and a shift count out of range are among the cases. + let parsed = vsh::arith::parse_arithmetic(&expression) + .unwrap_or_else(|e| panic!("`{expression}` did not parse: {e:#}")); + let _ = vsh::arith::eval_arith(&parsed, state); + } +} + +/// Every operator on every pair of edge operands: a value or an error, +/// never a panic. Exhaustive, so it does not depend on what a random run +/// happens to draw. +#[test] +fn arithmetic_never_panics_at_the_edges() { + let temp = TempDir::new().unwrap(); + let mut state = vsh::state::ShellState::new(temp.path().to_str().unwrap()).unwrap(); + for &a in ARITH_EDGES { + for &b in ARITH_EDGES { + for op in ARITH_BINARY_OPS { + for unary in ARITH_UNARY_OPS { + evaluate_all_shapes(&mut state, a, b, op, unary); + } + } + } + } + + // The three cases that panicked, with the values bash gives. + state.set_variable("m", i64::MIN.to_string()); + let eval = |expression: &str, state: &vsh::state::ShellState| { + vsh::arith::eval_arith(&vsh::arith::parse_arithmetic(expression).unwrap(), state).unwrap() + }; + assert_eq!(eval("m / -1", &state), i64::MIN); + assert_eq!(eval("m % -1", &state), 0); + assert_eq!(eval("-m", &state), i64::MIN); +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(500))] + + /// The same for operands drawn at random. + #[test] + fn prop_arithmetic_never_panics( + a in any::(), + b in any::(), + op in prop::sample::select(ARITH_BINARY_OPS.to_vec()), + unary in prop::sample::select(ARITH_UNARY_OPS.to_vec()), + ) { + let temp = TempDir::new().unwrap(); + let mut state = vsh::state::ShellState::new(temp.path().to_str().unwrap()).unwrap(); + evaluate_all_shapes(&mut state, a, b, op, unary); + } +} diff --git a/impl/rust-cli/tests/redirect_target_tests.rs b/impl/rust-cli/tests/redirect_target_tests.rs new file mode 100644 index 00000000..e2def3f2 --- /dev/null +++ b/impl/rust-cli/tests/redirect_target_tests.rs @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Jonathan D.A. Jewell +//! A built-in's redirected output goes to the file that was opened and +//! checked when the redirection was set up. +//! +//! This file holds one test, in a process of its own: capturing a +//! built-in's output redirects the standard output of the whole process. + +#![cfg(unix)] + +mod fixtures; + +use fixtures::sandbox; +use std::fs; +use vsh::redirection::{capture_and_redirect, Redirection}; + +/// Between the set-up and the write, the name is given to a file outside +/// the session root, as a hard link. The write used to open the name a +/// second time. That open was beneath the root, so a symbolic link was +/// refused, but a hard link is an ordinary file: the file outside was +/// truncated and written, without the check on the number of links that +/// the set-up had made. +#[test] +fn a_built_in_writes_to_the_file_that_was_opened_and_checked() { + let (sandbox, mut state) = sandbox(); + let victim = sandbox.outside.join("victim"); + fs::write(&victim, "PRECIOUS").unwrap(); + let out = sandbox.root.join("out"); + let redirects = vec![Redirection::Output { + file: "out".to_string(), + }]; + + capture_and_redirect(&redirects, &mut state, |_| { + fs::remove_file(&out).unwrap(); + fs::hard_link(&victim, &out).unwrap(); + println!("from the built-in"); + Ok(()) + }) + .unwrap(); + + assert_eq!(fs::read_to_string(&victim).unwrap(), "PRECIOUS"); +} diff --git a/impl/rust-cli/tests/regressions_parsed_path.rs b/impl/rust-cli/tests/regressions_parsed_path.rs new file mode 100644 index 00000000..3d9f34e7 --- /dev/null +++ b/impl/rust-cli/tests/regressions_parsed_path.rs @@ -0,0 +1,601 @@ +// SPDX-License-Identifier: MPL-2.0 +//! The runtime holes of the route plan, each as a test through the real +//! command line (`vsh -r -c ''`). +//! +//! Source: `docs/plans/VSH-ROUTE-PLAN-2026-11-04.adoc`, section 3.2 (holes a +//! to n) and the 2026-11-17 review (hole o). Every reproduction below was +//! re-run against `main` on 2026-11-17 and still failed. Hole p was found +//! the same day, in the scrub that closed hole d +//! (`docs/audits/2026-11-17-capstd-experiment.adoc`). +//! +//! # How to read this file +//! +//! Each test states the *safe* behaviour: what must be true after the +//! commands run. A hole is "open" while its check fails. +//! +//! [`KNOWN_OPEN`] lists the holes that are open today. The tests hold it to +//! the truth in both directions: +//! +//! * a hole in the list whose check passes fails the test, with a message to +//! remove it from the list (so a fix cannot land without the list +//! shrinking); +//! * a hole not in the list whose check fails is a regression. +//! +//! So the suite is green while the list is accurate, no test is skipped, and +//! the list is the record of what is still unsafe. It must become empty. +//! +//! A check returns `Err` only for the documented symptom. Anything else +//! unexpected (a set-up step failing, say) panics, so a hole cannot stay +//! "open" for the wrong reason. + +#![cfg(unix)] + +mod fixtures; + +use fixtures::tempfile::TempDir; +use std::fs; +use std::os::unix::fs::{symlink, PermissionsExt}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output}; + +/// Holes that are open today. See the module documentation. +const KNOWN_OPEN: &[&str] = &["b", "o", "p"]; + +/// Every hole this file tests. +const ALL_HOLES: &[&str] = &[ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", +]; + +/// Hold one hole's check against [`KNOWN_OPEN`]. +fn hole(name: &str, check: impl FnOnce() -> Result<(), String>) { + assert!(ALL_HOLES.contains(&name), "unlisted hole {name}"); + match (KNOWN_OPEN.contains(&name), check()) { + (true, Err(symptom)) => println!("hole {name} is still open: {symptom}"), + (true, Ok(())) => panic!( + "hole {name} no longer reproduces. Remove it from KNOWN_OPEN in \ + tests/regressions_parsed_path.rs." + ), + (false, Ok(())) => {} + (false, Err(symptom)) => panic!("hole {name} has regressed: {symptom}"), + } +} + +#[test] +fn known_open_names_only_real_holes() { + for name in KNOWN_OPEN { + assert!( + ALL_HOLES.contains(name), + "KNOWN_OPEN names unknown hole {name}" + ); + } +} + +/// A session root (`root`) inside a private directory, with a sibling +/// directory (`outside`) that the shell must never touch. +struct Sandbox { + temp: TempDir, + root: PathBuf, + outside: PathBuf, +} + +impl Sandbox { + fn new() -> Self { + let temp = TempDir::new().unwrap(); + let root = temp.path().join("root"); + let outside = temp.path().join("outside"); + fs::create_dir(&root).unwrap(); + fs::create_dir(&outside).unwrap(); + Self { + temp, + root, + outside, + } + } + + /// Run a command line through the parsed path. + fn vsh(&self, commands: &str) -> Output { + Command::new(env!("CARGO_BIN_EXE_vsh")) + .arg("-r") + .arg(&self.root) + .arg("-c") + .arg(commands) + // Wherever the shell keeps state outside the root, keep it + // inside this sandbox. + .env("XDG_STATE_HOME", self.temp.path().join("xdg-state")) + .env("XDG_CACHE_HOME", self.temp.path().join("xdg-cache")) + .env("XDG_DATA_HOME", self.temp.path().join("xdg-data")) + .env("NO_COLOR", "1") + .env("RUST_BACKTRACE", "0") + .output() + .expect("vsh should start") + } + + /// Run a command line through the parsed path, with the shell started + /// in the directory `started_in`. + fn vsh_from(&self, started_in: &Path, commands: &str) -> Output { + Command::new(env!("CARGO_BIN_EXE_vsh")) + .current_dir(started_in) + .arg("-r") + .arg(&self.root) + .arg("-c") + .arg(commands) + .env("XDG_STATE_HOME", self.temp.path().join("xdg-state")) + .env("XDG_CACHE_HOME", self.temp.path().join("xdg-cache")) + .env("XDG_DATA_HOME", self.temp.path().join("xdg-data")) + .env("NO_COLOR", "1") + .env("RUST_BACKTRACE", "0") + .output() + .expect("vsh should start") + } + + /// Run a command line that is only set-up: it must succeed. + fn vsh_ok(&self, commands: &str) { + let output = self.vsh(commands); + assert!( + output.status.success(), + "set-up `{commands}` failed: {}", + text(&output) + ); + } +} + +fn text(output: &Output) -> String { + format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ) +} + +fn read(path: &Path) -> String { + String::from_utf8_lossy(&fs::read(path).unwrap()).into_owned() +} + +/// The names in `dir`, sorted. +fn names(dir: &Path) -> Vec { + let mut names: Vec = fs::read_dir(dir) + .unwrap() + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .collect(); + names.sort(); + names +} + +/// Every regular file under `dir`, recursively, without following links. +fn files_under(dir: &Path) -> Vec { + let mut found = Vec::new(); + let mut pending = vec![dir.to_path_buf()]; + while let Some(next) = pending.pop() { + for entry in fs::read_dir(&next).unwrap() { + let entry = entry.unwrap(); + let kind = entry.file_type().unwrap(); + if kind.is_dir() { + pending.push(entry.path()); + } else if kind.is_file() { + found.push(entry.path()); + } + } + } + found +} + +/// Whether `haystack` holds `secret`, as raw bytes or as the JSON number +/// array the shell uses for undo data (`[83, 69, ...]`, any spacing). +fn holds_secret(haystack: &[u8], secret: &str) -> bool { + let raw = secret.as_bytes(); + if haystack.windows(raw.len()).any(|window| window == raw) { + return true; + } + let as_numbers: String = raw.iter().map(u8::to_string).collect::>().join(","); + let compact: Vec = haystack + .iter() + .copied() + .filter(|byte| !byte.is_ascii_whitespace()) + .collect(); + compact + .windows(as_numbers.len()) + .any(|window| window == as_numbers.as_bytes()) +} + +/// (a) Undoing an output redirection restores the file. +#[test] +fn hole_a_undo_of_a_redirection_restores_the_file() { + hole("a", || { + let sandbox = Sandbox::new(); + let output = sandbox.vsh("touch g; echo hi > g; undo"); + let content = read(&sandbox.root.join("g")); + if !output.status.success() || !content.is_empty() { + return Err(format!( + "undo of `echo hi > g` failed (rc {:?}); g holds {content:?}", + output.status.code() + )); + } + Ok(()) + }); +} + +/// (b) Undo does not destroy data written by another program since. +#[test] +fn hole_b_undo_does_not_delete_data_written_outside_the_shell() { + hole("b", || { + let sandbox = Sandbox::new(); + sandbox.vsh_ok("touch f"); + fs::write(sandbox.root.join("f"), "precious\n").unwrap(); + sandbox.vsh("undo"); + match fs::read(sandbox.root.join("f")) { + Ok(bytes) if bytes == b"precious\n" => Ok(()), + Ok(bytes) => Err(format!("f was changed to {bytes:?}")), + Err(_) => Err("undo deleted f, which another program had written to".into()), + } + }); +} + +/// (c) Obliterate is refused inside a transaction (ruling OD-3a): a +/// transaction promises rollback, and obliteration cannot be rolled back. +#[test] +fn hole_c_obliterate_is_refused_inside_a_transaction() { + hole("c", || { + let sandbox = Sandbox::new(); + sandbox.vsh("begin t; touch a; obliterate a --force"); + if sandbox.root.join("a").exists() { + Ok(()) + } else { + Err("obliterate ran inside an open transaction".into()) + } + }); +} + +/// (d) After obliterate, no earlier content of the file survives in the +/// shell's own state. +#[test] +fn hole_d_obliterate_scrubs_earlier_contents_from_the_state() { + hole("d", || { + let sandbox = Sandbox::new(); + sandbox.vsh_ok("echo SECRETA > f; echo b > f; obliterate f --force"); + assert!( + !sandbox.root.join("f").exists(), + "obliterate should remove f" + ); + let holders: Vec = files_under(sandbox.temp.path()) + .into_iter() + .filter(|path| holds_secret(&fs::read(path).unwrap(), "SECRETA")) + .map(|path| path.file_name().unwrap().to_string_lossy().into_owned()) + .collect(); + if holders.is_empty() { + Ok(()) + } else { + Err(format!("the obliterated content is still in {holders:?}")) + } + }); +} + +/// (e) Arithmetic at the edge of i64 does not panic. (It wraps, as bash +/// does and as the shell's own addition and multiplication already did.) +#[test] +fn hole_e_arithmetic_overflow_does_not_panic() { + hole("e", || { + let sandbox = Sandbox::new(); + let minimum = "m=$(( -9223372036854775807 - 1 ))"; + let mut panics = Vec::new(); + for expression in ["m / -1", "m % -1", "-m"] { + let output = sandbox.vsh(&format!("{minimum}; x=$(( {expression} )); echo done")); + if output.status.code() == Some(101) || text(&output).contains("panicked") { + panics.push(expression); + } + } + if panics.is_empty() { + Ok(()) + } else { + Err(format!("the shell panicked evaluating {panics:?}")) + } + }); +} + +/// (f) Undo after `cd` reverses the operation that was made, not a file of +/// the same name in the new directory. +#[test] +fn hole_f_undo_after_cd_targets_the_original_path() { + hole("f", || { + let sandbox = Sandbox::new(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/f"), "KEEP\n").unwrap(); + sandbox.vsh("touch f; cd sub; undo"); + let kept = fs::read(sandbox.root.join("sub/f")).ok(); + if kept.as_deref() != Some(b"KEEP\n".as_slice()) { + return Err("undo deleted or changed the user's own sub/f".into()); + } + if sandbox.root.join("f").exists() { + return Err("undo left the created f in place".into()); + } + Ok(()) + }); +} + +/// (g) `cd ..` at the root does not let later commands reach outside it: +/// not to create, not to write, and not to remove. +#[test] +fn hole_g_cd_cannot_leave_the_root() { + hole("g", || { + let sandbox = Sandbox::new(); + let parent = sandbox.temp.path(); + fs::write(parent.join("bystander"), "KEEP\n").unwrap(); + sandbox.vsh("cd ..; touch escaped"); + sandbox.vsh("cd ..; mkdir escaped_dir"); + sandbox.vsh("cd ..; echo x > escaped_out"); + sandbox.vsh("cd ..; rm bystander"); + let mut reached = Vec::new(); + for (name, command) in [ + ("escaped", "touch"), + ("escaped_dir", "mkdir"), + ("escaped_out", ">"), + ] { + if parent.join(name).exists() { + reached.push(format!("`{command}` created {name} outside the root")); + } + } + if !parent.join("bystander").exists() { + reached.push("`rm` removed a file outside the root".into()); + } + if reached.is_empty() { + Ok(()) + } else { + Err(reached.join("; ")) + } + }); +} + +/// (h) Undoing a removal restores the file's permissions with its content. +#[test] +fn hole_h_undo_of_rm_restores_the_mode() { + hole("h", || { + let sandbox = Sandbox::new(); + let secret = sandbox.root.join("s"); + fs::write(&secret, "TOPSECRET").unwrap(); + fs::set_permissions(&secret, fs::Permissions::from_mode(0o600)).unwrap(); + sandbox.vsh_ok("rm s; undo"); + assert_eq!( + read(&secret), + "TOPSECRET", + "undo should restore the content" + ); + let mode = fs::metadata(&secret).unwrap().permissions().mode() & 0o777; + if mode == 0o600 { + Ok(()) + } else { + Err(format!("the file came back with mode {mode:o}, not 600")) + } + }); +} + +/// (i) Obliterate does nothing if it cannot first record that it will: the +/// audit entry is written before the overwrite, not attempted after it. +/// +/// The set-up puts a directory where the audit log goes, so the entry cannot +/// be written. If the audit log moves out of the root, this set-up must move +/// with it. +#[test] +fn hole_i_obliterate_refuses_when_it_cannot_record_the_audit_entry() { + hole("i", || { + let sandbox = Sandbox::new(); + fs::write(sandbox.root.join("f"), "DATA\n").unwrap(); + fs::create_dir(sandbox.root.join(".vsh-audit.log")).unwrap(); + sandbox.vsh("obliterate f --force"); + match fs::read(sandbox.root.join("f")) { + Ok(bytes) if bytes == b"DATA\n" => Ok(()), + _ => Err("the file was obliterated although no audit entry could be written".into()), + } + }); +} + +/// (j) Writing through an in-root symlink does not change a file outside. +#[test] +fn hole_j_redirection_does_not_follow_a_symlink_out_of_the_root() { + hole("j", || { + let sandbox = Sandbox::new(); + let victim = sandbox.outside.join("victim"); + fs::write(&victim, "ORIGINAL\n").unwrap(); + symlink(&victim, sandbox.root.join("link")).unwrap(); + sandbox.vsh("echo PWNED > link"); + sandbox.vsh("echo MORE >> link"); + let content = read(&victim); + if content == "ORIGINAL\n" { + Ok(()) + } else { + Err(format!("the file outside the root now holds {content:?}")) + } + }); +} + +/// (k) Creating under an in-root symlink to an outside directory creates +/// nothing outside. +#[test] +fn hole_k_creation_does_not_follow_a_symlinked_directory_out_of_the_root() { + hole("k", || { + let sandbox = Sandbox::new(); + symlink(&sandbox.outside, sandbox.root.join("lp")).unwrap(); + sandbox.vsh("touch lp/new; mkdir lp/newdir"); + let created = names(&sandbox.outside); + if created.is_empty() { + Ok(()) + } else { + Err(format!("created outside the root: {created:?}")) + } + }); +} + +/// (l) Moving into an in-root symlink to an outside directory moves nothing +/// outside. +#[test] +fn hole_l_mv_does_not_follow_a_symlinked_directory_out_of_the_root() { + hole("l", || { + let sandbox = Sandbox::new(); + symlink(&sandbox.outside, sandbox.root.join("lp")).unwrap(); + sandbox.vsh("touch f; mv f lp/f"); + let moved = names(&sandbox.outside); + if moved.is_empty() { + Ok(()) + } else { + Err(format!("moved outside the root: {moved:?}")) + } + }); +} + +/// (m) Obliterating an in-root symlink does not destroy the file it points +/// to outside the root. +#[test] +fn hole_m_obliterate_does_not_follow_a_symlink_out_of_the_root() { + hole("m", || { + let sandbox = Sandbox::new(); + let victim = sandbox.outside.join("victim"); + fs::write(&victim, "PRECIOUS\n").unwrap(); + symlink(&victim, sandbox.root.join("vl")).unwrap(); + sandbox.vsh("obliterate vl --force"); + if fs::read(&victim).unwrap() == b"PRECIOUS\n" { + Ok(()) + } else { + Err("the file outside the root was overwritten".into()) + } + }); +} + +/// (n) A hard link inside the root to a file outside it does not let the +/// shell destroy or change that file. +#[test] +fn hole_n_hard_links_do_not_reach_files_outside_the_root() { + hole("n", || { + let sandbox = Sandbox::new(); + let victim = sandbox.outside.join("victim"); + let other = sandbox.outside.join("other"); + fs::write(&victim, "PRECIOUS\n").unwrap(); + fs::write(&other, "OTHER\n").unwrap(); + fs::hard_link(&victim, sandbox.root.join("hl")).unwrap(); + fs::hard_link(&other, sandbox.root.join("h2")).unwrap(); + sandbox.vsh("obliterate hl --force"); + sandbox.vsh("echo X > h2"); + let mut damaged = Vec::new(); + if fs::read(&victim).unwrap() != b"PRECIOUS\n" { + damaged.push("obliterate overwrote the outside file"); + } + if fs::read(&other).unwrap() != b"OTHER\n" { + damaged.push("redirection changed the outside file"); + } + if damaged.is_empty() { + Ok(()) + } else { + Err(damaged.join("; ")) + } + }); +} + +/// (o) After a rollback, `undo` does not try to reverse the rolled-back +/// operations again. +#[test] +fn hole_o_undo_after_rollback_does_not_reverse_twice() { + hole("o", || { + let sandbox = Sandbox::new(); + let output = sandbox.vsh("begin t; touch f; rollback; undo"); + assert!( + !sandbox.root.join("f").exists(), + "rollback should remove f: {}", + text(&output) + ); + if output.status.success() { + Ok(()) + } else { + Err(format!( + "undo after rollback failed: {}", + text(&output).lines().last().unwrap_or_default() + )) + } + }); +} + +/// (p) After obliterate, no earlier content of the file survives in the +/// shell's own state, under whatever name it was saved. +/// +/// The scrub of hole (d) finds the records of a file by its path. Content +/// that was saved while the file had another name is recorded under that +/// name, and is not found: `mv a b; obliterate b` leaves what `a` held. +/// The same holds for content saved through a symbolic link that has been +/// removed since, in a history written before redirections recorded the +/// file they changed. +#[test] +fn hole_p_obliterate_scrubs_contents_saved_under_an_earlier_name() { + hole("p", || { + let sandbox = Sandbox::new(); + sandbox.vsh_ok("echo SECRETP > a; echo b > a; mv a b; obliterate b --force"); + assert!( + !sandbox.root.join("b").exists(), + "obliterate should remove b" + ); + let holders: Vec = files_under(sandbox.temp.path()) + .into_iter() + .filter(|path| holds_secret(&fs::read(path).unwrap(), "SECRETP")) + .map(|path| path.file_name().unwrap().to_string_lossy().into_owned()) + .collect(); + if holders.is_empty() { + Ok(()) + } else { + Err(format!( + "the content saved under the earlier name is still in {holders:?}" + )) + } + }); +} + +// The tests below are not holes of the route plan. They were found in +// review of this branch and are fixed, so they are plain regression tests. + +/// The shell starts in its session root. Started with `-r` from another +/// directory, it used to leave the process where it was started: the +/// commands it ran worked there, outside the session root, and `$PWD` +/// named that directory. +#[test] +fn the_shell_starts_in_its_session_root() { + let sandbox = Sandbox::new(); + let root = fs::canonicalize(&sandbox.root).unwrap(); + + let output = sandbox.vsh_from( + &sandbox.outside, + "/bin/pwd; echo $PWD; /usr/bin/touch made-by-a-command", + ); + + assert!(output.status.success(), "{}", text(&output)); + let lines: Vec = String::from_utf8_lossy(&output.stdout) + .lines() + .map(str::to_string) + .collect(); + let root_text = root.to_string_lossy().into_owned(); + assert_eq!(lines, [root_text.clone(), root_text], "{}", text(&output)); + assert!(sandbox.root.join("made-by-a-command").exists()); + assert_eq!(names(&sandbox.outside), Vec::::new()); +} + +/// A built-in that fails after its `>` target was emptied leaves the +/// truncation on record, so undo restores the file. It was recorded only +/// when the built-in succeeded. +#[test] +fn a_failing_built_in_leaves_its_redirect_undoable() { + let sandbox = Sandbox::new(); + fs::write(sandbox.root.join("out"), "KEEP\n").unwrap(); + + let failed = sandbox.vsh("ls no-such-directory > out"); + assert!(!failed.status.success(), "{}", text(&failed)); + sandbox.vsh_ok("undo"); + + assert_eq!(read(&sandbox.root.join("out")), "KEEP\n"); +} + +/// A `>` target keeps its content when a later redirection of the same +/// command is refused. +#[test] +fn a_refused_redirection_leaves_the_other_targets_alone() { + let sandbox = Sandbox::new(); + fs::write(sandbox.root.join("out"), "KEEP\n").unwrap(); + symlink("nowhere", sandbox.root.join("dangling")).unwrap(); + + let refused = sandbox.vsh("echo hi > out 2> dangling"); + + assert!(!refused.status.success(), "{}", text(&refused)); + assert_eq!(read(&sandbox.root.join("out")), "KEEP\n"); +} diff --git a/impl/rust-cli/tests/rm_undo_tests.rs b/impl/rust-cli/tests/rm_undo_tests.rs new file mode 100644 index 00000000..862a4fb7 --- /dev/null +++ b/impl/rust-cli/tests/rm_undo_tests.rs @@ -0,0 +1,293 @@ +// SPDX-License-Identifier: MPL-2.0 +//! `rm` and its undo: the file comes back as it was, links are treated as +//! links, and nothing outside the session root is touched. + +#![cfg(unix)] + +mod fixtures; + +use fixtures::sandbox; +use std::fs; +use std::os::unix::fs::{symlink, PermissionsExt}; +use std::time::{Duration, UNIX_EPOCH}; +use vsh::commands; +use vsh::redirection::{RedirectSetup, Redirection}; +use vsh::state::{Operation, OperationType, ShellState}; + +#[test] +fn undo_of_rm_restores_content_mode_and_modification_time() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("secret"); + fs::write(&path, "TOPSECRET").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap(); + let long_ago = UNIX_EPOCH + Duration::new(1_500_000_000, 123_000_000); + fs::File::options() + .write(true) + .open(&path) + .unwrap() + .set_modified(long_ago) + .unwrap(); + + commands::rm(&mut state, "secret", false).unwrap(); + assert!(!path.exists()); + commands::undo(&mut state, 1, false).unwrap(); + + let meta = fs::metadata(&path).unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"TOPSECRET"); + assert_eq!(meta.permissions().mode() & 0o7777, 0o640); + assert_eq!(meta.modified().unwrap(), long_ago); +} + +#[test] +fn undo_of_rm_survives_a_restart() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("s"); + fs::write(&path, "X").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap(); + commands::rm(&mut state, "s", false).unwrap(); + drop(state); + + let mut state = ShellState::new(sandbox.root.to_str().unwrap()).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!( + fs::metadata(&path).unwrap().permissions().mode() & 0o7777, + 0o600 + ); +} + +#[test] +fn rm_of_a_symlink_removes_the_link_and_undo_puts_it_back() { + let (sandbox, mut state) = sandbox(); + let victim = sandbox.outside.join("victim"); + fs::write(&victim, "PRECIOUS").unwrap(); + symlink(&victim, sandbox.root.join("link")).unwrap(); + + commands::rm(&mut state, "link", false).unwrap(); + assert!(fs::symlink_metadata(sandbox.root.join("link")).is_err()); + assert_eq!(fs::read(&victim).unwrap(), b"PRECIOUS"); + // The link's target is recorded, not the target's content. + let op = state.history.last().unwrap(); + assert_eq!(op.op_type, OperationType::Unlink); + assert_eq!( + op.undo_data.as_deref(), + Some(victim.to_str().unwrap().as_bytes()) + ); + + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read_link(sandbox.root.join("link")).unwrap(), victim); + assert_eq!(fs::read(&victim).unwrap(), b"PRECIOUS"); +} + +#[test] +fn rm_can_remove_a_dangling_symlink() { + let (sandbox, mut state) = sandbox(); + symlink("nowhere", sandbox.root.join("dangling")).unwrap(); + commands::rm(&mut state, "dangling", false).unwrap(); + assert!(fs::symlink_metadata(sandbox.root.join("dangling")).is_err()); +} + +#[test] +fn rm_does_not_reach_through_a_symlinked_directory() { + let (sandbox, mut state) = sandbox(); + fs::write(sandbox.outside.join("victim"), "PRECIOUS").unwrap(); + symlink(&sandbox.outside, sandbox.root.join("lp")).unwrap(); + + assert!(commands::rm(&mut state, "lp/victim", false).is_err()); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"PRECIOUS" + ); + assert!(state.history.is_empty()); +} + +/// A link target is a string of bytes and need not be text. Undo of `rm` +/// puts back the same bytes. It used to store the target as text, with what +/// was not text replaced, and so made a link to a different path. +#[test] +fn undo_restores_a_link_whose_target_is_not_text() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + + let (sandbox, mut state) = sandbox(); + let target = OsStr::from_bytes(b"t\xffarget"); + let link = sandbox.root.join("link"); + symlink(target, &link).unwrap(); + + commands::rm(&mut state, "link", false).unwrap(); + assert!(fs::symlink_metadata(&link).is_err()); + + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read_link(&link).unwrap().as_os_str(), target); +} + +/// A link that is removed, put back and removed again (`rm link; undo; +/// redo`) can be put back again. The redone record used to lose the link's +/// target, so the second undo failed with "Missing symlink target for +/// undo". +#[test] +fn a_redone_rm_of_a_link_can_be_undone_again() { + let (sandbox, mut state) = sandbox(); + let link = sandbox.root.join("link"); + symlink("target", &link).unwrap(); + + commands::rm(&mut state, "link", false).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + commands::redo(&mut state, 1, false).unwrap(); + assert!(fs::symlink_metadata(&link).is_err()); + + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read_link(&link).unwrap().to_str(), Some("target")); +} + +/// A removal that is redone removes the file a second time, and its record +/// must hold the content again. It held none, so `rm f; undo; redo; undo` +/// put an empty file, readable by its owner only, in place of `f`. +#[test] +fn a_redone_rm_can_be_undone_again() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + fs::write(&path, "precious").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + commands::redo(&mut state, 1, false).unwrap(); + assert!(!path.exists()); + + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"precious"); + assert_eq!( + fs::metadata(&path).unwrap().permissions().mode() & 0o7777, + 0o640 + ); +} + +/// The redone removal keeps what the file holds when it is removed. That +/// is not always what the first removal saved: the file can have been +/// changed in between. +#[test] +fn a_redone_rm_keeps_what_the_file_holds_then() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + fs::write(&path, "first").unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + fs::write(&path, "changed since").unwrap(); + commands::redo(&mut state, 1, false).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + + assert_eq!(fs::read(&path).unwrap(), b"changed since"); +} + +/// A removal whose record holds no content cannot be undone. Undo used to +/// write an empty file in its place and report success. +#[test] +fn undo_of_a_removal_with_no_content_fails_and_creates_nothing() { + let (sandbox, mut state) = sandbox(); + state.record_operation(Operation::new( + OperationType::DeleteFile, + "gone".to_string(), + None, + )); + + let refused = commands::undo(&mut state, 1, false).unwrap_err(); + + assert!( + format!("{refused:#}").contains("holds no content"), + "{refused:#}" + ); + assert!(!sandbox.root.join("gone").exists()); +} + +/// What is redone must still be a regular file. A removal is not redone on +/// a directory or a link that has taken the file's place. +#[test] +fn a_removal_is_not_redone_on_something_that_is_not_a_regular_file() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + fs::write(&path, "content").unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + fs::remove_file(&path).unwrap(); + symlink(sandbox.outside.join("victim"), &path).unwrap(); + fs::write(sandbox.outside.join("victim"), "OUTSIDE").unwrap(); + + let refused = commands::redo(&mut state, 1, false).unwrap_err(); + + assert!( + format!("{refused:#}").contains("not a regular file"), + "{refused:#}" + ); + assert!(fs::symlink_metadata(&path).unwrap().is_symlink()); + assert_eq!( + fs::read(sandbox.outside.join("victim")).unwrap(), + b"OUTSIDE" + ); +} + +/// Undo of a removal puts the file back only if nothing has the name. +/// Whatever has taken it since is left as it is: undo used to write over +/// it, which replaced a file that another program had made. +#[test] +fn undo_of_rm_leaves_a_file_that_has_taken_the_name() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + fs::write(&path, "removed by the shell").unwrap(); + commands::rm(&mut state, "f", false).unwrap(); + fs::write(&path, "made by another program").unwrap(); + + let refused = commands::undo(&mut state, 1, false).unwrap_err(); + + assert!( + format!("{refused:#}").contains("has the name"), + "{refused:#}" + ); + assert_eq!(fs::read(&path).unwrap(), b"made by another program"); + + // With the name free again, the same undo goes through. + fs::remove_file(&path).unwrap(); + commands::undo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"removed by the shell"); +} + +/// Empty `f` as `command > f` does, and record it. +fn truncate_by_redirection(state: &mut ShellState, name: &str) { + let redirects = vec![Redirection::Output { + file: name.to_string(), + }]; + RedirectSetup::run(&redirects, state, |_, _| Ok(())).unwrap(); +} + +/// Undo of a truncation checks the file it writes to, as the redirection +/// did. A file that has gained a name outside the session root since is +/// not written: undo used to open the name with truncation, unchecked. +#[test] +fn undo_of_a_truncation_refuses_a_file_with_another_name() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + let other_name = sandbox.outside.join("other-name"); + fs::write(&path, "ORIGINAL").unwrap(); + truncate_by_redirection(&mut state, "f"); + fs::hard_link(&path, &other_name).unwrap(); + + let refused = commands::undo(&mut state, 1, false).unwrap_err(); + + assert!(format!("{refused:#}").contains("hard links"), "{refused:#}"); + assert_eq!(fs::read(&other_name).unwrap(), b""); +} + +/// Undo of a truncation does not make a file. If the file has been removed +/// since, there is nothing to put the content back into. +#[test] +fn undo_of_a_truncation_needs_the_file_to_be_there() { + let (sandbox, mut state) = sandbox(); + let path = sandbox.root.join("f"); + fs::write(&path, "ORIGINAL").unwrap(); + truncate_by_redirection(&mut state, "f"); + fs::remove_file(&path).unwrap(); + + assert!(commands::undo(&mut state, 1, false).is_err()); + assert!(!path.exists()); +} diff --git a/impl/rust-cli/tests/security_tests.rs b/impl/rust-cli/tests/security_tests.rs index 5cee2c91..4e32ce12 100644 --- a/impl/rust-cli/tests/security_tests.rs +++ b/impl/rust-cli/tests/security_tests.rs @@ -358,32 +358,55 @@ fn security_sandbox_enforcement() { // (tested via path traversal tests above) } +/// Nothing the shell creates is owned by anyone but the invoking user, and +/// nothing it creates carries a set-user-ID, set-group-ID or sticky bit. +/// +/// This holds as root and as an ordinary user, so the test does not skip. +/// (The test of this name used to contain no assertion in any branch.) +#[cfg(unix)] #[test] fn security_no_privilege_escalation() { - // Verify we're not running as root in environments that can be non-root. - // - // In containerised CI (GitHub Actions default runners, many Docker images) - // the test harness legitimately runs as root. Failing the whole security - // suite there is noise, not signal. We treat running-as-root as a warning - // the operator must acknowledge via VSH_ALLOW_ROOT_TESTS=1, but otherwise - // skip rather than fail so the suite stays portable. - #[cfg(unix)] - { - let euid = unsafe { libc::geteuid() }; - if euid == 0 { - if std::env::var("VSH_ALLOW_ROOT_TESTS").is_ok() { - eprintln!( - "warning: running security tests as root (VSH_ALLOW_ROOT_TESTS set); \ - do not do this on a real system" - ); - return; - } - eprintln!( - "security_no_privilege_escalation: SKIPPED (running as root). \ - Set VSH_ALLOW_ROOT_TESTS=1 to acknowledge." + use std::os::unix::fs::MetadataExt; + + let temp = TempDir::new().unwrap(); + let mut state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + vsh::commands::mkdir(&mut state, "dir", false).unwrap(); + vsh::commands::touch(&mut state, "dir/file", false).unwrap(); + vsh::commands::touch(&mut state, "top", false).unwrap(); + drop(state); + + let euid = nix::unistd::geteuid().as_raw(); + let mut checked = 0; + let mut pending = vec![temp.path().to_path_buf()]; + while let Some(dir) = pending.pop() { + for entry in fs::read_dir(&dir).unwrap() { + let path = entry.unwrap().path(); + let meta = fs::symlink_metadata(&path).unwrap(); + assert_eq!( + meta.uid(), + euid, + "{} is not owned by the invoking user", + path.display() + ); + assert_eq!( + meta.mode() & 0o7000, + 0, + "{} carries a set-ID or sticky bit (mode {:o})", + path.display(), + meta.mode() & 0o7777 ); + checked += 1; + if meta.is_dir() { + pending.push(path); + } } } + // dir, dir/file, top, and at least one state file. + assert!(checked >= 4, "only {checked} paths were checked"); + + // The shell binary itself must not be set-user-ID or set-group-ID. + let binary = fs::metadata(env!("CARGO_BIN_EXE_vsh")).unwrap(); + assert_eq!(binary.mode() & 0o6000, 0, "the vsh binary is set-ID"); } // ============================================================ diff --git a/impl/rust-cli/tests/tilde_expansion_tests.rs b/impl/rust-cli/tests/tilde_expansion_tests.rs index 5c5e975d..24f97992 100644 --- a/impl/rust-cli/tests/tilde_expansion_tests.rs +++ b/impl/rust-cli/tests/tilde_expansion_tests.rs @@ -6,6 +6,8 @@ //! It now runs globally via `expand_variables`, so `mkdir ~/foo`, //! `echo ~`, `touch ~/bar`, etc. all work. +mod fixtures; + use anyhow::Result; use tempfile::tempdir; use vsh::executable::ExecutableCommand; @@ -37,24 +39,55 @@ fn tilde_slash_path_expands_to_home_slash_path() { ); } +/// `~+` is the shell's working directory. It used to be the process's `PWD`, +/// which `cd` does not change. #[test] -fn tilde_plus_expands_to_pwd() { +fn tilde_plus_expands_to_the_working_directory() { let temp = tempdir().unwrap(); - let state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); - - std::env::set_var("PWD", "/some/where"); - assert_eq!(expand_variables("~+", &state), "/some/where"); - assert_eq!(expand_variables("~+/subdir", &state), "/some/where/subdir"); + let _directory = fixtures::hold_process_directory(); + let mut state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + let root = state.root.clone(); + + assert_eq!(expand_variables("~+", &state), root.to_string_lossy()); + + std::fs::create_dir(root.join("sub")).unwrap(); + parse_command("cd sub") + .unwrap() + .execute(&mut state) + .unwrap(); + let sub = root.join("sub"); + assert_eq!(expand_variables("~+", &state), sub.to_string_lossy()); + assert_eq!( + expand_variables("~+/file", &state), + format!("{}/file", sub.display()) + ); } +/// `~-` is the directory the shell was in before its last `cd`. It used to +/// be the process's `OLDPWD`, which `cd` does not change, so `cd ~-` went +/// wherever the shell's parent had last been. #[test] -fn tilde_minus_expands_to_oldpwd() { +fn tilde_minus_expands_to_the_previous_directory() { let temp = tempdir().unwrap(); - let state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); - - std::env::set_var("OLDPWD", "/old/dir"); - assert_eq!(expand_variables("~-", &state), "/old/dir"); - assert_eq!(expand_variables("~-/file", &state), "/old/dir/file"); + let _directory = fixtures::hold_process_directory(); + let mut state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + let root = state.root.clone(); + + // Before the first `cd` the shell has no previous directory of its own, + // and falls back to the `OLDPWD` it was started with, if there is one. + let inherited = std::env::var("OLDPWD").unwrap_or_else(|_| "~-".to_string()); + assert_eq!(expand_variables("~-", &state), inherited); + + std::fs::create_dir(root.join("sub")).unwrap(); + parse_command("cd sub") + .unwrap() + .execute(&mut state) + .unwrap(); + assert_eq!(expand_variables("~-", &state), root.to_string_lossy()); + assert_eq!( + expand_variables("~-/file", &state), + format!("{}/file", root.display()) + ); } #[test] @@ -104,19 +137,66 @@ fn tilde_expands_in_variable_assignments() -> Result<()> { Ok(()) } +/// `cd ~` still expands the tilde, but the shell is confined to its session +/// root (route plan OD-26, option a), so a home directory outside the root +/// is refused and the working directory does not move. +/// +/// Until 2026-11-17 this test asserted the opposite: that `cd ~` moved the +/// sandbox root itself to the home directory. That was runtime hole g. #[test] -fn cd_with_tilde_still_works() -> Result<()> { +fn cd_with_tilde_is_confined_to_the_session_root() -> Result<()> { let temp = tempdir()?; + let _directory = fixtures::hold_process_directory(); let mut state = ShellState::new(temp.path().to_str().unwrap())?; + let root = state.root.clone(); let home = std::env::var("HOME").unwrap_or_default(); + let home_is_outside = !std::fs::canonicalize(&home) + .map(|real| real.starts_with(&root)) + .unwrap_or(false); + assert!( + home_is_outside, + "this test needs HOME outside the session root" + ); - let cmd = parse_command("cd ~")?; - cmd.execute(&mut state)?; - - assert_eq!( - state.root, - std::fs::canonicalize(&home).unwrap_or_else(|_| std::path::PathBuf::from(&home)) + let refused = parse_command("cd ~")?.execute(&mut state); + assert!( + refused.is_err(), + "cd to a home outside the root must be refused" ); + assert_eq!(state.root, root, "the session root never moves"); + assert_eq!(state.cwd(), root, "the working directory did not move"); + + // A tilde path that lands inside the root is fine: `~-` is the previous + // directory. + std::fs::create_dir(root.join("sub"))?; + parse_command("cd sub")?.execute(&mut state)?; + assert_eq!(state.cwd(), root.join("sub")); + parse_command("cd ~-")?.execute(&mut state)?; + assert_eq!(state.cwd(), root); + assert_eq!(state.root, root); Ok(()) } + +/// The directory a tilde stands for is used as it is. It used to be read +/// for variables with the rest of the word, so in a working directory +/// named `cash$NAME`, `~+` named `cashvalue`. +#[test] +fn a_directory_named_like_a_variable_is_not_expanded() { + let temp = tempdir().unwrap(); + let mut state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + let here = state.root.join("cash$NAME"); + std::fs::create_dir(&here).unwrap(); + state.set_variable("NAME", "value"); + state.set_cwd(&here).unwrap(); + state.previous_dir = Some(here.clone()); + + for tilde in ["~+", "~-"] { + assert_eq!(expand_variables(tilde, &state), here.to_string_lossy()); + // What follows the directory is still read for variables. + assert_eq!( + expand_variables(&format!("{tilde}/$NAME"), &state), + format!("{}/value", here.display()) + ); + } +} diff --git a/impl/rust-cli/tests/working_directory_tests.rs b/impl/rust-cli/tests/working_directory_tests.rs new file mode 100644 index 00000000..d7571a35 --- /dev/null +++ b/impl/rust-cli/tests/working_directory_tests.rs @@ -0,0 +1,372 @@ +// SPDX-License-Identifier: MPL-2.0 +//! The working directory and the session root are two things. +//! +//! The session root is fixed when the shell starts. `cd` moves only the +//! working directory, and only within the session root. Paths in the history +//! are relative to the session root, so `undo` means the same thing wherever +//! the working directory is by then. + +#![cfg(unix)] + +mod fixtures; + +use fixtures::sandbox; +use std::fs; +use std::os::unix::fs::symlink; +use vsh::parser::parse_command; +use vsh::state::ShellState; +use vsh::ExecutableCommand; + +fn run(state: &mut ShellState, command: &str) -> anyhow::Result<()> { + parse_command(command)?.execute(state).map(|_| ()) +} + +#[test] +fn cd_moves_the_working_directory_and_never_the_root() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir_all(sandbox.root.join("a/b")).unwrap(); + + run(&mut state, "cd a").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("a")); + run(&mut state, "cd b").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("a/b")); + run(&mut state, "cd ..").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("a")); + run(&mut state, "cd -").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("a/b")); + run(&mut state, "cd").unwrap(); + assert_eq!( + state.cwd(), + sandbox.root, + "plain `cd` returns to the session root" + ); + assert_eq!(state.root, sandbox.root); +} + +#[test] +fn cd_cannot_go_above_the_root() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("a")).unwrap(); + + // `..` at the root stays at the root, as it does at `/`. + run(&mut state, "cd ..").unwrap(); + assert_eq!(state.cwd(), sandbox.root); + run(&mut state, "cd a").unwrap(); + run(&mut state, "cd ../../..").unwrap(); + assert_eq!(state.cwd(), sandbox.root); + + // An absolute path outside the root is refused. + let outside = sandbox.outside.to_str().unwrap().to_string(); + assert!(run(&mut state, &format!("cd {outside}")).is_err()); + assert_eq!(state.cwd(), sandbox.root); +} + +#[test] +fn cd_does_not_follow_a_symlink_out_of_the_root() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + symlink(&sandbox.outside, sandbox.root.join("out")).unwrap(); + fs::create_dir(sandbox.root.join("real")).unwrap(); + symlink("real", sandbox.root.join("in")).unwrap(); + + let refused = run(&mut state, "cd out").unwrap_err(); + assert!( + refused.to_string().contains("outside the session root"), + "{refused}" + ); + assert_eq!(state.cwd(), sandbox.root); + + // A link that stays inside the root is followed. + run(&mut state, "cd in").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("real")); +} + +#[test] +fn cd_accepts_a_real_absolute_path_inside_the_root_and_a_root_relative_one() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir_all(sandbox.root.join("a/b")).unwrap(); + + let real = sandbox.root.join("a/b"); + run(&mut state, &format!("cd {}", real.display())).unwrap(); + assert_eq!(state.cwd(), real); + + // `/a` is read from the session root, as `touch /a/x` would be. + run(&mut state, "cd /a").unwrap(); + assert_eq!(state.cwd(), sandbox.root.join("a")); +} + +#[test] +fn paths_are_relative_to_the_working_directory() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + + run(&mut state, "cd sub").unwrap(); + run(&mut state, "touch f").unwrap(); + run(&mut state, "mkdir d").unwrap(); + run(&mut state, "touch ../top").unwrap(); + assert!(sandbox.root.join("sub/f").is_file()); + assert!(sandbox.root.join("sub/d").is_dir()); + assert!(sandbox.root.join("top").is_file()); + + // The history holds paths from the session root. + let recorded: Vec<&str> = state.history.iter().map(|op| op.path.as_str()).collect(); + assert_eq!(recorded, ["sub/f", "sub/d", "top"]); +} + +#[test] +fn undo_after_cd_reverses_the_operation_that_was_made() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/f"), "KEEP").unwrap(); + + // Made at the root, undone from `sub`, where another `f` lives. + run(&mut state, "touch f").unwrap(); + run(&mut state, "cd sub").unwrap(); + run(&mut state, "undo").unwrap(); + assert!(!sandbox.root.join("f").exists()); + assert_eq!(fs::read(sandbox.root.join("sub/f")).unwrap(), b"KEEP"); + + // Made in `sub`, undone from the root. + run(&mut state, "touch g").unwrap(); + run(&mut state, "mv g h").unwrap(); + run(&mut state, "cd ..").unwrap(); + run(&mut state, "undo").unwrap(); + assert!(sandbox.root.join("sub/g").exists()); + assert!(!sandbox.root.join("sub/h").exists()); + run(&mut state, "undo").unwrap(); + assert!(!sandbox.root.join("sub/g").exists()); + + // And redo, from yet another directory. + fs::create_dir(sandbox.root.join("other")).unwrap(); + run(&mut state, "cd other").unwrap(); + run(&mut state, "redo").unwrap(); + assert!(sandbox.root.join("sub/g").exists()); +} + +#[test] +fn rollback_after_cd_reverses_the_operations_that_were_made() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + + run(&mut state, "begin t").unwrap(); + run(&mut state, "touch a").unwrap(); + run(&mut state, "cd sub").unwrap(); + run(&mut state, "touch b").unwrap(); + run(&mut state, "rollback").unwrap(); + assert!(!sandbox.root.join("a").exists()); + assert!(!sandbox.root.join("sub/b").exists()); +} + +/// Runs the built shell on `script` with `root` as the session root. A +/// redirection changes where the process writes, so these cases need a +/// process of their own. +fn shell(root: &std::path::Path, script: &str) -> std::process::Output { + std::process::Command::new(env!("CARGO_BIN_EXE_vsh")) + .arg("-r") + .arg(root) + .arg("-c") + .arg(script) + .current_dir(root) + .env("NO_COLOR", "1") + .env_remove("RUST_BACKTRACE") + .stdin(std::process::Stdio::null()) + .output() + .expect("start the built shell") +} + +/// A redirection made below the session root is recorded under the path of +/// the file it wrote. It used to be recorded with the working directory +/// added a second time (`sub/sub/f`), so undo failed or changed another +/// file. +#[test] +fn a_redirection_below_the_root_is_recorded_under_its_own_path() { + let (sandbox, _state) = sandbox(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + let file = sandbox.root.join("sub/f"); + + let run = shell(&sandbox.root, "cd sub; echo one > f; echo two > f"); + assert!( + run.status.success(), + "{}", + String::from_utf8_lossy(&run.stderr) + ); + assert_eq!(fs::read_to_string(&file).unwrap(), "two\n"); + let saved = fs::read_to_string(sandbox.root.join(".vsh_state.json")).unwrap(); + assert!(saved.contains("\"sub/f\""), "{saved}"); + assert!(!saved.contains("sub/sub"), "{saved}"); + + // Undo works from anywhere: here from the root, in a new shell. + let run = shell(&sandbox.root, "undo"); + assert!( + run.status.success(), + "{}", + String::from_utf8_lossy(&run.stderr) + ); + assert_eq!(fs::read_to_string(&file).unwrap(), "one\n"); + let run = shell(&sandbox.root, "undo"); + assert!( + run.status.success(), + "{}", + String::from_utf8_lossy(&run.stderr) + ); + assert!(!file.exists()); + assert!(!sandbox.root.join("sub/sub").exists()); +} + +/// `ls > file` lists the working directory, as `ls` alone does. It listed +/// the session root. +#[test] +fn a_redirected_ls_lists_the_working_directory() { + let (sandbox, _state) = sandbox(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/inside"), "").unwrap(); + fs::write(sandbox.root.join("at-root"), "").unwrap(); + + let run = shell(&sandbox.root, "cd sub; ls > listing"); + assert!( + run.status.success(), + "{}", + String::from_utf8_lossy(&run.stderr) + ); + + let listing = fs::read_to_string(sandbox.root.join("sub/listing")).unwrap(); + assert!(listing.contains("inside"), "{listing}"); + assert!(!listing.contains("at-root"), "{listing}"); +} + +/// The history stores paths as text. A directory whose name is not text +/// cannot be named there: an operation in it would be recorded under a +/// different path, and undo would act on that one. So the shell does not +/// enter it. A typed name is always text; this is reached through a link. +#[test] +fn a_directory_whose_name_is_not_text_is_not_entered() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + let odd = sandbox.root.join(OsStr::from_bytes(b"d\xff")); + fs::create_dir(&odd).unwrap(); + symlink(&odd, sandbox.root.join("link")).unwrap(); + + let refused = run(&mut state, "cd link").unwrap_err(); + + assert!( + format!("{refused:#}").contains("not valid UTF-8"), + "{refused:#}" + ); + assert_eq!(state.cwd(), sandbox.root); +} + +/// A relative path is resolved against the working directory as a path, +/// with `..` stopping at the session root. +#[test] +fn a_relative_path_is_resolved_against_the_working_directory() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir_all(sandbox.root.join("sub/deeper")).unwrap(); + run(&mut state, "cd sub/deeper").unwrap(); + let cwd = sandbox.root.join("sub/deeper"); + + assert_eq!(state.resolve_path("f"), cwd.join("f")); + assert_eq!(state.resolve_path("./a/../f"), cwd.join("f")); + assert_eq!(state.resolve_path("../f"), sandbox.root.join("sub/f")); + assert_eq!(state.resolve_path("../../../../f"), sandbox.root.join("f")); +} + +/// A redone operation is recorded under the path it has in the history, +/// wherever the redo is done from. It used to be read against the working +/// directory again: `touch f; undo; cd sub; redo` recorded `sub/f`, and the +/// next undo failed or removed another file. +#[test] +fn a_redo_from_another_directory_is_recorded_under_its_own_path() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/f"), "another file").unwrap(); + vsh::commands::touch(&mut state, "f", false).unwrap(); + vsh::commands::undo(&mut state, 1, false).unwrap(); + run(&mut state, "cd sub").unwrap(); + + vsh::commands::redo(&mut state, 1, false).unwrap(); + assert!(sandbox.root.join("f").exists()); + assert_eq!(state.history.last().unwrap().path, "f"); + + vsh::commands::undo(&mut state, 1, false).unwrap(); + assert!(!sandbox.root.join("f").exists()); + assert_eq!( + fs::read_to_string(sandbox.root.join("sub/f")).unwrap(), + "another file" + ); +} + +/// A copy made below the session root is redone from its own source. The +/// record kept the source as it was typed, so `cd sub; cp a b; undo; +/// cd ..; redo` copied the `a` of the session root, or failed if there was +/// none. +#[test] +fn a_copy_made_below_the_root_is_redone_from_its_own_source() { + let (sandbox, mut state) = sandbox(); + let _directory = fixtures::hold_process_directory(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + fs::write(sandbox.root.join("sub/a"), "the source").unwrap(); + fs::write(sandbox.root.join("a"), "another file").unwrap(); + run(&mut state, "cd sub").unwrap(); + vsh::commands::cp(&mut state, "a", "b", false).unwrap(); + vsh::commands::undo(&mut state, 1, false).unwrap(); + assert!(!sandbox.root.join("sub/b").exists()); + run(&mut state, "cd ..").unwrap(); + + vsh::commands::redo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(sandbox.root.join("sub/b")).unwrap(), b"the source"); + + // The redone record still names the source: it can be undone and + // redone again. + vsh::commands::undo(&mut state, 1, false).unwrap(); + assert!(!sandbox.root.join("sub/b").exists()); + vsh::commands::redo(&mut state, 1, false).unwrap(); + assert_eq!(fs::read(sandbox.root.join("sub/b")).unwrap(), b"the source"); +} + +/// A real path inside the session root names that file, for every +/// built-in. `pwd`, `$PWD`, `~+` and `~-` give real paths, and they used +/// to be read from the session root, which added the root a second time: +/// `cd sub; touch ~+/f` failed with "Parent directory does not exist". +#[test] +fn a_real_path_inside_the_root_names_that_file() { + let (sandbox, state) = sandbox(); + fs::create_dir(sandbox.root.join("sub")).unwrap(); + let real = sandbox.root.join("sub/f"); + + assert_eq!(state.resolve_path(real.to_str().unwrap()), real); + // `..` is still held inside the root. + let climbing = format!("{}/../../../etc/passwd", sandbox.root.display()); + assert_eq!( + state.resolve_path(&climbing), + sandbox.root.join("etc/passwd") + ); + // Any other absolute path is read from the session root, as before. + assert_eq!( + state.resolve_path("/etc/passwd"), + sandbox.root.join("etc/passwd") + ); + + let output = shell( + &sandbox.root, + "cd sub; touch ~+/made; mkdir ~+/dir; cd dir; touch ~-/second", + ); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(sandbox.root.join("sub/made").is_file()); + assert!(sandbox.root.join("sub/dir").is_dir()); + assert!(sandbox.root.join("sub/second").is_file()); +}