H: pin the token mode check for every group/world bit
Cover 0620, 0602, 0610 and 0601 in the loader and assert that startup validation and the per-request load give the same reason for each.
This commit is contained in:
1 parent
70af820838
commit
a09c64de9a
2 files changed
+54
No files matched your search
@@ -242,6 +242,19 @@ mod tests {
|
|||||||
assert!(!logs.contains("SHOULD_NOT_LEAK"), "{logs}");
|
assert!(!logs.contains("SHOULD_NOT_LEAK"), "{logs}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn group_or_world_write_and_execute_bits_are_rejected_too() {
|
||||||
|
// Any group or world access lets another user read the token (r, x on
|
||||||
|
// a readable file) or swap it (w). Each mode below sets a different
|
||||||
|
// bit, so a mask that misses one bit still passes the others.
|
||||||
|
let dir = TempDir::new("peer-secret");
|
||||||
|
for mode in [0o620, 0o602, 0o610, 0o601] {
|
||||||
|
let path = dir.write(&format!("token-{mode:o}"), b"pending\n", mode);
|
||||||
|
let err = token_err(load_token_at(&peer(path), None).unwrap_err());
|
||||||
|
assert_eq!(err, "group or world accessible", "{mode:o}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_symlink_cannot_borrow_the_credentials_directory_exemption() {
|
fn a_symlink_cannot_borrow_the_credentials_directory_exemption() {
|
||||||
let dir = TempDir::new("peer-secret");
|
let dir = TempDir::new("peer-secret");
|
||||||
|
|||||||
@@ -805,6 +805,47 @@ fn config_load_uses_the_token_loader_and_rereads_a_rotated_file() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The token mode must be judged the same by the startup check and by every
|
||||||
|
/// request that reloads it; group-write-only and execute bits once passed
|
||||||
|
/// startup and then failed each request.
|
||||||
|
#[test]
|
||||||
|
#[allow(clippy::result_large_err)] // figment::Jail closures return figment::Error
|
||||||
|
fn a_loose_token_fails_startup_and_per_request_checks_alike() {
|
||||||
|
Jail::expect_with(|jail| {
|
||||||
|
for mode in [0o620, 0o602, 0o610, 0o601] {
|
||||||
|
let name = format!("mode-{mode:o}.toml");
|
||||||
|
let token = jail.directory().join(format!("token-{mode:o}"));
|
||||||
|
std::fs::write(&token, "pending\n").unwrap();
|
||||||
|
std::fs::set_permissions(&token, std::fs::Permissions::from_mode(mode)).unwrap();
|
||||||
|
jail.create_file(
|
||||||
|
&name,
|
||||||
|
&format!(
|
||||||
|
"[peers]\nenabled = true\n[[peers.peer]]\nname = \"beta\"\nurl = \"http://127.0.0.1:1984\"\ntoken_file = \"{}\"\n",
|
||||||
|
token.display()
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
let cfg = Config::load(Some(Path::new(&name)), &CliOverrides::default())
|
||||||
|
.expect("the mode is a readiness check, not a parse error");
|
||||||
|
|
||||||
|
let startup = cfg
|
||||||
|
.validate_token_readiness()
|
||||||
|
.expect_err("group/world access must fail startup")
|
||||||
|
.to_string();
|
||||||
|
let per_request = match skyg_linux_tracker::peers::load_token(&cfg.peers.peer[0]) {
|
||||||
|
Err(PeerError::TokenFile(reason)) => reason,
|
||||||
|
other => panic!("{mode:o}: expected a token-file error, got {other:?}"),
|
||||||
|
};
|
||||||
|
assert_eq!(per_request, "group or world accessible", "{mode:o}");
|
||||||
|
assert!(
|
||||||
|
startup.contains(&format!("token_file {}: {per_request}", token.display())),
|
||||||
|
"{mode:o}: {startup}"
|
||||||
|
);
|
||||||
|
assert!(!startup.contains("pending"), "{mode:o}: {startup}");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn ping_without_a_token_is_unauthorized() {
|
async fn ping_without_a_token_is_unauthorized() {
|
||||||
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
||||||
|
|||||||
Reference in new issue
Block a user