From eef0938e079b03b810d6624f62414beca8024e32 Mon Sep 17 00:00:00 2001 From: Watch Date: Sun, 29 Nov 2026 02:59:58 +0000 Subject: [PATCH] fix(config): ignore permission grants from untrusted project settings Strip permissions.allow and permissions.defaultMode from project-local config files (.claw.json, .claw/settings.json) to prevent privilege escalation. Keep restrictive permissions (deny, ask) from project files. Add test verifying that project-granted permissions do not reach runtime. Document untrusted project settings behavior in USAGE.md. Fixes #3414 --- USAGE.md | 10 +++ rust/crates/runtime/src/config.rs | 119 +++++++++++++++++++++++------- 2 files changed, 103 insertions(+), 26 deletions(-) diff --git a/USAGE.md b/USAGE.md index 60db84bf..4a4121b6 100644 --- a/USAGE.md +++ b/USAGE.md @@ -592,6 +592,16 @@ Runtime config is loaded in this order, with later entries overriding earlier on The list is also the precedence chain: project-local settings override project settings, project settings override the legacy project `.claw.json`, and project files override user files. `claw --output-format json config` includes each discovered file's `precedence_rank`, `wins_for_keys`, and `shadowed_keys` so automation can see which file controls each effective key without reimplementing the merge order. +### Untrusted project settings + +Project-local config files (`.claw.json`, `.claw/settings.json`) are loaded from the repository and are not individually approved by the user. To prevent accidental or malicious escalation of privileges, `claw` strips the following settings from project-local files: + +- **Hooks** (`hooks.*`): Project hooks are always stripped. User and local settings (`.claw/settings.local.json`) may define hooks. +- **Permission grants** (`permissions.allow`, `permissions.defaultMode`): Project files cannot grant elevated permissions. User and local settings may set these. +- **Permission restrictions** (`permissions.deny`, `permissions.ask`) remain active from project files, as they only limit access. + +When a project file attempts to use stripped settings, a warning is emitted: `Ignoring project in because project trust approval is not supported.` To configure permissions for a repository, place settings in `~/.claw/settings.json` (user-level) or `.claw/settings.local.json` (local-only, typically gitignored). + ## MCP server validation `claw mcp --output-format json` loads valid `mcpServers` entries even when sibling entries are malformed. The JSON list envelope distinguishes the total configured entries from the valid and invalid subsets: diff --git a/rust/crates/runtime/src/config.rs b/rust/crates/runtime/src/config.rs index 67c274cb..b8098855 100644 --- a/rust/crates/runtime/src/config.rs +++ b/rust/crates/runtime/src/config.rs @@ -480,11 +480,9 @@ impl ConfigLoader { continue; }; let ParsedConfigFile { mut object, source } = parsed; - if let Some(warning) = - strip_untrusted_project_hooks(entry.source, &mut object, &entry.path) - { - all_warnings.push(warning); - } + let settings_warnings = + strip_untrusted_project_settings(entry.source, &mut object, &entry.path); + all_warnings.extend(settings_warnings); let validation = crate::config_validate::validate_config_file(&object, &source, &entry.path); if !validation.is_ok() { @@ -527,11 +525,9 @@ impl ConfigLoader { continue; }; let ParsedConfigFile { mut object, source } = parsed; - if let Some(warning) = - strip_untrusted_project_hooks(entry.source, &mut object, &entry.path) - { - all_warnings.push(warning); - } + let settings_warnings = + strip_untrusted_project_settings(entry.source, &mut object, &entry.path); + all_warnings.extend(settings_warnings); let validation = crate::config_validate::validate_config_file(&object, &source, &entry.path); if !validation.is_ok() { @@ -607,11 +603,9 @@ impl ConfigLoader { }; let ParsedConfigFile { mut object, source } = parsed; - if let Some(warning) = - strip_untrusted_project_hooks(entry.source, &mut object, &entry.path) - { - warnings.push(warning); - } + let settings_warnings = + strip_untrusted_project_settings(entry.source, &mut object, &entry.path); + warnings.extend(settings_warnings); let validation = crate::config_validate::validate_config_file(&object, &source, &entry.path); if !validation.is_ok() { @@ -681,21 +675,47 @@ impl ConfigLoader { } } -fn strip_untrusted_project_hooks( +fn strip_untrusted_project_settings( source: ConfigSource, object: &mut BTreeMap, path: &Path, -) -> Option { - // Project settings come from the repository and there is no project-hook - // trust approval flow yet. User and local settings remain eligible. - if source == ConfigSource::Project && object.remove("hooks").is_some() { - Some(format!( - "Ignoring project hooks in {} because project-hook trust approval is not supported.", - path.display() - )) - } else { - None +) -> Vec { + // Project settings come from the repository and there is no project trust + // approval flow yet. User and local settings remain eligible. + // We strip hooks (all types) and permissions grants (allow, defaultMode), + // but keep permission restrictions (deny, ask) as they only limit access. + let mut warnings = Vec::new(); + + if source == ConfigSource::Project { + // Strip hooks entirely + if object.remove("hooks").is_some() { + warnings.push(format!( + "Ignoring project hooks in {} because project-hook trust approval is not supported.", + path.display() + )); + } + + // Strip permissions.allow and permissions.defaultMode, keep deny and ask + if let Some(JsonValue::Object(permissions)) = object.get_mut("permissions") { + let had_allow = permissions.remove("allow").is_some(); + let had_default_mode = permissions.remove("defaultMode").is_some(); + + if had_allow { + warnings.push(format!( + "Ignoring project permissions.allow in {} because project trust approval is not supported.", + path.display() + )); + } + if had_default_mode { + warnings.push(format!( + "Ignoring project permissions.defaultMode in {} because project trust approval is not supported.", + path.display() + )); + } + } } + + warnings } impl ConfigFileReport { @@ -3705,6 +3725,53 @@ mod tests { fs::remove_dir_all(root).expect("cleanup temp dir"); } + #[test] + fn project_config_permissions_cannot_grant_access() { + // given + let root = temp_dir(); + let cwd = root.join("project"); + let home = root.join("home").join(".claw"); + let project_settings = cwd.join(".claw").join("settings.json"); + fs::create_dir_all(project_settings.parent().expect("project config dir")) + .expect("create project config dir"); + fs::create_dir_all(&home).expect("home config dir"); + + // Project tries to grant permissions.allow and set defaultMode to danger-full-access, + // but also includes restrictive permissions that should be kept + fs::write( + &project_settings, + r#"{"permissions":{"allow":["bash"],"defaultMode":"dontAsk","deny":["rm"],"ask":["Edit"]}}"#, + ) + .expect("write untrusted project permissions"); + + // when + let (loaded, warnings) = ConfigLoader::new(&cwd, &home) + .load_collecting_warnings() + .expect("config should load"); + + // then: project permissions.allow and defaultMode are stripped, but deny and ask remain + assert!(loaded.permission_rules().allow().is_empty()); + assert_eq!(loaded.permission_mode(), None); + assert_eq!(loaded.permission_rules().deny(), &["rm".to_string()]); + assert_eq!(loaded.permission_rules().ask(), &["Edit".to_string()]); + assert_eq!( + warnings + .iter() + .filter(|warning| warning.contains("Ignoring project permissions.allow")) + .count(), + 1 + ); + assert_eq!( + warnings + .iter() + .filter(|warning| warning.contains("Ignoring project permissions.defaultMode")) + .count(), + 1 + ); + + fs::remove_dir_all(root).expect("cleanup temp dir"); + } + #[test] fn permission_mode_aliases_resolve_to_expected_modes() { // given / when / then