generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 302
Reduce default fs_read trust permission to current working directory only #2824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+112
−19
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,23 +114,31 @@ impl FsRead { | |
} | ||
|
||
fn default_allow_read_only() -> bool { | ||
true | ||
false | ||
} | ||
|
||
let is_in_allowlist = matches_any_pattern(&agent.allowed_tools, "fs_read"); | ||
match agent.tools_settings.get("fs_read") { | ||
Some(settings) => { | ||
let settings = agent.tools_settings.get("fs_read").cloned() | ||
.unwrap_or_else(|| serde_json::json!({})); | ||
|
||
{ | ||
let Settings { | ||
allowed_paths, | ||
mut allowed_paths, | ||
denied_paths, | ||
allow_read_only, | ||
} = match serde_json::from_value::<Settings>(settings.clone()) { | ||
} = match serde_json::from_value::<Settings>(settings) { | ||
Ok(settings) => settings, | ||
Err(e) => { | ||
error!("Failed to deserialize tool settings for fs_read: {:?}", e); | ||
return PermissionEvalResult::Ask; | ||
}, | ||
}; | ||
|
||
// Always add current working directory to allowed paths | ||
if let Ok(cwd) = os.env.current_dir() { | ||
allowed_paths.push(cwd.to_string_lossy().to_string()); | ||
} | ||
|
||
let allow_set = { | ||
let mut builder = GlobSetBuilder::new(); | ||
for path in &allowed_paths { | ||
|
@@ -259,10 +267,7 @@ impl FsRead { | |
PermissionEvalResult::Ask | ||
}, | ||
} | ||
}, | ||
None if is_in_allowlist => PermissionEvalResult::Allow, | ||
_ => PermissionEvalResult::Ask, | ||
} | ||
} | ||
} | ||
|
||
|
||
pub async fn invoke(&self, os: &Os, updates: &mut impl Write) -> Result<InvokeOutput> { | ||
|
@@ -862,6 +867,7 @@ fn format_mode(mode: u32) -> [char; 9] { | |
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
use super::*; | ||
use crate::cli::agent::ToolSettingTarget; | ||
|
@@ -1397,7 +1403,7 @@ mod tests { | |
} | ||
|
||
#[tokio::test] | ||
async fn test_eval_perm() { | ||
async fn test_eval_perm_denied_path() { | ||
const DENIED_PATH_OR_FILE: &str = "/some/denied/path"; | ||
const DENIED_PATH_OR_FILE_GLOB: &str = "/denied/glob/**/path"; | ||
|
||
|
@@ -1447,4 +1453,88 @@ mod tests { | |
&& deny_list.iter().filter(|p| *p == DENIED_PATH_OR_FILE).collect::<Vec<_>>().len() == 2 | ||
)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_eval_perm_allowed_path_and_cwd() { | ||
|
||
// by default the fake env uses "/" as the CWD. | ||
// change it to a sub folder so we can test fs_read reading files outside CWD | ||
let os = Os::new().await.unwrap(); | ||
os.env.set_current_dir_for_test(PathBuf::from("/home/user")); | ||
|
||
let agent = Agent { | ||
name: "test_agent".to_string(), | ||
tools_settings: { | ||
let mut map = HashMap::new(); | ||
map.insert( | ||
ToolSettingTarget("fs_read".to_string()), | ||
serde_json::json!({ | ||
"allowedPaths": ["/explicitly/allowed/path"] | ||
}), | ||
); | ||
map | ||
}, | ||
..Default::default() // Not in allowed_tools, allow_read_only = false | ||
}; | ||
|
||
// Test 1: Explicitly allowed path should work | ||
let allowed_tool = serde_json::from_value::<FsRead>(serde_json::json!({ | ||
"operations": [ | ||
{ "path": "/explicitly/allowed/path", "mode": "Directory" }, | ||
{ "path": "/explicitly/allowed/path/file.txt", "mode": "Line" }, | ||
] | ||
})).unwrap(); | ||
let res = allowed_tool.eval_perm(&os, &agent); | ||
assert!(matches!(res, PermissionEvalResult::Allow)); | ||
|
||
// Test 2: CWD should always be allowed | ||
let cwd_tool = serde_json::from_value::<FsRead>(serde_json::json!({ | ||
"operations": [ | ||
{ "path": "/home/user/", "mode": "Directory" }, | ||
{ "path": "/home/user/file.txt", "mode": "Line" }, | ||
] | ||
})).unwrap(); | ||
let res = cwd_tool.eval_perm(&os, &agent); | ||
assert!(matches!(res, PermissionEvalResult::Allow)); | ||
|
||
// Test 3: Outside CWD and not explicitly allowed should ask | ||
let outside_tool = serde_json::from_value::<FsRead>(serde_json::json!({ | ||
"operations": [ | ||
{ "path": "/tmp/not/allowed/file.txt", "mode": "Line" } | ||
] | ||
})).unwrap(); | ||
let res = outside_tool.eval_perm(&os, &agent); | ||
assert!(matches!(res, PermissionEvalResult::Ask)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_eval_perm_no_settings_cwd_behavior() { | ||
let os = Os::new().await.unwrap(); | ||
os.env.set_current_dir_for_test(PathBuf::from("/home/user")); | ||
|
||
let agent = Agent { | ||
name: "test_agent".to_string(), | ||
tools_settings: HashMap::new(), // No fs_read settings | ||
..Default::default() | ||
}; | ||
|
||
// Test 1: CWD should be allowed even with no settings | ||
let cwd_tool = serde_json::from_value::<FsRead>(serde_json::json!({ | ||
"operations": [ | ||
{ "path": "/home/user/", "mode": "Directory" }, | ||
{ "path": "/home/user/file.txt", "mode": "Line" }, | ||
] | ||
})).unwrap(); | ||
let res = cwd_tool.eval_perm(&os, &agent); | ||
assert!(matches!(res, PermissionEvalResult::Allow)); | ||
|
||
// Test 2: Outside CWD should ask for permission | ||
let outside_tool = serde_json::from_value::<FsRead>(serde_json::json!({ | ||
"operations": [ | ||
{ "path": "/tmp/not/allowed/file.txt", "mode": "Line" } | ||
] | ||
})).unwrap(); | ||
let res = outside_tool.eval_perm(&os, &agent); | ||
assert!(matches!(res, PermissionEvalResult::Ask)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we don’t need an extra method here — if we want the default to be
false
, that’s already the default for bool.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed in latest rev @evanliu048