pub mod detect;
mod key;
use std::path::Path;
use crate::error::{CoreError, ErrorCode};
use crate::model::TomlDocument;
use crate::model::skill_meta::{SkillMeta, SkillSourceKind};
pub use crate::model::sources::{
Lapse, LapseReason, SourceDecision, SourceRecord, SourceScope, SourcesFile, TrustVia,
key_shape, without_insecure_prefix,
};
use crate::skills::store::META_DIR;
use crate::store::{CommitId, Operation, Store, StoreRead, Transaction, utc_datetime};
pub use detect::{SourceChange, SourceChangeKind};
pub use key::{AGGREGATOR_HOSTS, OFFICIAL_OWNERS, SourceRef};
pub const SOURCES_FILE: &str = "sources.toml";
pub const ENTITY: &str = "sources";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TrustGrant {
Once,
Always(SourceScope),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AskReason {
FirstTime,
NotTrusted,
Lapsed,
}
impl AskReason {
pub const fn as_str(self) -> &'static str {
match self {
AskReason::FirstTime => "first_time",
AskReason::NotTrusted => "not_trusted",
AskReason::Lapsed => "lapsed",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum TrustState {
Trusted { key: String, record: SourceRecord },
Blocked { key: String, record: SourceRecord },
Ask {
reason: AskReason,
lapse: Option<Lapse>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct TrustCheck {
pub source: SourceRef,
pub state: TrustState,
pub scopes: Vec<SourceScope>,
pub official: bool,
pub installed_skills: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct Admission {
pub check: TrustCheck,
pub recorded: Option<CommitId>,
}
pub fn load(read: &dyn StoreRead) -> Result<SourcesFile, CoreError> {
Ok(load_doc(read)?.into_value())
}
fn load_doc(read: &dyn StoreRead) -> Result<TomlDocument<SourcesFile>, CoreError> {
let Some(text) = read.read_file(SOURCES_FILE)? else {
return Ok(TomlDocument::new(SourcesFile::new()));
};
let path = Path::new(SOURCES_FILE);
let doc = TomlDocument::<SourcesFile>::parse(&text)
.map_err(|diagnostic| diagnostic.to_core_error(Some(path)))?;
if let Some(err) = doc.value().validate().first_error(Some(path)) {
return Err(err);
}
Ok(doc)
}
pub fn list(read: &dyn StoreRead) -> Result<Vec<(String, SourceRecord)>, CoreError> {
Ok(load(read)?.sources.into_iter().collect())
}
pub fn record_id(key: &str) -> String {
use sha2::{Digest, Sha256};
let digest = Sha256::digest(key.as_bytes());
digest[..8]
.iter()
.map(|byte| format!("{byte:02x}"))
.collect()
}
pub fn key_of_id(read: &dyn StoreRead, id: &str) -> Result<String, CoreError> {
load(read)?
.sources
.into_keys()
.find(|key| record_id(key) == id)
.ok_or_else(|| {
CoreError::new(
ErrorCode::NotFound,
"there is no such source decision any more; reload the list",
)
})
}
pub fn check(read: &dyn StoreRead, source: &SourceRef) -> Result<TrustCheck, CoreError> {
let file = load(read)?;
let state = state_of(&file, source);
let installed_skills = installed_skills(read, source)?;
let state = match state {
TrustState::Ask { lapse: None, .. } if !installed_skills.is_empty() => TrustState::Ask {
reason: AskReason::NotTrusted,
lapse: None,
},
state => state,
};
Ok(TrustCheck {
source: source.clone(),
state,
scopes: source.scopes(),
official: source.is_official(),
installed_skills,
})
}
fn state_of(file: &SourcesFile, source: &SourceRef) -> TrustState {
let decided = |keys: Vec<String>| -> Vec<(String, &SourceRecord)> {
keys.into_iter()
.filter_map(|key| file.sources.get(&key).map(|record| (key, record)))
.collect()
};
if let Some((key, record)) = decided(source.block_keys())
.into_iter()
.find(|(_, record)| record.decision == SourceDecision::Block)
{
return TrustState::Blocked {
key,
record: record.clone(),
};
}
let matching = decided(source.matching_keys());
match matching.first() {
Some((_, record)) if record.lapsed.is_some() => TrustState::Ask {
reason: AskReason::Lapsed,
lapse: record.lapsed.clone(),
},
Some((key, record)) => TrustState::Trusted {
key: key.clone(),
record: (*record).clone(),
},
None => TrustState::Ask {
reason: AskReason::FirstTime,
lapse: None,
},
}
}
fn installed_skills(read: &dyn StoreRead, source: &SourceRef) -> Result<Vec<String>, CoreError> {
let mut ids = Vec::new();
for name in read.list_files(META_DIR)? {
let Some(id) = name.strip_suffix(".toml") else {
continue;
};
let Some(text) = read.read_file(&format!("{META_DIR}/{name}"))? else {
continue;
};
let Ok(doc) = TomlDocument::<SkillMeta>::parse(&text) else {
continue;
};
let from_source = doc
.value()
.source
.as_ref()
.filter(|skill_source| skill_source.kind == SkillSourceKind::Git)
.and_then(|skill_source| skill_source.url.as_deref())
.and_then(|url| SourceRef::from_git_url(url).ok())
.is_some_and(|other| other.key() == source.key());
if from_source {
ids.push(id.to_owned());
}
}
Ok(ids)
}
fn blocked_error(source: &SourceRef, key: &str) -> CoreError {
CoreError::new(
ErrorCode::SourceBlocked,
format!(
"the source {} is blocked ({key}); remove the block with `surject source untrust {key}` \
or in Settings > Sources",
source.key()
),
)
}
fn trust_required(source: &SourceRef, reason: AskReason, lapse: Option<&Lapse>) -> CoreError {
let why = match (reason, lapse) {
(AskReason::Lapsed, Some(lapse)) => match lapse.reason {
LapseReason::HistoryRewritten => {
"its history was rewritten (force-push) since you trusted it".to_owned()
}
LapseReason::Moved => format!(
"it moved to {} since you trusted it",
lapse.detail.as_deref().unwrap_or("another location")
),
},
(AskReason::Lapsed, None) => "it changed since you trusted it".to_owned(),
(AskReason::FirstTime, _) => "it is the first time you install from it".to_owned(),
(AskReason::NotTrusted, _) => "you have not trusted it".to_owned(),
};
CoreError::new(
ErrorCode::SourceTrustRequired,
format!(
"the source {} needs your decision: {why}. Skills and MCP servers can run commands and \
read your files; only install from sources you trust",
source.key()
),
)
}
pub fn admit(
store: &Store<'_>,
source: &SourceRef,
grant: Option<TrustGrant>,
via: TrustVia,
) -> Result<Admission, CoreError> {
let check = would_admit(store, source, grant)?;
let recorded = match (&check.state, grant) {
(TrustState::Ask { .. }, Some(TrustGrant::Always(scope))) => {
trust(store, source, scope, via)?
}
_ => None,
};
Ok(Admission { check, recorded })
}
pub fn would_admit(
read: &dyn StoreRead,
source: &SourceRef,
grant: Option<TrustGrant>,
) -> Result<TrustCheck, CoreError> {
let check = check(read, source)?;
match (&check.state, grant) {
(TrustState::Blocked { key, .. }, _) => Err(blocked_error(source, key)),
(TrustState::Ask { reason, lapse }, None) => {
Err(trust_required(source, *reason, lapse.as_ref()))
}
(TrustState::Ask { .. } | TrustState::Trusted { .. }, _) => Ok(check),
}
}
pub fn refuse_blocked(read: &dyn StoreRead, source: &SourceRef) -> Result<TrustCheck, CoreError> {
let check = check(read, source)?;
match &check.state {
TrustState::Blocked { key, .. } => Err(blocked_error(source, key)),
_ => Ok(check),
}
}
fn scope_key(source: &SourceRef, scope: SourceScope) -> Result<String, CoreError> {
source.scope_key(scope).ok_or_else(|| {
CoreError::new(
ErrorCode::InvalidValue,
format!(
"the source {} has no {} scope",
source.key(),
scope.as_str()
),
)
})
}
fn change(
store: &Store<'_>,
op: &str,
summary: String,
edit: impl FnOnce(&mut Transaction<'_>, &mut SourcesFile) -> Result<bool, CoreError>,
) -> Result<Option<CommitId>, CoreError> {
let operation = Operation::new(op, summary).entity(ENTITY);
let committed = store.transaction(operation, |tx| {
let mut doc = load_doc(tx)?;
if !edit(tx, doc.value_mut())? {
return Ok(());
}
let text = doc.to_text()?;
tx.write(SOURCES_FILE, text)
})?;
Ok(committed.commit)
}
fn new_record(
tx: &Transaction<'_>,
decision: SourceDecision,
scope: SourceScope,
via: TrustVia,
url: Option<String>,
) -> SourceRecord {
SourceRecord {
decision,
scope,
decided_at: utc_datetime(tx.store().env().now()),
via: Some(via),
machine: Some(tx.machine().label.clone()),
url,
lapsed: None,
extra: toml::Table::new(),
}
}
fn covers(key: &str, other: &str) -> bool {
match key.strip_suffix('*') {
Some(prefix) => other.starts_with(prefix),
None => key == other,
}
}
pub fn record_key(
store: &Store<'_>,
key: &str,
decision: SourceDecision,
via: TrustVia,
url: Option<&str>,
) -> Result<Option<CommitId>, CoreError> {
let scope = match SourceRef::canonical_key(key) {
Ok((canonical, scope)) if canonical == key => scope,
Ok((canonical, _)) => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("{key:?} is not a canonical source key; it is written {canonical:?}"),
));
}
Err(_) => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{key:?} is not a source key: host/path for a repository, host/owner/* for \
an owner, host/* for a host"
),
));
}
};
if decision == SourceDecision::Trust
&& scope == SourceScope::Host
&& AGGREGATOR_HOSTS.contains(&without_insecure_prefix(key).trim_end_matches("/*"))
{
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{key} cannot be trusted as a whole: that host serves anyone's repositories; \
trust an owner or a repository"
),
));
}
let (op, summary) = match decision {
SourceDecision::Trust => (
"source.trust",
format!("source: trust {key} ({})", scope.as_str()),
),
SourceDecision::Block => ("source.block", format!("source: block {key}")),
};
change(store, op, summary, |tx, file| {
if decision == SourceDecision::Trust {
for (other, record) in &mut file.sources {
if record.decision == SourceDecision::Trust && covers(key, other) {
record.lapsed = None;
}
}
}
let record = new_record(tx, decision, scope, via, url.map(str::to_owned));
file.sources.insert(key.to_owned(), record);
Ok(true)
})
}
pub fn trust(
store: &Store<'_>,
source: &SourceRef,
scope: SourceScope,
via: TrustVia,
) -> Result<Option<CommitId>, CoreError> {
if !source.scopes().contains(&scope) {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{} cannot be trusted at the {} scope{}",
source.key(),
scope.as_str(),
if scope == SourceScope::Host {
": that host serves anyone's repositories; trust an owner or a repository"
} else {
""
}
),
));
}
let key = scope_key(source, scope)?;
let url = (scope == SourceScope::Repo).then(|| source.url());
record_key(store, &key, SourceDecision::Trust, via, url)
}
pub fn block(
store: &Store<'_>,
source: &SourceRef,
scope: SourceScope,
via: TrustVia,
) -> Result<Option<CommitId>, CoreError> {
let key = scope_key(source, scope)?;
let url = (scope == SourceScope::Repo).then(|| source.url());
record_key(store, &key, SourceDecision::Block, via, url)
}
fn missing_key(key: &str) -> CoreError {
CoreError::new(
ErrorCode::NotFound,
format!("there is no decision about the source {key:?}"),
)
}
pub fn block_key(
store: &Store<'_>,
key: &str,
via: TrustVia,
) -> Result<Option<CommitId>, CoreError> {
change(
store,
"source.block",
format!("source: block {key}"),
|tx, file| {
let Some(old) = file.sources.get(key) else {
return Err(missing_key(key));
};
if old.decision == SourceDecision::Block {
return Ok(false);
}
let mut record = new_record(tx, SourceDecision::Block, old.scope, via, old.url.clone());
record.extra = old.extra.clone();
file.sources.insert(key.to_owned(), record);
Ok(true)
},
)
}
pub fn untrust(store: &Store<'_>, key: &str) -> Result<Option<CommitId>, CoreError> {
change(
store,
"source.untrust",
format!("source: untrust {key}"),
|_, file| match file.sources.remove(key) {
Some(_) => Ok(true),
None => Err(missing_key(key)),
},
)
}
pub fn lapse(
store: &Store<'_>,
source: &SourceRef,
found: &SourceChange,
) -> Result<Option<CommitId>, CoreError> {
let current = load(store)?;
let TrustState::Trusted { record, .. } = state_of(¤t, source) else {
return Ok(None);
};
let reason = found.kind.reason();
let key = source.key().to_owned();
change(
store,
"source.lapse",
format!("source: trust lapsed for {key} ({})", reason.as_str()),
|tx, file| {
let lapse = Lapse {
reason,
at: utc_datetime(tx.store().env().now()),
detail: found.detail.clone(),
extra: toml::Table::new(),
};
match file.sources.get_mut(&key) {
Some(own) => own.lapsed = Some(lapse),
None => {
let mut own = record.clone();
own.scope = SourceScope::Repo;
own.url = Some(source.url().to_owned());
own.lapsed = Some(lapse);
own.extra = toml::Table::new();
file.sources.insert(key.clone(), own);
}
}
Ok(true)
},
)
}
#[cfg(test)]
mod tests;