use std::path::{Path, PathBuf};
use super::{Code, Failure};
pub(crate) const HEADER: &str = "x-leanctx-app-key";
pub(crate) const FILE: &str = lean_ctx::core::io_boundary::APP_KEY_FILE;
pub(crate) const APP_ONLY: &[&str] = &[
super::decisions_part::SEND,
super::decisions_part::SUPERSEDE,
super::decisions_part::REVOKE,
super::tasks_part::PERSON_PROPOSE,
super::tasks_part::PERSON_PREPARE,
super::unlink_part::KIND,
super::pickup_part::STATUS,
super::pickup_part::SET,
super::pickup_part::KEY,
super::chat_sign::PREPARE,
];
pub(crate) fn guards(kind: &str) -> bool {
APP_ONLY.contains(&kind)
}
pub(crate) fn path() -> Option<PathBuf> {
lean_ctx::core::data_dir::lean_ctx_data_dir()
.ok()
.map(|dir| dir.join("agents").join(FILE))
}
const MAX_FILE: u64 = 65;
pub(crate) fn admits_in(file: &Path, given: Option<&[u8]>) -> bool {
let Some(given) = given else {
return false;
};
let Some(held) = read_held(file) else {
return false;
};
let held = held.trim_ascii();
if !is_key(held) {
return false;
}
constant_time_eq(held, given.trim_ascii())
}
pub(crate) fn admits(given: Option<&[u8]>) -> bool {
path().is_some_and(|file| admits_in(&file, given))
}
pub(crate) fn refusal() -> Failure {
Failure::new(
Code::AppOnly,
"only LeanCTX Code sends this, after the person confirms it",
)
}
pub(crate) const ENV: &str = "LEAN_CTX_APP_KEY";
pub(crate) fn take_env_key() -> Option<Vec<u8>> {
let given = std::env::var_os(ENV);
unsafe { std::env::remove_var(ENV) };
given.map(|given| given.to_string_lossy().into_owned().into_bytes())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AppRoot {
pub(crate) config: PathBuf,
pub(crate) data: PathBuf,
}
impl AppRoot {
pub(crate) fn key_file(&self) -> PathBuf {
self.data.join("agents").join(FILE)
}
pub(crate) fn store(&self) -> lean_ctx_cloud::CloudStore {
lean_ctx_cloud::CloudStore::at(self.config.join("cloud"), self.data.join("cloud"))
}
}
pub(crate) fn app_only_gate(
args: &[String],
flags: &[&str],
given: Option<&[u8]>,
layout: impl FnOnce() -> Option<AppRoot>,
admits: impl FnOnce(&Path, Option<&[u8]>) -> bool,
) -> Result<Option<AppRoot>, Failure> {
if !args.iter().any(|a| flags.contains(&a.as_str())) {
return Ok(None);
}
let root = layout().ok_or_else(refusal)?;
if admits(&root.key_file(), given) {
Ok(Some(root))
} else {
Err(refusal())
}
}
pub(crate) fn checked_root() -> Option<AppRoot> {
let config = lean_ctx::core::paths::config_dir_read_only().ok()?;
let data = lean_ctx::core::data_dir::lean_ctx_data_dir().ok()?;
let home = dirs::home_dir();
let data_pin = std::env::var_os("LEAN_CTX_DATA_DIR")
.map(PathBuf::from)
.filter(|pin| !pin.as_os_str().to_string_lossy().trim().is_empty());
let mut standard_configs: Vec<PathBuf> = home
.iter()
.map(|home| home.join(".config").join("lean-ctx"))
.collect();
if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME").filter(|x| !x.is_empty()) {
standard_configs.push(PathBuf::from(xdg).join("lean-ctx"));
}
checked_root_at(
&config,
&data,
home.as_deref(),
data_pin.as_deref(),
&standard_configs,
)
}
fn checked_root_at(
config: &Path,
data: &Path,
home: Option<&Path>,
data_pin: Option<&Path>,
standard_configs: &[PathBuf],
) -> Option<AppRoot> {
if !plain_child(config, "cloud") || !plain_child(data, "agents") {
return None;
}
let (config, data) = (real(config), real(data));
if data_pin.is_some() && standard_configs.iter().any(|c| real(c) == data) {
return None;
}
let split = home.map(real).is_some_and(|home| {
config == home.join(".config").join("lean-ctx")
&& data == home.join(".local").join("share").join("lean-ctx")
});
(config == data || split).then_some(AppRoot { config, data })
}
fn real(path: &Path) -> PathBuf {
let mut rest = Vec::new();
let mut at = path;
loop {
if let Ok(found) = std::fs::canonicalize(at) {
return rest.iter().rev().fold(found, |dir, name| dir.join(name));
}
match (at.parent(), at.file_name()) {
(Some(parent), Some(name)) => {
rest.push(name.to_owned());
at = parent;
}
_ => return path.to_path_buf(),
}
}
}
fn plain_child(dir: &Path, name: &str) -> bool {
let child = dir.join(name);
match std::fs::symlink_metadata(&child) {
Ok(meta) => !meta.file_type().is_symlink(),
Err(_) => true,
}
}
#[cfg(unix)]
fn read_held(file: &Path) -> Option<Vec<u8>> {
use std::io::Read as _;
use std::os::unix::fs::{MetadataExt as _, OpenOptionsExt as _};
let opened = std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK)
.open(file)
.ok()?;
let meta = opened.metadata().ok()?;
let me = unsafe { libc::geteuid() };
if !meta.file_type().is_file()
|| meta.mode() & 0o7777 != 0o600
|| meta.uid() != me
|| meta.len() > MAX_FILE
{
return None;
}
let mut held = Vec::new();
opened.take(MAX_FILE + 1).read_to_end(&mut held).ok()?;
(held.len() as u64 <= MAX_FILE).then_some(held)
}
#[cfg(not(unix))]
fn read_held(file: &Path) -> Option<Vec<u8>> {
use std::io::Read as _;
let meta = std::fs::symlink_metadata(file).ok()?;
if lean_ctx::core::pathutil::is_symlink_or_reparse(&meta)
|| !meta.file_type().is_file()
|| meta.len() > MAX_FILE
{
return None;
}
let mut held = Vec::new();
std::fs::File::open(file)
.ok()?
.take(MAX_FILE + 1)
.read_to_end(&mut held)
.ok()?;
(held.len() as u64 <= MAX_FILE).then_some(held)
}
fn is_key(key: &[u8]) -> bool {
key.len() == 64 && key.iter().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
use subtle::ConstantTimeEq;
a.len() == b.len() && bool::from(a.ct_eq(b))
}
#[cfg(test)]
mod tests {
use super::*;
const KEY: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
fn key_file(content: &str) -> (tempfile::TempDir, PathBuf) {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join(FILE);
std::fs::write(&file, content).unwrap();
private(&file);
(dir, file)
}
#[cfg(unix)]
fn private(file: &Path) {
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(file, std::fs::Permissions::from_mode(0o600)).unwrap();
}
#[cfg(not(unix))]
fn private(_file: &Path) {}
#[cfg(unix)]
#[test]
fn a_key_file_that_is_not_the_persons_private_file_admits_nothing() {
use std::os::unix::fs::PermissionsExt as _;
let (_dir, file) = key_file(KEY);
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();
assert!(!admits_in(&file, Some(KEY.as_bytes())), "0644");
let (dir, target) = key_file(KEY);
let link = dir.path().join("link.key");
std::os::unix::fs::symlink(&target, &link).unwrap();
assert!(!admits_in(&link, Some(KEY.as_bytes())), "symlink");
let (_dir, file) = key_file(&format!("{KEY}\n\n\n"));
assert!(!admits_in(&file, Some(KEY.as_bytes())), "oversize");
}
#[cfg(unix)]
#[test]
fn a_fifo_in_place_of_the_key_file_is_refused_without_waiting() {
let dir = tempfile::tempdir().unwrap();
let fifo = dir.path().join(FILE);
let c = std::ffi::CString::new(fifo.to_string_lossy().as_bytes()).unwrap();
assert_eq!(unsafe { libc::mkfifo(c.as_ptr(), 0o600) }, 0);
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(admits_in(&fifo, Some(KEY.as_bytes())));
});
let admitted = rx
.recv_timeout(std::time::Duration::from_secs(5))
.expect("a FIFO never blocks the check");
assert!(!admitted);
}
#[test]
fn only_the_held_key_is_admitted() {
let (_dir, file) = key_file(&format!("{KEY}\n"));
assert!(admits_in(&file, Some(KEY.as_bytes())));
assert!(!admits_in(&file, None), "no header");
assert!(!admits_in(&file, Some(b"")), "an empty header");
let other = KEY.replace('0', "1");
assert!(!admits_in(&file, Some(other.as_bytes())), "another key");
assert!(!admits_in(&file, Some(&KEY.as_bytes()[..63])), "a prefix");
}
#[test]
fn without_a_key_file_nothing_is_admitted() {
let dir = tempfile::tempdir().unwrap();
assert!(!admits_in(&dir.path().join(FILE), Some(KEY.as_bytes())));
for held in ["", "\n", "short", &KEY.to_uppercase()] {
let (_dir, file) = key_file(held);
assert!(!admits_in(&file, Some(held.as_bytes())), "{held:?}");
}
}
#[test]
fn app_only_flags_need_one_root_for_the_key_file_and_the_records() {
let base = tempfile::tempdir().unwrap();
let home = base.path().join("home");
let config = home.join(".config").join("lean-ctx");
let data = home.join(".local").join("share").join("lean-ctx");
let own = base.path().join("agent-tmp");
for dir in [&config, &data, &own] {
std::fs::create_dir_all(dir).unwrap();
}
let standard = [config.clone()];
let at = |c: &Path, d: &Path, h: Option<&Path>| checked_root_at(c, d, h, None, &standard);
let split = at(&config, &data, Some(&home)).expect("default split");
assert_eq!(split.config, real(&config));
assert_eq!(split.data, real(&data));
assert!(at(&own, &own, None).is_some());
assert!(at(&config, &own, Some(&home)).is_none());
assert!(at(&config, &own.join("d"), Some(&own)).is_none());
assert!(checked_root_at(&config, &config, Some(&home), Some(&config), &standard).is_none());
#[cfg(unix)]
{
std::os::unix::fs::symlink(&config, own.join("cloud")).unwrap();
assert!(at(&own, &own, None).is_none());
let fake = base.path().join("fake");
std::fs::create_dir_all(fake.join(".local/share/lean-ctx")).unwrap();
std::os::unix::fs::symlink(home.join(".config"), fake.join(".config")).unwrap();
assert!(
at(
&fake.join(".config/lean-ctx"),
&fake.join(".local/share/lean-ctx"),
Some(&fake)
)
.is_none()
);
}
let new = base.path().join("new").join("lean-ctx");
assert_eq!(real(&new), real(base.path()).join("new").join("lean-ctx"));
}
#[cfg(not(windows))]
#[test]
fn the_checked_root_follows_the_variables_once() {
use lean_ctx::test_env::{VarGuard, set_var};
let _env = lean_ctx::core::data_dir::test_env_lock();
let base = tempfile::tempdir().unwrap();
let home = base.path().join("home");
let config = home.join(".config").join("lean-ctx");
let data = home.join(".local").join("share").join("lean-ctx");
let own = base.path().join("agent-tmp");
for dir in [&config, &data, &own] {
std::fs::create_dir_all(dir).unwrap();
}
let _home = VarGuard::set("HOME", &home);
let _xdg = VarGuard::set("XDG_CONFIG_HOME", "");
let _config_dir = VarGuard::set("LEAN_CTX_CONFIG_DIR", &config);
let _data_dir = VarGuard::set("LEAN_CTX_DATA_DIR", &data);
let root = checked_root().expect("default split");
assert_eq!(root.data, real(&data));
set_var("LEAN_CTX_DATA_DIR", &own);
assert!(checked_root().is_none(), "split roots");
set_var("LEAN_CTX_DATA_DIR", &config);
assert!(checked_root().is_none(), "collapsed onto the records");
let fresh = own.join("fresh");
set_var("LEAN_CTX_CONFIG_DIR", &fresh);
set_var("LEAN_CTX_DATA_DIR", &fresh);
let root = checked_root().expect("one directory");
assert_eq!(root.config, real(&own).join("fresh"));
assert_eq!(root.key_file(), real(&own).join("fresh/agents").join(FILE));
set_var("LEAN_CTX_DATA_DIR", &data);
set_var("LEAN_CTX_CONFIG_DIR", &config);
let store = root.store();
assert_eq!(store.config_dir(), real(&own).join("fresh/cloud"));
assert_eq!(store.data_dir(), real(&own).join("fresh/cloud"));
}
#[test]
fn the_app_only_gate_checks_the_layout_before_the_key() {
let args = |a: &[&str]| a.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>();
let flags = &["--chat-key", "--print-instance-key"];
let key = Some(KEY.as_bytes());
let root = AppRoot {
config: PathBuf::from("/r"),
data: PathBuf::from("/r"),
};
let one_root = || Some(root.clone());
let right = |file: &Path, given: Option<&[u8]>| {
file == root.key_file() && given == Some(KEY.as_bytes())
};
assert_eq!(
app_only_gate(&args(&["--json"]), flags, None, || None, |_, _| false),
Ok(None)
);
assert_eq!(
app_only_gate(&args(&["--chat-key", "-"]), flags, key, one_root, right),
Ok(Some(root.clone()))
);
let mut asked = false;
let refused = app_only_gate(
&args(&["--chat-key", "-"]),
flags,
key,
|| None,
|f, g| {
asked = true;
right(f, g)
},
)
.unwrap_err();
assert_eq!(refused.code.as_str(), "app_only");
assert!(!asked, "the key file is not even read");
assert!(
app_only_gate(
&args(&["--print-instance-key"]),
flags,
None,
one_root,
right
)
.is_err()
);
}
#[test]
fn the_env_key_is_taken_once_and_removed() {
let _env = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::set_var(ENV, KEY);
assert_eq!(take_env_key().as_deref(), Some(KEY.as_bytes()));
assert!(std::env::var_os(ENV).is_none());
assert_eq!(take_env_key(), None);
}
#[test]
fn the_send_to_agents_route_is_app_only() {
assert!(guards(super::super::decisions_part::SEND));
assert!(guards(super::super::decisions_part::SUPERSEDE));
assert!(guards(super::super::decisions_part::REVOKE));
assert!(guards(super::super::unlink_part::KIND));
assert!(!guards(super::super::decisions_part::KIND));
assert!(!guards(super::super::decisions_part::CREATE));
assert_eq!(refusal().code.as_str(), "app_only");
}
#[test]
fn chat_prepare_is_app_only_and_the_signed_sends_are_not() {
assert!(guards(super::super::chat_sign::PREPARE));
for kind in ["chat.post", "chat.dm", "bus.post"] {
assert!(!guards(kind), "{kind}");
}
}
}