fix(security): enforce action guards in file_write and scheduler (#269)
This commit is contained in:
parent
60f3282ad4
commit
3bdabdc7ec
2 changed files with 132 additions and 0 deletions
|
|
@ -138,6 +138,20 @@ async fn run_job_command(
|
|||
security: &SecurityPolicy,
|
||||
job: &CronJob,
|
||||
) -> (bool, String) {
|
||||
if !security.can_act() {
|
||||
return (
|
||||
false,
|
||||
"blocked by security policy: autonomy is read-only".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
if security.is_rate_limited() {
|
||||
return (
|
||||
false,
|
||||
"blocked by security policy: rate limit exceeded".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
if !security.is_command_allowed(&job.command) {
|
||||
return (
|
||||
false,
|
||||
|
|
@ -155,6 +169,13 @@ async fn run_job_command(
|
|||
);
|
||||
}
|
||||
|
||||
if !security.record_action() {
|
||||
return (
|
||||
false,
|
||||
"blocked by security policy: action budget exhausted".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
let output = Command::new("sh")
|
||||
.arg("-lc")
|
||||
.arg(&job.command)
|
||||
|
|
@ -261,6 +282,34 @@ mod tests {
|
|||
assert!(output.contains("/etc/passwd"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn run_job_command_blocks_readonly_mode() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let mut config = test_config(&tmp);
|
||||
config.autonomy.level = crate::security::AutonomyLevel::ReadOnly;
|
||||
let job = test_job("echo should-not-run");
|
||||
let security = SecurityPolicy::from_config(&config.autonomy, &config.workspace_dir);
|
||||
|
||||
let (success, output) = run_job_command(&config, &security, &job).await;
|
||||
assert!(!success);
|
||||
assert!(output.contains("blocked by security policy"));
|
||||
assert!(output.contains("read-only"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn run_job_command_blocks_rate_limited() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let mut config = test_config(&tmp);
|
||||
config.autonomy.max_actions_per_hour = 0;
|
||||
let job = test_job("echo should-not-run");
|
||||
let security = SecurityPolicy::from_config(&config.autonomy, &config.workspace_dir);
|
||||
|
||||
let (success, output) = run_job_command(&config, &security, &job).await;
|
||||
assert!(!success);
|
||||
assert!(output.contains("blocked by security policy"));
|
||||
assert!(output.contains("rate limit exceeded"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn execute_job_with_retry_recovers_after_first_failure() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue