Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent): restrict .jan/agent/ to AGENT.md for general tools

Akarshan Biswasa435e3ad2c17
diff --git a/src-tauri/src/core/agent/tools/gate.rs b/src-tauri/src/core/agent/tools/gate.rs
index 2948265ad..197002ee7 100644
--- a/src-tauri/src/core/agent/tools/gate.rs
+++ b/src-tauri/src/core/agent/tools/gate.rs
@@ -3,7 +3,9 @@ use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::core::agent::permissions::ToolPermissions;
-use crate::core::agent::tools::sandbox::{escapes_project, within_agent_workspace};
+use crate::core::agent::tools::sandbox::{
+ command_touches_restricted_agent_path, escapes_project, is_restricted_agent_path,
+};
use crate::core::agent::tools::{BuiltinTool, Capability};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
@@ -98,6 +100,24 @@ pub fn resolve_decision(
if perms.is_denied(tool.name) {
return Decision::HardDeny;
}
+ // Only the agent's own skills/memory/AGENT.md are reachable under .jan/agent/.
+ // Any other path there (agent.toml, the dir listing) is off-limits to every
+ // tool, ahead of allow rules so an allowed tool name cannot bypass it.
+ let hits_restricted = tool.path_args.iter().any(|key| {
+ args.get(key)
+ .and_then(|v| v.as_str())
+ .map(|p| is_restricted_agent_path(project_root, p))
+ .unwrap_or(false)
+ });
+ let exec_hits_restricted = tool.capability == Capability::Exec
+ && args
+ .get("command")
+ .and_then(|v| v.as_str())
+ .map(|c| command_touches_restricted_agent_path(project_root, c))
+ .unwrap_or(false);
+ if hits_restricted || exec_hits_restricted {
+ return Decision::HardDeny;
+ }
if perms.is_allowed(tool.name) {
return Decision::Allow;
}
@@ -120,22 +140,7 @@ pub fn resolve_decision(
Decision::Prompt(PromptKind::ReadEscape)
}
}
- Capability::Write => {
- // The agent's own skills/memory workspace is auto-writable: it is
- // agent metadata, not user source, so it skips the write prompt.
- let in_workspace = !tool.path_args.is_empty()
- && tool.path_args.iter().all(|key| {
- args.get(key)
- .and_then(|v| v.as_str())
- .map(|p| within_agent_workspace(project_root, p))
- .unwrap_or(false)
- });
- if in_workspace {
- Decision::Allow
- } else {
- gated(PromptKind::Write, grants)
- }
- }
+ Capability::Write => gated(PromptKind::Write, grants),
Capability::Exec => {
let command = args.get("command").and_then(|v| v.as_str()).unwrap_or("");
if grants.covers_command(command) {
@@ -210,6 +215,46 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}
+ #[test]
+ fn agent_config_is_off_limits_to_every_tool() {
+ let root = unique_root();
+ std::fs::create_dir_all(root.join(".jan/agent")).unwrap();
+ std::fs::write(root.join(".jan/agent/agent.toml"), b"[tools]\n").unwrap();
+ let perms = ToolPermissions::allow_all();
+ let grants = SessionGrants::default();
+ // Read/ls/find/grep and write/edit all hard-deny on agent.toml.
+ for tool in ["read", "ls", "find", "grep", "write", "edit"] {
+ let d = resolve_decision(
+ lookup(tool).unwrap(),
+ &json!({ "path": ".jan/agent/agent.toml" }),
+ &root,
+ &perms,
+ &grants,
+ );
+ assert_eq!(d, Decision::HardDeny, "{tool} on agent.toml must be denied");
+ }
+ // bash referencing it is denied too.
+ let d = resolve_decision(
+ lookup("bash").unwrap(),
+ &json!({"command": "cat .jan/agent/agent.toml"}),
+ &root,
+ &perms,
+ &grants,
+ );
+ assert_eq!(d, Decision::HardDeny);
+ // AGENT.md remains readable.
+ std::fs::write(root.join(".jan/agent/AGENT.md"), b"x").unwrap();
+ let d = resolve_decision(
+ lookup("read").unwrap(),
+ &json!({"path": ".jan/agent/AGENT.md"}),
+ &root,
+ &perms,
+ &grants,
+ );
+ assert_eq!(d, Decision::Allow);
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
#[test]
fn write_prompts_by_default() {
let root = unique_root();
@@ -280,45 +325,29 @@ mod tests {
}
#[test]
- fn write_to_agent_workspace_is_auto_allowed() {
+ fn general_tools_cannot_reach_skills_memory_or_config() {
let root = unique_root();
- let perms = ToolPermissions::new(PermissionDefault::ReadOnly, &[], &[], &[]);
+ let perms = ToolPermissions::allow_all();
let grants = SessionGrants::default();
- for path in [".jan/agent/skills/deploy.md", ".jan/agent/memory/notes.md"] {
- let d = resolve_decision(
- lookup("write").unwrap(),
- &json!({ "path": path }),
- &root,
- &perms,
- &grants,
- );
- assert_eq!(d, Decision::Allow, "workspace write should skip prompt: {path}");
+ // skills/ and memory/ are reachable only via the dedicated tools; general
+ // read/write/edit/ls/find/grep hard-deny there, same as agent.toml.
+ let paths = [
+ ".jan/agent/skills/deploy.md",
+ ".jan/agent/memory/notes.md",
+ ".jan/agent/agent.toml",
+ ];
+ for tool in ["read", "ls", "find", "grep", "write", "edit"] {
+ for path in paths {
+ let d = resolve_decision(
+ lookup(tool).unwrap(),
+ &json!({ "path": path }),
+ &root,
+ &perms,
+ &grants,
+ );
+ assert_eq!(d, Decision::HardDeny, "{tool} on {path} must be denied");
+ }
}
- // agent.toml is excluded from the exception -> still prompts.
- let d = resolve_decision(
- lookup("write").unwrap(),
- &json!({"path": ".jan/agent/agent.toml"}),
- &root,
- &perms,
- &grants,
- );
- assert_eq!(d, Decision::Prompt(PromptKind::Write));
- let _ = std::fs::remove_dir_all(&root);
- }
-
- #[test]
- fn deny_still_blocks_agent_workspace_write() {
- let root = unique_root();
- let perms = ToolPermissions::new(PermissionDefault::ReadOnly, &[], &s(&["write"]), &[]);
- let grants = SessionGrants::default();
- let d = resolve_decision(
- lookup("write").unwrap(),
- &json!({"path": ".jan/agent/skills/x.md"}),
- &root,
- &perms,
- &grants,
- );
- assert_eq!(d, Decision::HardDeny);
let _ = std::fs::remove_dir_all(&root);
}
diff --git a/src-tauri/src/core/agent/tools/sandbox.rs b/src-tauri/src/core/agent/tools/sandbox.rs
index 3a5b577f4..3be4c7165 100644
--- a/src-tauri/src/core/agent/tools/sandbox.rs
+++ b/src-tauri/src/core/agent/tools/sandbox.rs
@@ -18,21 +18,12 @@ pub fn escapes_project(project_root: &Path, raw: &str) -> Result<bool, String> {
Ok(!resolved.starts_with(&root))
}
-/// True iff `raw` resolves inside the agent's own workspace
-/// (`<project_root>/.jan/agent/skills/` or `.../memory/`). These hold agent
-/// metadata (skills it authors, memory it records), not user source, so writes
-/// there bypass the write prompt. `agent.toml` is deliberately NOT covered:
-/// letting the agent rewrite its own permission file would be a self-escalation.
-pub fn within_agent_workspace(project_root: &Path, raw: &str) -> bool {
- // Reject any `..` outright: this is an allow decision, so it must not rely on
- // `canonicalize_lenient`'s lenient handling of `..` in a non-existent tail,
- // which can diverge from how the OS resolves the path at write time.
- if Path::new(raw)
- .components()
- .any(|c| c == std::path::Component::ParentDir)
- {
- return false;
- }
+/// True iff `raw` resolves inside `<project_root>/.jan/agent/` to anything other
+/// than the `AGENT.md` instructions file. The `skills/` and `memory/` workspaces
+/// are reachable only through the dedicated skill_*/memory_* tools, never general
+/// read/write/edit/ls/find/grep/bash; agent.toml and any other config are fully
+/// off-limits. So the general filesystem tools see only AGENT.md under .jan/agent/.
+pub fn is_restricted_agent_path(project_root: &Path, raw: &str) -> bool {
let Ok(root) = project_root.canonicalize() else {
return false;
};
@@ -45,7 +36,20 @@ pub fn within_agent_workspace(project_root: &Path, raw: &str) -> bool {
return false;
};
let agent = root.join(".jan").join("agent");
- resolved.starts_with(agent.join("skills")) || resolved.starts_with(agent.join("memory"))
+ if !resolved.starts_with(&agent) {
+ return false;
+ }
+ resolved != agent.join("AGENT.md")
+}
+
+/// True iff a shell command references a restricted agent path (best-effort token
+/// scan). Splits on whitespace and shell metacharacters and checks each token so
+/// `cat .jan/agent/agent.toml` and its quoted/redirected variants are caught.
+pub fn command_touches_restricted_agent_path(project_root: &Path, command: &str) -> bool {
+ command
+ .split(|c: char| c.is_whitespace() || ";|&><()\"'`".contains(c))
+ .filter(|t| !t.is_empty())
+ .any(|t| is_restricted_agent_path(project_root, t))
}
/// Canonicalize a path that may not fully exist: canonicalize the deepest
@@ -150,16 +154,51 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}
+
+ #[test]
+ fn restricted_agent_path_covers_config_but_not_approved_surface() {
+ let root = unique_root();
+ std::fs::create_dir_all(root.join(".jan/agent/skills")).unwrap();
+ std::fs::create_dir_all(root.join(".jan/agent/memory")).unwrap();
+ std::fs::write(root.join(".jan/agent/agent.toml"), b"x").unwrap();
+ std::fs::write(root.join(".jan/agent/AGENT.md"), b"x").unwrap();
+ // Restricted: agent.toml, the agent dir listing, unknown config files.
+ assert!(is_restricted_agent_path(&root, ".jan/agent/agent.toml"));
+ assert!(is_restricted_agent_path(&root, "./.jan/agent/agent.toml"));
+ assert!(is_restricted_agent_path(
+ &root,
+ root.join(".jan/agent/agent.toml").to_str().unwrap()
+ ));
+ assert!(is_restricted_agent_path(&root, ".jan/agent"));
+ assert!(is_restricted_agent_path(&root, ".jan/agent/secrets.env"));
+ // skills/ and memory/ are reachable only via the dedicated tools, so they
+ // are restricted from the general filesystem tools too.
+ assert!(is_restricted_agent_path(&root, ".jan/agent/skills/deploy.md"));
+ assert!(is_restricted_agent_path(&root, ".jan/agent/memory/notes.md"));
+ // Only AGENT.md and unrelated project files stay reachable.
+ assert!(!is_restricted_agent_path(&root, ".jan/agent/AGENT.md"));
+ assert!(!is_restricted_agent_path(&root, "src/main.rs"));
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
#[test]
- fn agent_workspace_covers_skills_and_memory_only() {
+ fn command_scan_flags_restricted_agent_paths() {
let root = unique_root();
- assert!(within_agent_workspace(&root, ".jan/agent/skills/deploy.md"));
- assert!(within_agent_workspace(&root, ".jan/agent/memory/decisions.md"));
- // agent.toml and other project files are not the agent's scratch space.
- assert!(!within_agent_workspace(&root, ".jan/agent/agent.toml"));
- assert!(!within_agent_workspace(&root, "src/main.rs"));
- // Escapes via .. do not count as workspace.
- assert!(!within_agent_workspace(&root, ".jan/agent/skills/../../../etc/passwd"));
+ std::fs::create_dir_all(root.join(".jan/agent")).unwrap();
+ std::fs::write(root.join(".jan/agent/agent.toml"), b"x").unwrap();
+ assert!(command_touches_restricted_agent_path(
+ &root,
+ "cat .jan/agent/agent.toml"
+ ));
+ assert!(command_touches_restricted_agent_path(
+ &root,
+ "grep foo < .jan/agent/agent.toml"
+ ));
+ assert!(!command_touches_restricted_agent_path(
+ &root,
+ "cat .jan/agent/AGENT.md"
+ ));
+ assert!(!command_touches_restricted_agent_path(&root, "ls -la src"));
let _ = std::fs::remove_dir_all(&root);
}