use std::fs;
use std::path::{Path, PathBuf};
const MAX_BYTES: u64 = 1024 * 1024;
const KNOWN_HOOK_DIRS: &[&str] = &[
".husky",
".husky/_",
".githooks",
".git-hooks",
"githooks",
"git-hooks",
"scripts/git-hooks",
];
const LFS_FILTERS: &[&str] = &[
"git-lfs clean -- %f",
"git-lfs smudge -- %f",
"git-lfs filter-process",
];
#[must_use]
pub fn warnings(cwd: &Path, user_home: &Path) -> Vec<String> {
let mut seen: Vec<PathBuf> = Vec::new();
let mut lines = Vec::new();
for root in super::project_dirs(cwd, user_home) {
for file in config_files(&root) {
if seen.contains(&file) {
continue;
}
match read(&file) {
Read::TooLarge => lines.push(format!(
"{}: larger than {MAX_BYTES} bytes, not read; git reads it and may run \
what it names, so review it",
file.display()
)),
Read::Text(text) => {
let keys = program_keys(&text);
if !keys.is_empty() {
lines.push(format!(
"{}: sets {}, which git runs as a program, the agent's own \
background git included, when a session starts and before any \
hook, so OpenMoat can only warn: only `openmoat run` keeps that git \
in a sandbox; review it and remove what you did not add",
file.display(),
keys.join(", ")
));
}
}
Read::Missing => {}
}
seen.push(file);
}
}
lines
}
#[must_use]
pub fn warnings_here() -> Vec<String> {
let (Ok(cwd), Ok(user_home)) = (std::env::current_dir(), crate::home::user_home()) else {
return Vec::new();
};
warnings(&cwd, &user_home)
}
fn config_files(root: &Path) -> Vec<PathBuf> {
let dot_git = root.join(".git");
let mut dirs = Vec::new();
if dot_git.is_dir() {
dirs.push(dot_git);
} else if let Some(dir) = gitdir_of(&dot_git) {
if let Some(common) = common_dir(&dir) {
dirs.push(common);
}
dirs.push(dir);
}
dirs.iter()
.flat_map(|dir| [dir.join("config"), dir.join("config.worktree")])
.filter(|path| path.is_file())
.collect()
}
fn gitdir_of(file: &Path) -> Option<PathBuf> {
let Read::Text(text) = read(file) else {
return None;
};
let named = text.lines().find_map(|line| line.strip_prefix("gitdir:"))?;
Some(file.parent()?.join(named.trim()))
}
fn common_dir(dir: &Path) -> Option<PathBuf> {
let Read::Text(text) = read(&dir.join("commondir")) else {
return None;
};
Some(dir.join(text.trim()))
}
#[derive(Debug)]
enum Read {
Missing,
TooLarge,
Text(String),
}
fn read(path: &Path) -> Read {
let Ok(meta) = fs::metadata(path) else {
return Read::Missing;
};
if !meta.is_file() {
return Read::Missing;
}
if meta.len() > MAX_BYTES {
return Read::TooLarge;
}
fs::read(path).map_or(Read::Missing, |bytes| {
Read::Text(String::from_utf8_lossy(&bytes).into_owned())
})
}
fn program_keys(text: &str) -> Vec<String> {
let mut section = String::new();
let mut subsection: Option<String> = None;
let mut keys: Vec<String> = Vec::new();
for line in text.lines() {
let mut rest = line.trim();
if let Some(header) = rest.strip_prefix('[') {
let Some(end) = header.find(']') else {
continue;
};
(section, subsection) = parse_header(&header[..end]);
rest = header[end + 1..].trim();
}
if rest.is_empty() || rest.starts_with('#') || rest.starts_with(';') {
continue;
}
let (name, value) = rest.split_once('=').unwrap_or((rest, "true"));
let name = name.trim().to_ascii_lowercase();
if runs_program(§ion, subsection.as_deref(), &name, &value_of(value)) {
let key = match &subsection {
Some(sub) => format!("{section}.{sub}.{name}"),
None => format!("{section}.{name}"),
};
if !keys.contains(&key) {
keys.push(key);
}
}
}
keys
}
fn parse_header(header: &str) -> (String, Option<String>) {
let header = header.trim();
if let Some((section, quoted)) = header.split_once(char::is_whitespace) {
let sub = quoted.trim().trim_matches('"').replace("\\\"", "\"");
return (section.to_ascii_lowercase(), Some(sub));
}
match header.split_once('.') {
Some((section, sub)) => (section.to_ascii_lowercase(), Some(sub.to_ascii_lowercase())),
None => (header.to_ascii_lowercase(), None),
}
}
fn value_of(raw: &str) -> String {
let raw = raw.trim();
if let Some(quoted) = raw.strip_prefix('"') {
return quoted.split('"').next().unwrap_or_default().to_owned();
}
raw.split(['#', ';'])
.next()
.unwrap_or_default()
.trim()
.to_owned()
}
fn runs_program(section: &str, subsection: Option<&str>, name: &str, value: &str) -> bool {
let set = !value.is_empty();
match (section, subsection, name) {
("core", None, "fsmonitor") | ("pager", None, _) => set && !is_bool(value),
("core", None, "hookspath") => {
set && !KNOWN_HOOK_DIRS.contains(&value.trim_end_matches('/'))
}
("core", None, "pager" | "editor" | "sshcommand" | "gitproxy" | "askpass")
| ("sequence", None, "editor")
| ("diff", None, "external")
| ("diff", Some(_), "command" | "textconv")
| ("merge", Some(_), "driver")
| ("gpg", _, "program")
| ("include", None, "path")
| ("includeif", Some(_), "path")
| ("remote", Some(_), "uploadpack" | "receivepack")
| ("uploadpack", None, "packobjectshook") => set,
("filter", Some(_), "clean" | "smudge" | "process") => set && !LFS_FILTERS.contains(&value),
("credential", _, "helper") => {
value.starts_with('!') || value.contains('/') || value.contains('\\')
}
("alias", _, _) => value.starts_with('!'),
_ => false,
}
}
fn is_bool(value: &str) -> bool {
["true", "false", "yes", "no", "on", "off", "1", "0"]
.iter()
.any(|word| value.eq_ignore_ascii_case(word))
}
#[cfg(test)]
mod tests;