use crate::config::helpers::{optional_env, parse_bool_env, parse_optional_env, parse_string_env};
use crate::error::ConfigError;
#[derive(Debug, Clone)]
pub struct SandboxModeConfig {
pub enabled: bool,
pub policy: String,
pub allow_full_access: bool,
pub timeout_secs: u64,
pub memory_limit_mb: u64,
pub cpu_shares: u32,
pub image: String,
pub auto_pull_image: bool,
pub extra_allowed_domains: Vec<String>,
pub reaper_interval_secs: u64,
pub orphan_threshold_secs: u64,
}
impl Default for SandboxModeConfig {
fn default() -> Self {
Self {
enabled: true,
policy: "readonly".to_string(),
allow_full_access: false,
timeout_secs: 120,
memory_limit_mb: 2048,
cpu_shares: 1024,
image: "ironclaw-worker:latest".to_string(),
auto_pull_image: true,
extra_allowed_domains: Vec::new(),
reaper_interval_secs: 300,
orphan_threshold_secs: 600,
}
}
}
impl SandboxModeConfig {
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let ss = &settings.sandbox;
let extra_domains = optional_env("SANDBOX_EXTRA_DOMAINS")?
.map(|s| s.split(',').map(|d| d.trim().to_string()).collect())
.unwrap_or_else(|| {
if ss.extra_allowed_domains.is_empty() {
Vec::new()
} else {
ss.extra_allowed_domains.clone()
}
});
let reaper_interval_secs: u64 = parse_optional_env("SANDBOX_REAPER_INTERVAL_SECS", 300)?;
let orphan_threshold_secs: u64 = parse_optional_env("SANDBOX_ORPHAN_THRESHOLD_SECS", 600)?;
if reaper_interval_secs == 0 {
return Err(ConfigError::InvalidValue {
key: "SANDBOX_REAPER_INTERVAL_SECS".to_string(),
message: "must be greater than 0".to_string(),
});
}
if orphan_threshold_secs == 0 {
return Err(ConfigError::InvalidValue {
key: "SANDBOX_ORPHAN_THRESHOLD_SECS".to_string(),
message: "must be greater than 0".to_string(),
});
}
Ok(Self {
enabled: parse_bool_env("SANDBOX_ENABLED", ss.enabled)?,
policy: parse_string_env("SANDBOX_POLICY", ss.policy.clone())?,
allow_full_access: parse_bool_env("SANDBOX_ALLOW_FULL_ACCESS", false)?,
timeout_secs: parse_optional_env("SANDBOX_TIMEOUT_SECS", ss.timeout_secs)?,
memory_limit_mb: parse_optional_env("SANDBOX_MEMORY_LIMIT_MB", ss.memory_limit_mb)?,
cpu_shares: parse_optional_env("SANDBOX_CPU_SHARES", ss.cpu_shares)?,
image: parse_string_env("SANDBOX_IMAGE", ss.image.clone())?,
auto_pull_image: parse_bool_env("SANDBOX_AUTO_PULL", ss.auto_pull_image)?,
extra_allowed_domains: extra_domains,
reaper_interval_secs,
orphan_threshold_secs,
})
}
pub fn to_sandbox_config(&self) -> crate::sandbox::SandboxConfig {
use crate::sandbox::SandboxPolicy;
use std::time::Duration;
let mut policy = self.policy.parse().unwrap_or(SandboxPolicy::ReadOnly);
if policy == SandboxPolicy::FullAccess && !self.allow_full_access {
tracing::error!(
"SANDBOX_POLICY=full_access is set but SANDBOX_ALLOW_FULL_ACCESS is not \
set to 'true'. FullAccess bypasses Docker and runs commands directly on \
the host. Downgrading to WorkspaceWrite for safety. Set \
SANDBOX_ALLOW_FULL_ACCESS=true to explicitly enable FullAccess."
);
policy = SandboxPolicy::WorkspaceWrite;
}
let mut allowlist = crate::sandbox::default_allowlist();
allowlist.extend(self.extra_allowed_domains.clone());
crate::sandbox::SandboxConfig {
enabled: self.enabled,
policy,
allow_full_access: self.allow_full_access,
timeout: Duration::from_secs(self.timeout_secs),
memory_limit_mb: self.memory_limit_mb,
cpu_shares: self.cpu_shares,
network_allowlist: allowlist,
image: self.image.clone(),
auto_pull_image: self.auto_pull_image,
proxy_port: 0, }
}
}
#[derive(Debug, Clone)]
pub struct ClaudeCodeConfig {
pub enabled: bool,
pub config_dir: std::path::PathBuf,
pub model: String,
pub max_turns: u32,
pub memory_limit_mb: u64,
pub allowed_tools: Vec<String>,
}
fn default_claude_code_allowed_tools() -> Vec<String> {
[
"Read(*)",
"Write(*)",
"Edit(*)",
"Glob(*)",
"Grep(*)",
"NotebookEdit(*)",
"Bash(*)",
"Task(*)",
"WebFetch(*)",
"WebSearch(*)",
]
.into_iter()
.map(String::from)
.collect()
}
impl Default for ClaudeCodeConfig {
fn default() -> Self {
Self {
enabled: false,
config_dir: dirs::home_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.join(".claude"),
model: "sonnet".to_string(),
max_turns: 50,
memory_limit_mb: 4096,
allowed_tools: default_claude_code_allowed_tools(),
}
}
}
impl ClaudeCodeConfig {
pub fn from_env() -> Self {
match Self::resolve_env_only() {
Ok(c) => c,
Err(e) => {
tracing::warn!("Failed to resolve ClaudeCodeConfig: {e}, using defaults");
Self::default()
}
}
}
pub fn extract_oauth_token() -> Option<String> {
if cfg!(target_os = "macos") {
match std::process::Command::new("security")
.args([
"find-generic-password",
"-s",
"Claude Code-credentials",
"-w",
])
.output()
{
Ok(output) if output.status.success() => {
if let Ok(json) = String::from_utf8(output.stdout) {
return parse_oauth_access_token(json.trim());
}
}
Ok(_) => {
tracing::debug!("No Claude Code credentials in macOS Keychain");
}
Err(e) => {
tracing::debug!("Failed to query macOS Keychain: {e}");
}
}
}
if let Some(home) = dirs::home_dir() {
let creds_path = home.join(".claude").join(".credentials.json");
if let Ok(json) = std::fs::read_to_string(&creds_path) {
return parse_oauth_access_token(&json);
}
}
None
}
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let defaults = Self::default();
Ok(Self {
enabled: parse_bool_env("CLAUDE_CODE_ENABLED", settings.sandbox.claude_code_enabled)?,
config_dir: optional_env("CLAUDE_CONFIG_DIR")?
.map(std::path::PathBuf::from)
.unwrap_or(defaults.config_dir),
model: parse_string_env("CLAUDE_CODE_MODEL", defaults.model)?,
max_turns: parse_optional_env("CLAUDE_CODE_MAX_TURNS", defaults.max_turns)?,
memory_limit_mb: parse_optional_env(
"CLAUDE_CODE_MEMORY_LIMIT_MB",
defaults.memory_limit_mb,
)?,
allowed_tools: optional_env("CLAUDE_CODE_ALLOWED_TOOLS")?
.map(|s| {
s.split(',')
.map(|t| t.trim().to_string())
.filter(|t| !t.is_empty())
.collect()
})
.unwrap_or(defaults.allowed_tools),
})
}
fn resolve_env_only() -> Result<Self, ConfigError> {
let defaults = Self::default();
Ok(Self {
enabled: parse_bool_env("CLAUDE_CODE_ENABLED", defaults.enabled)?,
config_dir: optional_env("CLAUDE_CONFIG_DIR")?
.map(std::path::PathBuf::from)
.unwrap_or(defaults.config_dir),
model: parse_string_env("CLAUDE_CODE_MODEL", defaults.model)?,
max_turns: parse_optional_env("CLAUDE_CODE_MAX_TURNS", defaults.max_turns)?,
memory_limit_mb: parse_optional_env(
"CLAUDE_CODE_MEMORY_LIMIT_MB",
defaults.memory_limit_mb,
)?,
allowed_tools: optional_env("CLAUDE_CODE_ALLOWED_TOOLS")?
.map(|s| {
s.split(',')
.map(|t| t.trim().to_string())
.filter(|t| !t.is_empty())
.collect()
})
.unwrap_or(defaults.allowed_tools),
})
}
}
fn parse_oauth_access_token(json: &str) -> Option<String> {
let creds: serde_json::Value = serde_json::from_str(json).ok()?;
let token = creds["claudeAiOauth"]["accessToken"].as_str()?;
if !token.starts_with("sk-ant-oat") {
tracing::debug!("Ignoring credential store token with unexpected prefix");
return None;
}
Some(token.to_string())
}
#[cfg(test)]
mod tests {
use crate::config::sandbox::*;
use crate::testing::credentials::*;
#[test]
fn sandbox_mode_config_default_values() {
let cfg = SandboxModeConfig::default();
assert!(cfg.enabled);
assert_eq!(cfg.policy, "readonly");
assert_eq!(cfg.timeout_secs, 120);
assert_eq!(cfg.memory_limit_mb, 2048);
assert_eq!(cfg.cpu_shares, 1024);
assert_eq!(cfg.image, "ironclaw-worker:latest");
assert!(cfg.auto_pull_image);
assert!(cfg.extra_allowed_domains.is_empty());
}
#[test]
fn sandbox_mode_config_custom_values() {
let cfg = SandboxModeConfig {
enabled: false,
policy: "full_access".to_string(),
timeout_secs: 600,
memory_limit_mb: 4096,
cpu_shares: 512,
image: "custom-worker:v2".to_string(),
auto_pull_image: false,
extra_allowed_domains: vec!["example.com".to_string()],
reaper_interval_secs: 300,
orphan_threshold_secs: 600,
allow_full_access: false,
};
assert!(!cfg.enabled);
assert_eq!(cfg.policy, "full_access");
assert_eq!(cfg.timeout_secs, 600);
assert_eq!(cfg.memory_limit_mb, 4096);
assert_eq!(cfg.cpu_shares, 512);
assert_eq!(cfg.image, "custom-worker:v2");
assert!(!cfg.auto_pull_image);
assert_eq!(cfg.extra_allowed_domains, vec!["example.com"]);
}
#[test]
fn sandbox_mode_to_sandbox_config_propagates_fields() {
let mode = SandboxModeConfig {
enabled: true,
policy: "workspace_write".to_string(),
timeout_secs: 300,
memory_limit_mb: 1024,
cpu_shares: 2048,
image: "test:latest".to_string(),
auto_pull_image: false,
extra_allowed_domains: vec!["custom.example.com".to_string()],
reaper_interval_secs: 300,
orphan_threshold_secs: 600,
allow_full_access: false,
};
let sc = mode.to_sandbox_config();
assert!(sc.enabled);
assert_eq!(sc.policy, crate::sandbox::SandboxPolicy::WorkspaceWrite);
assert_eq!(sc.timeout, std::time::Duration::from_secs(300));
assert_eq!(sc.memory_limit_mb, 1024);
assert_eq!(sc.cpu_shares, 2048);
assert_eq!(sc.image, "test:latest");
assert!(!sc.auto_pull_image);
assert!(
sc.network_allowlist
.contains(&"custom.example.com".to_string()),
"expected custom domain in allowlist"
);
}
#[test]
fn sandbox_mode_to_sandbox_config_invalid_policy_falls_back_to_readonly() {
let mode = SandboxModeConfig {
policy: "garbage_value".to_string(),
..SandboxModeConfig::default()
};
let sc = mode.to_sandbox_config();
assert_eq!(sc.policy, crate::sandbox::SandboxPolicy::ReadOnly);
}
#[test]
fn sandbox_mode_to_sandbox_config_includes_default_allowlist() {
let mode = SandboxModeConfig::default();
let sc = mode.to_sandbox_config();
assert!(
!sc.network_allowlist.is_empty(),
"default allowlist should not be empty"
);
}
#[test]
fn claude_code_config_default_values() {
let cfg = ClaudeCodeConfig::default();
assert!(!cfg.enabled);
assert_eq!(cfg.model, "sonnet");
assert_eq!(cfg.max_turns, 50);
assert_eq!(cfg.memory_limit_mb, 4096);
assert!(cfg.config_dir.ends_with(".claude"));
assert!(!cfg.allowed_tools.is_empty());
assert!(cfg.allowed_tools.contains(&"Bash(*)".to_string()));
assert!(cfg.allowed_tools.contains(&"Read(*)".to_string()));
assert!(cfg.allowed_tools.contains(&"Edit(*)".to_string()));
assert!(cfg.allowed_tools.contains(&"Write(*)".to_string()));
assert!(cfg.allowed_tools.contains(&"Grep(*)".to_string()));
assert!(cfg.allowed_tools.contains(&"WebFetch(*)".to_string()));
}
#[test]
fn claude_code_config_custom_values() {
let cfg = ClaudeCodeConfig {
enabled: true,
config_dir: std::path::PathBuf::from("/opt/claude"),
model: "opus".to_string(),
max_turns: 100,
memory_limit_mb: 8192,
allowed_tools: vec!["Read(*)".to_string(), "Bash(*)".to_string()],
};
assert!(cfg.enabled);
assert_eq!(cfg.config_dir, std::path::PathBuf::from("/opt/claude"));
assert_eq!(cfg.model, "opus");
assert_eq!(cfg.max_turns, 100);
assert_eq!(cfg.memory_limit_mb, 8192);
assert_eq!(cfg.allowed_tools.len(), 2);
}
#[test]
fn parse_oauth_token_valid() {
let json = format!(
r#"{{"claudeAiOauth": {{"accessToken": "{}"}}}}"#,
TEST_ANTHROPIC_OAUTH_BASIC
);
let token = parse_oauth_access_token(&json);
assert_eq!(token, Some(TEST_ANTHROPIC_OAUTH_BASIC.to_string()));
}
#[test]
fn parse_oauth_token_missing_access_token() {
let json = r#"{"claudeAiOauth": {}}"#;
assert_eq!(parse_oauth_access_token(json), None);
}
#[test]
fn parse_oauth_token_missing_oauth_key() {
let json = r#"{"someOtherKey": {"accessToken": "tok"}}"#;
assert_eq!(parse_oauth_access_token(json), None);
}
#[test]
fn parse_oauth_token_invalid_json() {
assert_eq!(parse_oauth_access_token("not json at all"), None);
}
#[test]
fn parse_oauth_token_empty_string() {
assert_eq!(parse_oauth_access_token(""), None);
}
#[test]
fn parse_oauth_token_nested_extra_fields() {
let json = format!(
r#"{{
"claudeAiOauth": {{
"accessToken": "{}",
"refreshToken": "rt-abc",
"expiresAt": 1700000000
}}
}}"#,
TEST_ANTHROPIC_OAUTH_NESTED
);
assert_eq!(
parse_oauth_access_token(&json),
Some(TEST_ANTHROPIC_OAUTH_NESTED.to_string())
);
}
#[test]
fn parse_oauth_token_access_token_is_not_string() {
let json = r#"{"claudeAiOauth": {"accessToken": 12345}}"#;
assert_eq!(parse_oauth_access_token(json), None);
}
#[test]
fn parse_oauth_token_rejects_invalid_prefix() {
let json = r#"{"claudeAiOauth": {"accessToken": "not-an-oauth-token"}}"#;
assert_eq!(parse_oauth_access_token(json), None);
}
#[test]
fn default_allowed_tools_has_expected_count() {
let tools = default_claude_code_allowed_tools();
assert_eq!(tools.len(), 10);
}
#[test]
fn default_allowed_tools_all_have_glob_pattern() {
let tools = default_claude_code_allowed_tools();
for tool in &tools {
assert!(
tool.ends_with("(*)"),
"tool '{tool}' should end with '(*)' glob pattern"
);
}
}
#[test]
fn test_full_access_downgraded_without_allow() {
let config = SandboxModeConfig {
policy: "full_access".to_string(),
allow_full_access: false,
..Default::default()
};
let sandbox = config.to_sandbox_config();
assert_eq!(
sandbox.policy,
crate::sandbox::SandboxPolicy::WorkspaceWrite
);
assert!(!sandbox.allow_full_access);
}
#[test]
fn test_full_access_allowed_with_explicit_opt_in() {
let config = SandboxModeConfig {
policy: "full_access".to_string(),
allow_full_access: true,
..Default::default()
};
let sandbox = config.to_sandbox_config();
assert_eq!(sandbox.policy, crate::sandbox::SandboxPolicy::FullAccess);
assert!(sandbox.allow_full_access);
}
#[test]
fn test_non_full_access_policy_unaffected() {
let config = SandboxModeConfig {
policy: "workspace_write".to_string(),
allow_full_access: false,
..Default::default()
};
let sandbox = config.to_sandbox_config();
assert_eq!(
sandbox.policy,
crate::sandbox::SandboxPolicy::WorkspaceWrite
);
}
#[test]
fn sandbox_resolve_falls_back_to_settings() {
let _guard = crate::config::helpers::lock_env();
let mut settings = crate::settings::Settings::default();
settings.sandbox.cpu_shares = 99;
settings.sandbox.auto_pull_image = false;
settings.sandbox.enabled = false;
let cfg = SandboxModeConfig::resolve(&settings).expect("resolve");
assert!(!cfg.enabled);
assert_eq!(cfg.cpu_shares, 99);
assert!(!cfg.auto_pull_image);
}
#[test]
fn sandbox_env_overrides_settings() {
let _guard = crate::config::helpers::lock_env();
let mut settings = crate::settings::Settings::default();
settings.sandbox.timeout_secs = 999;
unsafe { std::env::set_var("SANDBOX_TIMEOUT_SECS", "5") };
let cfg = SandboxModeConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("SANDBOX_TIMEOUT_SECS") };
assert_eq!(cfg.timeout_secs, 5);
}
#[test]
fn claude_code_resolve_uses_settings_enabled() {
let _guard = crate::config::helpers::lock_env();
let mut settings = crate::settings::Settings::default();
settings.sandbox.claude_code_enabled = true;
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
assert!(cfg.enabled);
}
#[test]
fn claude_code_resolve_defaults_disabled() {
let _guard = crate::config::helpers::lock_env();
let settings = crate::settings::Settings::default();
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
assert!(!cfg.enabled);
}
#[test]
fn claude_code_env_overrides_settings() {
let _guard = crate::config::helpers::lock_env();
let mut settings = crate::settings::Settings::default();
settings.sandbox.claude_code_enabled = true;
unsafe { std::env::set_var("CLAUDE_CODE_ENABLED", "false") };
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("CLAUDE_CODE_ENABLED") };
assert!(!cfg.enabled);
}
#[test]
fn test_readonly_policy_unaffected() {
let config = SandboxModeConfig {
policy: "readonly".to_string(),
allow_full_access: false,
..Default::default()
};
let sandbox = config.to_sandbox_config();
assert_eq!(sandbox.policy, crate::sandbox::SandboxPolicy::ReadOnly);
}
}