mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-29 11:38:00 +08:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf4cc72a73 | ||
|
|
f64bd3f7f7 | ||
|
|
eef0938e07 |
No files matched your search
@@ -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`, legacy top-level `permissionMode`): 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 <setting> in <path> 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:
|
||||
|
||||
@@ -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,55 @@ impl ConfigLoader {
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_untrusted_project_hooks(
|
||||
fn strip_untrusted_project_settings(
|
||||
source: ConfigSource,
|
||||
object: &mut BTreeMap<String, JsonValue>,
|
||||
path: &Path,
|
||||
) -> Option<String> {
|
||||
// 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<String> {
|
||||
// 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()
|
||||
));
|
||||
}
|
||||
|
||||
// The legacy top-level `permissionMode` key selects the mode too.
|
||||
if object.remove("permissionMode").is_some() {
|
||||
warnings.push(format!(
|
||||
"Ignoring project permissionMode in {} because project 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 +3733,81 @@ 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 project_config_legacy_permission_mode_cannot_escalate() {
|
||||
// given
|
||||
let root = temp_dir();
|
||||
let cwd = root.join("project");
|
||||
let home = root.join("home").join(".claw");
|
||||
fs::create_dir_all(&cwd).expect("project dir");
|
||||
fs::create_dir_all(&home).expect("home config dir");
|
||||
fs::write(
|
||||
cwd.join(".claw.json"),
|
||||
r#"{"permissionMode":"danger-full-access"}"#,
|
||||
)
|
||||
.expect("write untrusted legacy project permission mode");
|
||||
|
||||
// when
|
||||
let (loaded, warnings) = ConfigLoader::new(&cwd, &home)
|
||||
.load_collecting_warnings()
|
||||
.expect("config should load");
|
||||
|
||||
// then
|
||||
assert_eq!(loaded.permission_mode(), None);
|
||||
assert!(warnings
|
||||
.iter()
|
||||
.any(|warning| warning.contains("Ignoring project permissionMode")));
|
||||
|
||||
fs::remove_dir_all(root).expect("cleanup temp dir");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_mode_aliases_resolve_to_expected_modes() {
|
||||
// given / when / then
|
||||
|
||||
@@ -1255,7 +1255,7 @@ mod tests {
|
||||
fs::create_dir_all(root.join(".claw")).expect("claw dir");
|
||||
fs::write(root.join("CLAUDE.md"), "Project rules").expect("write instructions");
|
||||
fs::write(
|
||||
root.join(".claw").join("settings.json"),
|
||||
root.join(".claw").join("settings.local.json"),
|
||||
r#"{"permissionMode":"acceptEdits"}"#,
|
||||
)
|
||||
.expect("write settings");
|
||||
@@ -1418,7 +1418,7 @@ mod tests {
|
||||
fs::create_dir_all(root.join(".claw")).expect("claw dir");
|
||||
fs::write(root.join("CLAUDE.md"), "Project rules").expect("write CLAUDE.md");
|
||||
fs::write(
|
||||
root.join(".claw").join("settings.json"),
|
||||
root.join(".claw").join("settings.local.json"),
|
||||
r#"{"permissionMode":"acceptEdits"}"#,
|
||||
)
|
||||
.expect("write settings");
|
||||
|
||||
Reference in new issue
Block a user