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

+81
View File
@@ -618,3 +618,84 @@ async fn upload_accepts_large_binary_over_default_body_limit() {
assert_eq!(meta["kind"], "binary");
let _ = std::fs::remove_file(meta["path"].as_str().unwrap());
}
#[tokio::test]
async fn download_serves_shared_file_as_attachment_and_blocks_escape() {
use strata_ctl::agent::share;
let (app, store) = build_app().await;
let sess = store
.create_session(
"Share",
"test-model",
&strata_ctl::store::SessionSettings::default(),
)
.unwrap();
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let folder = std::env::temp_dir().join(format!("strata-dl-{nanos}"));
std::fs::create_dir_all(&folder).unwrap();
store
.set_folder(&sess.id, folder.to_str().unwrap())
.unwrap();
let file = folder.join("report.txt");
std::fs::write(&file, b"hello download").unwrap();
let outside = std::env::temp_dir().join(format!("strata-dl-out-{nanos}.txt"));
std::fs::write(&outside, b"secret").unwrap();
// Happy path: the exact URL the tool produces downloads the file as an
// attachment, with a non-inline content type and nosniff.
let url = share::download_url(&sess.id, &file, Some("report.txt"));
let resp = app
.clone()
.oneshot(Request::builder().uri(&url).body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let h = resp.headers();
assert_eq!(
h.get(axum::http::header::CONTENT_TYPE).unwrap(),
"application/octet-stream"
);
let cd = h
.get(axum::http::header::CONTENT_DISPOSITION)
.unwrap()
.to_str()
.unwrap();
assert!(cd.starts_with("attachment;"), "{cd}");
assert!(cd.contains("filename=\"report.txt\""), "{cd}");
assert_eq!(h.get("x-content-type-options").unwrap(), "nosniff");
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(&bytes[..], b"hello download");
// Escape: an absolute path outside the allowed roots is refused, no leak.
let bad = share::download_url(&sess.id, &outside, None);
let (st, text) = req(&app, Method::GET, &bad, None).await;
assert_eq!(st, StatusCode::BAD_REQUEST, "{text}");
assert!(!text.contains("secret"), "{text}");
// `..` traversal out of the personal folder is refused too.
let trav = folder.join("../").join(outside.file_name().unwrap());
let bad2 = share::download_url(&sess.id, &trav, None);
let (st2, _) = req(&app, Method::GET, &bad2, None).await;
assert_eq!(st2, StatusCode::BAD_REQUEST);
// Unknown session -> 404.
let (st3, _) = req(
&app,
Method::GET,
&format!(
"/api/sessions/nope/download?path={}",
share::pct_encode(file.to_str().unwrap())
),
None,
)
.await;
assert_eq!(st3, StatusCode::NOT_FOUND);
let _ = std::fs::remove_dir_all(&folder);
let _ = std::fs::remove_file(&outside);
}