feat: web_fetch and web_search tools (Brave, SearXNG)
This commit is contained in:
1 parent
846756c0bd
commit
45d8a44b53
7 files changed
+683
-17
No files matched your search
@@ -198,6 +198,7 @@ pub fn needs_permit(tool: &str, args: &serde_json::Value) -> bool {
|
||||
match tool {
|
||||
"bash" => !readonly_cmd(a("command").trim()),
|
||||
"read" => secret(a("path")),
|
||||
"web_fetch" => true,
|
||||
_ => gated(tool),
|
||||
}
|
||||
}
|
||||
@@ -242,6 +243,24 @@ const RISKY: &[&str] = &[
|
||||
// ponytail: patterns are `*` globs on the full command line; add argument-aware rules if needed.
|
||||
pub fn request(tool: &str, args: &serde_json::Value) -> Request {
|
||||
let s = |k: &str| args[k].as_str().unwrap_or_default().to_string();
|
||||
if tool == "web_fetch" {
|
||||
let url = s("url");
|
||||
let host = url
|
||||
.split_once("://")
|
||||
.map_or(url.as_str(), |x| x.1)
|
||||
.split(['/', '?', '#'])
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.rsplit('@')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
return Request {
|
||||
tool: tool.into(),
|
||||
detail: url,
|
||||
rule: format!("web:{host}"),
|
||||
};
|
||||
}
|
||||
if tool != "bash" {
|
||||
return Request {
|
||||
tool: tool.into(),
|
||||
@@ -439,6 +458,20 @@ mod tests {
|
||||
assert!(ask("write", "path", "a") && ask("edit", "path", "a"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_fetch_asks_once_per_domain() {
|
||||
let a = |u: &str| serde_json::json!({ "url": u });
|
||||
assert!(needs_permit("web_fetch", &a("https://docs.rs/x")));
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let mut al = Allow::load(d.path());
|
||||
let r = request("web_fetch", &a("https://user@docs.rs/serde?x=1"));
|
||||
assert_eq!(r.rule, "web:docs.rs");
|
||||
assert!(!al.allows(&r));
|
||||
al.add(&r.rule);
|
||||
assert!(al.allows(&request("web_fetch", &a("https://docs.rs/other"))));
|
||||
assert!(!al.allows(&request("web_fetch", &a("https://evil.test/"))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rules_match_narrowly() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user