use serde_json::{Map, Value};
use super::Generated;
pub(super) const OWNED: &[(&str, &[&str])] = &[
("enabled", &[]),
("failIfUnavailable", &[]),
("allowUnsandboxedCommands", &[]),
("excludedCommands", &[]),
(
"filesystem",
&["denyRead", "allowRead", "denyWrite", "allowWrite"],
),
(
"network",
&[
"allowedDomains",
"deniedDomains",
"strictAllowlist",
"httpProxyPort",
"socksProxyPort",
"tlsTerminate",
],
),
("credentials", &["envVars", "files", "allowPlaintextInject"]),
];
pub(super) fn strip_stale(sandbox: &mut Map<String, Value>, generated: &Generated) {
for (key, subs) in OWNED {
if subs.is_empty() {
continue;
}
let keep: Vec<&str> = generated
.sandbox
.get(*key)
.and_then(Value::as_object)
.into_iter()
.flat_map(|m| m.keys().map(String::as_str))
.collect();
if let Some(Value::Object(nested)) = sandbox.get_mut(*key) {
nested.retain(|sub, _| !subs.contains(&sub.as_str()) || keep.contains(&sub.as_str()));
if nested.is_empty() {
sandbox.remove(*key);
}
}
}
}