agent: share_file tool + containment-checked download endpoint

The agent can hand the user a same-origin download link for a file on its
filesystem (outbound complement to uploads). share_file(path, name?) returns a
Markdown link the GUI renders as a download chip.

Safety is containment, not trust: a shared path must canonicalize inside the
session personal folder or the shared uploads/ dir (.. and symlink escapes
refused), regular files only, 64 MiB cap. GET /api/sessions/{id}/download
re-validates independently (author != verifier), forces attachment +
application/octet-stream + nosniff, bare 400 out-of-bounds, 404 unknown session.

TOOL_USE seed migration appends the share_file manual without clobbering
user-edited rows. README documents the tool surface, endpoint and boundary;
Status refreshed to the verified 191 passed / 3 ignored.
This commit is contained in:
adm committed 2026-11-29 01:23:18 +01:00
1 parent 28568f0e89
commit 2f5b263d4e
11 files changed
+668 -15

No files matched your search

+26 -6
View File
@@ -10,6 +10,7 @@
//! testable without the engine and independent of the concrete runner.
pub mod notepad;
pub mod share;
pub mod shell;
use std::future::Future;
@@ -26,12 +27,14 @@ use shell::{ShellRequest, ShellRunner};
/// Default cap on model↔tool iterations, so a misbehaving model cannot loop forever.
pub const MAX_ITERS: usize = 24;
/// The tools offered to the agent: the universal `shell_exec` (action) and the
/// `notepad` (per-session context management).
/// The tools offered to the agent: the universal `shell_exec` (action), the
/// `notepad` (per-session context management) and `share_file` (hand the user a
/// download link for a file the agent has on disk).
pub fn agent_tools() -> Json {
json!([
shell::shell_exec_declaration(),
notepad::notepad_declaration()
notepad::notepad_declaration(),
share::share_file_declaration()
])
}
@@ -78,6 +81,7 @@ async fn execute_tool(
session_id: &str,
tc: &ToolCall,
default_cwd: Option<&str>,
workspace_root: Option<&std::path::Path>,
) -> String {
let args: Json = match serde_json::from_str(&tc.function.arguments) {
Ok(v) => v,
@@ -96,6 +100,7 @@ async fn execute_tool(
}
}
"notepad" => notepad::execute(store, session_id, &args).await,
"share_file" => share::execute(session_id, &args, default_cwd, workspace_root).await,
other => format!("error: unknown tool '{other}'"),
}
}
@@ -142,6 +147,9 @@ pub struct LoopContext {
pub runner: Arc<dyn ShellRunner>,
pub store: Arc<dyn Store>,
pub default_cwd: Option<String>,
/// Parent of the per-session folders and the shared uploads dir; the second
/// root `share_file` may expose files from. None in tests without a workspace.
pub workspace_root: Option<std::path::PathBuf>,
pub max_iters: usize,
pub progress: Option<UnboundedSender<AgentEvent>>,
}
@@ -166,6 +174,7 @@ where
runner,
store,
default_cwd,
workspace_root,
max_iters,
progress,
} = ctx;
@@ -229,8 +238,15 @@ where
arguments: tc.function.arguments.clone(),
},
);
let output =
execute_tool(&runner, &store, session_id, tc, default_cwd.as_deref()).await;
let output = execute_tool(
&runner,
&store,
session_id,
tc,
default_cwd.as_deref(),
workspace_root.as_deref(),
)
.await;
emit(
&progress,
AgentEvent::ToolResult {
@@ -386,6 +402,7 @@ mod tests {
runner,
store: store.clone(),
default_cwd: None,
workspace_root: None,
max_iters: 8,
progress: Some(tx),
},
@@ -441,6 +458,7 @@ mod tests {
runner,
store,
default_cwd: None,
workspace_root: None,
max_iters: 3,
progress: None,
},
@@ -471,7 +489,7 @@ mod tests {
arguments: "{}".into(),
},
};
let out = execute_tool(&runner, &store, &sess.id, &tc, None).await;
let out = execute_tool(&runner, &store, &sess.id, &tc, None, None).await;
assert!(out.contains("unknown tool"));
}
@@ -520,6 +538,7 @@ mod tests {
runner,
store: store.clone(),
default_cwd: None,
workspace_root: None,
max_iters: 8,
progress: None,
},
@@ -584,6 +603,7 @@ mod tests {
runner,
store: store.clone(),
default_cwd: Some("/tmp".into()),
workspace_root: None,
max_iters: 8,
progress: None,
},