use std::cell::RefCell;
use super::{Grant, GrantSource, Role, base_region, grant_matchers};
thread_local! {
static EXTRA: RefCell<Vec<Grant>> = const { RefCell::new(Vec::new()) };
}
pub(super) fn with_extra<T>(f: impl FnOnce(&[Grant]) -> T) -> T {
EXTRA.with(|extra| f(&extra.borrow()))
}
pub(crate) fn with_hypothetical_grant<T>(path: &str, read: bool, write: bool, f: impl FnOnce() -> T) -> T {
struct Clear;
impl Drop for Clear {
fn drop(&mut self) {
EXTRA.with(|extra| extra.borrow_mut().clear());
}
}
let grants = grant_matchers(path)
.into_iter()
.map(|matcher| Grant { matcher, read, write, source: GrantSource::UserConfig });
EXTRA.with(|extra| *extra.borrow_mut() = grants.collect());
let _clear = Clear;
f()
}
pub(crate) fn ungranted_role(path: &str) -> Role {
base_region(&super::super::locus::canonicalize(path))
}
pub(crate) fn claude_read_grants(settings_json: &str) -> Vec<String> {
super::claude_read_grant_paths(settings_json)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::resolve::write_target_verdict;
#[test]
fn a_hypothetical_grant_widens_only_while_it_is_asked_about() {
let home = crate::process_home::var().unwrap_or_else(|_| "/home/tester".to_string());
let path = format!("{home}/hypothetical-probe/notes.txt");
assert!(!write_target_verdict(&path).is_allowed(), "the probe must start refused");
let inside =
with_hypothetical_grant(&format!("{home}/hypothetical-probe"), false, true, || write_target_verdict(&path).is_allowed());
assert!(inside, "the grant must apply while it is asked about");
assert!(!write_target_verdict(&path).is_allowed(), "the grant outlived its question");
}
#[test]
fn a_hypothetical_grant_is_cleared_after_a_panic() {
let home = crate::process_home::var().unwrap_or_else(|_| "/home/tester".to_string());
let path = format!("{home}/hypothetical-probe/notes.txt");
let unwound =
std::panic::catch_unwind(|| with_hypothetical_grant(&format!("{home}/hypothetical-probe"), true, true, || panic!("boom")));
assert!(unwound.is_err());
assert!(!write_target_verdict(&path).is_allowed(), "a panic left the grant in place");
}
#[test]
fn the_ungranted_role_ignores_grants() {
let home = crate::process_home::var().unwrap_or_else(|_| "/home/tester".to_string());
let ssh = format!("{home}/.ssh/id_rsa");
assert!(ungranted_role(&ssh).reads_secret);
let still = with_hypothetical_grant(&format!("{home}/.ssh"), true, true, || ungranted_role(&ssh).reads_secret);
assert!(still, "a grant changed the role it is measured against");
}
}