diff --git a/USAGE.md b/USAGE.md index 60db84bfa1..26af243ddd 100644 --- a/USAGE.md +++ b/USAGE.md @@ -211,7 +211,7 @@ Global workspace override flags: `--cwd PATH`, `-C PATH`, and `--directory PATH` Supported permission modes (default: `workspace-write`): -- `read-only` allows inspection-only local tools such as file reads, glob/grep searches, local skills, and status-style reporting. It does not allow workspace mutation, network-fetch/search tools, or arbitrary command execution. +- `read-only` allows inspection-only local tools such as file reads, glob/grep searches, local skills, and status-style reporting. It does not allow workspace mutation, network-fetch/search tools, or arbitrary command execution. `read-only` is a ceiling: even if an allow rule is configured, it cannot grant access to tools requiring higher permissions. - `workspace-write` is the safe default. It allows reads plus direct file-editing tools inside the current workspace, including write/edit/notebook/config/plan-mode updates, while still gating network-fetch/search tools, arbitrary shell execution, subagent launches, REPL subprocesses, and other full-access tools behind an explicit escalation. - `danger-full-access` allows every registered tool requirement, including arbitrary command execution, web fetch/search, subagent launches, subprocess REPLs, and unrestricted tool access. Select it only with an explicit `--permission-mode danger-full-access`, `--dangerously-skip-permissions`, `--skip-permissions`, env, or config opt-in. diff --git a/rust/crates/runtime/src/permissions.rs b/rust/crates/runtime/src/permissions.rs index 66c0f8856a..32a250b8ef 100644 --- a/rust/crates/runtime/src/permissions.rs +++ b/rust/crates/runtime/src/permissions.rs @@ -312,7 +312,13 @@ impl PermissionPolicy { // The ladder, in one place. An `allow` rule is an explicit grant for // this tool and outranks the mode; otherwise the mode decides through // [`PermissionMode::covers`]. - if allow_rule.is_some() || current_mode.covers(required_mode) { + // + // Exception: read-only is a ceiling. Even with an allow rule, a tool + // requiring a higher permission mode cannot run in read-only mode. This + // ensures that allow rules cannot be used to bypass read-only protection + // in untrusted configurations (e.g., project-scope .claw/settings.json). + let allow_rule_applies = allow_rule.is_some() && current_mode != PermissionMode::ReadOnly; + if allow_rule_applies || current_mode.covers(required_mode) { return PermissionOutcome::Allow; } @@ -748,13 +754,15 @@ mod tests { #[test] fn applies_rule_based_denials_and_allows() { + // Test allow/deny rules in a higher permission mode where they can take effect. + // read-only is a ceiling, so allow rules cannot lift it, but deny rules still work. let rules = RuntimePermissionRuleConfig::new( vec!["bash(git:*)".to_string()], vec!["bash(rm -rf:*)".to_string()], Vec::new(), Vec::new(), ); - let policy = PermissionPolicy::new(PermissionMode::ReadOnly) + let policy = PermissionPolicy::new(PermissionMode::WorkspaceWrite) .with_tool_requirement("bash", PermissionMode::DangerFullAccess) .with_permission_rules(&rules); @@ -764,7 +772,7 @@ mod tests { ); assert!(matches!( policy.authorize("bash", r#"{"command":"rm -rf /tmp/x"}"#, None), - PermissionOutcome::Deny { reason } if reason.contains("denied by rule") + PermissionOutcome::Deny { ref reason } if reason.contains("denied by rule") )); } @@ -890,4 +898,151 @@ mod tests { Some("hook requested confirmation") ); } + + #[test] + fn read_only_ceiling_deny_allow_rule_for_workspace_write_tool() { + let rules = RuntimePermissionRuleConfig::new( + vec!["write_file(*)".to_string()], + Vec::new(), + Vec::new(), + Vec::new(), + ); + let policy = PermissionPolicy::new(PermissionMode::ReadOnly) + .with_tool_requirement("write_file", PermissionMode::WorkspaceWrite) + .with_permission_rules(&rules); + + let outcome = policy.authorize("write_file", r#"{"path":"test.txt","content":"x"}"#, None); + + assert!( + matches!( + outcome, + PermissionOutcome::Deny { ref reason } if reason.contains("read-only") + ), + "allow rule should not lift read-only mode: {outcome:?}" + ); + } + + #[test] + fn read_only_ceiling_deny_allow_rule_for_edit_file() { + let rules = RuntimePermissionRuleConfig::new( + vec!["edit_file(*)".to_string()], + Vec::new(), + Vec::new(), + Vec::new(), + ); + let policy = PermissionPolicy::new(PermissionMode::ReadOnly) + .with_tool_requirement("edit_file", PermissionMode::WorkspaceWrite) + .with_permission_rules(&rules); + + let outcome = policy.authorize( + "edit_file", + r#"{"path":"test.txt","old_string":"a","new_string":"b"}"#, + None, + ); + + assert!( + matches!( + outcome, + PermissionOutcome::Deny { ref reason } if reason.contains("read-only") + ), + "allow rule should not lift read-only mode: {outcome:?}" + ); + } + + #[test] + fn read_only_ceiling_deny_allow_rule_for_danger_full_access_tool() { + let rules = RuntimePermissionRuleConfig::new( + vec!["bash(git:*)".to_string()], + Vec::new(), + Vec::new(), + Vec::new(), + ); + let policy = PermissionPolicy::new(PermissionMode::ReadOnly) + .with_tool_requirement("bash", PermissionMode::DangerFullAccess) + .with_permission_rules(&rules); + + let outcome = policy.authorize("bash", r#"{"command":"git status"}"#, None); + + assert!( + matches!( + outcome, + PermissionOutcome::Deny { ref reason } if reason.contains("read-only") + ), + "allow rule should not lift read-only mode: {outcome:?}" + ); + } + + #[test] + fn plan_mode_ceiling_deny_allow_rule_for_workspace_write_tool() { + // plan mode is equivalent to read-only + let rules = RuntimePermissionRuleConfig::new( + vec!["write_file(*)".to_string()], + Vec::new(), + Vec::new(), + Vec::new(), + ); + let policy = + PermissionPolicy::new(PermissionMode::ReadOnly) // plan parses to ReadOnly + .with_tool_requirement("write_file", PermissionMode::WorkspaceWrite) + .with_permission_rules(&rules); + + let outcome = policy.authorize("write_file", r#"{"path":"test.txt","content":"x"}"#, None); + + assert!( + matches!( + outcome, + PermissionOutcome::Deny { ref reason } if reason.contains("read-only") + ), + "allow rule should not lift read-only mode (plan is read-only): {outcome:?}" + ); + } + + #[test] + fn hook_allow_should_not_lift_read_only_ceiling() { + let rules = RuntimePermissionRuleConfig::new(vec![], vec![], vec![], vec![]); + let policy = PermissionPolicy::new(PermissionMode::ReadOnly) + .with_tool_requirement("write_file", PermissionMode::WorkspaceWrite) + .with_permission_rules(&rules); + let context = PermissionContext::new( + Some(PermissionOverride::Allow), + Some("hook approved".to_string()), + ); + + let outcome = policy.authorize_with_context( + "write_file", + r#"{"path":"test.txt","content":"x"}"#, + &context, + None, + ); + + assert!( + matches!( + outcome, + PermissionOutcome::Deny { ref reason } if reason.contains("read-only") + ), + "hook allow should not lift read-only ceiling: {outcome:?}" + ); + } + + #[test] + fn allow_rule_still_works_in_workspace_write_mode() { + // Confirm that allow rules still work correctly in higher permission modes + let rules = RuntimePermissionRuleConfig::new( + vec!["bash(git:*)".to_string()], + Vec::new(), + Vec::new(), + Vec::new(), + ); + let policy = PermissionPolicy::new(PermissionMode::WorkspaceWrite) + .with_tool_requirement("bash", PermissionMode::DangerFullAccess) + .with_permission_rules(&rules); + + let outcome = policy.authorize("bash", r#"{"command":"git status"}"#, None); + + assert_eq!( + outcome, + PermissionOutcome::Allow, + "allow rule should still work in workspace-write mode" + ); + } }