Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 71 additions & 11 deletions src/permissions/rules/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use std::path::Path;

use serde::Deserialize;

use super::rule::{Rule, Spec};
use super::split::{split_command, Segment};
use super::{Effect, Layer, PathContext, RuleLists, Subject, Verdict, PROJECT_SETTINGS};
Expand Down Expand Up @@ -58,7 +56,7 @@ impl PermissionRules {
match read_project(workspace) {
Ok(Some(lists)) => rules.add(&lists, Layer::Project, home.as_deref()),
Ok(None) => {}
Err(why) => rules.warnings.push(why),
Err(why) => rules.refuse_for_project(&why),
}
rules
}
Expand Down Expand Up @@ -111,6 +109,28 @@ impl PermissionRules {
}
}

/// The project's settings file is there and cannot be used: fail closed,
/// as for a broken managed file. A deny rule in a file incode cannot read
/// (a top-level `"deny"` instead of `"permissions": {"deny": ...}`, a typo,
/// bad JSON) was silently dropped before, and the call it was written to
/// stop ran (measured: issue #19's own steps wrote `secret.txt`). The
/// reason travels in the catch-all's text, so the refusal every call gets
/// says what to fix, on a `-t` run too, not only under `/permissions`.
///
/// Called by [`PermissionRules::load`] when `read_project` fails.
fn refuse_for_project(&mut self, why: &str) {
let warning =
format!("{why}; every tool call is refused until {PROJECT_SETTINGS} is fixed");
if let Ok(mut rule) = Rule::parse("*", None) {
rule.text = format!("*: {warning}");
self.deny.push(Sourced {
rule,
layer: Layer::Project,
});
}
self.warnings.push(warning);
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.deny.is_empty() && self.ask.is_empty() && self.allow.is_empty()
Expand Down Expand Up @@ -311,12 +331,14 @@ fn host_matches(url: &str, domain: &str) -> bool {

/// The project's lists, from [`PROJECT_SETTINGS`]. `None` when there is no
/// file, or no `permissions` in it.
///
/// # Errors
/// When the file cannot be read, is not JSON, or holds a key incode does not
/// know, at the top or inside `permissions`: such a key is most often a rule
/// list in the wrong place (`{"deny": [...]}`), and ignoring it would drop the
/// rule while the run goes on ([`PermissionRules::refuse_for_project`]).
fn read_project(workspace: &Path) -> Result<Option<RuleLists>, String> {
#[derive(Deserialize)]
struct ProjectSettings {
#[serde(default)]
permissions: Option<RuleLists>,
}
const RIGHT_SHAPE: &str = r#"{"permissions": {"deny": [...], "ask": [...]}}"#;
let path = workspace.join(PROJECT_SETTINGS);
let text = match crate::text::read_authored(&path) {
Ok(text) => text,
Expand All @@ -326,7 +348,45 @@ fn read_project(workspace: &Path) -> Result<Option<RuleLists>, String> {
if text.trim().is_empty() {
return Ok(None);
}
serde_json::from_str::<ProjectSettings>(&text)
.map(|s| s.permissions)
.map_err(|e| format!("{} is not valid settings: {e}", path.display()))
let value: serde_json::Value = serde_json::from_str(&text)
.map_err(|e| format!("{} is not valid JSON: {e}", path.display()))?;
let unknown = |object: &serde_json::Map<String, serde_json::Value>, known: &[&str]| {
object
.keys()
.filter(|k| !known.contains(&k.as_str()))
.map(|k| format!("`{k}`"))
.collect::<Vec<_>>()
};
let Some(top) = value.as_object() else {
return Err(format!(
"{} must be a JSON object such as {RIGHT_SHAPE}",
path.display()
));
};
let stray = unknown(top, &["permissions"]);
if !stray.is_empty() {
return Err(format!(
"{} has {} at the top level, which incode does not read (rules go under \
`permissions`: {RIGHT_SHAPE})",
path.display(),
stray.join(", ")
));
}
let Some(permissions) = top.get("permissions") else {
return Ok(None);
};
if let Some(lists) = permissions.as_object() {
let stray = unknown(lists, &["deny", "ask", "allow"]);
if !stray.is_empty() {
return Err(format!(
"{}: `permissions` has {}, which incode does not read (it reads `deny`, `ask` \
and `allow`)",
path.display(),
stray.join(", ")
));
}
}
serde_json::from_value::<RuleLists>(permissions.clone())
.map(Some)
.map_err(|e| format!("{}: `permissions` is not valid: {e}", path.display()))
}
68 changes: 68 additions & 0 deletions src/permissions/rules/tests/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,74 @@ fn project_settings_with_a_byte_order_mark_still_deny() {
);
}

/// **A PROJECT SETTINGS FILE INCODE CANNOT USE REFUSES EVERY CALL, AND SAYS
/// WHY.** Issue #19's own steps wrote `{"deny":["Write(secret.txt)"]}`: a
/// top-level `deny` incode does not read. It was dropped without a word, and
/// the Write it was meant to stop ran (measured on a real `-t` run). Now that
/// file, a typo inside `permissions`, and bad JSON each fail closed, the
/// refusal naming the file and the fix. Control: the right shape denies only
/// what it lists.
#[test]
fn a_project_settings_file_incode_cannot_use_refuses_every_call_and_says_why() {
let ws_for = |dir: &tempfile::TempDir| PathContext {
workspace: dir.path().to_path_buf(),
home: None,
};
let write = |path: &str| Subject::Paths(vec![path.to_string()]);
for (text, says) in [
(
r#"{"deny":["Write(secret.txt)"]}"#,
"`deny` at the top level",
),
(
r#"{"permissions": {"denny": ["Write(secret.txt)"]}}"#,
"`permissions` has `denny`",
),
(
r#"{"permissions": {"deny": ["Write(secret.txt)"]"#,
"not valid JSON",
),
(r#"["Write(secret.txt)"]"#, "must be a JSON object"),
] {
let dir = tempfile::tempdir().expect("dir");
std::fs::create_dir_all(dir.path().join(".incode")).expect("mkdir");
std::fs::write(dir.path().join(PROJECT_SETTINGS), text).expect("write");
let rules = PermissionRules::load(dir.path());
let ws = ws_for(&dir);
for (tool, subject) in [
("Write", write("secret.txt")),
("Read", write("other.txt")),
("Bash", Subject::Command("ls".to_string())),
] {
let verdict = rules.decide(tool, &subject, &ws).expect("decided");
assert_eq!(verdict.effect, Effect::Deny, "{text}: {tool}");
assert_eq!(verdict.layer, Layer::Project, "{text}");
let said = verdict.describe();
assert!(said.contains(says), "{text}: {said}");
assert!(said.contains(PROJECT_SETTINGS), "{text}: {said}");
}
assert_eq!(rules.warnings().len(), 1, "{:?}", rules.warnings());
}

let dir = tempfile::tempdir().expect("dir");
std::fs::create_dir_all(dir.path().join(".incode")).expect("mkdir");
std::fs::write(
dir.path().join(PROJECT_SETTINGS),
r#"{"permissions": {"deny": ["Write(secret.txt)"]}}"#,
)
.expect("write");
let rules = PermissionRules::load(dir.path());
let ws = ws_for(&dir);
assert!(rules.warnings().is_empty(), "{:?}", rules.warnings());
assert_eq!(
rules
.decide("Write", &write("secret.txt"), &ws)
.map(|v| v.effect),
Some(Effect::Deny)
);
assert_eq!(rules.decide("Write", &write("other.txt"), &ws), None);
}

// --- a turn's allowlist ---------------------------------------------------

fn listed(texts: &[&str]) -> Allowlist {
Expand Down
Loading