use lean_ctx_protocol::cloud::knowledge::EngineScreeningV1;
pub const SCANNER_VERSION: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Blocked {
Secret(String),
Pii(String),
Redaction,
Forbidden,
LocalPath,
BrokenRules(usize),
}
impl std::fmt::Display for Blocked {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Secret(kind) => write!(f, "credential ({kind})"),
Self::Pii(kind) => write!(f, "personal data ({kind})"),
Self::Redaction => f.write_str("matches a redaction rule"),
Self::Forbidden => f.write_str("matches a forbidden egress pattern or org filter"),
Self::LocalPath => f.write_str("contains a local home path"),
Self::BrokenRules(n) => write!(
f,
"cannot be screened: {n} egress forbidden pattern(s) of this machine do not compile, so nothing goes to LeanCTX Cloud until they are fixed (`lean-ctx policy` / `[egress] forbidden_patterns`)"
),
}
}
}
pub struct Screen {
custom_secret_patterns: Vec<String>,
redaction_excludes: std::sync::Arc<Vec<regex::Regex>>,
forbidden: Vec<regex::Regex>,
broken: usize,
home: Option<String>,
counts: EngineScreeningV1,
}
impl Screen {
pub fn for_installation() -> Self {
let config = lean_ctx::core::config::Config::load();
let cloud = crate::policy::installed_filter_rules();
let forbidden = lean_ctx::core::policy::runtime::active()
.map(|policy| policy.resolved.egress.forbidden_patterns.clone())
.unwrap_or_default()
.into_iter()
.chain(cloud.redaction.into_iter().map(|(_, pattern)| pattern))
.chain(cloud.blocked.into_iter().map(|(_, pattern)| pattern))
.map(|p| regex::Regex::new(&p))
.collect();
Self::with_compiled(
config.secret_detection.custom_patterns.clone(),
lean_ctx::core::redaction::config_exclude_patterns(),
forbidden,
)
}
pub(crate) fn with_compiled(
custom_secret_patterns: Vec<String>,
redaction_excludes: std::sync::Arc<Vec<regex::Regex>>,
compiled: Vec<Result<regex::Regex, regex::Error>>,
) -> Self {
let total = compiled.len();
let forbidden: Vec<regex::Regex> = compiled.into_iter().filter_map(Result::ok).collect();
let broken = total - forbidden.len();
if broken > 0 {
tracing::warn!("cloud screening: {broken} forbidden pattern(s) do not compile");
}
let mut screen = Self::with_rules(custom_secret_patterns, redaction_excludes, forbidden);
screen.broken = broken;
screen
}
pub fn problem(&self) -> Option<String> {
(self.broken > 0).then(|| {
format!(
"LeanCTX Cloud content traffic is paused: {}",
Blocked::BrokenRules(self.broken)
)
})
}
pub(crate) fn with_rules(
custom_secret_patterns: Vec<String>,
redaction_excludes: std::sync::Arc<Vec<regex::Regex>>,
forbidden: Vec<regex::Regex>,
) -> Self {
let home = dirs::home_dir()
.map(|h| h.to_string_lossy().into_owned())
.filter(|h| h.len() > 1);
Self {
custom_secret_patterns,
redaction_excludes,
forbidden,
broken: 0,
home,
counts: EngineScreeningV1 {
scanner_version: SCANNER_VERSION,
secrets_removed: 0,
pii_removed: 0,
items_dropped: 0,
},
}
}
pub fn check(&mut self, text: &str) -> Result<(), Blocked> {
let verdict = self.verdict(text);
if let Err(reason) = &verdict {
self.counts.items_dropped += 1;
match reason {
Blocked::Secret(_) | Blocked::Redaction => self.counts.secrets_removed += 1,
Blocked::Pii(_) => self.counts.pii_removed += 1,
Blocked::Forbidden | Blocked::LocalPath | Blocked::BrokenRules(_) => {}
}
}
verdict
}
fn verdict(&self, text: &str) -> Result<(), Blocked> {
if self.broken > 0 {
return Err(Blocked::BrokenRules(self.broken));
}
let original = text;
let neutral = super::screening_exceptions::neutralise(text);
let text: &str = &neutral;
if text.contains("[REDACTED") {
return Err(Blocked::Secret("masked in store".into()));
}
let findings = lean_ctx::core::secret_detection::detect_secrets_with_custom(
text,
&self.custom_secret_patterns,
);
if let Some(first) = findings.first() {
return Err(Blocked::Secret(first.pattern_name.to_string()));
}
if lean_ctx::core::redaction::redact_text_with_excludes(text, &self.redaction_excludes)
!= text
{
return Err(Blocked::Redaction);
}
if let Some((class, _)) = lean_ctx::core::input_filters::pii::detect(text).first() {
return Err(Blocked::Pii((*class).to_string()));
}
if lean_ctx::core::sensitivity::classify_content(text)
>= lean_ctx::core::sensitivity::SensitivityLevel::Confidential
{
return Err(Blocked::Pii("card or account number".into()));
}
if self.forbidden.iter().any(|re| re.is_match(original)) {
return Err(Blocked::Forbidden);
}
if let Some(home) = &self.home
&& text.contains(home.as_str())
{
return Err(Blocked::LocalPath);
}
Ok(())
}
pub fn counts(&self) -> EngineScreeningV1 {
self.counts
}
}
#[cfg(test)]
mod tests {
use super::*;
fn screen() -> Screen {
Screen::with_rules(Vec::new(), std::sync::Arc::new(Vec::new()), Vec::new())
}
#[test]
fn canary_secrets_and_personal_data_never_pass() {
let mut s = screen();
for canary in [
concat!("deploy with AK", "IAIOSFODNN7EXAMPLE as key"),
concat!("token gh", "p_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789"),
"-----BEGIN RSA PRIVATE KEY-----\nMIIEow",
"mail jane.doe@example.com for access",
"card 4111 1111 1111 1111",
] {
assert!(s.check(canary).is_err(), "{canary}");
}
assert!(s.check("LeanCTX Cloud never blocks a tool call.").is_ok());
let counts = s.counts();
assert_eq!(counts.items_dropped, 5);
assert!(counts.secrets_removed >= 3 && counts.pii_removed >= 1);
}
#[test]
fn forbidden_patterns_and_home_paths_are_dropped() {
let _home = lean_ctx::core::data_dir::test_env_lock();
let mut s = Screen::with_rules(
Vec::new(),
std::sync::Arc::new(Vec::new()),
vec![regex::Regex::new("project-falcon").unwrap()],
);
assert_eq!(s.check("codename project-falcon"), Err(Blocked::Forbidden));
if let Some(home) = dirs::home_dir().filter(|h| h.to_string_lossy().len() > 1) {
let text = format!("see {}/notes.txt", home.display());
assert_eq!(s.check(&text), Err(Blocked::LocalPath));
}
}
#[test]
#[allow(clippy::invalid_regex)] fn an_invalid_forbidden_pattern_fails_closed() {
let mut s = Screen::with_compiled(
Vec::new(),
std::sync::Arc::new(Vec::new()),
vec![regex::Regex::new("falcon"), regex::Regex::new("(unclosed")],
);
assert_eq!(
s.check("LeanCTX Cloud never blocks a tool call."),
Err(Blocked::BrokenRules(1))
);
let problem = s.problem().expect("named");
assert!(problem.contains("do not compile"), "{problem}");
let fine = Screen::with_compiled(
Vec::new(),
std::sync::Arc::new(Vec::new()),
vec![regex::Regex::new("falcon")],
);
assert_eq!(fine.problem(), None);
}
}