P2 slice 4: access
Agent tokens (wb_ and 64 hex digits), one book and one name each, kept only as SHA-256 in a chain log and in the OS keyring; made, listed and revoked only through the admin socket (`wanderbooks token ...`). The door checks them, scopes cover every tool, every call is in the access log (fail closed), and the owner passphrase is an Argon2id hash. Setup's terminal test waits for its prompts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
1 parent
a298d154b6
commit
d1f3ad891a
28 files changed
+5504
-116
No files matched your search
@@ -152,3 +152,156 @@ fn another_home_ignores_this_accounts_data_home() {
|
||||
"XDG_DATA_HOME was used"
|
||||
);
|
||||
}
|
||||
|
||||
/// Run `args` at a terminal (a pseudo-terminal, through util-linux
|
||||
/// `script`), typing `lines` one at a time: its exit code and what the
|
||||
/// terminal showed.
|
||||
fn at_terminal(args: &[String], lines: &[&str]) -> (i32, String) {
|
||||
use std::io::Write;
|
||||
let command = std::iter::once(env!("CARGO_BIN_EXE_wanderbooks").to_string())
|
||||
.chain(args.iter().cloned())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let mut child = Command::new("script")
|
||||
.args(["-qefc", &command, "/dev/null"])
|
||||
.env_remove("XDG_DATA_HOME")
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.expect("util-linux script");
|
||||
let mut stdin = child.stdin.take().unwrap();
|
||||
// What it shows, as it shows it: each line is typed only once it has
|
||||
// asked for it (a prompt ending ": "), never before it stops echoing.
|
||||
let shown_so_far = std::sync::Arc::new(std::sync::Mutex::new(Vec::<u8>::new()));
|
||||
let mut stdout = child.stdout.take().unwrap();
|
||||
let reader = {
|
||||
let shown = shown_so_far.clone();
|
||||
std::thread::spawn(move || {
|
||||
use std::io::Read;
|
||||
let mut buf = [0u8; 4096];
|
||||
while let Ok(n) = stdout.read(&mut buf) {
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
shown.lock().unwrap().extend_from_slice(&buf[..n]);
|
||||
}
|
||||
})
|
||||
};
|
||||
let prompts = |shown: &[u8]| {
|
||||
String::from_utf8_lossy(shown)
|
||||
.split(['\r', '\n'])
|
||||
.filter(|l| l.ends_with(": ") || l.ends_with(':'))
|
||||
.count()
|
||||
};
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
|
||||
while prompts(&shown_so_far.lock().unwrap()) <= i {
|
||||
if child.try_wait().unwrap().is_some() || std::time::Instant::now() > deadline {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
}
|
||||
// It turns its echo off just after asking.
|
||||
std::thread::sleep(std::time::Duration::from_millis(150));
|
||||
let _ = stdin.write_all(format!("{line}\n").as_bytes());
|
||||
}
|
||||
// Kept open until it ends: script stops when its input does.
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
|
||||
while child.try_wait().unwrap().is_none() {
|
||||
assert!(std::time::Instant::now() < deadline, "it never ended");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
drop(stdin);
|
||||
let out = child.wait_with_output().unwrap();
|
||||
let _ = reader.join();
|
||||
let shown = format!(
|
||||
"{}{}",
|
||||
String::from_utf8_lossy(&shown_so_far.lock().unwrap()),
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
(out.status.code().unwrap_or(-1), shown)
|
||||
}
|
||||
|
||||
const FIRST: &str = "correct horse battery";
|
||||
const SECOND: &str = "a different staple phrase";
|
||||
|
||||
#[test]
|
||||
fn setup_at_a_terminal_sets_the_owner_passphrase() {
|
||||
let home = tempfile::Builder::new()
|
||||
.prefix("wb-pass-")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
let args = vec![
|
||||
"setup".to_string(),
|
||||
"--home".into(),
|
||||
home.path().display().to_string(),
|
||||
];
|
||||
let (code, shown) = at_terminal(&args, &[FIRST, FIRST]);
|
||||
assert_eq!(code, 0, "{shown}");
|
||||
assert!(shown.contains("The owner passphrase is set."), "{shown}");
|
||||
assert!(!shown.contains(FIRST), "the passphrase was shown: {shown}");
|
||||
assert!(!shown.contains(home.path().to_str().unwrap()), "{shown}");
|
||||
let auth = home.path().join(".local/share/wanderbooks/auth");
|
||||
assert!(ledger_store::passphrase::verify(&auth, FIRST).unwrap());
|
||||
assert_eq!(mode(&auth), 0o700);
|
||||
assert_eq!(mode(&auth.join("owner.json")), 0o600);
|
||||
// Run again, it asks for nothing more.
|
||||
let (code, shown) = at_terminal(&args, &[]);
|
||||
assert_eq!(code, 0, "{shown}");
|
||||
assert!(!shown.contains("passphrase"), "{shown}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_passphrase_is_typed_by_a_person_and_changed_only_with_the_one_in_force() {
|
||||
let home = tempfile::Builder::new()
|
||||
.prefix("wb-pass-")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
// Set up without a terminal: the folder, and a note.
|
||||
let out = wanderbooks()
|
||||
.args(["setup", "--home"])
|
||||
.arg(home.path())
|
||||
.env_remove("XDG_DATA_HOME")
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
assert!(String::from_utf8_lossy(&out.stdout).contains("wanderbooks passphrase"));
|
||||
let root = home.path().join(".local/share/wanderbooks");
|
||||
let auth = root.join("auth");
|
||||
assert!(!ledger_store::passphrase::is_set(&auth).unwrap());
|
||||
let args = vec![
|
||||
"passphrase".to_string(),
|
||||
"--root".into(),
|
||||
root.display().to_string(),
|
||||
];
|
||||
// No terminal (a script, an agent): refused, nothing set.
|
||||
let out = wanderbooks()
|
||||
.args(&args)
|
||||
.stdin(std::process::Stdio::null())
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(out.status.code(), Some(1), "{out:?}");
|
||||
assert!(String::from_utf8_lossy(&out.stderr).contains("typed by a person"));
|
||||
assert!(!ledger_store::passphrase::is_set(&auth).unwrap());
|
||||
// Too short, or the two not matching: nothing set.
|
||||
for lines in [["short", "short"], [FIRST, SECOND]] {
|
||||
let (code, shown) = at_terminal(&args, &lines);
|
||||
assert_eq!(code, 1, "{shown}");
|
||||
assert!(!ledger_store::passphrase::is_set(&auth).unwrap(), "{shown}");
|
||||
}
|
||||
let (code, shown) = at_terminal(&args, &[FIRST, FIRST]);
|
||||
assert_eq!(code, 0, "{shown}");
|
||||
// A change needs the one in force.
|
||||
let (code, shown) = at_terminal(&args, &["not the passphrase at all"]);
|
||||
assert_eq!(code, 1, "{shown}");
|
||||
assert!(shown.contains("isn't the owner passphrase"), "{shown}");
|
||||
assert!(ledger_store::passphrase::verify(&auth, FIRST).unwrap());
|
||||
let (code, shown) = at_terminal(&args, &[FIRST, SECOND, SECOND]);
|
||||
assert_eq!(code, 0, "{shown}");
|
||||
assert!(!shown.contains(FIRST) && !shown.contains(SECOND), "{shown}");
|
||||
assert!(ledger_store::passphrase::verify(&auth, SECOND).unwrap());
|
||||
assert!(!ledger_store::passphrase::verify(&auth, FIRST).unwrap());
|
||||
let kept = std::fs::read_to_string(auth.join("owner.json")).unwrap();
|
||||
assert!(!kept.contains("staple"), "the text itself was kept");
|
||||
}
|
||||
Reference in new issue
Block a user