use super::roots::{write_denial_names, Roots, WorktreeParents};
use super::sandbox_escape;
use std::path::{Path, PathBuf};
pub(super) fn scene_file_write_rules(roots: &Roots) -> String {
let Some(state_root) = &roots.state_root else {
return String::new();
};
state_root_scene_files_regexes(state_root, roots.paths.linked_scene_dirs_resolved())
.iter()
.map(|regex| format!("(deny file-write* (regex \"{}\"))\n", sandbox_escape(regex)))
.collect()
}
pub(super) fn deny_write_rules(roots: &Roots, denied: &[String]) -> String {
let names = write_denial_names(denied);
if names.is_empty() {
return String::new();
}
let names = alternatives(&names.iter().map(String::as_str).collect::<Vec<_>>());
roots
.write_roots()
.map(|root| {
let regex = format!(
r"^{}/(.*/)?({names})(\.[^/]*)?(/|$)",
regex_escape(&root.display().to_string())
);
format!(
"(deny file-write* (regex \"{}\"))\n",
sandbox_escape(®ex)
)
})
.collect()
}
pub(super) fn worktree_control_file_rules(parents: &WorktreeParents) -> String {
let files = alternatives(base::paths::GIT_WORKTREE_CONTROL_FILES);
let link = regex_escape(base::paths::REPOSITORY_CONFIG_DIR);
let controls = parents
.admin
.iter()
.map(|dir| (dir, &files))
.chain(parents.links.iter().map(|dir| (dir, &link)))
.map(|(dir, names)| {
format!(
"^{}/[^/]+/({names})$",
regex_escape(&dir.display().to_string())
)
});
let worktrees = parents
.links
.iter()
.map(|dir| format!("^{}(/[^/]+)?$", regex_escape(&dir.display().to_string())));
controls
.chain(worktrees)
.map(|regex| {
format!(
"(deny file-write* (regex \"{}\"))\n",
sandbox_escape(®ex)
)
})
.collect()
}
pub(super) fn credential_read_rules(roots: &Roots) -> String {
roots
.paths
.credential_locations(&roots.paths.engine_dirs_in(&roots.cwd))
.iter()
.flat_map(credential_read_regexes)
.map(|regex| format!("(deny file-read* (regex \"{}\"))\n", sandbox_escape(®ex)))
.collect()
}
fn credential_read_regexes(located: &base::paths::Located) -> Vec<String> {
let base = regex_escape(&located.base.display().to_string());
let under_base = if located.under_each_scene {
format!("^{base}/scenes/[^/]+/")
} else {
format!("^{base}/")
};
let linked = located
.linked_dirs_resolved()
.into_iter()
.map(|dir| format!("^{}/", regex_escape(&dir.display().to_string())));
std::iter::once(under_base)
.chain(linked)
.map(|dir| secret_regex(&dir, located.secret))
.collect()
}
fn secret_regex(dir: &str, secret: base::paths::Secret) -> String {
use base::paths::{KeyFileNames, Secret};
match secret {
Secret::KeyFiles(names @ KeyFileNames::StateRoot) => {
format!(
r"{dir}\.?({})[^/]*\.json[^/]*$",
alternatives(names.stems())
)
}
Secret::KeyFiles(names @ KeyFileNames::Project) => {
format!(r"{dir}\.?({})[^/]*$", alternatives(names.stems()))
}
Secret::Tree(path) | Secret::DirName(path) => {
format!("{dir}{}(/|$)", regex_escape(path))
}
}
}
fn state_root_scene_files_regexes(state_root: &Path, linked: &[PathBuf]) -> Vec<String> {
let files = alternatives(base::paths::STATE_ROOT_SCENE_FILES);
std::iter::once(format!(
"^{}/(scenes/[^/]+/)?({files})$",
regex_escape(&state_root.display().to_string()),
))
.chain(
linked
.iter()
.map(|dir| format!("^{}/({files})$", regex_escape(&dir.display().to_string()))),
)
.collect()
}
fn alternatives(names: &[&str]) -> String {
names
.iter()
.map(|name| regex_escape(name))
.collect::<Vec<_>>()
.join("|")
}
fn regex_escape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
if r"\.^$|?*+()[]{}".contains(c) {
out.push('\\');
}
out.push(c);
}
out
}