use openmoat_core::Kind;
use openmoat_core::ir::Enforcement;
use toml_edit::{Item, value};
use super::{Generated, Mode};
use crate::sandbox::patterns::{Spot, has_glob, literal_tree, push_unique, split, spot};
const CODEX_PROTECTED: &[&str] = &[".git", ".agents", ".codex", ".aws"];
const MAX_CASE_VARIANTS: usize = 32;
pub(super) fn denied_dirs(ir: &Enforcement) -> Vec<String> {
let mut names = Vec::new();
for rule in &ir.fs.write.deny {
let (positive, _) = split(&rule.patterns);
for pattern in positive {
if let Spot::Anywhere(rest) = spot(pattern)
&& let Some(name) = literal_tree(rest).filter(|n| !n.contains('/'))
{
push_unique(&mut names, name.to_owned());
}
}
}
names
}
pub fn deny_names(generated: &mut Generated) {
let Some(workspace) = generated
.profile
.get_mut("filesystem")
.and_then(|fs| fs.get_mut(":workspace_roots"))
.and_then(Item::as_table_mut)
else {
return;
};
let globs: Vec<String> = workspace
.iter()
.filter(|(key, mode)| key.starts_with("**/") && mode.as_str() == Some("deny"))
.map(|(key, _)| key.to_owned())
.collect();
let mut read_only = Vec::new();
for path in globs.iter().filter_map(|g| g.strip_prefix("**/")) {
match path.split_once('/') {
None if !has_glob(path) => {
workspace.insert(path, value(Mode::Deny.as_str()));
}
Some((dir, _))
if generated.denied_dirs.iter().any(|d| d == dir)
&& !CODEX_PROTECTED.contains(&dir)
&& !workspace.contains_key(dir) =>
{
workspace.insert(dir, value(Mode::Read.as_str()));
read_only.push(dir.to_owned());
}
_ => {}
}
}
if !read_only.is_empty() {
generated.report.loss(
Kind::FsWrite,
"codex.linux-agent-dirs",
format!(
"on Linux sandboxed commands cannot write in a workspace root's {}, so they \
cannot plant another agent's settings there; while a command runs a missing \
one shows up as an empty file",
read_only
.iter()
.map(|d| format!("`{d}`"))
.collect::<Vec<_>>()
.join(" or ")
),
);
}
case_variants(workspace, &globs, &mut generated.report);
if !globs.is_empty() {
generated.report.allowance(
Kind::FsWrite,
"codex.linux-write-globs",
globs,
"Codex on Linux hides only the files a deny glob matches when a command starts, \
so sandboxed commands may create a missing match, except a name directly in a \
workspace root (`.env`, `.envrc`) and anything in its `.claude` or `.cursor`, \
which the profile also denies or makes read-only by path; the hook still denies \
the agent's own writes",
);
}
}
fn case_variants(
workspace: &mut toml_edit::Table,
globs: &[String],
report: &mut crate::sandbox::Report,
) {
let mut past_cap = Vec::new();
for glob in globs {
match spellings(glob) {
Some(spellings) => {
for spelling in spellings {
if !workspace.contains_key(&spelling) {
workspace.insert(&spelling, value(Mode::Deny.as_str()));
}
}
}
None => past_cap.push(glob.clone()),
}
}
if !past_cap.is_empty() {
report.loss(
Kind::FsWrite,
"codex.linux-case-variants",
format!(
"Codex on Linux compares deny globs with file names in their letter case, so in \
a directory that folds case (`chattr +F`) a file that exists in another case is \
not hidden by {}; the profile writes every case of shorter names only, and the \
hook still decides the agent's own reads and writes",
past_cap
.iter()
.map(|g| format!("`{g}`"))
.collect::<Vec<_>>()
.join(", ")
),
);
}
}
fn spellings(glob: &str) -> Option<Vec<String>> {
if glob.contains(['[', '{', '\\']) {
return None;
}
let letters: Vec<usize> = glob
.char_indices()
.filter(|(_, c)| c.is_ascii_alphabetic())
.map(|(i, _)| i)
.collect();
let count = 1_usize.checked_shl(u32::try_from(letters.len()).ok()?)?;
if count > MAX_CASE_VARIANTS {
return None;
}
let lower = glob.to_ascii_lowercase();
let mut out = Vec::new();
for mask in 0..count {
let mut spelling = lower.clone().into_bytes();
for (bit, &at) in letters.iter().enumerate() {
if mask & (1 << bit) != 0 {
spelling[at] = spelling[at].to_ascii_uppercase();
}
}
let spelling = String::from_utf8(spelling).ok()?;
if spelling != glob {
out.push(spelling);
}
}
Some(out)
}