use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use std::time::SystemTime;
use lean_ctx_protocol::cloud::effective_policy::EffectivePolicyV1;
use super::cloud_floor;
pub const ANY_AGENT: &str = "*";
type FileStamp = Option<(SystemTime, u64, u64)>;
fn stamp(path: &Path) -> Result<FileStamp, ()> {
match std::fs::metadata(path) {
Ok(meta) => {
#[cfg(unix)]
let ino = std::os::unix::fs::MetadataExt::ino(&meta);
#[cfg(not(unix))]
let ino = 0;
Ok(Some((meta.modified().map_err(|_| ())?, meta.len(), ino)))
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(_) => Err(()),
}
}
struct CachedFloor {
dir: PathBuf,
stamps: (FileStamp, FileStamp),
text: Option<String>,
floor: Result<Option<cloud_floor::Floor>, String>,
}
static FLOOR_CACHE: Mutex<Option<CachedFloor>> = Mutex::new(None);
fn checked_floor(floor_dir: &Path) -> Result<(Option<String>, Option<cloud_floor::Floor>), String> {
let stamps = (
stamp(&floor_dir.join(cloud_floor::FLOOR_FILE)),
stamp(&floor_dir.join(cloud_floor::REQUIRED_FILE)),
);
let stamps = match stamps {
(Ok(a), Ok(b)) => Some((a, b)),
_ => None,
};
let mut cache = FLOOR_CACHE
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let (Some(stamps), Some(hit)) = (stamps, cache.as_ref())
&& hit.dir == floor_dir
&& hit.stamps == stamps
{
return hit.floor.clone().map(|floor| (hit.text.clone(), floor));
}
let (text, required) = cloud_floor::read(floor_dir)?;
let floor = cloud_floor::check(text.as_deref(), required.as_deref());
if let Some(stamps) = stamps {
*cache = Some(CachedFloor {
dir: floor_dir.to_path_buf(),
stamps,
text: text.clone(),
floor: floor.clone(),
});
}
floor.map(|floor| (text, floor))
}
pub(super) fn vault_policy_in(
floor_dir: &Path,
vault_id: &str,
) -> Result<Option<EffectivePolicyV1>, String> {
let (_, floor) = checked_floor(floor_dir)?;
Ok(floor.map(|floor| floor.policy.for_vault(Some(vault_id)).clone()))
}
pub(super) fn policies_in(
floor_dir: &Path,
project: Option<&Path>,
) -> Result<Option<Vec<EffectivePolicyV1>>, String> {
let (text, floor) = checked_floor(floor_dir)?;
let Some(floor) = floor else {
return Ok(None);
};
let policies = match project {
Some(project) => {
let vault = text.as_deref().and_then(|text| {
let cloud_data = floor_dir.parent()?.parent()?;
cloud_floor::vault_for(text, cloud_data, project)
});
vec![floor.policy.for_vault(vault.as_deref()).clone()]
}
None => std::iter::once(floor.policy.effective)
.chain(floor.policy.vaults.into_iter().map(|v| v.effective))
.collect(),
};
Ok(Some(policies))
}
pub fn known_agent(name: &str) -> Option<&'static str> {
let name = name.trim().to_ascii_lowercase();
Some(match name.as_str() {
"claude" | "claude-code" | "claude code" => "claude-code",
"codex" | "codex-cli" | "codex-mcp-client" => "codex",
"cursor" | "cursor-agent" | "cursor-vscode" => "cursor",
"gemini" | "gemini-cli" => "gemini-cli",
"copilot" | "gh copilot" | "github-copilot" => "copilot",
"opencode" => "opencode",
_ => return None,
})
}
pub fn agent_id(client_name: &str) -> String {
if let Some(known) = known_agent(client_name) {
return known.to_string();
}
let id: String = client_name
.trim()
.to_ascii_lowercase()
.chars()
.map(|c| if c == ' ' { '-' } else { c })
.filter(|c| {
c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_' | '.' | ':' | '-')
})
.take(64)
.collect();
let id = id.trim_start_matches(['_', '.', ':', '-']);
if id.is_empty() {
"unknown".to_string()
} else {
id.to_string()
}
}
pub fn agent_refusal_in(
floor_dir: &Path,
project: Option<&Path>,
client_name: &str,
) -> Option<String> {
let policies = match policies_in(floor_dir, project) {
Ok(Some(policies)) => policies,
Ok(None) => return None,
Err(error) => {
return Some(format!(
"{error}; every agent is refused until it is repaired"
));
}
};
let agent = agent_id(client_name);
let refused = policies.iter().any(|policy| {
policy
.agents
.as_ref()
.and_then(|a| a.allowed.as_ref())
.is_some_and(|allowed| !allowed.iter().any(|a| a == ANY_AGENT || *a == agent))
});
refused.then(|| format!("agent {agent:?} is not allowed by the team policy (agents.allowed)"))
}
pub type ProxyRoutingProbe = fn(&str) -> bool;
static PROXY_ROUTING_PROBE: OnceLock<ProxyRoutingProbe> = OnceLock::new();
pub fn set_proxy_routing_probe(probe: ProxyRoutingProbe) {
let _ = PROXY_ROUTING_PROBE.set(probe);
}
fn agent_is_routed(agent: &str) -> bool {
PROXY_ROUTING_PROBE.get().is_some_and(|probe| probe(agent))
}
pub fn proxy_requirement_refusal_in(
floor_dir: &Path,
project: Option<&Path>,
client_name: &str,
routed: impl Fn(&str) -> bool,
) -> Option<String> {
let policies = match policies_in(floor_dir, project) {
Ok(Some(policies)) => policies,
Ok(None) => return None,
Err(error) => {
return Some(format!(
"{error}; every agent is refused until it is repaired"
));
}
};
let required = policies
.iter()
.any(|policy| policy.security.require_proxy == Some(true));
if !required {
return None;
}
let agent = agent_id(client_name);
(!routed(&agent)).then(|| {
format!(
"the team policy requires model traffic through the LeanCTX proxy \
(security.require_proxy) and agent {agent:?} is not routed through it"
)
})
}
const NO_DATA_DIR: &str =
"the LeanCTX data directory cannot be resolved, so the team policy cannot be checked";
pub fn agent_refusal(project: Option<&Path>, client_name: &str) -> Option<String> {
let Some(dir) = cloud_floor::floor_dir() else {
return Some(format!(
"{NO_DATA_DIR}; every agent is refused until it is repaired"
));
};
agent_refusal_in(&dir, project, client_name)
}
pub fn proxy_requirement_refusal(project: Option<&Path>, client_name: &str) -> Option<String> {
let Some(dir) = cloud_floor::floor_dir() else {
return Some(format!(
"{NO_DATA_DIR}; every agent is refused until it is repaired"
));
};
proxy_requirement_refusal_in(&dir, project, client_name, agent_is_routed)
}
pub fn provider_for_host(host: &str) -> Option<&'static str> {
const EXACT: &[(&str, &str)] = &[
("api.anthropic.com", "anthropic"),
("api.openai.com", "openai"),
("chatgpt.com", "openai"),
("generativelanguage.googleapis.com", "google"),
("aiplatform.googleapis.com", "google"),
("api.mistral.ai", "mistral"),
("api.deepseek.com", "deepseek"),
("api.x.ai", "xai"),
("api.groq.com", "groq"),
("openrouter.ai", "openrouter"),
("api.cohere.com", "cohere"),
("api.cohere.ai", "cohere"),
("api.together.xyz", "together"),
("api.fireworks.ai", "fireworks"),
("localhost", "local"),
("127.0.0.1", "local"),
("::1", "local"),
];
const SUFFIX: &[(&str, &str)] = &[
(".openai.azure.com", "azure"),
(".services.ai.azure.com", "azure"),
(".cognitiveservices.azure.com", "azure"),
("-aiplatform.googleapis.com", "google"),
];
if let Some((_, provider)) = EXACT.iter().find(|(h, _)| *h == host) {
return Some(provider);
}
if is_bedrock_runtime_host(host) {
return Some("bedrock");
}
SUFFIX
.iter()
.find(|(suffix, _)| host.len() > suffix.len() && host.ends_with(suffix))
.map(|(_, provider)| *provider)
}
fn is_bedrock_runtime_host(host: &str) -> bool {
let labels: Vec<&str> = host.split('.').collect();
matches!(
labels.as_slice(),
[service, region, "amazonaws", "com"]
if matches!(*service, "bedrock-runtime" | "bedrock-runtime-fips")
&& is_aws_region(region)
)
}
fn is_aws_region(region: &str) -> bool {
let parts: Vec<&str> = region.split('-').collect();
let [country, middle @ .., number] = parts.as_slice() else {
return false;
};
country.len() == 2
&& country.bytes().all(|b| b.is_ascii_lowercase())
&& !middle.is_empty()
&& middle
.iter()
.all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_lowercase()))
&& !number.is_empty()
&& number.bytes().all(|b| b.is_ascii_digit())
}
pub fn upstream_host(upstream_base: &str) -> Option<String> {
let rest = upstream_base.trim();
let rest = rest.split_once("://").map_or(rest, |(_, r)| r);
let authority = rest.split(['/', '\\', '?', '#']).next().unwrap_or("");
let host_port = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
let host = if let Some(v6) = host_port.strip_prefix('[') {
let (inside, _) = v6.split_once(']')?;
if !inside
.chars()
.all(|c| c.is_ascii_hexdigit() || c == ':' || c == '.')
{
return None;
}
inside
} else {
let host = host_port.rsplit_once(':').map_or(host_port, |(h, _)| h);
if host.contains(':') {
return None;
}
host
};
let host = host.trim_end_matches('.').to_ascii_lowercase();
let plain = !host.is_empty()
&& host
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':'));
plain.then_some(host)
}
fn entry_admits(entry: &str, host: &str, provider: Option<&str>) -> bool {
let entry = entry.trim().to_ascii_lowercase();
let entry = entry.trim_end_matches('.');
entry == host || provider.is_some_and(|p| p == entry)
}
pub fn provider_refusal_in(
floor_dir: &Path,
upstream_base: &str,
) -> Result<Option<String>, String> {
let Some(policies) = policies_in(floor_dir, None)? else {
return Ok(None);
};
let host = upstream_host(upstream_base);
let provider = host.as_deref().and_then(provider_for_host);
let refused = policies.iter().any(|policy| {
policy
.security
.allowed_providers
.as_ref()
.is_some_and(|allowed| {
host.as_deref()
.is_none_or(|h| !allowed.iter().any(|e| entry_admits(e, h, provider)))
})
});
Ok(refused.then(|| {
let shown = host.as_deref().unwrap_or("an unparsable upstream");
match provider {
Some(p) => format!(
"provider {p:?} ({shown}) is not allowed by the team policy (security.allowed_providers)"
),
None => format!(
"host {shown:?} is not a known model provider and is not listed in the team policy (security.allowed_providers)"
),
}
}))
}
pub fn provider_refusal(upstream_base: &str) -> Result<Option<String>, String> {
let Some(dir) = cloud_floor::floor_dir() else {
return Err(NO_DATA_DIR.to_string());
};
provider_refusal_in(&dir, upstream_base)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[allow(clippy::needless_pass_by_value)]
fn install(floor_dir: &Path, effective: serde_json::Value, vaults: serde_json::Value) {
std::fs::create_dir_all(floor_dir).unwrap();
let floor = json!({
"schema": cloud_floor::FLOOR_SCHEMA, "version": 3,
"digest": format!("sha256:{}", "a".repeat(64)),
"not_after": "2099-01-01T00:00:00Z",
"policy": {"effective": effective, "vaults": vaults}
});
std::fs::write(floor_dir.join(cloud_floor::FLOOR_FILE), floor.to_string()).unwrap();
std::fs::write(
floor_dir.join(cloud_floor::REQUIRED_FILE),
r#"{"version":3}"#,
)
.unwrap();
}
#[allow(clippy::needless_pass_by_value)]
fn policy(security: serde_json::Value, agents: Option<serde_json::Value>) -> serde_json::Value {
let mut p = json!({
"schema": "leanctx.cloud.policy/v1",
"security": security, "context": {}, "sharing": {}
});
if let Some(agents) = agents {
p["agents"] = agents;
}
p
}
#[test]
fn missing_data_dir_refusal_names_the_team_policy() {
assert!(NO_DATA_DIR.contains("the team policy cannot be checked"));
assert!(!NO_DATA_DIR.contains("Cloud"));
}
#[test]
fn agent_ids_come_from_the_host_table_or_the_name_form() {
assert_eq!(agent_id("claude-code"), "claude-code");
assert_eq!(agent_id(" Claude Code "), "claude-code");
assert_eq!(agent_id("codex-mcp-client"), "codex");
assert_eq!(agent_id("Cursor-VSCode"), "cursor");
assert_eq!(agent_id("Zed Editor"), "zed-editor");
assert_eq!(agent_id("acp:zed"), "acp:zed");
assert_eq!(agent_id("-ünï-"), "n-");
assert_eq!(agent_id(""), "unknown");
assert_eq!(agent_id("€€"), "unknown");
assert_eq!(agent_id(&"x".repeat(100)).len(), 64);
}
#[test]
fn allowed_agents_admit_listed_ids_and_the_wildcard() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
assert_eq!(agent_refusal_in(&floor_dir, None, "anything"), None);
install(
&floor_dir,
policy(
json!({}),
Some(json!({"allowed": ["claude-code", "codex"]})),
),
json!([]),
);
assert_eq!(
agent_refusal_in(&floor_dir, Some(dir.path()), "claude-code"),
None
);
assert_eq!(agent_refusal_in(&floor_dir, None, "codex-cli"), None);
let refusal = agent_refusal_in(&floor_dir, None, "Cursor").unwrap();
assert!(
refusal.contains("\"cursor\"") && refusal.contains("agents.allowed"),
"{refusal}"
);
install(
&floor_dir,
policy(json!({}), Some(json!({"allowed": ["*"]}))),
json!([]),
);
assert_eq!(agent_refusal_in(&floor_dir, None, "Cursor"), None);
install(
&floor_dir,
policy(json!({}), Some(json!({"allowed": []}))),
json!([]),
);
assert!(agent_refusal_in(&floor_dir, None, "claude-code").is_some());
install(
&floor_dir,
policy(json!({"pii": "redact"}), None),
json!([]),
);
assert_eq!(agent_refusal_in(&floor_dir, None, "Cursor"), None);
}
#[test]
fn without_a_project_every_vault_policy_applies() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(
&floor_dir,
policy(json!({"allowed_providers": ["anthropic", "openai"]}), None),
json!([{
"vault_id": "0".repeat(32),
"effective": policy(json!({"allowed_providers": ["anthropic"]}), Some(json!({"allowed": ["codex"]})))
}]),
);
assert_eq!(
provider_refusal_in(&floor_dir, "https://api.anthropic.com").unwrap(),
None
);
let refusal = provider_refusal_in(&floor_dir, "https://api.openai.com/v1")
.unwrap()
.unwrap();
assert!(refusal.contains("\"openai\""), "{refusal}");
assert!(
provider_refusal_in(&floor_dir, "https://foundry.example.com")
.unwrap()
.is_some()
);
assert_eq!(
agent_refusal_in(&floor_dir, Some(dir.path()), "cursor"),
None
);
assert!(agent_refusal_in(&floor_dir, None, "cursor").is_some());
}
#[test]
fn a_damaged_floor_refuses_every_agent_and_provider() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(&floor_dir, policy(json!({}), None), json!([]));
std::fs::write(floor_dir.join(cloud_floor::FLOOR_FILE), "{").unwrap();
assert!(agent_refusal_in(&floor_dir, None, "claude-code").is_some());
assert!(provider_refusal_in(&floor_dir, "https://api.anthropic.com").is_err());
std::fs::remove_file(floor_dir.join(cloud_floor::FLOOR_FILE)).unwrap();
assert!(agent_refusal_in(&floor_dir, None, "claude-code").is_some());
assert!(provider_refusal_in(&floor_dir, "https://api.anthropic.com").is_err());
}
#[test]
fn upstream_hosts_parse_as_an_http_client_resolves_them() {
assert_eq!(
upstream_host("https://api.openai.com/v1").as_deref(),
Some("api.openai.com")
);
assert_eq!(
upstream_host("HTTPS://API.Anthropic.com.:443").as_deref(),
Some("api.anthropic.com")
);
assert_eq!(
upstream_host("http://api.openai.com@evil.host:8080/x").as_deref(),
Some("evil.host")
);
assert_eq!(
upstream_host("https://evil.com\\@api.openai.com").as_deref(),
Some("evil.com")
);
assert_eq!(upstream_host("http://[::1]:11434").as_deref(), Some("::1"));
assert_eq!(
upstream_host("api.mistral.ai").as_deref(),
Some("api.mistral.ai")
);
assert_eq!(upstream_host("https://api.openai.com%2eevil.host"), None);
assert_eq!(upstream_host("https://"), None);
assert_eq!(upstream_host("http://::1:11434"), None);
assert_eq!(upstream_host("http://evil.host:x:443"), None);
assert_eq!(provider_for_host("api.openai.com"), Some("openai"));
assert_eq!(provider_for_host("team.openai.azure.com"), Some("azure"));
assert_eq!(provider_for_host("openai.azure.com"), None);
assert_eq!(provider_for_host("api.openai.com.evil.host"), None);
assert_eq!(provider_for_host("evilapi.openai.com"), None);
}
#[test]
fn bedrock_is_only_the_regional_runtime_host() {
for host in [
"bedrock-runtime.us-east-1.amazonaws.com",
"bedrock-runtime-fips.us-east-1.amazonaws.com",
"bedrock-runtime.eu-central-1.amazonaws.com",
"bedrock-runtime.us-gov-west-1.amazonaws.com",
] {
assert_eq!(provider_for_host(host), Some("bedrock"), "{host}");
}
for host in [
"bedrock-runtime.mybucket.s3.amazonaws.com",
"bedrock-runtime.eu-central-1.extra.amazonaws.com",
"bedrock-runtime..amazonaws.com",
"bedrock-runtime.amazonaws.com",
"bedrock-runtime.mybucket.amazonaws.com",
"bedrock-runtime.us-1.amazonaws.com",
"bedrock-runtime.us-east-.amazonaws.com",
"bedrock-runtime.usa-east-1.amazonaws.com",
"bedrock.us-east-1.amazonaws.com",
] {
assert_eq!(provider_for_host(host), None, "{host}");
}
}
#[test]
fn the_provider_comes_from_the_upstream_host_not_from_local_names() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(
&floor_dir,
policy(
json!({"allowed_providers": ["anthropic", "llm.example.com"]}),
None,
),
json!([]),
);
let refusal = provider_refusal_in(&floor_dir, "https://evil.host/v1")
.unwrap()
.unwrap();
assert!(refusal.contains("\"evil.host\""), "{refusal}");
assert!(
provider_refusal_in(&floor_dir, "https://anthropic.proxy.attacker.net")
.unwrap()
.is_some()
);
assert!(
provider_refusal_in(&floor_dir, "https://api.anthropic.com.attacker.net")
.unwrap()
.is_some()
);
assert_eq!(
provider_refusal_in(&floor_dir, "https://api.anthropic.com").unwrap(),
None
);
assert_eq!(
provider_refusal_in(&floor_dir, "https://LLM.example.com:8443/v1").unwrap(),
None
);
assert!(
provider_refusal_in(&floor_dir, "http://127.0.0.1:11434")
.unwrap()
.is_some()
);
assert!(
provider_refusal_in(&floor_dir, "not a url at all")
.unwrap()
.is_some()
);
}
#[test]
fn require_proxy_refuses_an_agent_that_is_not_routed() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(&floor_dir, policy(json!({}), None), json!([]));
assert_eq!(
proxy_requirement_refusal_in(&floor_dir, None, "cursor", |_| false),
None
);
install(
&floor_dir,
policy(json!({"require_proxy": false}), None),
json!([{
"vault_id": "0".repeat(32),
"effective": policy(json!({"require_proxy": true}), None)
}]),
);
let routed = |agent: &str| agent == "claude-code";
assert_eq!(
proxy_requirement_refusal_in(&floor_dir, None, "Claude Code", routed),
None
);
let refusal =
proxy_requirement_refusal_in(&floor_dir, None, "codex-mcp-client", routed).unwrap();
assert!(refusal.contains("security.require_proxy"), "{refusal}");
assert!(refusal.contains("\"codex\""), "{refusal}");
assert_eq!(
proxy_requirement_refusal_in(&floor_dir, Some(dir.path()), "codex", |_| false),
None
);
}
#[test]
fn require_proxy_fails_closed_without_a_floor_or_a_probe() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(&floor_dir, policy(json!({}), None), json!([]));
std::fs::write(floor_dir.join(cloud_floor::FLOOR_FILE), "{").unwrap();
assert!(proxy_requirement_refusal_in(&floor_dir, None, "claude-code", |_| true).is_some());
assert!(PROXY_ROUTING_PROBE.get().is_none());
assert!(!agent_is_routed("claude-code"));
}
#[test]
fn the_parsed_floor_is_cached_until_a_file_changes() {
let dir = tempfile::tempdir().unwrap();
let floor_dir = dir.path().join("cloud").join("policy").join("effective");
install(
&floor_dir,
policy(json!({}), Some(json!({"allowed": ["codex"]}))),
json!([]),
);
assert!(agent_refusal_in(&floor_dir, None, "cursor").is_some());
assert!(agent_refusal_in(&floor_dir, None, "cursor").is_some());
let tmp = floor_dir.join("floor.json.tmp");
let floor = json!({
"schema": cloud_floor::FLOOR_SCHEMA, "version": 3,
"digest": format!("sha256:{}", "a".repeat(64)),
"not_after": "2099-01-01T00:00:00Z",
"policy": {"effective": policy(json!({}), Some(json!({"allowed": ["cursor"]}))), "vaults": []}
});
std::fs::write(&tmp, floor.to_string()).unwrap();
std::fs::rename(&tmp, floor_dir.join(cloud_floor::FLOOR_FILE)).unwrap();
assert_eq!(agent_refusal_in(&floor_dir, None, "cursor"), None);
std::fs::write(floor_dir.join(cloud_floor::FLOOR_FILE), "{").unwrap();
assert!(agent_refusal_in(&floor_dir, None, "cursor").is_some());
}
}