use std::{
io::Read,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::{
auth::storage::{Storage, Tokens},
platform, repository,
types::Timestamp,
};
use super::AgentId;
const TRUST_FILE: &str = "trust.json";
const TRUST_LOCK: &str = "trust";
const HASH_CHUNK_BYTES: usize = 64 * 1024;
const TRUST_SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum TrustScope {
GitRepo {
#[serde(with = "crate::platform::path_serde")]
common_dir: PathBuf,
},
Directory {
#[serde(with = "crate::platform::path_serde")]
canonical_path: PathBuf,
},
}
impl TrustScope {
pub fn for_worktree(worktree: &Path, identity: &repository::Identity) -> Result<Self, String> {
let canonical =
crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
if let Some(common_dir) = &identity.common_dir {
let common = crate::workspace::canonical_target(common_dir)
.map_err(|error| error.to_string())?;
Ok(Self::GitRepo { common_dir: common })
} else {
Ok(Self::Directory {
canonical_path: canonical,
})
}
}
pub fn display_label(&self) -> String {
match self {
Self::GitRepo { common_dir } => {
format!("Git repository ({})", platform::path_text(common_dir))
}
Self::Directory { canonical_path } => platform::path_text(canonical_path),
}
}
pub fn disclosure(&self) -> &'static str {
match self {
Self::GitRepo { .. } => {
"Trusting a Git repository covers all branches and worktrees of that repository; checking out an untrusted branch will not re-prompt."
}
Self::Directory { .. } => {
"Trusting this directory allows the ACP agent to run natively inside it and read any local configuration files before prompting."
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LauncherFingerprint {
#[serde(with = "crate::platform::path_serde")]
pub resolved_executable: PathBuf,
pub executable_sha256: String,
pub args_hash: String,
}
impl LauncherFingerprint {
pub fn compute(resolved_executable: &Path, args: &[String]) -> Result<Self, String> {
let canonical = platform::canonicalize(resolved_executable).map_err(|error| {
format!(
"Cannot resolve ACP executable {}: {error}.",
platform::path_text(resolved_executable)
)
})?;
let executable_sha256 = hash_executable(&canonical)?;
let args_hash = hash_args(args);
Ok(Self {
resolved_executable: canonical,
executable_sha256,
args_hash,
})
}
pub fn runtime_fingerprint(&self, auth_generation: u64) -> String {
let mut hasher = Sha256::new();
hasher.update(platform::path_text(&self.resolved_executable).as_bytes());
hasher.update([0]);
hasher.update(self.executable_sha256.as_bytes());
hasher.update([0]);
hasher.update(self.args_hash.as_bytes());
hasher.update([0]);
hasher.update(auth_generation.to_le_bytes());
hex_digest(hasher.finalize().as_slice())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustRecord {
pub agent_id: AgentId,
pub scope: TrustScope,
pub fingerprint: LauncherFingerprint,
pub granted_at: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustRequirement {
pub agent_id: AgentId,
pub agent_name: String,
pub scope: TrustScope,
pub fingerprint: LauncherFingerprint,
pub disclosure: String,
pub reason: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SavedTrust {
version: u32,
#[serde(default)]
entries: Vec<TrustRecord>,
}
impl Default for SavedTrust {
fn default() -> Self {
Self {
version: TRUST_SCHEMA_VERSION,
entries: Vec::new(),
}
}
}
pub fn resolve_executable(
program: &str,
worktree: Option<&Path>,
reject_inside_worktree: bool,
) -> Result<PathBuf, String> {
let probe_dir =
platform::private_tempdir_in("artifactize-resolve-", &platform::temporary_directory())
.map_err(|error| format!("Cannot create executable resolution tempdir: {error}."))?;
let program_path = platform::program_path(program)
.map_err(|error| format!("Invalid ACP program path {program:?}: {error}."))?;
let resolved = platform::program::resolve(&program_path, probe_dir.path())
.map_err(|error| format!("Cannot find ACP executable {program:?}: {error}."))?;
let canonical = platform::canonicalize(&resolved).map_err(|error| {
format!(
"Cannot canonicalize ACP executable {}: {error}.",
platform::path_text(&resolved)
)
})?;
if reject_inside_worktree && let Some(worktree) = worktree {
let canonical_worktree =
crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
if platform::is_within(&canonical, &canonical_worktree) {
return Err(format!(
"Refusing to launch built-in ACP preset executable {} located inside the workspace {}.",
platform::path_text(&canonical),
platform::path_text(&canonical_worktree),
));
}
}
Ok(canonical)
}
pub fn hash_executable(path: &Path) -> Result<String, String> {
let mut file = platform::open_regular_input(path)
.map_err(|error| {
format!(
"Cannot open ACP executable {}: {error}.",
platform::path_text(path)
)
})?
.ok_or_else(|| {
format!(
"ACP executable {} is not a regular file.",
platform::path_text(path)
)
})?;
let mut hasher = Sha256::new();
let mut buffer = [0u8; HASH_CHUNK_BYTES];
loop {
let count = file.read(&mut buffer).map_err(|error| {
format!(
"Cannot read ACP executable {}: {error}.",
platform::path_text(path)
)
})?;
if count == 0 {
break;
}
hasher.update(&buffer[..count]);
}
Ok(hex_digest(hasher.finalize().as_slice()))
}
pub fn hash_args(args: &[String]) -> String {
let mut hasher = Sha256::new();
for arg in args {
hasher.update((arg.len() as u64).to_le_bytes());
hasher.update(arg.as_bytes());
}
hex_digest(hasher.finalize().as_slice())
}
fn hex_digest(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for &byte in bytes {
out.push(HEX[(byte >> 4) as usize] as char);
out.push(HEX[(byte & 0x0f) as usize] as char);
}
out
}
pub fn list_trust(state: Option<&Path>, repo: Option<&Path>) -> Result<Vec<TrustRecord>, String> {
let storage = Storage::inspect(state, repo, Tokens::ChatTrust)?;
Ok(storage
.read::<SavedTrust>(TRUST_FILE)?
.unwrap_or_default()
.entries)
}
pub fn check_trust(
state: Option<&Path>,
repo: Option<&Path>,
agent_id: &AgentId,
agent_name: &str,
scope: &TrustScope,
fingerprint: &LauncherFingerprint,
) -> Result<Result<(), TrustRequirement>, String> {
let entries = list_trust(state, repo)?;
if entries.iter().any(|entry| {
&entry.agent_id == agent_id && &entry.scope == scope && &entry.fingerprint == fingerprint
}) {
return Ok(Ok(()));
}
let changed_binary = entries
.iter()
.any(|entry| &entry.agent_id == agent_id && &entry.scope == scope);
let reason = if changed_binary {
format!(
"The executable or arguments for {agent_name} changed since you last trusted this workspace."
)
} else {
format!(
"{agent_name} will run natively in this workspace and can read repository settings, hooks and MCP configs before any permission prompt."
)
};
Ok(Err(TrustRequirement {
agent_id: agent_id.clone(),
agent_name: agent_name.to_owned(),
scope: scope.clone(),
fingerprint: fingerprint.clone(),
disclosure: scope.disclosure().to_owned(),
reason,
}))
}
pub async fn grant_trust(
state: Option<&Path>,
repo: Option<&Path>,
agent_id: AgentId,
scope: TrustScope,
fingerprint: LauncherFingerprint,
) -> Result<TrustRecord, String> {
let storage = Storage::new(state, repo, Tokens::ChatTrust)?;
let _lock = storage.lock(TRUST_LOCK).await?;
let mut saved: SavedTrust = storage.read(TRUST_FILE)?.unwrap_or_default();
saved
.entries
.retain(|entry| !(entry.agent_id == agent_id && entry.scope == scope));
let record = TrustRecord {
agent_id,
scope,
fingerprint,
granted_at: crate::broker::now(),
};
saved.entries.push(record.clone());
storage.save(TRUST_FILE, &saved)?;
Ok(record)
}
pub async fn revoke_trust(
state: Option<&Path>,
repo: Option<&Path>,
agent_id: &AgentId,
scope: &TrustScope,
) -> Result<bool, String> {
let storage = Storage::new(state, repo, Tokens::ChatTrust)?;
let _lock = storage.lock(TRUST_LOCK).await?;
let mut saved: SavedTrust = storage.read(TRUST_FILE)?.unwrap_or_default();
let before = saved.entries.len();
saved
.entries
.retain(|entry| !(&entry.agent_id == agent_id && &entry.scope == scope));
let removed = saved.entries.len() != before;
if removed {
storage.save(TRUST_FILE, &saved)?;
}
Ok(removed)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn trust_requires_reconsent_when_executable_bytes_change() {
let root = crate::test_os::tempdir();
let state = root.path().join("state");
let worktree = root.path().join("work");
let bin_dir = root.path().join("bin");
std::fs::create_dir_all(&worktree).unwrap();
std::fs::create_dir_all(&bin_dir).unwrap();
let exe = bin_dir.join("agent-bin");
std::fs::write(&exe, b"version-1").unwrap();
let agent: AgentId = "antigravity".parse().unwrap();
let scope = TrustScope::for_worktree(&worktree, &repository::Identity::default()).unwrap();
assert!(matches!(scope, TrustScope::Directory { .. }));
let fp1 = LauncherFingerprint::compute(&exe, &["acp".to_owned()]).unwrap();
assert!(
check_trust(
Some(&state),
Some(&worktree),
&agent,
"Antigravity",
&scope,
&fp1
)
.unwrap()
.is_err()
);
grant_trust(
Some(&state),
Some(&worktree),
agent.clone(),
scope.clone(),
fp1.clone(),
)
.await
.unwrap();
assert!(
check_trust(
Some(&state),
Some(&worktree),
&agent,
"Antigravity",
&scope,
&fp1
)
.unwrap()
.is_ok()
);
std::fs::write(&exe, b"version-2").unwrap();
let fp2 = LauncherFingerprint::compute(&exe, &["acp".to_owned()]).unwrap();
assert_ne!(fp1.executable_sha256, fp2.executable_sha256);
let req = check_trust(
Some(&state),
Some(&worktree),
&agent,
"Antigravity",
&scope,
&fp2,
)
.unwrap()
.unwrap_err();
assert!(req.reason.contains("changed"));
assert!(
revoke_trust(Some(&state), Some(&worktree), &agent, &scope)
.await
.unwrap()
);
assert!(
list_trust(Some(&state), Some(&worktree))
.unwrap()
.is_empty()
);
}
#[test]
fn builtin_preset_rejects_executable_inside_worktree() {
let root = crate::test_os::tempdir();
let worktree = root.path().join("repo");
std::fs::create_dir_all(&worktree).unwrap();
let malicious = crate::test_os::executable_named(&worktree, "agy");
std::fs::write(&malicious, b"fake-acp-binary\n").unwrap();
crate::test_os::make_executable(&malicious);
let error = resolve_executable(&platform::path_text(&malicious), Some(&worktree), true)
.unwrap_err();
assert!(error.contains("inside the workspace"), "{error}");
}
}