use std::{
collections::{HashMap, HashSet},
env, fmt, fs,
io::Write,
path::{Path, PathBuf},
};
use crate::error::{Error, Result};
use crate::{checks, checks::Severity, context::ContextConfig};
use serde_derive::{Deserialize, Serialize};
use tracing::debug;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LlmConfig {
#[serde(default = "default_llm_provider")]
pub provider: String,
#[serde(default = "default_llm_model")]
pub model: String,
#[serde(default)]
pub base_url: Option<String>,
#[serde(default = "default_llm_timeout_ms")]
pub timeout_ms: u64,
#[serde(default = "default_llm_max_tokens")]
pub max_tokens: u32,
}
fn default_llm_provider() -> String {
"anthropic".into()
}
fn default_llm_model() -> String {
"claude-sonnet-4-20250514".into()
}
const fn default_llm_timeout_ms() -> u64 {
5000
}
const fn default_llm_max_tokens() -> u32 {
512
}
pub const SUPPORTED_LLM_PROVIDERS: &[&str] = &["anthropic", "openai-compatible"];
impl LlmConfig {
pub fn validate(&self) -> Result<()> {
if !SUPPORTED_LLM_PROVIDERS.contains(&self.provider.as_str()) {
return Err(Error::Config(format!(
"invalid LLM provider: '{}'. Valid values: {}",
self.provider,
SUPPORTED_LLM_PROVIDERS.join(", ")
)));
}
if self.model.trim().is_empty() {
return Err(Error::Config("LLM model must not be empty".into()));
}
if self.timeout_ms == 0 {
return Err(Error::Config(
"LLM timeout must be greater than 0 milliseconds".into(),
));
}
if let Some(url) = &self.base_url {
if !(url.starts_with("http://") || url.starts_with("https://")) {
return Err(Error::Config(format!(
"invalid LLM base URL: '{url}' (must start with http:// or https://)"
)));
}
}
Ok(())
}
}
impl Default for LlmConfig {
fn default() -> Self {
Self {
provider: default_llm_provider(),
model: default_llm_model(),
base_url: None,
timeout_ms: default_llm_timeout_ms(),
max_tokens: default_llm_max_tokens(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Shell,
Ai,
Wrap,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum InheritOr<T> {
#[default]
Inherit,
Set(T),
}
impl<T> InheritOr<T> {
pub const fn as_set(&self) -> Option<&T> {
match self {
Self::Inherit => None,
Self::Set(v) => Some(v),
}
}
}
impl<T: serde::Serialize> serde::Serialize for InheritOr<T> {
fn serialize<S: serde::Serializer>(&self, ser: S) -> std::result::Result<S::Ok, S::Error> {
match self {
Self::Inherit => ser.serialize_str("inherit"),
Self::Set(v) => v.serialize(ser),
}
}
}
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for InheritOr<T> {
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> std::result::Result<Self, D::Error> {
let v = serde_yaml::Value::deserialize(de)?;
if let serde_yaml::Value::String(s) = &v {
if s == "inherit" {
return Ok(Self::Inherit);
}
}
let inner = T::deserialize(v).map_err(serde::de::Error::custom)?;
Ok(Self::Set(inner))
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct AgentConfig {
#[serde(default = "default_auto_deny_severity")]
pub auto_deny_severity: Severity,
#[serde(default)]
pub require_human_approval: bool,
#[serde(default = "default_true")]
pub context_escalation: bool,
#[serde(default)]
pub allow_safe: bool,
#[serde(default = "default_true")]
pub self_protection: bool,
#[serde(default)]
pub llm_review: bool,
#[serde(default)]
pub challenge: InheritOr<Challenge>,
#[serde(default)]
pub min_severity: InheritOr<Option<Severity>>,
#[serde(default)]
pub severity_escalation: InheritOr<SeverityEscalationConfig>,
}
const fn default_true() -> bool {
true
}
const fn default_auto_deny_severity() -> Severity {
Severity::High
}
impl Default for AgentConfig {
fn default() -> Self {
Self {
auto_deny_severity: default_auto_deny_severity(),
require_human_approval: false,
context_escalation: true,
allow_safe: false,
self_protection: true,
llm_review: false,
challenge: InheritOr::Inherit,
min_severity: InheritOr::Inherit,
severity_escalation: InheritOr::Inherit,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, Eq)]
pub struct SandboxConfig {
#[serde(default)]
pub deny_read: Vec<String>,
#[serde(default)]
pub deny_write: Vec<String>,
#[serde(default)]
pub exclude: Vec<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct WrappersConfig {
#[serde(default)]
pub tools: HashMap<String, WrapperToolConfig>,
#[serde(default)]
pub challenge: InheritOr<Challenge>,
#[serde(default)]
pub min_severity: InheritOr<Option<Severity>>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct WrapperToolConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub delimiter: Option<String>,
#[serde(default)]
pub check_groups: Vec<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct SeverityEscalationConfig {
#[serde(default = "default_severity_escalation_enabled")]
pub enabled: bool,
#[serde(default = "default_severity_critical")]
pub critical: Challenge,
#[serde(default = "default_severity_high")]
pub high: Challenge,
#[serde(default = "default_severity_medium")]
pub medium: Challenge,
#[serde(default = "default_severity_low")]
pub low: Challenge,
#[serde(default = "default_severity_info")]
pub info: Challenge,
}
const fn default_severity_escalation_enabled() -> bool {
true
}
const fn default_severity_critical() -> Challenge {
Challenge::Yes
}
const fn default_severity_high() -> Challenge {
Challenge::Math
}
const fn default_severity_medium() -> Challenge {
Challenge::Enter
}
const fn default_severity_low() -> Challenge {
Challenge::Enter
}
const fn default_severity_info() -> Challenge {
Challenge::Enter
}
impl Default for SeverityEscalationConfig {
fn default() -> Self {
Self {
enabled: default_severity_escalation_enabled(),
critical: default_severity_critical(),
high: default_severity_high(),
medium: default_severity_medium(),
low: default_severity_low(),
info: default_severity_info(),
}
}
}
impl SeverityEscalationConfig {
#[must_use]
pub const fn challenge_for_severity(&self, severity: Severity) -> Option<Challenge> {
if !self.enabled {
return None;
}
Some(match severity {
Severity::Critical => self.critical,
Severity::High => self.high,
Severity::Medium => self.medium,
Severity::Low => self.low,
Severity::Info => self.info,
})
}
}
const DEFAULT_SETTING_FILE_NAME: &str = "settings.yaml";
pub fn read_regular_file(path: &Path) -> std::io::Result<Vec<u8>> {
if !fs::metadata(path)?.is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"not a regular file",
));
}
fs::read(path)
}
pub fn read_regular_string(path: &Path) -> std::io::Result<String> {
String::from_utf8(read_regular_file(path)?)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
}
pub const CONFIG_DIR_ENV: &str = "SHELLFIRM_CONFIG_DIR";
pub fn config_dir_from_env(value: Option<&str>) -> Result<Option<PathBuf>> {
let Some(v) = value.map(str::trim).filter(|v| !v.is_empty()) else {
return Ok(None);
};
let dir = PathBuf::from(v);
if dir.is_dir() {
Ok(Some(dir))
} else {
Err(Error::Config(format!(
"{CONFIG_DIR_ENV}={}: {}",
dir.display(),
if dir.exists() {
"not a directory"
} else {
"no such directory"
}
)))
}
}
pub const MAX_SETTINGS_BACKUPS: usize = 3;
#[derive(Debug)]
pub struct SettingsLock {
#[cfg(unix)]
_lock: Option<nix::fcntl::Flock<fs::File>>,
#[cfg(not(unix))]
_file: fs::File,
}
fn sparse_merge(
path: &str,
root: &mut serde_yaml::Value,
new: &serde_yaml::Value,
default: &serde_yaml::Value,
) {
let (Some(new_map), Some(root_map)) = (new.as_mapping(), root.as_mapping_mut()) else {
return;
};
let dynamic = DYNAMIC_MAP_PATHS.contains(&path);
if dynamic {
root_map.retain(|k, _| new_map.contains_key(k));
}
for (k, v) in new_map {
let sub_default = default.get(k);
let in_file = root_map.contains_key(k);
let differs = sub_default.map_or_else(|| !is_empty_yaml(v), |d| d != v);
let key_name = k.as_str().unwrap_or_default();
let sub_path = if path.is_empty() {
key_name.to_string()
} else {
format!("{path}.{key_name}")
};
let differs = differs || (sub_path == "llm" && !in_file && v.is_mapping());
if WHOLE_MAP_PATHS.contains(&sub_path.as_str()) {
if differs {
root_map.insert(k.clone(), v.clone());
} else {
root_map.remove(k);
}
continue;
}
let both_maps = v.is_mapping()
&& root_map.get(k).is_none_or(serde_yaml::Value::is_mapping)
&& sub_default.is_some_and(serde_yaml::Value::is_mapping);
if both_maps && (in_file || differs) {
let entry = root_map
.entry(k.clone())
.or_insert_with(|| serde_yaml::Value::Mapping(serde_yaml::Mapping::new()));
sparse_merge(
&sub_path,
entry,
v,
sub_default.unwrap_or(&serde_yaml::Value::Null),
);
let empty = entry
.as_mapping()
.is_some_and(serde_yaml::Mapping::is_empty);
if empty && !differs && sub_path != "llm" {
root_map.remove(k);
} else if empty && sub_path == "llm" {
if let (Some(entry), Some(p)) = (entry.as_mapping_mut(), v.get("provider")) {
entry.insert(serde_yaml::Value::String("provider".into()), p.clone());
}
}
} else if in_file && !differs && (is_empty_yaml(v) || v.is_sequence()) {
root_map.remove(k);
} else if in_file && !differs && DROP_AT_DEFAULT_PATHS.contains(&sub_path.as_str()) {
root_map.remove(k);
} else if in_file || differs {
root_map.insert(k.clone(), v.clone());
}
}
}
const DROP_AT_DEFAULT_PATHS: &[&str] =
&["blast_radius", "blast_radius_budget_ms", "approved_notice"];
fn is_empty_yaml(v: &serde_yaml::Value) -> bool {
match v {
serde_yaml::Value::Null => true,
serde_yaml::Value::Sequence(s) => s.is_empty(),
serde_yaml::Value::Mapping(m) => m.is_empty(),
_ => false,
}
}
fn top_level_key(line: &str) -> Option<&str> {
if line.starts_with([' ', '\t', '#', '-']) {
return None;
}
let (k, _) = line.split_once(':')?;
let k = k.trim().trim_matches(['"', '\'']);
(!k.is_empty()).then_some(k)
}
fn reattach_comments(old: &str, new_yaml: &str) -> String {
let mut header: Vec<&str> = Vec::new();
let mut per_key: HashMap<String, Vec<&str>> = HashMap::new();
let mut lines = old.lines().peekable();
while let Some(l) = lines.peek() {
let t = l.trim_start();
if t.starts_with('#') || t.is_empty() {
header.push(l);
lines.next();
} else {
break;
}
}
let mut pending: Vec<&str> = Vec::new();
for l in lines {
if l.trim_start().starts_with('#') {
pending.push(l);
} else if let Some(k) = top_level_key(l) {
if !pending.is_empty() {
per_key.insert(k.to_string(), std::mem::take(&mut pending));
}
} else if !l.trim().is_empty() {
pending.clear();
}
}
while header.last().is_some_and(|l| l.trim().is_empty()) {
header.pop();
}
let mut out = String::new();
for h in &header {
out.push_str(h);
out.push('\n');
}
for l in new_yaml.lines() {
if let Some(c) = top_level_key(l).and_then(|k| per_key.get(k)) {
for cl in c {
out.push_str(cl);
out.push('\n');
}
}
out.push_str(l);
out.push('\n');
}
out
}
const SETTINGS_HEADER: &str =
"# shellfirm settings. Keys that are not listed use the built-in defaults.\n\
# Reference: https://github.com/kaplanelad/shellfirm/blob/main/docs/config.md\n";
pub const DEFAULT_CHALLENGE: Challenge = Challenge::Math;
fn default_enabled_groups() -> Vec<String> {
DEFAULT_ENABLED_GROUPS
.iter()
.map(|s| (*s).to_string())
.collect()
}
const fn default_audit_enabled() -> bool {
true
}
const fn default_blast_radius() -> bool {
true
}
const fn default_approved_notice() -> bool {
true
}
pub const DEFAULT_BLAST_RADIUS_BUDGET_MS: u64 = 400;
pub const MAX_BLAST_RADIUS_BUDGET_MS: u64 = 5_000;
const fn default_blast_radius_budget_ms() -> u64 {
DEFAULT_BLAST_RADIUS_BUDGET_MS
}
const fn default_audit_max_files() -> usize {
crate::audit::DEFAULT_MAX_AUDIT_FILES
}
pub const DEFAULT_ENABLED_GROUPS: [&str; 23] = [
"aws",
"azure",
"base",
"database",
"docker",
"exfil",
"flyio",
"fs",
"gcp",
"git",
"github",
"heroku",
"kubernetes",
"mongodb",
"mysql",
"netlify",
"network",
"npm",
"psql",
"redis",
"shell",
"terraform",
"vercel",
];
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Challenge {
Enter,
Math,
Yes,
}
#[derive(Debug)]
pub struct Config {
pub root_folder: PathBuf,
pub setting_file_path: PathBuf,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Settings {
#[serde(default)]
pub challenge: Challenge,
#[serde(default = "default_enabled_groups")]
pub enabled_groups: Vec<String>,
#[serde(default)]
pub disabled_groups: Vec<String>,
#[serde(default)]
pub ignores_patterns_ids: Vec<String>,
#[serde(default)]
pub deny_patterns_ids: Vec<String>,
#[serde(default)]
pub context: ContextConfig,
#[serde(default = "default_audit_enabled")]
pub audit_enabled: bool,
#[serde(default = "default_audit_max_files")]
pub audit_max_files: usize,
#[serde(default = "default_blast_radius")]
pub blast_radius: bool,
#[serde(default = "default_blast_radius_budget_ms")]
pub blast_radius_budget_ms: u64,
#[serde(default = "default_approved_notice")]
pub approved_notice: bool,
#[serde(default)]
pub min_severity: Option<Severity>,
#[serde(default)]
pub agent: AgentConfig,
#[serde(default)]
pub llm: Option<LlmConfig>,
#[serde(default)]
pub wrappers: WrappersConfig,
#[serde(default)]
pub severity_escalation: SeverityEscalationConfig,
#[serde(default)]
pub group_escalation: HashMap<String, Challenge>,
#[serde(default)]
pub check_escalation: HashMap<String, Challenge>,
#[serde(default)]
pub sandbox: SandboxConfig,
}
impl fmt::Display for Challenge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Math => write!(f, "Math"),
Self::Enter => write!(f, "Enter"),
Self::Yes => write!(f, "Yes"),
}
}
}
impl Default for Challenge {
fn default() -> Self {
DEFAULT_CHALLENGE
}
}
impl Challenge {
pub fn from_string(str: &str) -> Result<Self> {
match str.to_lowercase().as_str() {
"math" => Ok(Self::Math),
"enter" => Ok(Self::Enter),
"yes" => Ok(Self::Yes),
_ => Err(Error::Config("given challenge name not found".into())),
}
}
#[must_use]
pub fn stricter(self, other: Self) -> Self {
std::cmp::max(self, other)
}
#[must_use]
pub fn is_stricter_than(self, other: Self) -> bool {
self > other
}
}
impl Default for Settings {
fn default() -> Self {
Self {
challenge: DEFAULT_CHALLENGE,
enabled_groups: default_enabled_groups(),
disabled_groups: vec![],
ignores_patterns_ids: vec![],
deny_patterns_ids: vec![],
context: ContextConfig::default(),
audit_enabled: default_audit_enabled(),
audit_max_files: default_audit_max_files(),
blast_radius: default_blast_radius(),
blast_radius_budget_ms: default_blast_radius_budget_ms(),
approved_notice: default_approved_notice(),
min_severity: None,
agent: AgentConfig::default(),
llm: None,
wrappers: WrappersConfig::default(),
severity_escalation: SeverityEscalationConfig::default(),
group_escalation: HashMap::new(),
check_escalation: HashMap::new(),
sandbox: SandboxConfig::default(),
}
}
}
impl Config {
pub fn new(path: Option<&str>) -> Result<Self> {
let package_name = env!("CARGO_PKG_NAME");
let config_folder = match path {
Some(p) => PathBuf::from(p),
None => match config_dir_from_env(std::env::var(CONFIG_DIR_ENV).ok().as_deref())? {
Some(dir) => dir,
None => match dirs::config_dir() {
Some(conf_dir) => conf_dir.join(package_name),
None => return Err(Error::Config("could not get directory path".into())),
},
},
};
let setting_file_path = config_folder.join(DEFAULT_SETTING_FILE_NAME);
let setting_config = Self {
root_folder: config_folder,
setting_file_path,
};
debug!("configuration settings: {setting_config:?}");
Ok(setting_config)
}
pub fn default_location() -> Result<Self> {
let dir = dirs::config_dir()
.map(|d| d.join(env!("CARGO_PKG_NAME")))
.ok_or_else(|| Error::Config("could not get directory path".into()))?;
Self::new(Some(&dir.display().to_string()))
}
#[must_use]
pub fn audit_log_path(&self) -> PathBuf {
self.root_folder.join("audit.log")
}
#[must_use]
pub fn custom_checks_dir(&self) -> PathBuf {
self.root_folder.join("checks")
}
pub fn get_settings_from_file(&self) -> Result<Settings> {
match self.read_config_file() {
Ok(content) => parse_settings_strict(&content).map_err(|e| {
Error::Config(format!(
"invalid settings file {}: {e}",
self.setting_file_path.display()
))
}),
Err(_) if !self.setting_file_path.exists() => Ok(Settings::default()),
Err(e) => Err(Error::Config(format!(
"could not read settings file {}: {e}",
self.setting_file_path.display()
))),
}
}
#[must_use]
pub fn load_settings_lenient(&self) -> (Settings, Option<String>) {
match self.get_settings_from_file() {
Ok(s) => (s, None),
Err(e) => {
let settings = self
.read_config_file()
.map(|content| parse_settings_lenient(&content).0)
.unwrap_or_default();
(settings, Some(e.to_string()))
}
}
}
#[must_use]
pub fn settings_warnings(&self) -> Vec<String> {
let Ok(content) = self.read_config_file() else {
return Vec::new();
};
let mut out = unknown_settings_key_warnings(&content);
if let Some(msg) = serde_yaml::from_str::<serde_yaml::Value>(&content)
.ok()
.and_then(|v| v.get("audit_max_files").and_then(serde_yaml::Value::as_u64))
.filter(|n| *n == 0)
.and_then(|_| audit_max_files_error(0))
{
out.push(format!("{msg}; the default is used"));
}
if let Ok(s) = parse_settings_strict(&content) {
if s.blast_radius_budget_ms > MAX_BLAST_RADIUS_BUDGET_MS {
out.push(format!(
"`blast_radius_budget_ms: {}` is capped at {MAX_BLAST_RADIUS_BUDGET_MS} (ms); a \
longer wait could push agent hooks toward their timeout",
s.blast_radius_budget_ms
));
}
if s.audit_max_files > crate::audit::MAX_AUDIT_FILES_LIMIT {
out.push(format!(
"`audit_max_files: {}` is capped at {}",
s.audit_max_files,
crate::audit::MAX_AUDIT_FILES_LIMIT
));
}
}
out
}
pub fn reset_config(&self) -> Result<()> {
self.ensure_config_dir()?;
self.write_settings_atomically(SETTINGS_HEADER)
}
pub fn backup_settings_file(&self) -> Result<Option<PathBuf>> {
if !self.setting_file_path.is_file() {
return Ok(None);
}
if self
.read_config_file()
.is_ok_and(|c| c.trim() == SETTINGS_HEADER.trim())
{
return Ok(None);
}
let stamp = crate::audit::now_timestamp().replace([':', '-'], "");
let mut backup = self
.setting_file_path
.with_file_name(format!("{DEFAULT_SETTING_FILE_NAME}.bak.{stamp}"));
let mut n = 1;
while backup.exists() {
backup = self
.setting_file_path
.with_file_name(format!("{DEFAULT_SETTING_FILE_NAME}.bak.{stamp}.{n}"));
n += 1;
}
fs::copy(&self.setting_file_path, &backup).map_err(|e| {
Error::Config(format!(
"could not back up {} to {}: {e}",
self.setting_file_path.display(),
backup.display()
))
})?;
self.prune_backups(MAX_SETTINGS_BACKUPS);
Ok(Some(backup))
}
fn prune_backups(&self, keep: usize) {
let prefix = format!("{DEFAULT_SETTING_FILE_NAME}.bak.");
let mut backups: Vec<(std::time::SystemTime, String, PathBuf)> =
fs::read_dir(&self.root_folder)
.into_iter()
.flatten()
.filter_map(std::result::Result::ok)
.filter_map(|e| {
let name = e.file_name().to_string_lossy().to_string();
if !name.starts_with(&prefix) {
return None;
}
let mtime = e.metadata().and_then(|m| m.modified()).ok()?;
Some((mtime, name, e.path()))
})
.collect();
backups.sort();
let excess = backups.len().saturating_sub(keep);
for (_, _, path) in backups.into_iter().take(excess) {
let _ = fs::remove_file(path);
}
}
pub fn cleanup_edit_leftovers(&self, keep: Option<&std::path::Path>) {
for entry in fs::read_dir(&self.root_folder)
.into_iter()
.flatten()
.filter_map(std::result::Result::ok)
{
let name = entry.file_name().to_string_lossy().to_string();
let path = entry.path();
if name.starts_with(".settings.edit-")
&& path
.extension()
.is_some_and(|e| e.eq_ignore_ascii_case("yaml"))
&& Some(path.as_path()) != keep
{
let _ = fs::remove_file(path);
}
}
}
pub fn lock_settings(&self) -> Result<SettingsLock> {
self.ensure_config_dir()?;
let path = self.root_folder.join(".settings.lock");
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt as _;
const ERROR_SHARING_VIOLATION: i32 = 32;
const RETRY: std::time::Duration = std::time::Duration::from_millis(10);
const GIVE_UP: std::time::Duration = std::time::Duration::from_secs(60);
let start = std::time::Instant::now();
loop {
match fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.share_mode(0)
.open(&path)
{
Ok(file) => return Ok(SettingsLock { _file: file }),
Err(e)
if e.raw_os_error() == Some(ERROR_SHARING_VIOLATION)
&& start.elapsed() < GIVE_UP =>
{
std::thread::sleep(RETRY);
}
Err(e) => {
return Err(Error::Config(format!(
"could not lock {}: {e}",
path.display()
)))
}
}
}
}
#[cfg(not(windows))]
let file = fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&path)
.map_err(|e| Error::Config(format!("could not open {}: {e}", path.display())))?;
#[cfg(unix)]
{
match nix::fcntl::Flock::lock(file, nix::fcntl::FlockArg::LockExclusive) {
Ok(l) => Ok(SettingsLock { _lock: Some(l) }),
Err((_, e)) => Err(Error::Config(format!(
"could not lock {}: {e}",
path.display()
))),
}
}
#[cfg(not(any(unix, windows)))]
{
Ok(SettingsLock { _file: file })
}
}
pub fn modify_settings<T>(
&self,
f: impl FnOnce(&mut Settings) -> Result<(T, bool)>,
) -> Result<T> {
let _lock = self.lock_settings()?;
let mut settings = self.get_settings_from_file()?;
let (out, changed) = f(&mut settings)?;
if changed {
self.save_settings_unlocked(&settings)?;
}
Ok(out)
}
pub fn save_settings_text(&self, content: &str) -> Result<()> {
parse_settings_strict(content)?;
self.ensure_config_dir()?;
self.write_settings_atomically(content)
}
fn write_settings_atomically(&self, content: &str) -> Result<()> {
let path = &self.setting_file_path;
let existing_perms = fs::metadata(path).ok().map(|m| m.permissions());
if let Some(perms) = &existing_perms {
if perms.readonly() {
return Err(Error::Config(format!(
"settings file {} is read-only; not modifying it",
path.display()
)));
}
}
let tmp = path.with_file_name(format!(
".{DEFAULT_SETTING_FILE_NAME}.tmp-{}",
std::process::id()
));
let write = || -> std::io::Result<()> {
let mut file = fs::File::create(&tmp)?;
file.write_all(content.as_bytes())?;
file.sync_all()?;
if let Some(perms) = &existing_perms {
fs::set_permissions(&tmp, perms.clone())?;
}
fs::rename(&tmp, path)
};
write().map_err(|e| {
let _ = fs::remove_file(&tmp);
Error::Config(format!(
"could not write settings file {}: {e}",
path.display()
))
})
}
fn ensure_config_dir(&self) -> Result<()> {
if let Err(err) = fs::create_dir_all(&self.root_folder) {
if err.kind() != std::io::ErrorKind::AlreadyExists {
return Err(Error::Config(format!("could not create folder: {err}")));
}
debug!("configuration folder found: {}", self.root_folder.display());
} else {
debug!(
"configuration created in path: {}",
self.root_folder.display()
);
}
Ok(())
}
pub fn save_settings_file_from_struct(&self, settings: &Settings) -> Result<()> {
let _lock = self.lock_settings()?;
self.save_settings_unlocked(settings)
}
fn save_settings_unlocked(&self, settings: &Settings) -> Result<()> {
self.ensure_config_dir()?;
let old_text = self.read_config_file().unwrap_or_default();
let mut root = self.read_config_as_value().map_err(|e| {
Error::Config(format!(
"refusing to overwrite {} because it is not valid YAML ({e}); \
fix it with `shellfirm config edit` or `shellfirm config reset`",
self.setting_file_path.display()
))
})?;
if !root.is_mapping() {
root = serde_yaml::Value::Mapping(serde_yaml::Mapping::default());
}
let new_value = serde_yaml::to_value(settings)?;
let mut default_value = serde_yaml::to_value(Settings::default())?;
if new_value
.get("llm")
.is_some_and(serde_yaml::Value::is_mapping)
{
if let Some(map) = default_value.as_mapping_mut() {
map.insert(
serde_yaml::Value::String("llm".into()),
serde_yaml::to_value(LlmConfig::default())?,
);
}
}
sparse_merge("", &mut root, &new_value, &default_value);
let body = if root.as_mapping().is_some_and(serde_yaml::Mapping::is_empty) {
String::new()
} else {
serde_yaml::to_string(&root)?
};
let mut content = reattach_comments(&old_text, &body);
if content.trim().is_empty() {
content = SETTINGS_HEADER.to_string();
}
self.write_settings_atomically(&content)?;
debug!(
"settings file saved to: {}",
self.setting_file_path.display()
);
Ok(())
}
pub fn read_config_file(&self) -> Result<String> {
Ok(read_regular_string(&self.setting_file_path)?)
}
pub fn read_config_as_value(&self) -> Result<serde_yaml::Value> {
let empty_mapping = || serde_yaml::Value::Mapping(serde_yaml::Mapping::default());
match self.read_config_file() {
Ok(content) => {
let value: serde_yaml::Value = serde_yaml::from_str(&content)?;
if value.is_null() {
Ok(empty_mapping())
} else {
Ok(value)
}
}
Err(_) if !self.setting_file_path.exists() => Ok(empty_mapping()),
Err(e) => Err(e),
}
}
pub fn save_config_from_value(&self, value: &serde_yaml::Value) -> Result<()> {
self.ensure_config_dir()?;
let yaml_str = serde_yaml::to_string(value)?;
let _settings: Settings = parse_settings_strict(&yaml_str)?;
self.write_settings_atomically(&yaml_str)
}
}
impl Settings {
#[must_use]
pub const fn blast_radius_on(&self) -> bool {
self.blast_radius && self.blast_radius_budget_ms > 0
}
pub const fn set_blast_radius_on(&mut self, on: bool) {
self.blast_radius = true;
if !on {
self.blast_radius_budget_ms = 0;
} else if self.blast_radius_budget_ms == 0 {
self.blast_radius_budget_ms = DEFAULT_BLAST_RADIUS_BUDGET_MS;
}
}
#[must_use]
pub fn blast_radius_budget(&self) -> u64 {
self.blast_radius_budget_ms.min(MAX_BLAST_RADIUS_BUDGET_MS)
}
pub fn command_scope(&self) -> crate::env::CommandScope {
crate::env::CommandScope::enter(self.blast_radius_budget())
}
pub fn get_active_checks(&self) -> Result<Vec<checks::Check>> {
let enabled: HashSet<&str> = self.enabled_groups.iter().map(String::as_str).collect();
let disabled: HashSet<&str> = self.disabled_groups.iter().map(String::as_str).collect();
let ignores = self.effective_ignores();
Ok(checks::all_checks_cached()
.iter()
.filter(|c| enabled.contains(c.from.as_str()))
.filter(|c| !disabled.contains(c.from.as_str()))
.filter(|c| !ignores.contains(c.id.as_str()))
.cloned()
.collect())
}
pub fn get_active_checks_with_custom(
&self,
custom: &[checks::Check],
) -> Result<Vec<checks::Check>> {
let enabled: HashSet<&str> = self.enabled_groups.iter().map(String::as_str).collect();
let disabled: HashSet<&str> = self.disabled_groups.iter().map(String::as_str).collect();
let ignores = self.effective_ignores();
let keep = |c: &checks::Check| -> bool {
enabled.contains(c.from.as_str())
&& !disabled.contains(c.from.as_str())
&& !ignores.contains(c.id.as_str())
};
let mut out: Vec<checks::Check> = checks::all_checks_cached()
.iter()
.filter(|c| keep(c))
.cloned()
.collect();
out.extend(custom.iter().filter(|c| keep(c)).cloned());
Ok(out)
}
pub fn migrate_custom_groups_into_enabled_groups(
&mut self,
custom: &[checks::Check],
) -> Vec<String> {
use std::collections::BTreeSet;
let enabled: HashSet<&str> = self.enabled_groups.iter().map(String::as_str).collect();
let disabled: HashSet<&str> = self.disabled_groups.iter().map(String::as_str).collect();
let mut to_add: BTreeSet<String> = BTreeSet::new();
for c in custom {
if !enabled.contains(c.from.as_str()) && !disabled.contains(c.from.as_str()) {
to_add.insert(c.from.clone());
}
}
let added: Vec<String> = to_add.into_iter().collect();
self.enabled_groups.extend(added.iter().cloned());
added
}
#[must_use]
pub fn effective_ignores(&self) -> HashSet<&str> {
self.ignores_patterns_ids
.iter()
.map(String::as_str)
.filter(|id| !self.deny_patterns_ids.iter().any(|d| d == id))
.collect()
}
#[must_use]
pub fn ignore_deny_conflicts(&self) -> Vec<String> {
self.ignores_patterns_ids
.iter()
.filter(|id| self.deny_patterns_ids.contains(id))
.cloned()
.collect()
}
#[must_use]
pub const fn get_active_groups(&self) -> &Vec<String> {
&self.enabled_groups
}
#[must_use]
pub fn default_groups_not_enabled(&self) -> Vec<&'static str> {
DEFAULT_ENABLED_GROUPS
.iter()
.copied()
.filter(|g| {
!self.enabled_groups.iter().any(|e| e == g)
|| self.disabled_groups.iter().any(|d| d == g)
})
.collect()
}
}
#[must_use]
pub fn enable_groups_command(groups: &[&str]) -> String {
let mut cmd = String::from("shellfirm config groups");
for g in groups {
cmd.push_str(" --enable ");
cmd.push_str(g);
}
cmd
}
#[derive(Debug, Clone)]
pub struct ResolvedSettings {
pub challenge: Challenge,
pub min_severity: Option<Severity>,
pub severity_escalation: SeverityEscalationConfig,
}
impl Settings {
#[must_use]
pub fn resolved_for(&self, mode: Mode) -> ResolvedSettings {
let (challenge_o, min_sev_o, esc_o) = match mode {
Mode::Shell => (None, None, None),
Mode::Ai => (
self.agent.challenge.as_set().copied(),
self.agent.min_severity.as_set().copied(),
self.agent.severity_escalation.as_set().cloned(),
),
Mode::Wrap => (
self.wrappers.challenge.as_set().copied(),
self.wrappers.min_severity.as_set().copied(),
None,
),
};
ResolvedSettings {
challenge: challenge_o.unwrap_or(self.challenge),
min_severity: min_sev_o.unwrap_or(self.min_severity),
severity_escalation: esc_o.unwrap_or_else(|| self.severity_escalation.clone()),
}
}
}
pub fn value_set(
root: &mut serde_yaml::Value,
path: &str,
new_value: serde_yaml::Value,
) -> Result<()> {
let segments: Vec<&str> = path.split('.').collect();
let mut current = root;
for (i, segment) in segments.iter().enumerate() {
if i == segments.len() - 1 {
let map = current.as_mapping_mut().ok_or_else(|| {
Error::Config(format!("expected a mapping at parent of '{path}'"))
})?;
map.insert(serde_yaml::Value::String((*segment).to_string()), new_value);
return Ok(());
}
let key = serde_yaml::Value::String((*segment).to_string());
if !current.as_mapping().is_some_and(|m| m.contains_key(&key)) {
let map = current
.as_mapping_mut()
.ok_or_else(|| Error::Config(format!("expected a mapping at '{segment}'")))?;
map.insert(
key,
serde_yaml::Value::Mapping(serde_yaml::Mapping::default()),
);
}
current = current
.get_mut(segment)
.ok_or_else(|| Error::Config(format!("failed to descend into '{segment}'")))?;
}
Ok(())
}
pub fn parse_settings_strict(content: &str) -> Result<Settings> {
let value: serde_yaml::Value =
serde_yaml::from_str(content).map_err(|e| locate_error(content, e))?;
if value.is_null() {
return Ok(Settings::default());
}
if !value.is_mapping() {
return Err(Error::Config(
"expected a YAML mapping of `key: value` settings at the top level".into(),
));
}
let mut settings: Settings =
serde_yaml::from_str(content).map_err(|e| locate_error(content, e))?;
if audit_max_files_error(settings.audit_max_files).is_some() {
settings.audit_max_files = default_audit_max_files();
}
Ok(settings)
}
fn audit_max_files_error(n: usize) -> Option<String> {
(n == 0).then(|| {
format!(
"`audit_max_files` must be at least 1 (0 would delete the audit history on \
every rotation); remove the key to keep the default of {}",
crate::audit::DEFAULT_MAX_AUDIT_FILES
)
})
}
fn locate_error(content: &str, e: serde_yaml::Error) -> Error {
let msg = e.to_string();
if msg.contains(" line ") || !msg.contains("duplicate") {
return Error::Yaml(e);
}
duplicate_key_line(content, &msg).map_or(Error::Yaml(e), |(key, line)| {
Error::Config(format!(
"duplicate key `{key}` at line {line} (each key may appear only once)"
))
})
}
fn duplicate_key_line(content: &str, msg: &str) -> Option<(String, usize)> {
let key = msg
.split_once("with key \"")
.and_then(|(_, rest)| rest.split_once('"'))
.map(|(k, _)| k.to_string())
.or_else(|| {
msg.split_once("duplicate field `")
.and_then(|(_, rest)| rest.split_once('`'))
.map(|(k, _)| k.to_string())
})?;
let mut seen: HashMap<usize, usize> = HashMap::new();
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim_start();
let indent = line.len() - trimmed.len();
let trimmed = trimmed.trim_start_matches("- ");
let Some(rest) = trimmed.strip_prefix(key.as_str()) else {
continue;
};
let rest = rest.trim_start_matches(['"', '\'']);
if rest.trim_start().starts_with(':') {
let n = seen.entry(indent).or_insert(0);
*n += 1;
if *n > 1 {
return Some((key, i + 1));
}
}
}
None
}
fn wrap_path(path: &[serde_yaml::Value], v: serde_yaml::Value) -> serde_yaml::Value {
path.iter().rev().fold(v, |acc, k| {
let mut m = serde_yaml::Mapping::new();
m.insert(k.clone(), acc);
serde_yaml::Value::Mapping(m)
})
}
fn path_label(path: &[serde_yaml::Value]) -> String {
path.iter()
.map(|k| {
k.as_str()
.map_or_else(|| "?".to_string(), ToString::to_string)
})
.collect::<Vec<_>>()
.join(".")
}
fn lenient_value(
path: &mut Vec<serde_yaml::Value>,
v: &serde_yaml::Value,
dropped: &mut Vec<String>,
) -> Option<serde_yaml::Value> {
let probe = |val: serde_yaml::Value, path: &[serde_yaml::Value]| {
serde_yaml::from_value::<Settings>(wrap_path(path, val))
};
let Err(err) = probe(v.clone(), path) else {
return Some(v.clone());
};
if let (Some(map), true) = (v.as_mapping(), path.len() < 4) {
let mut kept = serde_yaml::Mapping::new();
for (k, sub) in map {
path.push(k.clone());
if let Some(ok) = lenient_value(path, sub, dropped) {
kept.insert(k.clone(), ok);
}
path.pop();
}
let kept = serde_yaml::Value::Mapping(kept);
if probe(kept.clone(), path).is_ok() {
return Some(kept);
}
}
dropped.push(format!(
"`{}` ignored (using default): {}",
path_label(path),
one_line(&err.to_string())
));
None
}
#[must_use]
pub fn parse_settings_lenient(content: &str) -> (Settings, Vec<String>) {
let Ok(value) = serde_yaml::from_str::<serde_yaml::Value>(content) else {
return (
Settings::default(),
vec!["the file is not valid YAML; using built-in defaults".into()],
);
};
let Some(map) = value.as_mapping() else {
return (Settings::default(), Vec::new());
};
let mut kept = serde_yaml::Mapping::new();
let mut dropped = Vec::new();
for (k, v) in map {
let mut path = vec![k.clone()];
if let Some(ok) = lenient_value(&mut path, v, &mut dropped) {
kept.insert(k.clone(), ok);
}
}
let mut settings: Settings =
serde_yaml::from_value(serde_yaml::Value::Mapping(kept)).unwrap_or_default();
if let Some(msg) = audit_max_files_error(settings.audit_max_files) {
settings.audit_max_files = default_audit_max_files();
dropped.push(format!("`audit_max_files` ignored (using default): {msg}"));
}
(settings, dropped)
}
#[must_use]
pub fn known_settings_keys() -> Vec<String> {
serde_yaml::to_value(Settings::default())
.ok()
.and_then(|v| {
v.as_mapping().map(|m| {
m.keys()
.filter_map(|k| k.as_str().map(ToString::to_string))
.collect()
})
})
.unwrap_or_default()
}
const WHOLE_MAP_PATHS: &[&str] = &["context.production_env_vars"];
const DYNAMIC_MAP_PATHS: &[&str] = &[
"group_escalation",
"check_escalation",
"context.production_env_vars",
"wrappers.tools",
];
fn settings_schema() -> serde_yaml::Value {
let s = Settings {
llm: Some(LlmConfig {
base_url: Some(String::new()),
..LlmConfig::default()
}),
..Settings::default()
};
serde_yaml::to_value(s).unwrap_or(serde_yaml::Value::Null)
}
fn wrapper_tool_schema() -> serde_yaml::Value {
serde_yaml::to_value(WrapperToolConfig {
delimiter: Some(String::new()),
check_groups: Vec::new(),
})
.unwrap_or(serde_yaml::Value::Null)
}
fn collect_unknown(
prefix: &str,
value: &serde_yaml::Value,
schema: &serde_yaml::Value,
out: &mut Vec<String>,
) {
let (Some(map), Some(schema_map)) = (value.as_mapping(), schema.as_mapping()) else {
return;
};
for (k, v) in map {
let Some(name) = k.as_str() else { continue };
let path = if prefix.is_empty() {
name.to_string()
} else {
format!("{prefix}.{name}")
};
match schema_map.get(k) {
None => out.push(path),
Some(sub) => {
if path == "wrappers.tools" {
if let Some(tools) = v.as_mapping() {
let tool_schema = wrapper_tool_schema();
for (tk, tv) in tools {
let tp = format!("{path}.{}", tk.as_str().unwrap_or("?"));
collect_unknown(&tp, tv, &tool_schema, out);
}
}
} else if !DYNAMIC_MAP_PATHS.contains(&path.as_str()) {
collect_unknown(&path, v, sub, out);
}
}
}
}
}
#[must_use]
pub fn unknown_settings_keys(content: &str) -> Vec<String> {
let Ok(value) = serde_yaml::from_str::<serde_yaml::Value>(content) else {
return Vec::new();
};
let mut out = Vec::new();
collect_unknown("", &value, &settings_schema(), &mut out);
out
}
#[must_use]
pub fn unknown_settings_key_warnings(content: &str) -> Vec<String> {
let schema = settings_schema();
unknown_settings_keys(content)
.into_iter()
.map(|path| {
let (parent, leaf) = path.rsplit_once('.').unwrap_or(("", path.as_str()));
let siblings: Vec<String> = parent
.split('.')
.filter(|p| !p.is_empty())
.try_fold(&schema, |node, seg| node.get(seg))
.and_then(serde_yaml::Value::as_mapping)
.map(|m| {
m.keys()
.filter_map(|k| k.as_str().map(ToString::to_string))
.collect()
})
.unwrap_or_default();
let hint = suggest_similar(leaf, siblings.iter().map(String::as_str), 1)
.into_iter()
.next()
.map_or_else(String::new, |s| format!(" — did you mean `{s}`?"));
format!("unknown setting `{path}` is ignored{hint}")
})
.collect()
}
fn term_is_monochrome(term: &str) -> bool {
let t = term.trim().to_ascii_lowercase();
matches!(
t.as_str(),
"" | "dumb"
| "unknown"
| "vt52"
| "vt100"
| "vt102"
| "vt220"
| "vt320"
| "ansi-m"
| "linux-m"
) || t.ends_with("-mono")
|| t.ends_with("-m")
}
#[must_use]
pub fn color_preference(get: impl Fn(&str) -> Option<String>) -> Option<bool> {
if get("CLICOLOR_FORCE").is_some_and(|v| !v.is_empty() && v != "0") {
return Some(true);
}
if get("NO_COLOR").is_some_and(|v| !v.is_empty()) || get("CLICOLOR").as_deref() == Some("0") {
return Some(false);
}
if get("TERM").is_some_and(|t| term_is_monochrome(&t)) {
return Some(false);
}
None
}
#[must_use]
pub fn unicode_locale(get: impl Fn(&str) -> Option<String>) -> bool {
["LC_ALL", "LC_CTYPE", "LANG"]
.into_iter()
.filter_map(&get)
.find(|v| !v.is_empty())
.is_none_or(|l| {
let l = l.to_ascii_lowercase();
l.contains("utf-8") || l.contains("utf8")
})
}
#[must_use]
pub fn unicode_output() -> bool {
static U: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*U.get_or_init(|| unicode_locale(|k| std::env::var(k).ok()))
}
#[must_use]
pub fn glyph(unicode: &'static str, ascii: &'static str) -> &'static str {
if unicode_output() {
unicode
} else {
ascii
}
}
#[must_use]
pub fn ascii_fallback(text: &str) -> String {
if unicode_output() {
return text.to_string();
}
text.replace('—', "-")
.replace('→', "->")
.replace('✓', "ok")
.replace('✗', "x")
.replace('…', "...")
.replace('·', "-")
.replace('‹', "<")
.replace('›', ">")
.replace('×', "x")
}
fn levenshtein(a: &str, b: &str) -> usize {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
let mut prev: Vec<usize> = (0..=b.len()).collect();
let mut cur = vec![0; b.len() + 1];
for (i, ca) in a.iter().enumerate() {
cur[0] = i + 1;
for (j, cb) in b.iter().enumerate() {
let cost = usize::from(ca != cb);
cur[j + 1] = (prev[j] + cost).min(prev[j + 1] + 1).min(cur[j] + 1);
}
std::mem::swap(&mut prev, &mut cur);
}
prev[b.len()]
}
#[must_use]
pub fn suggest_similar<'a>(
needle: &str,
candidates: impl IntoIterator<Item = &'a str>,
max: usize,
) -> Vec<String> {
let norm = |s: &str| -> String {
s.to_lowercase()
.chars()
.filter(|c| *c != '_' && *c != '-')
.collect()
};
let n_low = needle.to_lowercase();
let n_norm = norm(needle);
if n_norm.is_empty() {
return Vec::new();
}
let n_name = n_low.rsplit(':').next().unwrap_or(&n_low).to_string();
let mut scored: Vec<(usize, String)> = candidates
.into_iter()
.filter(|c| !c.eq_ignore_ascii_case(needle))
.filter_map(|c| {
let c_low = c.to_lowercase();
let c_norm = norm(c);
let mut score = levenshtein(&n_low, &c_low).min(levenshtein(&n_norm, &c_norm));
let c_name = c_low.rsplit(':').next().unwrap_or(&c_low);
if norm(c_name) == norm(&n_name) {
score = score.min(1);
}
if c_norm.starts_with(&n_norm) && n_norm.len() >= 3 {
score = score.min(1);
}
if is_one_transposition(&n_low, &c_low) {
score = score.min(1);
}
let limit = (n_norm.len() / 3).clamp(1, 3);
if score <= limit {
return Some((score, c.to_string()));
}
if n_name.len() >= 4 && c_low.contains(&n_name) {
return Some((limit + 1, c.to_string()));
}
None
})
.collect();
scored.sort();
scored.dedup_by(|a, b| a.1 == b.1);
scored.into_iter().take(max).map(|(_, c)| c).collect()
}
fn is_one_transposition(a: &str, b: &str) -> bool {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
if a.len() != b.len() || a.len() < 2 {
return false;
}
let diff: Vec<usize> = (0..a.len()).filter(|&i| a[i] != b[i]).collect();
matches!(diff.as_slice(), [i, j] if *j == i + 1 && a[*i] == b[*j] && a[*j] == b[*i])
}
pub const EXIT_USAGE: i32 = 2;
#[must_use]
pub fn unknown_group_message<'a>(
name: &str,
available: impl IntoIterator<Item = &'a str>,
) -> String {
let mut groups: Vec<&str> = available.into_iter().collect();
groups.sort_unstable();
groups.dedup();
let hint = suggest_similar(name, groups.iter().copied(), 3);
let hint = if hint.is_empty() {
String::new()
} else {
format!(" Did you mean: {}?", hint.join(", "))
};
crate::context::escape_invisible(&format!(
"unknown check group '{name}'.{hint} Known groups: {}",
groups.join(", ")
))
}
#[must_use]
pub fn check_id_format_problem(id: &str) -> Option<String> {
let shown = crate::context::escape_invisible(id);
let Some((group, name)) = id.split_once(':') else {
return Some(format!(
"id `{shown}` must have the form `group:name` (lowercase letters, digits, `_`; \
the group may also use `-`)"
));
};
let group_ok = !group.is_empty()
&& group
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_');
let name_ok = !name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
if group_ok && name_ok {
None
} else {
Some(format!(
"id `{shown}` should be lowercase `group:name` (letters, digits, `_`; the group may \
also use `-`), like the built-in checks"
))
}
}
#[must_use]
pub fn check_id_has_hidden_chars(id: &str) -> bool {
id.chars()
.any(|c| c.is_whitespace() || c.is_control() || crate::context::is_invisible(c))
}
#[derive(Debug, Default, Clone)]
pub struct CustomChecksLoad {
pub checks: Vec<checks::Check>,
pub warnings: Vec<String>,
pub explicit_challenges: HashMap<String, Challenge>,
}
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn load_custom_checks_report(checks_dir: &std::path::Path) -> CustomChecksLoad {
let mut out = CustomChecksLoad::default();
if !checks_dir.is_dir() {
return out;
}
let mut paths: Vec<PathBuf> = match fs::read_dir(checks_dir) {
Ok(entries) => {
let all: Vec<PathBuf> = entries
.filter_map(std::result::Result::ok)
.map(|e| e.path())
.collect();
let mut subdirs: Vec<&PathBuf> = all
.iter()
.filter(|p| p.is_dir() && dir_has_yaml(p))
.collect();
subdirs.sort();
for d in subdirs {
out.warnings.push(format!(
"{}: subdirectory ignored (custom checks are read only from *.yaml / *.yml \
files directly in {}; move the files up)",
d.display(),
checks_dir.display()
));
}
all.into_iter()
.filter(|p| p.is_file())
.filter(|p| p.extension().is_some_and(|e| e == "yaml" || e == "yml"))
.collect()
}
Err(e) => {
out.warnings.push(format!(
"could not read custom checks directory {}: {e}",
checks_dir.display()
));
return out;
}
};
paths.sort();
let builtin: HashSet<&str> = checks::all_checks_cached()
.iter()
.map(|c| c.id.as_str())
.collect();
let mut seen: HashSet<String> = HashSet::new();
for path in paths {
let content = match fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
out.warnings
.push(format!("{}: skipped (could not read: {e})", path.display()));
continue;
}
};
let raw: serde_yaml::Value = match serde_yaml::from_str(&content) {
Ok(v) => v,
Err(e) => {
out.warnings
.push(format!("{}: skipped (invalid YAML: {e})", path.display()));
continue;
}
};
if raw.is_null() {
continue;
}
if !raw.is_sequence() {
out.warnings.push(format!(
"{}: skipped (expected a YAML list of checks)",
path.display()
));
continue;
}
let parsed: Vec<checks::Check> = match serde_yaml::from_str(&content) {
Ok(v) => v,
Err(e) => {
out.warnings
.push(format!("{}: skipped ({e})", path.display()));
continue;
}
};
let raw_items = raw.as_sequence().cloned().unwrap_or_default();
for (check, raw_item) in parsed.into_iter().zip(raw_items) {
if check.id.trim().is_empty() {
out.warnings.push(format!(
"{}: skipped a check with an empty id",
path.display()
));
continue;
}
if check_id_has_hidden_chars(&check.id) {
out.warnings.push(format!(
"{}: skipped check `{}` (the id contains spaces, control, bidi or \
zero-width characters)",
path.display(),
crate::context::escape_invisible(&check.id)
));
continue;
}
if let Some(problem) = check_id_format_problem(&check.id) {
out.warnings
.push(format!("{}: check {problem}", path.display()));
}
if builtin.contains(check.id.as_str()) {
out.warnings.push(format!(
"{}: skipped check `{}` (the id is already used by a built-in check)",
path.display(),
check.id
));
continue;
}
if !seen.insert(check.id.clone()) {
out.warnings.push(format!(
"{}: skipped check `{}` (duplicate id; the first definition wins)",
path.display(),
check.id
));
continue;
}
if let Some(m) = raw_item.as_mapping() {
for k in m.keys().filter_map(serde_yaml::Value::as_str) {
if !crate::policy::CHECK_KEYS.contains(&k) {
let hint = suggest_similar(k, crate::policy::CHECK_KEYS.iter().copied(), 1)
.into_iter()
.next()
.map_or_else(String::new, |s| format!(" — did you mean `{s}`?"));
out.warnings.push(format!(
"{}: check `{}`: unknown key `{k}` is ignored{hint}",
path.display(),
check.id
));
}
}
}
let mut check = check;
if check
.alternative
.as_deref()
.is_some_and(|a| a.trim().is_empty())
{
out.warnings.push(format!(
"{}: check `{}`: empty `alternative` is ignored (remove the key or give a \
command)",
path.display(),
check.id
));
check.alternative = None;
}
if check
.alternative_info
.as_deref()
.is_some_and(|a| a.trim().is_empty())
{
out.warnings.push(format!(
"{}: check `{}`: empty `alternative_info` is ignored",
path.display(),
check.id
));
check.alternative_info = None;
}
if check.alternative.is_none() && check.alternative_info.is_some() {
out.warnings.push(format!(
"{}: check `{}`: `alternative_info` without an `alternative` is not shown",
path.display(),
check.id
));
}
let removed = crate::policy::removed_text_chars(&check);
if crate::policy::sanitize_check_text(&mut check) && !removed.is_empty() {
out.warnings.push(format!(
"{}: check `{}`: removed {} from its text (control or bidi characters \
are not shown)",
path.display(),
check.id,
removed.join(", ")
));
}
let groups = check.test.captures_len().saturating_sub(1);
for f in &check.filters {
if let checks::Filter::PathExists(n) = f {
if *n > groups {
out.warnings.push(format!(
"{}: check `{}`: PathExists capture group {n} does not exist (the \
regex has {groups}); the check never matches",
path.display(),
check.id
));
}
}
}
if raw_item.get("challenge").is_some() {
out.explicit_challenges
.insert(check.id.clone(), check.challenge);
}
if let Some(w) = alternative_trigger_warning(&check) {
out.warnings.push(format!("{}: {w}", path.display()));
}
out.checks.push(check);
}
}
let mut seen_w = HashSet::new();
out.warnings = out
.warnings
.iter()
.map(|w| crate::policy::sanitize_untrusted_text(&one_line(w)))
.filter(|w| seen_w.insert(w.clone()))
.collect();
out
}
struct EveryPathExists;
impl crate::env::Environment for EveryPathExists {
fn var(&self, _key: &str) -> Option<String> {
None
}
fn current_dir(&self) -> Result<std::path::PathBuf> {
Ok(std::path::PathBuf::from("/"))
}
fn path_exists(&self, _path: &std::path::Path) -> bool {
true
}
fn home_dir(&self) -> Option<std::path::PathBuf> {
None
}
fn run_command(&self, _cmd: &str, _args: &[&str], _timeout_ms: u64) -> Option<String> {
None
}
fn read_file(&self, path: &std::path::Path) -> Result<String> {
Err(Error::Other(format!("not read: {}", path.display())))
}
fn find_file_upward(
&self,
_start: &std::path::Path,
_name: &str,
) -> Option<std::path::PathBuf> {
None
}
}
fn alternative_trigger_warning(check: &checks::Check) -> Option<String> {
let alt = check.alternative.as_deref()?;
let hit =
checks::run_check_on_command_with_env(checks::all_checks_cached(), alt, &EveryPathExists)
.into_iter()
.filter(|m| m.id != check.id && m.severity >= check.severity)
.max_by_key(|m| m.severity)?;
Some(format!(
"check `{}` ({}): its alternative `{alt}` triggers the built-in check `{}` ({}); \
suggest a safer command",
check.id, check.severity, hit.id, hit.severity
))
}
fn dir_has_yaml(dir: &std::path::Path) -> bool {
fs::read_dir(dir).is_ok_and(|entries| {
entries.filter_map(std::result::Result::ok).any(|e| {
e.path()
.extension()
.is_some_and(|x| x == "yaml" || x == "yml")
})
})
}
#[must_use]
pub fn one_line(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}
#[must_use]
pub fn known_check_ids(custom: &[checks::Check]) -> HashSet<String> {
checks::all_checks_cached()
.iter()
.chain(custom.iter())
.map(|c| c.id.clone())
.collect()
}
pub const IGNORABLE_BUILTIN_RULE_IDS: &[&str] = &["shellfirm-self:agent_instructions_edit"];
pub const ALWAYS_ON_BUILTIN_RULE_IDS: &[&str] = &[
"shellfirm-self:credential_file_edit",
"shellfirm-self:persistence_file_edit",
"shellfirm-self:persistence_file_write",
"shellfirm-self:persistence_install",
"shellfirm-self:invalid_project_policy",
"shellfirm:input_too_large",
"shellfirm:invalid_hook_input",
];
pub const BUILTIN_RULE_IDS: &[&str] = &[
"shellfirm-self:agent_hooks_bypass",
"shellfirm-self:agent_instructions_edit",
"shellfirm-self:audit_clear",
"shellfirm-self:bash_hook_options",
"shellfirm-self:bulk_rewrite_protected",
"shellfirm-self:config_change",
"shellfirm-self:config_env_override",
"shellfirm-self:connect_fail_open",
"shellfirm-self:connect_uninstall",
"shellfirm-self:credential_file_edit",
"shellfirm-self:init_uninstall",
"shellfirm-self:install_other_version",
"shellfirm-self:invalid_project_policy",
"shellfirm-self:persistence_file_edit",
"shellfirm-self:persistence_file_write",
"shellfirm-self:persistence_install",
"shellfirm-self:protected_file_edit",
"shellfirm-self:protected_file_write",
"shellfirm-self:rc_file_edit",
"shellfirm-self:rc_hook_edit",
"shellfirm-self:remove_zsh_hook",
"shellfirm-self:reset_hook_arrays",
"shellfirm-self:sandbox_remove",
"shellfirm-self:shadow_binary",
"shellfirm-self:trap_debug",
"shellfirm-self:uninstall_binary",
"shellfirm-self:unset_hook_function",
"shellfirm-self:zle_accept_line",
"shellfirm:input_too_large",
"shellfirm:invalid_hook_input",
];
#[must_use]
pub fn is_builtin_protection_id(id: &str) -> bool {
id.starts_with("shellfirm-self:") || id.starts_with("shellfirm:")
}
#[must_use]
pub fn builtin_protection_refusal(id: &str, list: &str) -> Option<String> {
if !is_builtin_protection_id(id) {
return None;
}
let ignorable = IGNORABLE_BUILTIN_RULE_IDS.contains(&id);
if list == "ignores_patterns_ids" && ignorable {
return None;
}
let shown = crate::context::escape_invisible(id);
if !BUILTIN_RULE_IDS.contains(&id) {
let mut msg =
format!("unknown check id `{shown}`: no check or built-in agent protection has it");
if let Some(s) = suggest_similar(id, BUILTIN_RULE_IDS.iter().copied(), 1)
.into_iter()
.next()
{
msg.push_str("; did you mean `");
msg.push_str(&s);
msg.push_str("`?");
}
return Some(msg);
}
let mut msg = if ALWAYS_ON_BUILTIN_RULE_IDS.contains(&id) {
format!(
"`{shown}` is a built-in agent protection, not a check; it is always on (even with \
`agent.self_protection: false`) and cannot be ignored or denied by id"
)
} else {
format!(
"`{shown}` is a built-in agent protection, not a check; it is controlled by \
`agent.self_protection` in settings.yaml (`shellfirm config edit`)"
)
};
if ALWAYS_ON_BUILTIN_RULE_IDS.contains(&id) {
return Some(msg);
}
if list == "deny_patterns_ids" {
msg.push_str(", and it already blocks or asks on its own");
}
if ignorable {
msg.push_str("; it can only be ignored, not denied");
} else if list == "ignores_patterns_ids" {
msg.push_str("; the only one that can be ignored by id is ");
msg.push_str(&IGNORABLE_BUILTIN_RULE_IDS.join(", "));
}
Some(msg)
}
pub const ELEVATED_CONTEXTS: &str = "SSH, or git / kubectl did not answer in time";
pub const CRITICAL_CONTEXTS: &str = "root, production k8s / env, sensitive paths, and commands \
that push to, rewrite or delete a protected branch";
#[must_use]
pub fn known_groups(custom: &[checks::Check]) -> std::collections::BTreeSet<String> {
checks::all_checks_cached()
.iter()
.chain(custom.iter())
.map(|c| c.from.clone())
.collect()
}
#[must_use]
pub fn unknown_reference_warnings(
settings: &Settings,
custom: &[checks::Check],
extra_ids: &[String],
) -> Vec<String> {
let mut ids = known_check_ids(custom);
ids.extend(extra_ids.iter().cloned());
let groups = known_groups(custom);
let mut out = Vec::new();
let id_lists: [(&str, Vec<&String>); 3] = [
(
"ignores_patterns_ids",
settings.ignores_patterns_ids.iter().collect(),
),
(
"deny_patterns_ids",
settings.deny_patterns_ids.iter().collect(),
),
("check_escalation", {
let mut v: Vec<&String> = settings.check_escalation.keys().collect();
v.sort();
v
}),
];
for (key, list) in id_lists {
for id in list {
if key != "check_escalation" && is_builtin_protection_id(id) {
if let Some(why) = builtin_protection_refusal(id, key) {
out.push(format!("`{key}`: {why} (it has no effect here)"));
}
continue;
}
if !ids.contains(id.as_str()) {
let hint = suggest_similar(id, ids.iter().map(String::as_str), 3);
let hint = if hint.is_empty() {
String::new()
} else {
crate::context::escape_invisible(&format!(
" — did you mean {}?",
hint.join(", ")
))
};
let effect = if key == "deny_patterns_ids" {
"it denies nothing"
} else {
"it has no effect"
};
out.push(format!(
"`{key}`: unknown check id `{}` ({effect}){hint}",
crate::context::escape_invisible(id)
));
}
}
}
let group_lists: [(&str, Vec<&String>); 3] = [
("enabled_groups", settings.enabled_groups.iter().collect()),
("disabled_groups", settings.disabled_groups.iter().collect()),
("group_escalation", {
let mut v: Vec<&String> = settings.group_escalation.keys().collect();
v.sort();
v
}),
];
for (key, list) in group_lists {
for g in list {
if !groups.contains(g.as_str()) {
let hint = suggest_similar(g, groups.iter().map(String::as_str), 3);
let hint = if hint.is_empty() {
String::new()
} else {
crate::context::escape_invisible(&format!(
" — did you mean {}?",
hint.join(", ")
))
};
out.push(format!(
"`{key}`: unknown group `{}` (it has no effect){hint}",
crate::context::escape_invisible(g)
));
}
}
}
out
}
impl Settings {
pub fn apply_custom_check_challenges(&mut self, explicit: &HashMap<String, Challenge>) {
for (id, ch) in explicit {
let entry = self.check_escalation.entry(id.clone()).or_insert(*ch);
*entry = entry.stricter(*ch);
}
}
pub fn get_active_checks_enforced(
&self,
custom: &[checks::Check],
policy: Option<&crate::policy::ProjectPolicy>,
) -> Result<Vec<checks::Check>> {
let mut out = self.get_active_checks_with_custom(custom)?;
let Some(policy) = policy else {
return Ok(out);
};
let named = policy.referenced_check_ids();
if named.is_empty() {
return Ok(out);
}
let present: HashSet<String> = out.iter().map(|c| c.id.clone()).collect();
let policy_own: HashSet<&str> = policy.checks.iter().map(|c| c.id.as_str()).collect();
for c in checks::all_checks_cached().iter().chain(custom.iter()) {
if named.contains(&c.id)
&& !present.contains(&c.id)
&& !policy_own.contains(c.id.as_str())
{
out.push(c.clone());
}
}
Ok(out)
}
}
#[cfg(test)]
mod test_config {
use std::fs::read_dir;
use tree_fs::Tree;
use super::*;
fn initialize_config_folder(temp_dir: &Tree) -> Config {
let temp_dir = temp_dir.root.join("app");
Config::new(Some(&temp_dir.display().to_string())).unwrap()
}
fn initialize_config_folder_with_file(temp_dir: &Tree) -> Config {
let config = initialize_config_folder(temp_dir);
config.reset_config().unwrap();
config
}
#[test]
fn new_config_does_not_create_files() {
let temp_dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = initialize_config_folder(&temp_dir);
assert!(!config.root_folder.is_dir());
assert!(!config.setting_file_path.is_file());
}
#[test]
fn get_settings_returns_defaults_without_file() {
let temp_dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = initialize_config_folder(&temp_dir);
let settings = config.get_settings_from_file().unwrap();
assert_eq!(settings.challenge, DEFAULT_CHALLENGE);
assert_eq!(settings.enabled_groups, default_enabled_groups());
assert!(settings.audit_enabled);
}
#[test]
fn default_groups_not_enabled_lists_missing_and_disabled_defaults() {
assert!(Settings::default().default_groups_not_enabled().is_empty());
let mut enabled: Vec<String> = default_enabled_groups()
.into_iter()
.filter(|g| g != "shell" && g != "exfil")
.collect();
enabled.push("my_team".into());
let s = parse_settings_strict(
&serde_yaml::to_string(&serde_yaml::Mapping::from_iter([
(
"enabled_groups".into(),
serde_yaml::to_value(&enabled).unwrap(),
),
(
"disabled_groups".into(),
serde_yaml::to_value(["docker"]).unwrap(),
),
]))
.unwrap(),
)
.unwrap();
assert_eq!(
s.default_groups_not_enabled(),
vec!["docker", "exfil", "shell"]
);
let s = parse_settings_strict("enabled_groups: [git, fs]\n").unwrap();
let missing = s.default_groups_not_enabled();
assert!(!missing.contains(&"git") && missing.contains(&"aws"));
assert_eq!(
enable_groups_command(&["shell", "exfil"]),
"shellfirm config groups --enable shell --enable exfil"
);
}
#[test]
fn can_reset_config() {
let temp_dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = initialize_config_folder_with_file(&temp_dir);
let mut settings = config.get_settings_from_file().unwrap();
settings.challenge = Challenge::Yes;
config.save_settings_file_from_struct(&settings).unwrap();
assert_eq!(
config.get_settings_from_file().unwrap().challenge,
Challenge::Yes
);
config.reset_config().unwrap();
assert_eq!(
config.get_settings_from_file().unwrap().challenge,
Challenge::Math
);
let files: Vec<String> = read_dir(&config.root_folder)
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().to_string())
.filter(|n| n != ".settings.lock")
.collect();
assert_eq!(files, vec!["settings.yaml".to_string()]);
}
#[test]
fn read_config_as_value_empty_file_returns_empty_mapping() {
let temp_dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = initialize_config_folder_with_file(&temp_dir);
let root = config.read_config_as_value().unwrap();
let mapping = root.as_mapping().expect("should be a Mapping, not Null");
assert!(mapping.is_empty());
let mut root = root;
value_set(
&mut root,
"challenge",
serde_yaml::Value::String("Enter".into()),
)
.unwrap();
assert_eq!(root.get("challenge").unwrap().as_str().unwrap(), "Enter");
}
#[test]
fn sparse_config_on_fresh_install() {
let temp_dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = initialize_config_folder(&temp_dir);
assert!(!config.setting_file_path.exists());
let root = config.read_config_as_value().unwrap();
assert!(root.as_mapping().unwrap().is_empty());
let mut root = root;
value_set(
&mut root,
"challenge",
serde_yaml::Value::String("Yes".into()),
)
.unwrap();
config.save_config_from_value(&root).unwrap();
let content = config.read_config_file().unwrap();
assert!(content.contains("challenge"));
assert!(!content.contains("enabled_groups"));
let settings = config.get_settings_from_file().unwrap();
assert_eq!(settings.challenge, Challenge::Yes);
assert_eq!(settings.enabled_groups, default_enabled_groups());
}
#[test]
fn mode_enum_variants_exist() {
let _ = Mode::Shell;
let _ = Mode::Ai;
let _ = Mode::Wrap;
}
#[test]
fn mode_is_copy_and_eq() {
let m1 = Mode::Shell;
let m2 = m1;
assert_eq!(m1, m2);
assert_ne!(Mode::Shell, Mode::Ai);
}
#[test]
fn inherit_or_deserializes_inherit_keyword() {
let v: InheritOr<String> = serde_yaml::from_str("inherit").unwrap();
assert!(matches!(v, InheritOr::Inherit));
}
#[test]
fn inherit_or_deserializes_set_value() {
let v: InheritOr<String> = serde_yaml::from_str("\"hello\"").unwrap();
assert!(matches!(v, InheritOr::Set(s) if s == "hello"));
}
#[test]
fn inherit_or_serializes_inherit_as_keyword() {
let v: InheritOr<String> = InheritOr::Inherit;
let s = serde_yaml::to_string(&v).unwrap();
assert_eq!(s.trim(), "inherit");
}
#[test]
fn inherit_or_serializes_set_value() {
let v: InheritOr<String> = InheritOr::Set("hi".into());
let s = serde_yaml::to_string(&v).unwrap();
assert_eq!(s.trim(), "hi");
}
#[test]
fn inherit_or_default_is_inherit() {
let v: InheritOr<u32> = InheritOr::default();
assert!(matches!(v, InheritOr::Inherit));
}
#[test]
fn inherit_or_with_option_inner_handles_null() {
let v: InheritOr<Option<Severity>> = serde_yaml::from_str("null").unwrap();
assert!(matches!(v, InheritOr::Set(None)));
}
#[test]
fn inherit_or_with_option_inner_handles_inherit() {
let v: InheritOr<Option<Severity>> = serde_yaml::from_str("inherit").unwrap();
assert!(matches!(v, InheritOr::Inherit));
}
#[test]
fn agent_config_default_has_inherit_overrides() {
let a = AgentConfig::default();
assert!(matches!(a.challenge, InheritOr::Inherit));
assert!(matches!(a.min_severity, InheritOr::Inherit));
assert!(matches!(a.severity_escalation, InheritOr::Inherit));
}
#[test]
fn agent_config_loads_old_yaml_with_no_overrides() {
let yaml = "auto_deny_severity: High\nrequire_human_approval: false\n";
let a: AgentConfig = serde_yaml::from_str(yaml).unwrap();
assert!(matches!(a.challenge, InheritOr::Inherit));
assert!(matches!(a.min_severity, InheritOr::Inherit));
}
#[test]
fn agent_config_loads_explicit_overrides() {
let yaml = "auto_deny_severity: High\n\
require_human_approval: false\n\
challenge: Yes\n\
min_severity: Low\n";
let a: AgentConfig = serde_yaml::from_str(yaml).unwrap();
assert!(matches!(a.challenge, InheritOr::Set(Challenge::Yes)));
assert!(matches!(
a.min_severity,
InheritOr::Set(Some(Severity::Low))
));
}
#[test]
fn wrappers_config_default_has_inherit_overrides() {
let w = WrappersConfig::default();
assert!(matches!(w.challenge, InheritOr::Inherit));
assert!(matches!(w.min_severity, InheritOr::Inherit));
}
#[test]
fn wrappers_config_loads_old_yaml() {
let yaml = "tools: {}\n";
let w: WrappersConfig = serde_yaml::from_str(yaml).unwrap();
assert!(matches!(w.challenge, InheritOr::Inherit));
assert!(matches!(w.min_severity, InheritOr::Inherit));
}
#[test]
fn wrappers_config_loads_explicit_overrides() {
let yaml = "tools: {}\nchallenge: Yes\nmin_severity: High\n";
let w: WrappersConfig = serde_yaml::from_str(yaml).unwrap();
assert!(matches!(w.challenge, InheritOr::Set(Challenge::Yes)));
assert!(matches!(
w.min_severity,
InheritOr::Set(Some(Severity::High))
));
}
#[test]
fn resolved_for_shell_returns_global() {
let mut s = Settings::default();
s.challenge = Challenge::Math;
s.min_severity = Some(Severity::Medium);
let r = s.resolved_for(Mode::Shell);
assert_eq!(r.challenge, Challenge::Math);
assert_eq!(r.min_severity, Some(Severity::Medium));
}
#[test]
fn resolved_for_ai_inherits_when_no_overrides() {
let mut s = Settings::default();
s.challenge = Challenge::Math;
s.min_severity = Some(Severity::Medium);
let r = s.resolved_for(Mode::Ai);
assert_eq!(r.challenge, Challenge::Math);
assert_eq!(r.min_severity, Some(Severity::Medium));
}
#[test]
fn resolved_for_ai_uses_overrides_when_set() {
let mut s = Settings::default();
s.challenge = Challenge::Math;
s.min_severity = Some(Severity::Medium);
s.agent.challenge = InheritOr::Set(Challenge::Yes);
s.agent.min_severity = InheritOr::Set(Some(Severity::Low));
let r = s.resolved_for(Mode::Ai);
assert_eq!(r.challenge, Challenge::Yes);
assert_eq!(r.min_severity, Some(Severity::Low));
}
#[test]
fn resolved_for_wrap_uses_wrappers_overrides() {
let mut s = Settings::default();
s.challenge = Challenge::Math;
s.wrappers.challenge = InheritOr::Set(Challenge::Yes);
let r = s.resolved_for(Mode::Wrap);
assert_eq!(r.challenge, Challenge::Yes);
}
#[test]
fn resolved_for_severity_escalation_override_ai() {
let mut s = Settings::default();
let mut custom = SeverityEscalationConfig::default();
custom.high = Challenge::Yes;
s.agent.severity_escalation = InheritOr::Set(custom.clone());
let r = s.resolved_for(Mode::Ai);
assert_eq!(r.severity_escalation.high, Challenge::Yes);
let r_shell = s.resolved_for(Mode::Shell);
assert_eq!(r_shell.severity_escalation.high, Challenge::Math); }
#[test]
fn get_active_checks_with_custom_excludes_disabled_custom_group() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.enabled_groups = vec!["git".into()]; let custom = vec![Check {
id: "my_team:thing".into(),
test: Regex::new("foo").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let result = s.get_active_checks_with_custom(&custom).unwrap();
assert!(result.iter().all(|c| c.id != "my_team:thing"));
}
#[test]
fn get_active_checks_with_custom_includes_enabled_custom_group() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.enabled_groups = vec!["git".into(), "my_team".into()];
let custom = vec![Check {
id: "my_team:thing".into(),
test: Regex::new("foo").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let result = s.get_active_checks_with_custom(&custom).unwrap();
assert!(result.iter().any(|c| c.id == "my_team:thing"));
}
#[test]
fn get_active_checks_with_custom_respects_ignores() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.enabled_groups = vec!["my_team".into()];
s.ignores_patterns_ids = vec!["my_team:thing".into()];
let custom = vec![Check {
id: "my_team:thing".into(),
test: Regex::new("foo").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let result = s.get_active_checks_with_custom(&custom).unwrap();
assert!(result.iter().all(|c| c.id != "my_team:thing"));
}
#[test]
fn migrate_custom_groups_into_enabled_groups_adds_missing() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.enabled_groups = vec!["git".into()];
let custom = vec![Check {
id: "my_team:foo".into(),
test: Regex::new("x").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let added = s.migrate_custom_groups_into_enabled_groups(&custom);
assert_eq!(added, vec!["my_team".to_string()]);
assert!(s.enabled_groups.contains(&"my_team".to_string()));
}
#[test]
fn migrate_custom_groups_idempotent() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.enabled_groups = vec!["git".into(), "my_team".into()];
let custom = vec![Check {
id: "my_team:foo".into(),
test: Regex::new("x").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let added = s.migrate_custom_groups_into_enabled_groups(&custom);
assert!(added.is_empty());
}
#[test]
fn migrate_custom_groups_skips_disabled_groups() {
use crate::checks::Check;
use regex::Regex;
let mut s = Settings::default();
s.disabled_groups = vec!["my_team".into()];
let custom = vec![Check {
id: "my_team:foo".into(),
test: Regex::new("x").unwrap().into(),
description: "x".into(),
from: "my_team".into(),
challenge: Challenge::Math,
filters: vec![],
alternative: None,
alternative_info: None,
severity: Severity::Medium,
}];
let added = s.migrate_custom_groups_into_enabled_groups(&custom);
assert!(added.is_empty()); assert!(!s.enabled_groups.contains(&"my_team".to_string()));
}
#[test]
fn old_settings_yaml_round_trips_with_inherit_defaults() {
let yaml = "challenge: Yes\n\
enabled_groups: [git, fs]\n\
disabled_groups: []\n\
ignores_patterns_ids: []\n\
deny_patterns_ids: []\n\
audit_enabled: true\n\
blast_radius: true\n\
min_severity: Medium\n\
agent:\n auto_deny_severity: High\n require_human_approval: false\n\
wrappers:\n tools: {}\n";
let s: Settings = serde_yaml::from_str(yaml).unwrap();
assert!(matches!(s.agent.challenge, InheritOr::Inherit));
assert!(matches!(s.wrappers.min_severity, InheritOr::Inherit));
let out = serde_yaml::to_string(&s).unwrap();
let s2: Settings = serde_yaml::from_str(&out).unwrap();
assert_eq!(format!("{:?}", s), format!("{:?}", s2));
}
}
#[cfg(test)]
mod test_loading {
use super::*;
fn config_in(tree: &tree_fs::Tree) -> Config {
Config::new(Some(&tree.root.join("app").display().to_string())).unwrap()
}
#[test]
fn strict_load_reports_file_and_line_and_never_writes() {
let tree = tree_fs::TreeBuilder::default()
.add(
"app/settings.yaml",
"deny_patterns_ids: [git:force_push]\nchallenge: Banana\n",
)
.create()
.unwrap();
let config = config_in(&tree);
let err = config.get_settings_from_file().unwrap_err().to_string();
assert!(err.contains("settings.yaml"), "{err}");
assert!(err.contains("line 2"), "{err}");
let (lenient, e) = config.load_settings_lenient();
assert!(e.is_some());
assert_eq!(
lenient.deny_patterns_ids,
vec!["git:force_push".to_string()]
);
assert_eq!(lenient.challenge, Challenge::default());
assert_eq!(
config.read_config_file().unwrap(),
"deny_patterns_ids: [git:force_push]\nchallenge: Banana\n"
);
}
#[cfg(unix)]
fn within_seconds<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(f());
});
rx.recv_timeout(std::time::Duration::from_secs(10))
.expect("blocked on a non-regular file")
}
#[cfg(unix)]
fn mkfifo(path: &Path) {
assert!(std::process::Command::new("mkfifo")
.arg(path)
.status()
.unwrap()
.success());
}
#[cfg(unix)]
#[test]
fn a_fifo_settings_file_is_an_error_not_a_hang() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = config_in(&tree);
fs::create_dir_all(&config.root_folder).unwrap();
mkfifo(&config.setting_file_path);
let (strict, lenient, warnings) = within_seconds(move || {
(
config.get_settings_from_file().map_err(|e| e.to_string()),
config.load_settings_lenient(),
config.settings_warnings(),
)
});
let err = strict.unwrap_err();
assert!(err.contains("could not read settings file"), "{err}");
assert!(err.contains("not a regular file"), "{err}");
assert_eq!(lenient.1.as_deref(), Some(err.as_str()));
assert!(warnings.is_empty());
}
#[cfg(unix)]
#[test]
fn a_fifo_project_policy_is_not_applied_and_does_not_hang() {
let tree = tree_fs::TreeBuilder::default()
.add("repo/.git/HEAD", "ref: refs/heads/main\n")
.create()
.unwrap();
let repo = tree.root.join("repo");
let policy = repo.join(crate::policy::POLICY_FILENAME);
mkfifo(&policy);
let load = within_seconds(move || crate::policy::load(&crate::env::RealEnvironment, &repo));
assert!(load.policy.is_none());
assert_eq!(load.not_applied, vec![policy]);
assert!(
load.errors[0].message.contains("not a regular file"),
"{:?}",
load.errors
);
}
#[test]
fn read_regular_file_rejects_a_directory() {
let tree = tree_fs::TreeBuilder::default()
.add("f.txt", "x")
.create()
.unwrap();
assert_eq!(read_regular_string(&tree.root.join("f.txt")).unwrap(), "x");
let e = read_regular_file(&tree.root).unwrap_err();
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
}
#[test]
fn wrong_type_and_garbage_are_errors() {
assert!(parse_settings_strict("disabled_groups: git\n").is_err());
assert!(parse_settings_strict("::: [").is_err());
assert!(parse_settings_strict("- a\n- b\n").is_err());
assert!(parse_settings_strict("").is_ok());
assert!(parse_settings_strict("# comment only\n").is_ok());
let (s, dropped) = parse_settings_lenient("disabled_groups: git\nmin_severity: High\n");
assert_eq!(dropped.len(), 1);
assert_eq!(s.min_severity, Some(Severity::High));
assert!(s.disabled_groups.is_empty());
}
#[test]
fn unknown_keys_are_detected() {
let u = unknown_settings_keys("deny_pattern_ids: [x]\nchallenge: Yes\n");
assert_eq!(u, vec!["deny_pattern_ids".to_string()]);
}
#[test]
fn save_refuses_to_overwrite_invalid_yaml() {
let tree = tree_fs::TreeBuilder::default()
.add("app/settings.yaml", "::: [ broken")
.create()
.unwrap();
let config = config_in(&tree);
assert!(config
.save_settings_file_from_struct(&Settings::default())
.is_err());
assert_eq!(config.read_config_file().unwrap(), "::: [ broken");
}
#[cfg(unix)]
#[test]
fn save_refuses_read_only_file() {
use std::os::unix::fs::PermissionsExt;
let tree = tree_fs::TreeBuilder::default()
.add("app/settings.yaml", "challenge: Yes\n")
.create()
.unwrap();
let config = config_in(&tree);
fs::set_permissions(&config.setting_file_path, fs::Permissions::from_mode(0o444)).unwrap();
let err = config
.save_settings_file_from_struct(&Settings::default())
.unwrap_err()
.to_string();
assert!(err.contains("read-only"), "{err}");
}
#[test]
fn custom_checks_bad_file_does_not_drop_others() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
"- id: team:deploy\n from: team\n test: deploy\n description: d\n challenge: Yes\n- id: team:other\n from: team\n test: other\n description: d\n",
)
.add("checks/broken.yaml", "- id: b:x\n from: b\n test: 'foo(('\n description: d\n")
.add("checks/mapping.yml", "id: m:x\n")
.add(
"checks/dups.yaml",
"- id: git:force_push\n from: git\n test: x\n description: d\n- id: team:deploy\n from: team\n test: y\n description: d\n",
)
.add("checks/README.md", "not yaml")
.create()
.unwrap();
let load = load_custom_checks_report(&tree.root.join("checks"));
let ids: Vec<&str> = load.checks.iter().map(|c| c.id.as_str()).collect();
assert!(ids.contains(&"team:deploy"));
assert!(ids.contains(&"team:other"));
assert!(!ids.contains(&"git:force_push"));
assert_eq!(load.checks.len(), 2, "{ids:?}");
assert!(load.warnings.iter().any(|w| w.contains("broken.yaml")));
assert!(load.warnings.iter().any(|w| w.contains("mapping.yml")));
assert!(load.warnings.iter().any(|w| w.contains("built-in")));
assert!(load.warnings.iter().any(|w| w.contains("duplicate")));
}
#[test]
fn custom_check_explicit_challenge_becomes_check_escalation_floor() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
"- id: team:a\n from: team\n test: a\n description: d\n challenge: Yes\n- id: team:b\n from: team\n test: b\n description: d\n",
)
.create()
.unwrap();
let load = load_custom_checks_report(&tree.root.join("checks"));
assert_eq!(
load.explicit_challenges.get("team:a"),
Some(&Challenge::Yes)
);
assert!(!load.explicit_challenges.contains_key("team:b"));
let mut s = Settings::default();
s.apply_custom_check_challenges(&load.explicit_challenges);
assert_eq!(s.check_escalation.get("team:a"), Some(&Challenge::Yes));
}
#[test]
fn policy_named_checks_survive_personal_filters() {
let policy = crate::policy::parse_policy("version: 1\ndeny: [git:force_push]\n").unwrap();
for s in [
Settings {
ignores_patterns_ids: vec!["git:force_push".into()],
..Settings::default()
},
Settings {
disabled_groups: vec!["git".into()],
..Settings::default()
},
Settings {
enabled_groups: vec!["fs".into()],
..Settings::default()
},
] {
let checks = s.get_active_checks_enforced(&[], Some(&policy)).unwrap();
assert!(checks.iter().any(|c| c.id == "git:force_push"));
let without = s.get_active_checks_enforced(&[], None).unwrap();
assert!(!without.iter().any(|c| c.id == "git:force_push"));
}
}
#[test]
fn known_groups_include_off_by_default_groups() {
assert!(known_groups(&[]).contains("shell"));
}
#[test]
fn backup_copies_file() {
let tree = tree_fs::TreeBuilder::default()
.add("app/settings.yaml", "challenge: Yes\n")
.create()
.unwrap();
let config = config_in(&tree);
let b = config.backup_settings_file().unwrap().unwrap();
assert_eq!(fs::read_to_string(b).unwrap(), "challenge: Yes\n");
}
#[test]
fn llm_config_validation() {
assert!(LlmConfig::default().validate().is_ok());
let bad = LlmConfig {
provider: "nope".into(),
..LlmConfig::default()
};
assert!(bad.validate().is_err());
let bad = LlmConfig {
base_url: Some("ftp://x".into()),
..LlmConfig::default()
};
assert!(bad.validate().is_err());
}
}
#[cfg(test)]
mod test_settings {
use super::*;
#[test]
fn can_get_active_checks() {
assert!(Settings::default().get_active_checks().is_ok());
}
#[test]
fn can_get_settings_from_file() {
let groups = Settings::default().get_active_groups().clone();
assert_eq!(groups, DEFAULT_ENABLED_GROUPS.to_vec());
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&tree.root.display().to_string())).unwrap();
fs::write(&config.setting_file_path, "challenge: Yes\n").unwrap();
let from_file = config.get_settings_from_file().unwrap();
assert_eq!(from_file.challenge, Challenge::Yes);
assert_eq!(from_file.enabled_groups, DEFAULT_ENABLED_GROUPS.to_vec());
}
#[test]
fn settings_yaml_roundtrip_preserves_enabled_groups() {
let original = Settings::default();
let yaml = serde_yaml::to_string(&original).unwrap();
let restored: Settings = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(restored.enabled_groups, original.enabled_groups);
assert!(
!restored.enabled_groups.is_empty(),
"enabled_groups must not be empty after roundtrip"
);
}
#[test]
fn default_settings_produce_nonempty_active_checks() {
let checks = Settings::default().get_active_checks().unwrap();
assert!(
!checks.is_empty(),
"Settings::default() must produce active checks"
);
let groups: std::collections::HashSet<&str> =
checks.iter().map(|c| c.from.as_str()).collect();
assert!(groups.contains("fs"), "fs group must be active");
assert!(groups.contains("git"), "git group must be active");
}
#[test]
fn settings_file_roundtrip_produces_matches() {
let temp = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = Config::new(Some(&temp.root.join("app").display().to_string())).unwrap();
config.reset_config().unwrap();
let settings = config.get_settings_from_file().unwrap();
let checks = settings.get_active_checks().unwrap();
assert!(
!checks.is_empty(),
"Active checks must not be empty after file roundtrip"
);
let matches = crate::checks::run_check_on_command(&checks, "git push --force origin main");
assert!(
!matches.is_empty(),
"git push --force must match after file roundtrip"
);
}
#[test]
fn old_includes_field_falls_back_to_default_enabled_groups() {
let old_yaml = "challenge: Math\nincludes:\n - base\n - fs\n - git\n";
let settings: Settings = serde_yaml::from_str(old_yaml).unwrap();
assert_eq!(settings.enabled_groups, default_enabled_groups());
let checks = settings.get_active_checks().unwrap();
assert!(!checks.is_empty());
}
#[test]
fn lenient_parse_keeps_valid_siblings() {
let (s, dropped) = parse_settings_lenient(
"agent:\n auto_deny_severity: Low\n challenge: Banana\ncontext:\n escalation:\n elevated: Nope\n critical: Enter\n",
);
assert_eq!(s.agent.auto_deny_severity, Severity::Low);
assert_eq!(s.context.escalation.critical, Challenge::Enter);
assert_eq!(dropped.len(), 2, "{dropped:?}");
assert!(
dropped.iter().any(|d| d.contains("agent.challenge")),
"{dropped:?}"
);
}
#[test]
fn nested_unknown_keys_are_reported_with_suggestions() {
let w = unknown_settings_key_warnings(
"agent:\n auto_deny_severty: Low\nchalenge: Yes\ngroup_escalation:\n anything: Math\nwrappers:\n tools:\n psql:\n delimter: ';'\n",
);
assert_eq!(w.len(), 3, "{w:?}");
assert!(w
.iter()
.any(|x| x.contains("agent.auto_deny_severty") && x.contains("auto_deny_severity")));
assert!(w
.iter()
.any(|x| x.contains("`chalenge`") && x.contains("`challenge`")));
assert!(w.iter().any(|x| x.contains("wrappers.tools.psql.delimter")));
}
#[test]
fn duplicate_key_error_has_a_line_number() {
let err = parse_settings_strict("challenge: Math\nmin_severity: High\nchallenge: Yes\n")
.unwrap_err()
.to_string();
assert!(err.contains("line 3"), "{err}");
}
#[test]
fn personal_deny_beats_personal_ignore() {
let s = Settings {
ignores_patterns_ids: vec!["git:reset".into()],
deny_patterns_ids: vec!["git:reset".into()],
..Settings::default()
};
assert!(s
.get_active_checks()
.unwrap()
.iter()
.any(|c| c.id == "git:reset"));
assert!(s
.get_active_checks_with_custom(&[])
.unwrap()
.iter()
.any(|c| c.id == "git:reset"));
assert_eq!(s.ignore_deny_conflicts(), vec!["git:reset".to_string()]);
}
#[test]
fn nested_sparse_write_and_comments_are_kept() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&tree.root.join("app").display().to_string())).unwrap();
fs::create_dir_all(&config.root_folder).unwrap();
fs::write(
&config.setting_file_path,
"# ACME team baseline\n# keep me\n\n# the challenge\nchallenge: Yes\n",
)
.unwrap();
config
.modify_settings(|s| {
s.context.protected_branches.push("develop".into());
Ok(((), true))
})
.unwrap();
let text = fs::read_to_string(&config.setting_file_path).unwrap();
assert!(
text.starts_with("# ACME team baseline\n# keep me\n"),
"{text}"
);
assert!(text.contains("# the challenge\nchallenge: Yes"), "{text}");
assert!(text.contains("develop"));
assert!(
!text.contains("production_k8s_patterns"),
"whole context block written:\n{text}"
);
assert!(!text.contains("escalation"), "{text}");
config
.modify_settings(|s| {
s.group_escalation.insert("git".into(), Challenge::Yes);
Ok(((), true))
})
.unwrap();
config
.modify_settings(|s| {
s.group_escalation.clear();
Ok(((), true))
})
.unwrap();
let s = config.get_settings_from_file().unwrap();
assert!(s.group_escalation.is_empty());
}
#[test]
fn config_dir_env_value_is_validated() {
let tree = tree_fs::TreeBuilder::default()
.add("file", "x")
.create()
.unwrap();
assert!(config_dir_from_env(None).unwrap().is_none());
assert!(config_dir_from_env(Some(" ")).unwrap().is_none());
let dir = tree.root.display().to_string();
assert_eq!(
config_dir_from_env(Some(&format!(" {dir} "))).unwrap(),
Some(tree.root.clone())
);
let e = config_dir_from_env(Some(&tree.root.join("file").display().to_string()))
.unwrap_err()
.to_string();
assert!(
e.contains("SHELLFIRM_CONFIG_DIR") && e.contains("not a directory"),
"{e}"
);
let e = config_dir_from_env(Some(&tree.root.join("nope").display().to_string()))
.unwrap_err()
.to_string();
assert!(e.contains("no such directory"), "{e}");
}
#[test]
fn keys_back_at_their_defaults_are_removed() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&tree.root.join("app").display().to_string())).unwrap();
fs::create_dir_all(&config.root_folder).unwrap();
fs::write(&config.setting_file_path, "audit_enabled: true\n").unwrap();
let edit = |f: &dyn Fn(&mut Settings)| {
config
.modify_settings(|s| {
f(s);
Ok(((), true))
})
.unwrap();
};
edit(&|s| {
s.context
.production_env_vars
.insert("STAGE".into(), "prod".into());
});
let loaded = config.get_settings_from_file().unwrap();
assert!(loaded.context.production_env_vars.contains_key("STAGE"));
assert!(loaded.context.production_env_vars.contains_key("NODE_ENV"));
edit(&|s| {
s.context.production_env_vars.remove("STAGE");
});
edit(&|s| s.context.protected_branches.push("hotfix/*".into()));
edit(&|s| s.context.protected_branches.retain(|b| b != "hotfix/*"));
edit(&|s| s.context.production_k8s_patterns.push("staging".into()));
edit(&|s| s.context.production_k8s_patterns.retain(|b| b != "staging"));
let text = fs::read_to_string(&config.setting_file_path).unwrap();
assert!(!text.contains("context"), "{text}");
assert!(text.contains("audit_enabled: true"), "{text}");
edit(&|s| s.llm = Some(LlmConfig::default()));
let text = fs::read_to_string(&config.setting_file_path).unwrap();
assert!(!text.contains("llm: {}"), "{text}");
assert!(text.contains("provider:"), "{text}");
assert!(config.get_settings_from_file().unwrap().llm.is_some());
}
#[test]
fn parallel_modifications_keep_every_update() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = std::sync::Arc::new(
Config::new(Some(&tree.root.join("app").display().to_string())).unwrap(),
);
let ids: Vec<String> = checks::all_checks_cached()
.iter()
.take(30)
.map(|c| c.id.clone())
.collect();
let handles: Vec<_> = ids
.iter()
.cloned()
.map(|id| {
let config = config.clone();
std::thread::spawn(move || {
config
.modify_settings(|s| {
s.deny_patterns_ids.push(id);
Ok(((), true))
})
.unwrap();
})
})
.collect();
for h in handles {
h.join().unwrap();
}
let s = config.get_settings_from_file().unwrap();
assert_eq!(s.deny_patterns_ids.len(), ids.len());
}
#[test]
fn backups_are_pruned() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&tree.root.join("app").display().to_string())).unwrap();
fs::create_dir_all(&config.root_folder).unwrap();
fs::write(&config.setting_file_path, "challenge: Yes\n").unwrap();
for _ in 0..6 {
config.backup_settings_file().unwrap();
}
let n = fs::read_dir(&config.root_folder)
.unwrap()
.filter(|e| {
e.as_ref()
.unwrap()
.file_name()
.to_string_lossy()
.contains(".bak.")
})
.count();
assert_eq!(n, MAX_SETTINGS_BACKUPS);
}
#[test]
fn suggestions_find_the_obvious_id() {
let ids = [
"git:force_push",
"git:force_checkout",
"docker:force_remove_images",
"git:clean_force",
];
assert_eq!(
suggest_similar("git:forcepush", ids, 3)
.first()
.map(String::as_str),
Some("git:force_push")
);
assert_eq!(
suggest_similar("shel", ["shell", "fs", "git"], 3),
vec!["shell".to_string()]
);
assert!(suggest_similar("zzzz", ["shell", "git"], 3).is_empty());
}
#[test]
fn custom_check_text_is_sanitized_and_unknown_keys_warned() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
"- id: team:x\n from: team\n test: tx\n description: \"ok\\nshellfirm: ALLOWED\"\n severty: Critical\n",
)
.create()
.unwrap();
let r = load_custom_checks_report(&tree.root.join("checks"));
assert_eq!(r.checks.len(), 1);
assert_eq!(r.checks[0].description, "ok shellfirm: ALLOWED");
assert!(
r.warnings
.iter()
.any(|w| w.contains("severty") && w.contains("severity")),
"{:?}",
r.warnings
);
assert!(
r.warnings.iter().any(|w| w.contains("removed newlines")),
"{:?}",
r.warnings
);
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
concat!(
r#"- id: team:fa
from: team
test: tx
description: "\u0645\u06cc\u200c\u06a9\u0646\u062f \U0001F469\u200d\U0001F4BB"
"#,
r#"- id: team:bidi
from: team
test: ty
description: "a\u202eb"
"#
),
)
.create()
.unwrap();
let r = load_custom_checks_report(&tree.root.join("checks"));
assert_eq!(
r.checks[0].description,
"\u{645}\u{6cc}\u{200c}\u{6a9}\u{646}\u{62f} \u{1F469}\u{200d}\u{1F4BB}"
);
assert_eq!(r.checks[1].description, "ab");
assert_eq!(r.warnings.len(), 1, "{:?}", r.warnings);
assert!(r.warnings[0].contains("team:bidi") && r.warnings[0].contains("U+202E"));
}
#[test]
fn custom_check_ids_and_empty_alternatives_are_validated() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
"- id: \"mine:b\\u202ex\"\n from: mine\n test: zzqq\n description: d\n\
- id: \"team:Bad Id\"\n from: team\n test: zzbad\n description: d\n\
- id: team:Upper\n from: team\n test: zzup\n description: d\n alternative: \"\"\n alternative_info: \" \"\n\
- id: team:ok\n from: team\n test: zzok\n description: d\n",
)
.add("checks/sub/more.yaml", "- id: team:sub\n from: team\n test: zzsub\n description: d\n")
.create()
.unwrap();
let r = load_custom_checks_report(&tree.root.join("checks"));
let ids: Vec<&str> = r.checks.iter().map(|c| c.id.as_str()).collect();
assert_eq!(ids, vec!["team:Upper", "team:ok"], "{:?}", r.warnings);
let all = r.warnings.join("\n");
assert!(
all.contains("mine:b\\u{202e}x") && !all.contains('\u{202e}'),
"{all}"
);
assert!(all.contains("team:Bad Id"), "{all}");
assert!(all.contains("id `team:Upper` should be lowercase"), "{all}");
assert!(all.contains("empty `alternative` is ignored"), "{all}");
assert!(all.contains("empty `alternative_info` is ignored"), "{all}");
assert!(all.contains("subdirectory ignored"), "{all}");
let upper = r.checks.iter().find(|c| c.id == "team:Upper").unwrap();
assert!(upper.alternative.is_none() && upper.alternative_info.is_none());
assert!(check_id_format_problem("fs-strict:any_deletion").is_none());
assert!(check_id_format_problem("nomad").is_some());
}
#[test]
fn a_custom_alternative_that_triggers_a_built_in_is_reported() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/team.yaml",
"- id: team:wipe\n from: team\n test: \"wipe\\\\s+cache\"\n description: d\n severity: Medium\n alternative: \"rm -rf ~\"\n\
- id: team:fine\n from: team\n test: zzfine\n description: d\n severity: Critical\n alternative: \"git status\"\n\
- id: team:lower\n from: team\n test: zzlow\n description: d\n severity: Critical\n alternative: \"git stash clear\"\n",
)
.create()
.unwrap();
let r = load_custom_checks_report(&tree.root.join("checks"));
assert_eq!(r.checks.len(), 3, "{:?}", r.warnings);
let all = r.warnings.join("\n");
assert!(
all.contains("check `team:wipe`") && all.contains("alternative `rm -rf ~` triggers"),
"{all}"
);
assert!(!all.contains("team:fine"), "{all}");
assert!(!all.contains("team:lower"), "{all}");
}
#[test]
fn group_typos_get_a_suggestion() {
let m = unknown_group_message("gti", ["git", "fs", "aws"]);
assert!(m.contains("Did you mean: git?"), "{m}");
assert!(m.starts_with("unknown check group 'gti'."), "{m}");
}
#[test]
fn custom_path_exists_out_of_range_is_warned() {
let tree = tree_fs::TreeBuilder::default()
.add(
"checks/50-path.yaml",
"- id: team:path\n from: team\n test: \"zap\\\\s+(\\\\S+)\"\n description: x\n filters: [{type: PathExists, value: 3}]\n- id: team:ok\n from: team\n test: \"zop\\\\s+(\\\\S+)\"\n description: y\n filters: [{type: PathExists, value: 1}]\n",
)
.create()
.unwrap();
let r = load_custom_checks_report(&tree.root.join("checks"));
assert_eq!(r.checks.len(), 2);
let w: Vec<&String> = r
.warnings
.iter()
.filter(|w| w.contains("PathExists"))
.collect();
assert_eq!(w.len(), 1, "{:?}", r.warnings);
assert!(w[0].contains("team:path") && w[0].contains("group 3") && w[0].contains("has 1"));
}
#[test]
fn a_huge_impact_budget_is_capped_with_a_warning() {
let s = parse_settings_strict("blast_radius_budget_ms: 600000\n").unwrap();
assert_eq!(s.blast_radius_budget(), MAX_BLAST_RADIUS_BUDGET_MS);
let temp = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&temp.root.display().to_string())).unwrap();
std::fs::write(
&config.setting_file_path,
"blast_radius_budget_ms: 600000\n",
)
.unwrap();
assert!(config
.settings_warnings()
.iter()
.any(|w| w.contains("blast_radius_budget_ms: 600000") && w.contains("capped at 5000")));
std::fs::write(&config.setting_file_path, "blast_radius_budget_ms: 5000\n").unwrap();
assert!(config.settings_warnings().is_empty());
}
#[test]
fn audit_max_files_zero_is_rejected_and_never_used() {
let s = parse_settings_strict("audit_max_files: 0\n").unwrap();
assert_eq!(s.audit_max_files, crate::audit::DEFAULT_MAX_AUDIT_FILES);
let dir = tree_fs::TreeBuilder::default()
.create()
.expect("create tree");
let config = Config::new(Some(&dir.root.display().to_string())).unwrap();
std::fs::write(&config.setting_file_path, "audit_max_files: 0\n").unwrap();
assert!(config
.settings_warnings()
.iter()
.any(|w| w.contains("audit_max_files") && w.contains("default is used")));
let (s, dropped) = parse_settings_lenient("audit_max_files: 0\nchallenge: Yes\n");
assert_eq!(s.audit_max_files, crate::audit::DEFAULT_MAX_AUDIT_FILES);
assert_eq!(s.challenge, Challenge::Yes);
assert!(dropped.iter().any(|d| d.contains("audit_max_files")));
assert_eq!(
parse_settings_strict("audit_max_files: 2\n")
.unwrap()
.audit_max_files,
2
);
}
#[test]
fn unknown_ids_and_groups_in_the_file_are_warned() {
let s = parse_settings_strict(
"deny_patterns_ids: [git:resett]\nignores_patterns_ids: [git:reset]\n\
enabled_groups: [gti, git]\ndisabled_groups: [herok]\n\
check_escalation: {git:forse_push: Yes}\ngroup_escalation: {kube: Yes}\n",
)
.unwrap();
let w = unknown_reference_warnings(&s, &[], &[]);
assert_eq!(w.len(), 5, "{w:?}");
assert!(w.iter().any(|m| m.contains("git:resett")
&& m.contains("deny_patterns_ids")
&& m.contains("git:reset")));
assert!(w
.iter()
.any(|m| m.contains("`enabled_groups`: unknown group `gti`")));
assert!(w
.iter()
.any(|m| m.contains("herok") && m.contains("heroku")));
assert!(w.iter().any(|m| m.contains("git:forse_push")));
assert!(w.iter().any(|m| m.contains("`kube`")));
assert!(unknown_reference_warnings(&Settings::default(), &[], &[]).is_empty());
let p = parse_settings_strict("ignores_patterns_ids: [team:pol]\n").unwrap();
assert!(unknown_reference_warnings(&p, &[], &["team:pol".to_string()]).is_empty());
}
#[test]
fn color_and_locale_rules() {
let env = |pairs: &'static [(&'static str, &'static str)]| {
move |k: &str| {
pairs
.iter()
.find(|(n, _)| *n == k)
.map(|(_, v)| (*v).to_string())
}
};
assert_eq!(color_preference(env(&[])), None);
assert_eq!(color_preference(env(&[("NO_COLOR", "")])), None);
assert_eq!(color_preference(env(&[("NO_COLOR", "1")])), Some(false));
assert_eq!(color_preference(env(&[("CLICOLOR", "0")])), Some(false));
assert_eq!(color_preference(env(&[("TERM", "dumb")])), Some(false));
assert_eq!(color_preference(env(&[("TERM", "vt100")])), Some(false));
assert_eq!(
color_preference(env(&[("TERM", "xterm-mono")])),
Some(false)
);
assert_eq!(color_preference(env(&[("TERM", "VT220")])), Some(false));
assert_eq!(color_preference(env(&[("TERM", "")])), Some(false));
assert_eq!(color_preference(env(&[("TERM", "xterm-256color")])), None);
assert_eq!(
color_preference(env(&[("NO_COLOR", "1"), ("CLICOLOR_FORCE", "1")])),
Some(true)
);
assert!(unicode_locale(env(&[])));
assert!(unicode_locale(env(&[("LANG", "en_US.UTF-8")])));
assert!(!unicode_locale(env(&[
("LC_ALL", "C"),
("LANG", "en_US.UTF-8")
])));
assert!(unicode_locale(env(&[("LC_ALL", ""), ("LANG", "C.utf8")])));
}
#[test]
fn reset_header_only_file_is_not_backed_up() {
let tree = tree_fs::TreeBuilder::default().create().unwrap();
let config = Config::new(Some(&tree.root.join("app").display().to_string())).unwrap();
config.reset_config().unwrap();
assert!(config.backup_settings_file().unwrap().is_none());
fs::write(&config.setting_file_path, "challenge: Yes\n").unwrap();
assert!(config.backup_settings_file().unwrap().is_some());
}
}