Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Bot downloads land in the bot's folder from kwant's browser container (20g) (#686)

Oleksandr Ivanchenko44bb19c627d6
diff --git a/src/sandbox/browser.rs b/src/sandbox/browser.rs
index caf95fb2..12dd8ae5 100644
--- a/src/sandbox/browser.rs
+++ b/src/sandbox/browser.rs
@@ -1696,19 +1696,19 @@ impl BrowserController {
/// Starts this session's own download/chooser connection, landing downloads under
/// `workspace/downloads/` and reporting (but not auto-answering) file pickers it opens.
- /// Idempotent: a session already running its own connection keeps it. `local`, `context_id`
+ /// Idempotent: a session already running its own connection keeps it. `home`, `context_id`
/// and `staging_root` are passed straight to [`super::browser_downloads::DownloadCenter::start`].
pub async fn configure_downloads(
&self,
workspace: std::path::PathBuf,
- local: bool,
+ home: super::browser_downloads::DownloadHome,
context_id: Option<String>,
staging_root: std::path::PathBuf,
) {
if self.downloads.lock().unwrap_or_else(|e| e.into_inner()).is_some() {
return;
}
- let center = super::browser_downloads::DownloadCenter::start(self.clone(), workspace, local, context_id, staging_root).await;
+ let center = super::browser_downloads::DownloadCenter::start(self.clone(), workspace, home, context_id, staging_root).await;
let mut guard = self.downloads.lock().unwrap_or_else(|e| e.into_inner());
if guard.is_none() {
*guard = Some(center);
diff --git a/src/sandbox/browser_downloads.rs b/src/sandbox/browser_downloads.rs
index 7f4c6cbe..f81f64ee 100644
--- a/src/sandbox/browser_downloads.rs
+++ b/src/sandbox/browser_downloads.rs
@@ -18,10 +18,18 @@
//! `downloads/`. Reconnecting to follow a popup polls `get_ws_url()` every few seconds rather than
//! reacting the instant a popup opens, so capture on a brand new popup can lag by that long.
//!
-//! Remote Chrome (`KWNT_BROWSER_CDP_URL`) has no filesystem in common with this server, so a
-//! staging path means nothing there: `local` sessions get `allowAndName` at the shared staging
-//! path; every other session gets `deny` with `eventsEnabled: true`, and a download attempt is
-//! reported back as failed rather than tracked.
+//! Where Chrome keeps the bytes depends on where it runs ([`DownloadHome`]):
+//! - Chrome on this computer (`Local`) gets `allowAndName` at the shared staging path.
+//! - kwant's own browser container (`Container`, vm mode with no `KWNT_BROWSER_CDP_URL`) gets
+//! `allowAndName` at [`CONTAINER_DOWNLOAD_DIR`] on the container's own tmpfs, so Chrome names
+//! the file by its download guid there. Once it completes, kwant streams it out with
+//! `docker exec <container> head -c` into the staging folder (`docker cp` can't read a tmpfs mount),
+//! lands it like a local one, and removes the container copy. The tmpfs is 512 MB and Chrome's
+//! own profile lives there too, so these downloads stop at [`CONTAINER_MAX_DOWNLOAD_BYTES`].
+//! - A browser on another computer (`Elsewhere`, `KWNT_BROWSER_CDP_URL`) has no filesystem and
+//! no Docker in common with this server: it gets `deny` with `eventsEnabled: true`, and a
+//! download attempt is reported back as failed rather than tracked. kwant never fetches the
+//! URL itself instead (it could reach this server's own ports).
use super::browser::{connect_cdp, BrowserController, BrowserError, WsStream};
use super::safe_fs;
@@ -49,14 +57,63 @@ const JOB_WAIT: Duration = Duration::from_secs(20);
/// A staged download left in the shared incoming folder this long without being landed (no
/// center's frame tree ever claimed it, or a crash interrupted the move) is swept as an orphan.
const INCOMING_ORPHAN_AGE: Duration = Duration::from_secs(3600);
-/// Remote Chrome has no filesystem in common with this server, so a download can't be kept.
-const REMOTE_DOWNLOAD_REFUSAL: &str = "Downloads need the browser on this computer for now.";
+/// A browser on another computer has no filesystem in common with this server, so a download
+/// can't be kept: say so in words the bot and the person can act on.
+const REMOTE_DOWNLOAD_REFUSAL: &str = "This browser runs on another computer, so kwant can't keep its downloads. \
+Copy the file's link instead, or switch this bot to Local computer.";
+/// Where Chrome in kwant's browser container writes downloads: on the container's own tmpfs,
+/// each file named by its download guid (`allowAndName`), never by anything the page chose.
+pub const CONTAINER_DOWNLOAD_DIR: &str = "/tmp/kwnt-dl";
+/// The container's `/tmp` is a 512 MB tmpfs that Chrome's own profile shares: a download is
+/// stopped once it's past this, so it can't fill the browser's disk.
+const CONTAINER_MAX_DOWNLOAD_BYTES: u64 = 400 * 1024 * 1024;
+const CONTAINER_TOO_BIG: &str =
+ "it's over 400 MB, the most this bot's browser can hold. Copy the file's link instead, or switch this bot to Local computer.";
+/// The longest one `docker exec` may take: copying a finished download out of the container, or
+/// a small chore (making the folder, removing a file).
+const CONTAINER_COPY_WAIT: Duration = Duration::from_secs(300);
+const CONTAINER_CHORE_WAIT: Duration = Duration::from_secs(15);
+/// The image's own non-root user (the one Chrome runs as): every `docker exec` kwant runs in
+/// the container uses it, so a future image that defaults to root can't quietly widen them.
+const CONTAINER_USER: &str = "browser";
-#[derive(Clone)]
+/// Where a session's Chrome keeps a download, and so how it reaches the bot's `downloads/`.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum DownloadHome {
+ /// Chrome on this computer: it writes straight into the shared staging folder.
+ Local,
+ /// kwant's own browser container, by name: Chrome writes into [`CONTAINER_DOWNLOAD_DIR`] on
+ /// the container's tmpfs, and kwant copies the finished file out with Docker.
+ Container(String),
+ /// A browser kwant was pointed at on another computer (`KWNT_BROWSER_CDP_URL`): no shared
+ /// disk, no Docker, so downloads are refused.
+ Elsewhere,
+}
+
+impl DownloadHome {
+ /// The largest download this home can take.
+ fn cap(&self) -> u64 {
+ match self {
+ Self::Container(_) => CONTAINER_MAX_DOWNLOAD_BYTES,
+ _ => MAX_DOWNLOAD_BYTES,
+ }
+ }
+
+ /// Why a download over [`Self::cap`] was stopped, in the bot's own words.
+ fn too_big(&self) -> &'static str {
+ match self {
+ Self::Container(_) => CONTAINER_TOO_BIG,
+ _ => "it passed 1 GB.",
+ }
+ }
+}
+
+#[derive(Clone, Debug)]
enum DlStatus {
InProgress,
Done(String),
- Cancelled,
+ /// Stopped before it finished, with why.
+ Cancelled(String),
Failed(String),
}
@@ -68,6 +125,11 @@ struct DownloadEntry {
received: u64,
status: DlStatus,
reported_terminal: bool,
+ /// kwant asked Chrome to cancel it for passing the cap, so the `canceled` that follows says
+ /// so (and the cancel isn't sent again on every later progress event).
+ too_big: Option<&'static str>,
+ /// The container's file is being copied out: a newer download must leave it alone.
+ copying: bool,
}
#[derive(Clone)]
@@ -130,7 +192,7 @@ pub struct DownloadCenter {
}
impl DownloadCenter {
- /// `local`: whether this Chrome shares a filesystem with this server (see the module doc).
+ /// `home`: where this Chrome keeps a download (see the module doc).
/// `browser_context_id`: the session's own context, for a non-default context (VM / job
/// pages); `None` for a local page, which shares the kwant profile's default context.
/// `staging_root`: one folder shared by every session, outside any bot's workspace.
@@ -140,12 +202,12 @@ impl DownloadCenter {
/// part of whatever request just opened this session, never something a later, unrelated
/// request races against and appears to have caused. A browser slow or unreachable past that
/// wait just keeps retrying in the background, same as before this wait existed.
- pub async fn start(ctrl: BrowserController, workspace: PathBuf, local: bool, browser_context_id: Option<String>, staging_root: PathBuf) -> Self {
+ pub async fn start(ctrl: BrowserController, workspace: PathBuf, home: DownloadHome, browser_context_id: Option<String>, staging_root: PathBuf) -> Self {
let shared = Arc::new(StdMutex::new(Shared::default()));
let (tx, rx) = mpsc::channel(8);
let (ready_tx, ready_rx) = oneshot::channel();
let center = Self { shared: shared.clone(), jobs: tx };
- let session = Session { workspace, local, browser_context_id, staging_root };
+ let session = Session { workspace, home, browser_context_id, staging_root };
tokio::spawn(run(ctrl, session, shared, rx, ready_tx));
let _ = tokio::time::timeout(Duration::from_secs(2), ready_rx).await;
center
@@ -175,8 +237,8 @@ impl DownloadCenter {
);
json!({"status": "done", "name": entry.name, "path": rel, "note": note})
}
- DlStatus::Cancelled => {
- let note = format!("The download of {} was stopped: it passed 1 GB.", entry.name);
+ DlStatus::Cancelled(why) => {
+ let note = format!("The download of {} was stopped: {why}", entry.name);
json!({"status": "cancelled", "name": entry.name, "note": note})
}
DlStatus::Failed(why) => {
@@ -361,11 +423,19 @@ impl DownloadCenter {
/// clippy's argument-count lint.
struct Session {
workspace: PathBuf,
- local: bool,
+ home: DownloadHome,
browser_context_id: Option<String>,
staging_root: PathBuf,
}
+/// What `handle_event` needs to land a finished download: borrowed from [`Session`].
+struct Landing<'a> {
+ workspace: &'a Path,
+ home: &'a DownloadHome,
+ staging_root: &'a Path,
+ browser_context_id: Option<&'a str>,
+}
+
async fn run(
ctrl: BrowserController,
session: Session,
@@ -373,7 +443,13 @@ async fn run(
mut jobs: mpsc::Receiver<Job>,
ready: oneshot::Sender<()>,
) {
- let Session { workspace, local, browser_context_id, staging_root } = session;
+ let Session { workspace, home, browser_context_id, staging_root } = session;
+ let landing = Landing {
+ workspace: &workspace,
+ home: &home,
+ staging_root: &staging_root,
+ browser_context_id: browser_context_id.as_deref(),
+ };
let mut ready = Some(ready);
let _ = tokio::fs::create_dir_all(&staging_root).await;
loop {
@@ -393,7 +469,10 @@ async fn run(
}
};
let msg_id = AtomicU64::new(1);
- if setup(&mut ws, &msg_id, &shared, &staging_root, local, browser_context_id.as_deref()).await.is_err() {
+ if let DownloadHome::Container(name) = &home {
+ prepare_container_dir(name).await;
+ }
+ if setup(&mut ws, &msg_id, &shared, &staging_root, &home, browser_context_id.as_deref()).await.is_err() {
tokio::time::sleep(RETRY_WAIT).await;
continue;
}
@@ -407,13 +486,13 @@ async fn run(
biased;
job = jobs.recv() => {
let Some(job) = job else { return };
- let result = job_call(&mut ws, &msg_id, &shared, &workspace, local, &job.method, job.params).await;
+ let result = job_call(&mut ws, &msg_id, &shared, &landing, &job.method, job.params).await;
let failed = result.is_err();
let _ = job.reply.send(result);
if failed { break; }
}
msg = ws.next() => {
- if on_message(msg, &mut ws, &msg_id, &shared, &workspace, local).await.is_err() { break; }
+ if on_message(msg, &mut ws, &msg_id, &shared, &landing).await.is_err() { break; }
}
_ = follow.tick() => {
if let Ok(fresh) = ctrl.get_ws_url().await {
@@ -425,9 +504,9 @@ async fn run(
}
}
-/// `incoming`: the shared staging folder (outside any workspace) every session points
+/// `incoming`: the shared staging folder (outside any workspace) every local session points
/// `Browser.setDownloadBehavior` at, so the global setting can't single out one bot's folder.
-/// `local`: allow downloads there; otherwise deny them but keep hearing about attempts. Also
+/// `home`: see [`download_behavior_params`]. Also
/// seeds `shared.frames` from this page's own frame tree, for `handle_event` to claim a download
/// only once it sees a `frameId` that's actually this page's.
async fn setup(
@@ -435,11 +514,11 @@ async fn setup(
msg_id: &AtomicU64,
shared: &Arc<StdMutex<Shared>>,
incoming: &Path,
- local: bool,
+ home: &DownloadHome,
browser_context_id: Option<&str>,
) -> Result<(), BrowserError> {
call_raw(ws, msg_id, "Page.enable", json!({})).await?;
- let behavior = download_behavior_params(local, incoming, browser_context_id);
+ let behavior = download_behavior_params(home, incoming, browser_context_id);
call_raw(ws, msg_id, "Browser.setDownloadBehavior", behavior).await?;
call_raw(ws, msg_id, "Page.setInterceptFileChooserDialog", json!({"enabled": true})).await?;
if let Ok(tree) = call_raw(ws, msg_id, "Page.getFrameTree", json!({})).await {
@@ -450,17 +529,23 @@ async fn setup(
Ok(())
}
-/// Builds `Browser.setDownloadBehavior`'s params for the Fix #4 mode switch: a `local` session
-/// (one with a filesystem in common with this server) gets `allowAndName` at the shared staging
-/// path; every other session (remote Chrome, with none) gets `deny`, kept to `eventsEnabled` so a
-/// download attempt is still reported rather than silently vanishing.
-fn download_behavior_params(local: bool, incoming: &Path, browser_context_id: Option<&str>) -> Value {
+/// Builds `Browser.setDownloadBehavior`'s params for where this Chrome runs: on this computer it
+/// gets `allowAndName` at the shared staging path; in kwant's browser container, `allowAndName`
+/// at [`CONTAINER_DOWNLOAD_DIR`] on the container's own tmpfs (the staging path means nothing
+/// inside it); on another computer, `deny`, kept to `eventsEnabled` so a download attempt is
+/// still reported rather than silently vanishing.
+fn download_behavior_params(home: &DownloadHome, incoming: &Path, browser_context_id: Option<&str>) -> Value {
+ let download_path = match home {
+ DownloadHome::Local => Some(incoming.to_string_lossy().to_string()),
+ DownloadHome::Container(_) => Some(CONTAINER_DOWNLOAD_DIR.to_string()),
+ DownloadHome::Elsewhere => None,
+ };
let mut behavior = json!({
- "behavior": if local { "allowAndName" } else { "deny" },
+ "behavior": if download_path.is_some() { "allowAndName" } else { "deny" },
"eventsEnabled": true,
});
- if local {
- behavior["downloadPath"] = json!(incoming.to_string_lossy());
+ if let Some(path) = download_path {
+ behavior["downloadPath"] = json!(path);
}
if let Some(ctx) = browser_context_id {
behavior["browserContextId"] = json!(ctx);
@@ -468,6 +553,17 @@ fn download_behavior_params(local: bool, incoming: &Path, browser_context_id: Op
behavior
}
+/// `Browser.cancelDownload`'s params. Chrome looks the guid up in one browser context only, so a
+/// session in its own context (vm mode) must name it: without it real Chrome answers "No download
+/// item found for the given GUID" and the download runs on.
+fn cancel_params(guid: &str, browser_context_id: Option<&str>) -> Value {
+ let mut params = json!({"guid": guid});
+ if let Some(ctx) = browser_context_id {
+ params["browserContextId"] = json!(ctx);
+ }
+ params
+}
+
/// Recurses through a `Page.getFrameTree` result (`{frame: {id}, childFrames: [...]}`), collecting
/// every frame id belonging to this page.
fn collect_frame_ids(node: &Value, out: &mut HashSet<String>) {
@@ -511,14 +607,27 @@ async fn call_raw(ws: &mut WsStream, msg_id: &AtomicU64, method: &str, params: V
.map_err(|_| BrowserError::Timeout)?
}
+/// Sends a request from inside event handling without waiting for its reply. Waiting there (as
+/// [`call_raw`] does) drops every other message in the meantime: Chrome sends a cancelled
+/// download's `canceled` progress event before its `cancelDownload` reply, so the download stayed
+/// "in progress" for good, and a reply a [`job_call`] was waiting for could be lost the same way.
+/// The reply has no `method`, so [`on_message`] passes over it; a refusal (the download already
+/// finished) needs nothing either, since its `completed` event comes through the usual way.
+async fn send_only(ws: &mut WsStream, msg_id: &AtomicU64, method: &str, params: Value) {
+ let id = msg_id.fetch_add(1, Ordering::SeqCst);
+ let request = json!({"id": id, "method": method, "params": params});
+ if let Err(e) = ws.send(Message::Text(request.to_string())).await {
+ tracing::warn!("browser downloads: couldn't send {method}: {e}");
+ }
+}
+
/// Sends one request on behalf of a caller awaiting its own result, meanwhile dispatching any
/// unsolicited events (downloads, choosers) that arrive while waiting, instead of dropping them.
async fn job_call(
ws: &mut WsStream,
msg_id: &AtomicU64,
shared: &Arc<StdMutex<Shared>>,
- workspace: &Path,
- local: bool,
+ landing: &Landing<'_>,
method: &str,
params: Value,
) -> Result<Value, BrowserError> {
@@ -537,7 +646,7 @@ async fn job_call(
}
}
}
- on_message(msg, ws, msg_id, shared, workspace, local).await?;
+ on_message(msg, ws, msg_id, shared, landing).await?;
}
}
@@ -546,15 +655,14 @@ async fn on_message(
ws: &mut WsStream,
msg_id: &AtomicU64,
shared: &Arc<StdMutex<Shared>>,
- workspace: &Path,
- local: bool,
+ landing: &Landing<'_>,
) -> Result<(), BrowserError> {
match msg {
Some(Ok(Message::Text(txt))) => {
let Ok(v) = serde_json::from_str::<Value>(&txt) else { return Ok(()) };
if let Some(method) = v.get("method").and_then(Value::as_str) {
let params = v.get("params").cloned().unwrap_or(Value::Null);
- handle_event(ws, msg_id, method, params, shared, workspace, local).await;
+ handle_event(ws, msg_id, method, params, shared, landing).await;
}
Ok(())
}
@@ -570,37 +678,61 @@ async fn handle_event(
method: &str,
params: Value,
shared: &Arc<StdMutex<Shared>>,
- workspace: &Path,
- local: bool,
+ landing: &Landing<'_>,
) {
+ let home = landing.home;
match method {
"Browser.downloadWillBegin" => {
let Some(guid) = params["guid"].as_str().map(str::to_string) else { return };
let frame_id = params["frameId"].as_str().unwrap_or_default();
let name = sanitize_name(params["suggestedFilename"].as_str().unwrap_or("download"));
- let mut s = shared.lock().unwrap_or_else(|e| e.into_inner());
- // Another bot's session may see this event too (see the module doc's accepted gap):
- // only claim it when the frame it started on is actually this page's own.
- if !s.frames.contains(frame_id) {
- return;
+ let replaced = {
+ let mut s = shared.lock().unwrap_or_else(|e| e.into_inner());
+ // Another bot's session may see this event too (see the module doc's accepted gap):
+ // only claim it when the frame it started on is actually this page's own.
+ if !s.frames.contains(frame_id) {
+ return;
+ }
+ // This center tracks one download at a time. In the container an earlier one still
+ // running would go on filling the shared tmpfs with nobody left to cap, land or remove
+ // it, so it is stopped and removed as the new one takes its place.
+ let replaced = match (home, s.download.as_ref()) {
+ (DownloadHome::Container(_), Some(prev))
+ if prev.guid != guid && matches!(prev.status, DlStatus::InProgress) && !prev.copying && plain_guid(&prev.guid) =>
+ {
+ Some(prev.guid.clone())
+ }
+ _ => None,
+ };
+ let status = match home {
+ DownloadHome::Elsewhere => DlStatus::Failed(REMOTE_DOWNLOAD_REFUSAL.into()),
+ // the guid names the file inside the container: never let it be a path
+ DownloadHome::Container(_) if !plain_guid(&guid) => {
+ DlStatus::Failed("the browser gave the download a name kwant can't use.".into())
+ }
+ _ => DlStatus::InProgress,
+ };
+ s.download = Some(DownloadEntry {
+ guid,
+ name,
+ total: None,
+ received: 0,
+ status,
+ reported_terminal: false,
+ too_big: None,
+ copying: false,
+ });
+ replaced
+ };
+ if let (DownloadHome::Container(container), Some(prev)) = (home, replaced) {
+ send_only(ws, msg_id, "Browser.cancelDownload", cancel_params(&prev, landing.browser_context_id)).await;
+ tokio::spawn(remove_from_container(container.clone(), prev));
}
- s.download = Some(DownloadEntry {
- guid,
- name,
- total: None,
- received: 0,
- status: if local {
- DlStatus::InProgress
- } else {
- DlStatus::Failed(REMOTE_DOWNLOAD_REFUSAL.into())
- },
- reported_terminal: false,
- });
}
"Browser.downloadProgress" => {
- // Remote mode already answered `downloadWillBegin` as failed and never allowed the
- // download to start: nothing here is this center's to track.
- if !local {
+ // A browser on another computer already answered `downloadWillBegin` as failed and
+ // never allowed the download to start: nothing here is this center's to track.
+ if *home == DownloadHome::Elsewhere {
return;
}
let Some(guid) = params["guid"].as_str().map(str::to_string) else { return };
@@ -608,8 +740,10 @@ async fn handle_event(
let received = params["receivedBytes"].as_u64().unwrap_or(0);
let total = params["totalBytes"].as_u64().filter(|t| *t > 0);
let file_path = params["filePath"].as_str().map(str::to_string);
+ let cap = home.cap();
let mut finish = None;
let mut over_cap = false;
+ let mut clear_container = false;
{
let mut s = shared.lock().unwrap_or_else(|e| e.into_inner());
// `downloadProgress` carries no `frameId`, so unlike `downloadWillBegin` there's
@@ -618,25 +752,67 @@ async fn handle_event(
// the setting turns out to be browser-wide), only ever update a guid this center
// already claimed via a frame-checked `downloadWillBegin`; anything else is ignored.
let Some(entry) = s.download.as_mut().filter(|e| e.guid == guid) else { return };
- if let Some(t) = total {
- entry.total = Some(t);
- }
- entry.received = received;
- match state.as_str() {
- "completed" => match file_path {
- Some(path) => finish = Some((entry.name.clone(), path)),
- None => entry.status = DlStatus::Failed("Chrome didn't say where the file landed.".into()),
- },
- "canceled" => entry.status = DlStatus::Cancelled,
- _ if received > MAX_DOWNLOAD_BYTES => over_cap = true,
- _ => {}
+ if !matches!(entry.status, DlStatus::InProgress) {
+ // refused at the start (an unusable guid): stop it, and leave nothing behind
+ over_cap = state == "inProgress" && matches!(entry.status, DlStatus::Failed(_)) && entry.too_big.is_none();
+ if over_cap {
+ entry.too_big = Some("the browser gave the download a name kwant can't use.");
+ }
+ clear_container = matches!(state.as_str(), "completed" | "canceled");
+ } else {
+ if let Some(t) = total {
+ entry.total = Some(t);
+ }
+ entry.received = received;
+ match state.as_str() {
+ "completed" => match (home, file_path) {
+ // it can finish between two progress events, past the cap: never copy that out
+ (DownloadHome::Container(_), _) if entry.too_big.is_some() || received.max(total.unwrap_or(0)) > cap => {
+ entry.status = DlStatus::Cancelled(entry.too_big.unwrap_or(home.too_big()).to_string());
+ clear_container = true;
+ }
+ // the container's file is the guid kwant checked, whatever path Chrome reports
+ (DownloadHome::Container(_), _) => {
+ entry.copying = true;
+ finish = Some((entry.name.clone(), String::new()));
+ }
+ (_, Some(path)) => finish = Some((entry.name.clone(), path)),
+ (_, None) => entry.status = DlStatus::Failed("Chrome didn't say where the file landed.".into()),
+ },
+ "canceled" => {
+ entry.status = DlStatus::Cancelled(entry.too_big.unwrap_or("the browser stopped it.").to_string());
+ clear_container = true;
+ }
+ // a size the server announced up front is enough to stop it before it fills anything
+ _ if entry.too_big.is_none() && (received > cap || total.is_some_and(|t| t > cap)) => {
+ entry.too_big = Some(home.too_big());
+ over_cap = true;
+ }
+ _ => {}
+ }
}
}
if over_cap {
- let _ = call_raw(ws, msg_id, "Browser.cancelDownload", json!({"guid": guid})).await;
+ send_only(ws, msg_id, "Browser.cancelDownload", cancel_params(&guid, landing.browser_context_id)).await;
}
- if let Some((name, path)) = finish {
- tokio::spawn(land_download(workspace.to_path_buf(), shared.clone(), guid, name, path));
+ match (home, finish) {
+ (DownloadHome::Container(container), Some((name, _))) => {
+ tokio::spawn(land_from_container(
+ container.clone(),
+ landing.workspace.to_path_buf(),
+ landing.staging_root.to_path_buf(),
+ shared.clone(),
+ guid,
+ name,
+ ));
+ }
+ (_, Some((name, path))) => {
+ tokio::spawn(land_download(landing.workspace.to_path_buf(), shared.clone(), guid, name, path));
+ }
+ (DownloadHome::Container(container), None) if clear_container && plain_guid(&guid) => {
+ tokio::spawn(remove_from_container(container.clone(), guid));
+ }
+ _ => {}
}
}
"Page.fileChooserOpened" => {
@@ -689,36 +865,166 @@ async fn describe_accept(ws: &mut WsStream, msg_id: &AtomicU64, backend_node_id:
/// side via `allowAndName`, so this is the one place a name actually has to be chosen).
async fn land_download(workspace: PathBuf, shared: Arc<StdMutex<Shared>>, guid: String, name: String, staged_path: String) {
let landing_guid = guid.clone();
- let result = tokio::task::spawn_blocking(move || -> std::io::Result<String> {
- std::fs::create_dir_all(&workspace)?;
- let root = safe_fs::Dir::open_strict(&workspace)?;
- let dir = root.make_dirs(Path::new("downloads"))?;
- let mut src = std::fs::File::open(&staged_path)?;
- let existing: HashSet<String> = dir
- .list()?
- .into_iter()
- .filter(|e| matches!(e.kind, safe_fs::Kind::File))
- .map(|e| e.name.to_string_lossy().to_string())
- .collect();
- let final_name = collision_safe_name(&existing, &name);
- dir.replace_file(OsStr::new(&final_name), &format!(".landing-{landing_guid}"), &mut src, 0o600)?;
- drop(src);
- let _ = std::fs::remove_file(&staged_path);
- Ok(final_name)
- })
- .await;
+ let result = tokio::task::spawn_blocking(move || land_staged(&workspace, &landing_guid, &name, Path::new(&staged_path)))
+ .await
+ .unwrap_or_else(|_| Err(std::io::Error::other("something went wrong landing the download")));
+ finish_landing(&shared, &guid, result);
+}
+
+/// The blocking part of landing: `staged` (a file in the staging folder) becomes
+/// `downloads/<name>` in `workspace`, through [`safe_fs`], and the staged copy is removed.
+fn land_staged(workspace: &Path, guid: &str, name: &str, staged: &Path) -> std::io::Result<String> {
+ std::fs::create_dir_all(workspace)?;
+ let root = safe_fs::Dir::open_strict(workspace)?;
+ let dir = root.make_dirs(Path::new("downloads"))?;
+ let mut src = std::fs::File::open(staged)?;
+ let existing: HashSet<String> = dir
+ .list()?
+ .into_iter()
+ .filter(|e| matches!(e.kind, safe_fs::Kind::File))
+ .map(|e| e.name.to_string_lossy().to_string())
+ .collect();
+ let final_name = collision_safe_name(&existing, name);
+ dir.replace_file(OsStr::new(&final_name), &format!(".landing-{guid}"), &mut src, 0o600)?;
+ drop(src);
+ let _ = std::fs::remove_file(staged);
+ Ok(final_name)
+}
+
+fn finish_landing(shared: &Arc<StdMutex<Shared>>, guid: &str, result: std::io::Result<String>) {
let mut s = shared.lock().unwrap_or_else(|e| e.into_inner());
if let Some(entry) = &mut s.download {
if entry.guid == guid {
entry.status = match result {
- Ok(Ok(final_name)) => DlStatus::Done(format!("downloads/{final_name}")),
- Ok(Err(e)) => DlStatus::Failed(e.to_string()),
- Err(_) => DlStatus::Failed("something went wrong landing the download".into()),
+ Ok(final_name) => DlStatus::Done(format!("downloads/{final_name}")),
+ Err(e) => DlStatus::Failed(e.to_string()),
};
}
}
}
+/// Chrome names a download by a guid it makes up (a UUID); that guid is then a file name inside
+/// the container and in staging, so anything else (a separator, `..`, an empty string) is refused.
+fn plain_guid(guid: &str) -> bool {
+ !guid.is_empty() && guid.len() <= 64 && guid.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
+}
+
+/// The download's file inside the container, and the partial file Chrome may keep beside it.
+fn container_paths(guid: &str) -> [String; 2] {
+ [format!("{CONTAINER_DOWNLOAD_DIR}/{guid}"), format!("{CONTAINER_DOWNLOAD_DIR}/{guid}.crdownload")]
+}
+
+/// One small `docker` chore (making the folder, removing a file) through the browser service's
+/// own Docker helper, with a time limit.
+async fn docker_chore(args: &[&str]) -> Result<(), String> {
+ super::service::BrowserService::docker(args, CONTAINER_CHORE_WAIT.as_secs()).await.map(|_| ()).map_err(|e| e.to_string())
+}
+
+/// Makes sure the container has its download folder (its tmpfs starts empty after every
+/// restart), and sweeps anything an interrupted landing left there over an hour ago. It runs
+/// when a session connects or reconnects, so the sweep is a backstop, not a timer.
+async fn prepare_container_dir(container: &str) {
+ if let Err(e) = docker_chore(&["exec", "-u", CONTAINER_USER, container, "mkdir", "-p", CONTAINER_DOWNLOAD_DIR]).await {
+ tracing::warn!("browser downloads: couldn't make {CONTAINER_DOWNLOAD_DIR} in {container}: {e}");
+ return;
+ }
+ let _ = docker_chore(&["exec", "-u", CONTAINER_USER, container, "find", CONTAINER_DOWNLOAD_DIR, "-mindepth", "1", "-mmin", "+60", "-delete"]).await;
+}
+
+/// Removes a download's file (and any partial file) from the container's tmpfs.
+async fn remove_from_container(container: String, guid: String) {
+ let [file, partial] = container_paths(&guid);
+ if let Err(e) = docker_chore(&["exec", "-u", CONTAINER_USER, &container, "rm", "-f", "--", &file, &partial]).await {
+ tracing::warn!("browser downloads: couldn't remove {file} from {container}: {e}");
+ }
+}
+
+/// Copies `guid`'s finished file out of the container into `staging/<guid>`, streaming it
+/// straight to disk (`docker cp` can't read a tmpfs mount), and refuses one past the cap.
+async fn copy_out_of_container(container: &str, staging: &Path, guid: &str) -> Result<PathBuf, String> {
+ tokio::fs::create_dir_all(staging).await.map_err(|e| e.to_string())?;
+ let staged = staging.join(guid);
+ let _ = tokio::fs::remove_file(&staged).await;
+ let mut options = std::fs::OpenOptions::new();
+ options.write(true).create_new(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ options.mode(0o600);
+ }
+ let file = options.open(&staged).map_err(|e| e.to_string())?;
+ let [source, _] = container_paths(guid);
+ // `head -c`, not `cat`: whatever sits at that path (a symlink to /dev/zero, say), at most one
+ // byte past the cap ever reaches this disk, and the size check below then refuses it.
+ let limit = (CONTAINER_MAX_DOWNLOAD_BYTES + 1).to_string();
+ let mut command = tokio::process::Command::new("docker");
+ command.args(["exec", "-u", CONTAINER_USER, container, "head", "-c", &limit, "--", &source]);
+ let run = run_into_file(command, file);
+ let outcome = match tokio::time::timeout(CONTAINER_COPY_WAIT, run).await {
+ Ok(Ok(out)) if out.status.success() => match tokio::fs::metadata(&staged).await {
+ Ok(meta) if meta.len() > CONTAINER_MAX_DOWNLOAD_BYTES => Err(CONTAINER_TOO_BIG.to_string()),
+ Ok(_) => Ok(()),
+ Err(e) => Err(e.to_string()),
+ },
+ Ok(Ok(out)) => Err(format!("couldn't copy it out of the browser: {}", String::from_utf8_lossy(&out.stderr).trim())),
+ Ok(Err(e)) => Err(format!("couldn't copy it out of the browser: {e}")),
+ Err(_) => Err("copying it out of the browser took too long.".into()),
+ };
+ match outcome {
+ Ok(()) => Ok(staged),
+ Err(why) => {
+ let _ = tokio::fs::remove_file(&staged).await;
+ Err(why)
+ }
+ }
+}
+
+/// Runs `command` with its stdout written straight into `file` and its stderr captured.
+/// `Command::output()` can't be used here: tokio's replaces any stdout set beforehand with a
+/// pipe, so the file stayed empty and the whole download sat in memory instead. The child is
+/// killed if this future is dropped (the copy-out's time limit).
+async fn run_into_file(mut command: tokio::process::Command, file: std::fs::File) -> std::io::Result<std::process::Output> {
+ command
+ .stdin(std::process::Stdio::null())
+ .stdout(std::process::Stdio::from(file))
+ .stderr(std::process::Stdio::piped())
+ .kill_on_drop(true)
+ .spawn()?
+ .wait_with_output()
+ .await
+}
+
+/// A download finished inside kwant's browser container: copy it out into staging, land it in
+/// the bot's `downloads/` the same way as a local one, and leave nothing behind in the container
+/// or in staging whatever happens.
+async fn land_from_container(
+ container: String,
+ workspace: PathBuf,
+ staging: PathBuf,
+ shared: Arc<StdMutex<Shared>>,
+ guid: String,
+ name: String,
+) {
+ let copied = copy_out_of_container(&container, &staging, &guid).await;
+ remove_from_container(container, guid.clone()).await;
+ let result = match copied {
+ Ok(staged) => {
+ let landing_guid = guid.clone();
+ let landed = tokio::task::spawn_blocking(move || {
+ let landed = land_staged(&workspace, &landing_guid, &name, &staged);
+ if landed.is_err() {
+ let _ = std::fs::remove_file(&staged);
+ }
+ landed
+ })
+ .await;
+ landed.unwrap_or_else(|_| Err(std::io::Error::other("something went wrong landing the download")))
+ }
+ Err(why) => Err(std::io::Error::other(why)),
+ };
+ finish_landing(&shared, &guid, result);
+}
+
/// Deletes any file left in the shared incoming folder older than `INCOMING_ORPHAN_AGE`: a
/// backstop for a staged download no center's frame tree ever claimed, or one `land_download`
/// never got to finish moving before a restart.
@@ -781,34 +1087,315 @@ fn human_size(bytes: u64) -> String {
mod tests {
use super::*;
+ // The copy-out streams the container file to disk: stdout goes into the staged file, not a
+ // pipe (73f24f58 used `output()`, which landed an empty file in the real-Chromium run).
+ #[tokio::test]
+ async fn the_copy_out_writes_its_bytes_into_the_staged_file() {
+ let dir = tempfile::tempdir().unwrap();
+ let source = dir.path().join("source");
+ let bytes: Vec<u8> = (0..300_000u32).map(|i| (i % 251) as u8).collect();
+ std::fs::write(&source, &bytes).unwrap();
+ let staged = dir.path().join("staged");
+ let file = std::fs::File::create(&staged).unwrap();
+ let mut command = tokio::process::Command::new("head");
+ command.args(["-c", "300001", "--"]).arg(&source);
+ let out = run_into_file(command, file).await.unwrap();
+ assert!(out.status.success());
+ assert!(out.stdout.is_empty(), "nothing is held in memory");
+ assert_eq!(std::fs::read(&staged).unwrap(), bytes);
+
+ // past the limit, `head -c` stops one byte over, which the size check then refuses
+ let file = std::fs::File::create(&staged).unwrap();
+ let mut command = tokio::process::Command::new("head");
+ command.args(["-c", "1000", "--"]).arg(&source);
+ run_into_file(command, file).await.unwrap();
+ assert_eq!(std::fs::metadata(&staged).unwrap().len(), 1000);
+
+ // a missing file is a failure with its reason on stderr
+ let file = std::fs::File::create(&staged).unwrap();
+ let mut command = tokio::process::Command::new("head");
+ command.args(["-c", "10", "--"]).arg(dir.path().join("missing"));
+ let out = run_into_file(command, file).await.unwrap();
+ assert!(!out.status.success());
+ assert!(!out.stderr.is_empty());
+ }
+
// Fix #4: local sessions (a filesystem in common with this server) get downloads kept at the
- // shared staging path; every other session (remote Chrome) gets them refused up front, since
+ // shared staging path; a browser on another computer gets them refused up front, since
// there's nowhere in common to land the bytes.
#[test]
fn local_sessions_allow_and_name_at_the_staging_path() {
let incoming = Path::new("/tmp/kwnt-download-tmp");
- let params = download_behavior_params(true, incoming, None);
+ let params = download_behavior_params(&DownloadHome::Local, incoming, None);
assert_eq!(params["behavior"], "allowAndName");
assert_eq!(params["eventsEnabled"], true);
assert_eq!(params["downloadPath"], "/tmp/kwnt-download-tmp");
assert!(params.get("browserContextId").is_none());
}
+ // 20g: kwant's own browser container keeps the download on its own tmpfs, never at a host
+ // path (which means nothing inside the container).
#[test]
- fn remote_sessions_deny_but_keep_hearing_about_attempts() {
+ fn the_managed_container_allows_and_names_on_its_own_tmpfs() {
+ let incoming = Path::new("/home/kwnt/attachments/download-tmp");
+ let params = download_behavior_params(&DownloadHome::Container("kwnt-browser-x".into()), incoming, Some("ctx-9"));
+ assert_eq!(params["behavior"], "allowAndName");
+ assert_eq!(params["eventsEnabled"], true);
+ assert_eq!(params["downloadPath"], CONTAINER_DOWNLOAD_DIR);
+ assert_eq!(params["browserContextId"], "ctx-9");
+ assert!(CONTAINER_DOWNLOAD_DIR.starts_with("/tmp/"), "only /tmp is writable in the read-only container");
+ }
+
+ #[test]
+ fn a_browser_on_another_computer_denies_but_keeps_hearing_about_attempts() {
let incoming = Path::new("/tmp/kwnt-download-tmp");
- let params = download_behavior_params(false, incoming, None);
+ let params = download_behavior_params(&DownloadHome::Elsewhere, incoming, None);
assert_eq!(params["behavior"], "deny");
assert_eq!(params["eventsEnabled"], true);
assert!(params.get("downloadPath").is_none(), "a remote session has no filesystem in common with this server");
}
#[test]
- fn a_non_default_browser_context_is_passed_through_either_way() {
+ fn a_non_default_browser_context_is_passed_through_every_way() {
let incoming = Path::new("/tmp/kwnt-download-tmp");
- let local = download_behavior_params(true, incoming, Some("ctx-123"));
- assert_eq!(local["browserContextId"], "ctx-123");
- let remote = download_behavior_params(false, incoming, Some("ctx-123"));
- assert_eq!(remote["browserContextId"], "ctx-123");
+ for home in [DownloadHome::Local, DownloadHome::Container("c".into()), DownloadHome::Elsewhere] {
+ assert_eq!(download_behavior_params(&home, incoming, Some("ctx-123"))["browserContextId"], "ctx-123", "{home:?}");
+ }
+ }
+
+ #[test]
+ fn a_cancel_names_the_sessions_own_browser_context() {
+ assert_eq!(cancel_params("g", Some("ctx-1")), json!({"guid": "g", "browserContextId": "ctx-1"}));
+ assert_eq!(cancel_params("g", None), json!({"guid": "g"}));
+ }
+
+ #[test]
+ fn the_container_cap_leaves_room_on_its_512_mb_tmpfs() {
+ let home = DownloadHome::Container("c".into());
+ assert!(home.cap() <= 400 * 1024 * 1024);
+ assert_eq!(DownloadHome::Local.cap(), MAX_DOWNLOAD_BYTES);
+ assert!(home.too_big().contains("400 MB"), "{}", home.too_big());
+ assert!(DownloadHome::Local.too_big().contains("1 GB"));
+ }
+
+ #[test]
+ fn the_refusal_for_another_computer_says_what_to_do_instead() {
+ assert!(!REMOTE_DOWNLOAD_REFUSAL.contains("for now"));
+ assert!(REMOTE_DOWNLOAD_REFUSAL.contains("another computer"));
+ assert!(REMOTE_DOWNLOAD_REFUSAL.contains("Copy the file's link"));
+ assert!(REMOTE_DOWNLOAD_REFUSAL.contains("Local computer"));
+ }
+
+ // The guid becomes a file name inside the container and in staging: only Chrome's own
+ // UUID shape is accepted, never a path.
+ #[test]
+ fn only_a_plain_guid_names_a_file() {
+ assert!(plain_guid("0b9d3c2e-6a1f-4c7e-9a52-3f1e2d4c5b6a"));
+ let long = "a".repeat(65);
+ for bad in ["", "../etc/passwd", "a/b", "..", "x y", "a\\b", long.as_str()] {
+ assert!(!plain_guid(bad), "{bad:?}");
+ }
+ let [file, partial] = container_paths("abc-1");
+ assert_eq!(file, "/tmp/kwnt-dl/abc-1");
+ assert_eq!(partial, "/tmp/kwnt-dl/abc-1.crdownload");
+ }
+
+ fn container_landing<'a>(home: &'a DownloadHome, dir: &'a Path) -> Landing<'a> {
+ Landing { workspace: dir, home, staging_root: dir, browser_context_id: Some("ctx-1") }
+ }
+
+ fn claimed() -> Arc<StdMutex<Shared>> {
+ let shared = Arc::new(StdMutex::new(Shared::default()));
+ shared.lock().unwrap().frames.insert("F1".into());
+ shared
+ }
+
+ /// Feeds one event through `handle_event` against a stand-in browser that answers every
+ /// request with an empty result; returns the requests it was sent (a cancel, say).
+ async fn feed(shared: &Arc<StdMutex<Shared>>, landing: &Landing<'_>, method: &str, params: Value) -> Vec<Value> {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let seen = Arc::new(StdMutex::new(Vec::new()));
+ let log = seen.clone();
+ let server = tokio::spawn(async move {
+ let (stream, _) = listener.accept().await.unwrap();
+ let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap();
+ while let Some(Ok(Message::Text(txt))) = ws.next().await {
+ let v: Value = serde_json::from_str(&txt).unwrap();
+ log.lock().unwrap().push(v.clone());
+ let _ = ws.send(Message::Text(json!({"id": v["id"], "result": {}}).to_string())).await;
+ }
+ });
+ let mut ws = connect_cdp(&format!("ws://{addr}")).await.unwrap();
+ handle_event(&mut ws, &AtomicU64::new(1), method, params, shared, landing).await;
+ // a cancel is sent without waiting for its reply: let the stand-in read it before closing
+ let _ = ws.close(None).await;
+ let _ = tokio::time::timeout(Duration::from_secs(5), server).await;
+ let sent = seen.lock().unwrap().clone();
+ sent
+ }
+
+ // Chrome's own order (seen in the real-Chromium run): the `canceled` event comes before the
+ // `cancelDownload` reply. Both reach a pending job's loop, the download ends as too big, and
+ // the job still gets its own reply.
+ #[tokio::test]
+ async fn a_cancel_mid_job_keeps_chromes_canceled_event_and_the_jobs_reply() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let big = 450u64 * 1024 * 1024;
+ let server = tokio::spawn(async move {
+ let (stream, _) = listener.accept().await.unwrap();
+ let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap();
+ let mut job_id = Value::Null;
+ while let Some(Ok(Message::Text(txt))) = ws.next().await {
+ let v: Value = serde_json::from_str(&txt).unwrap();
+ let event = |method: &str, params: Value| Message::Text(json!({"method": method, "params": params}).to_string());
+ match v["method"].as_str() {
+ Some("Browser.cancelDownload") => {
+ ws.send(event("Browser.downloadProgress", json!({"guid": "g9", "state": "canceled", "receivedBytes": 4096, "totalBytes": big}))).await.unwrap();
+ ws.send(Message::Text(json!({"id": v["id"], "result": {}}).to_string())).await.unwrap();
+ ws.send(Message::Text(json!({"id": job_id, "result": {"done": true}}).to_string())).await.unwrap();
+ }
+ _ => {
+ job_id = v["id"].clone();
+ ws.send(event("Browser.downloadWillBegin", json!({"guid": "g9", "frameId": "F1", "suggestedFilename": "big.mp4"}))).await.unwrap();
+ ws.send(event("Browser.downloadProgress", json!({"guid": "g9", "state": "inProgress", "receivedBytes": 0, "totalBytes": big}))).await.unwrap();
+ }
+ }
+ }
+ });
+ let mut ws = connect_cdp(&format!("ws://{addr}")).await.unwrap();
+ let result = job_call(&mut ws, &AtomicU64::new(1), &shared, &landing, "Runtime.evaluate", json!({})).await;
+ assert_eq!(result.unwrap(), json!({"done": true}), "the job's own reply is not lost");
+ let entry = shared.lock().unwrap().download.clone().unwrap();
+ assert!(matches!(&entry.status, DlStatus::Cancelled(why) if why.contains("over 400 MB")), "{:?}", entry.status);
+ server.abort();
+ }
+
+ #[tokio::test]
+ async fn another_computers_browser_refuses_with_the_clearer_message() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Elsewhere;
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g1", "frameId": "F1", "suggestedFilename": "clip.mp4"})).await;
+ let center = DownloadCenter { shared: shared.clone(), jobs: mpsc::channel(1).0 };
+ let report = center.report().unwrap();
+ assert_eq!(report["status"], "failed");
+ assert!(report["note"].as_str().unwrap().contains("runs on another computer"), "{report}");
+ }
+
+ #[tokio::test]
+ async fn a_container_download_announced_past_the_cap_is_stopped_as_too_big() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g2", "frameId": "F1", "suggestedFilename": "big.mp4"})).await;
+ let sent = feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g2", "state": "inProgress", "receivedBytes": 1024, "totalBytes": 450 * 1024 * 1024})).await;
+ assert!(shared.lock().unwrap().download.as_ref().unwrap().too_big.is_some(), "cancel was asked for on the announced size");
+ assert_eq!(sent.len(), 1, "{sent:?}");
+ assert_eq!(sent[0]["method"], "Browser.cancelDownload");
+ assert_eq!(sent[0]["params"], json!({"guid": "g2", "browserContextId": "ctx-1"}));
+ // a later progress event doesn't ask again
+ let sent = feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g2", "state": "inProgress", "receivedBytes": 4096, "totalBytes": 450 * 1024 * 1024})).await;
+ assert!(sent.is_empty(), "{sent:?}");
+ feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g2", "state": "canceled", "receivedBytes": 2048, "totalBytes": 450 * 1024 * 1024})).await;
+ let center = DownloadCenter { shared: shared.clone(), jobs: mpsc::channel(1).0 };
+ let report = center.report().unwrap();
+ assert_eq!(report["status"], "cancelled");
+ assert!(report["note"].as_str().unwrap().contains("over 400 MB"), "{report}");
+ }
+
+ #[tokio::test]
+ async fn a_container_download_that_finishes_past_the_cap_is_never_copied_out() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g5", "frameId": "F1", "suggestedFilename": "big.bin"})).await;
+ // no size announced, and it finished before a progress event could pass the cap
+ feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g5", "state": "completed", "receivedBytes": 401 * 1024 * 1024})).await;
+ let entry = shared.lock().unwrap().download.clone().unwrap();
+ assert!(!entry.copying, "no copy-out was started");
+ assert!(matches!(&entry.status, DlStatus::Cancelled(why) if why.contains("over 400 MB")), "{:?}", entry.status);
+ }
+
+ #[tokio::test]
+ async fn a_container_download_at_the_cap_is_still_copied_out() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g6", "frameId": "F1", "suggestedFilename": "ok.bin"})).await;
+ feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g6", "state": "completed", "receivedBytes": CONTAINER_MAX_DOWNLOAD_BYTES})).await;
+ assert!(shared.lock().unwrap().download.as_ref().unwrap().copying, "the copy-out started");
+ }
+
+ #[tokio::test]
+ async fn a_second_container_download_stops_the_first() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g1", "frameId": "F1", "suggestedFilename": "a.bin"})).await;
+ let sent = feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g7", "frameId": "F1", "suggestedFilename": "b.bin"})).await;
+ assert_eq!(sent.len(), 1, "{sent:?}");
+ assert_eq!(sent[0]["method"], "Browser.cancelDownload");
+ assert_eq!(sent[0]["params"], json!({"guid": "g1", "browserContextId": "ctx-1"}));
+ assert_eq!(shared.lock().unwrap().download.as_ref().unwrap().guid, "g7");
+ }
+
+ #[tokio::test]
+ async fn a_second_container_download_leaves_one_being_copied_out_alone() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g8", "frameId": "F1", "suggestedFilename": "a.bin"})).await;
+ shared.lock().unwrap().download.as_mut().unwrap().copying = true;
+ let sent = feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g9", "frameId": "F1", "suggestedFilename": "b.bin"})).await;
+ assert!(sent.is_empty(), "{sent:?}");
+ }
+
+ #[tokio::test]
+ async fn a_second_local_download_sends_no_cancel() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Local;
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "l1", "frameId": "F1", "suggestedFilename": "a.bin"})).await;
+ let sent = feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "l2", "frameId": "F1", "suggestedFilename": "b.bin"})).await;
+ assert!(sent.is_empty(), "{sent:?}");
+ }
+
+ #[tokio::test]
+ async fn a_container_download_from_another_bots_frame_is_never_claimed() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "g3", "frameId": "OTHER", "suggestedFilename": "x.txt"})).await;
+ feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "g3", "state": "completed", "receivedBytes": 3, "totalBytes": 3})).await;
+ assert!(shared.lock().unwrap().download.is_none());
+ }
+
+ #[tokio::test]
+ async fn a_container_download_with_an_unusable_guid_fails_and_is_cancelled() {
+ let dir = tempfile::tempdir().unwrap();
+ let home = DownloadHome::Container("kwnt-browser-unit-test-none".into());
+ let shared = claimed();
+ let landing = container_landing(&home, dir.path());
+ feed(&shared, &landing, "Browser.downloadWillBegin", json!({"guid": "../x", "frameId": "F1", "suggestedFilename": "x.txt"})).await;
+ let sent = feed(&shared, &landing, "Browser.downloadProgress", json!({"guid": "../x", "state": "inProgress", "receivedBytes": 3})).await;
+ assert_eq!(sent.first().map(|v| v["method"].clone()), Some(json!("Browser.cancelDownload")));
+ let s = shared.lock().unwrap();
+ let entry = s.download.as_ref().unwrap();
+ assert!(matches!(entry.status, DlStatus::Failed(_)));
+ assert!(entry.too_big.is_some(), "the download was cancelled");
}
}
diff --git a/src/sandbox/service.rs b/src/sandbox/service.rs
index 8d73e0ca..09dcebca 100644
--- a/src/sandbox/service.rs
+++ b/src/sandbox/service.rs
@@ -1,6 +1,7 @@
//! One browser service per installation; isolated CDP contexts per bot/team.
//! The private CDP endpoint is used only by the server, never by the web client.
use super::browser::{outage, BrowserController, BrowserError, Outage};
+use super::browser_downloads::DownloadHome;
use super::chrome_download::{self, ChromeSetup};
use super::browser_proxy::{BrowserProxy, ENV_BYPASS, ENV_PROXY};
use super::computer::{self, ComputerMode};
@@ -294,7 +295,7 @@ impl BrowserService {
})
}
- async fn docker(args: &[&str], seconds: u64) -> Result<String, BrowserError> {
+ pub(crate) async fn docker(args: &[&str], seconds: u64) -> Result<String, BrowserError> {
let out = timeout(Duration::from_secs(seconds), Command::new("docker")
.args(args).kill_on_drop(true).output()).await
.map_err(|_| BrowserError::Timeout)?
@@ -673,7 +674,8 @@ impl BrowserService {
// download staging, outside any bot's workspace (see `browser_downloads.rs`'s doc).
let download_staging = self.root.join("attachments").join("download-tmp");
let context_id = (!session.context.is_empty()).then_some(session.context.clone());
- ctrl.configure_downloads(workspace.to_path_buf(), local, context_id, download_staging).await;
+ let home = download_home(local, self.endpoint.is_some(), &self.root);
+ ctrl.configure_downloads(workspace.to_path_buf(), home, context_id, download_staging).await;
Ok(ctrl)
}
@@ -854,6 +856,19 @@ pub fn managed_run_args(name: &str, root: &Path, proxy: Option<&BrowserProxy>) -
args
}
+/// Where a session's browser keeps its downloads: Chrome on this computer, kwant's own browser
+/// container (vm mode with no `KWNT_BROWSER_CDP_URL`), or a browser on another computer that
+/// kwant only reaches over DevTools.
+pub fn download_home(local: bool, external_endpoint: bool, root: &Path) -> DownloadHome {
+ if local {
+ DownloadHome::Local
+ } else if external_endpoint {
+ DownloadHome::Elsewhere
+ } else {
+ DownloadHome::Container(managed_container_name(root))
+ }
+}
+
/// The VM browser container of the kwant whose data folder is `root`: one name per installation
/// avoids collisions between local kwant instances.
pub fn managed_container_name(root: &Path) -> String {
@@ -870,6 +885,17 @@ mod reaper_tests {
const LIMIT: Duration = Duration::from_secs(30 * 60);
const MIN: Duration = Duration::from_secs(60);
+ // 20g: only a browser kwant runs itself in Docker gets its downloads copied out; a browser
+ // the person pointed kwant at elsewhere keeps refusing, and local mode wins over both.
+ #[test]
+ fn downloads_come_home_from_the_managed_container_only() {
+ let root = Path::new("/srv/kwnt-data");
+ assert_eq!(download_home(true, false, root), DownloadHome::Local);
+ assert_eq!(download_home(true, true, root), DownloadHome::Local);
+ assert_eq!(download_home(false, true, root), DownloadHome::Elsewhere);
+ assert_eq!(download_home(false, false, root), DownloadHome::Container(managed_container_name(root)));
+ }
+
#[test]
fn a_container_whose_data_folder_is_gone_is_removed_running_or_not() {
for running in [true, false] {
diff --git a/tests/browser_container_download_test.rs b/tests/browser_container_download_test.rs
new file mode 100644
index 00000000..53023b17
--- /dev/null
+++ b/tests/browser_container_download_test.rs
@@ -0,0 +1,255 @@
+//! 20g: a vm-mode bot's download from kwant's own browser container lands in the bot's own
+//! `downloads/`, against real Chromium in the `kwnt-browser:v3` image. Run with
+//! `cargo test --test browser_container_download_test -- --ignored` on a host with Docker and that
+//! image. Ignored by default so CI and ordinary test runs never start a container.
+//!
+//! The container can't reach a server on the host (its network stops at the host firewall), so the
+//! pages and files are served from inside the container itself with its own `socat`, on loopback
+//! ports only Chrome there can see.
+
+use axum::{
+ body::{to_bytes, Body},
+ http::{Request, StatusCode},
+ Router,
+};
+use kwnt::{
+ broker::PermissionBroker,
+ config::Config,
+ db::{spaces::SpaceId, Database},
+ router::LayaRouter,
+ sandbox::service::managed_container_name,
+ server::{
+ continuity::SelfScope,
+ create_router, mcp_runtime,
+ routes::{get_or_create_browser_controller, resolve_sandbox_target_info},
+ AppState,
+ },
+ tools::ToolRunner,
+};
+use serde_json::{json, Value};
+use std::io::Write;
+use std::process::{Command, Stdio};
+use std::time::Duration;
+use tower::ServiceExt;
+
+/// 300 KB of bytes no text encoding would leave unchanged, to prove the copy is byte-exact.
+fn test_bytes() -> Vec<u8> {
+ let mut x: u32 = 0x2545_f491;
+ (0..300 * 1024)
+ .map(|_| {
+ x ^= x << 13;
+ x ^= x >> 17;
+ x ^= x << 5;
+ (x >> 24) as u8
+ })
+ .collect()
+}
+
+/// 450 MB, past the container's 400 MB cap, announced up front in `Content-Length`.
+const BIG_BYTES: u64 = 450 * 1024 * 1024;
+
+const PAGE: &str = "<!doctype html><title>dl</title><body>\
+<button onclick=\"location.href='http://127.0.0.1:8000/'\">Get the file</button>\
+<button onclick=\"location.href='http://127.0.0.1:8002/'\">Get the big file</button>";
+
+fn docker(args: &[&str]) -> Option<String> {
+ let out = Command::new("docker").args(args).output().ok()?;
+ out.status.success().then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
+}
+
+fn docker_stdin(args: &[&str], input: &[u8]) {
+ let mut child = Command::new("docker").args(args).stdin(Stdio::piped()).spawn().expect("docker");
+ child.stdin.take().unwrap().write_all(input).unwrap();
+ assert!(child.wait().unwrap().success(), "docker {args:?}");
+}
+
+/// Removes the container whatever the test's outcome, so a failure leaves nothing running.
+struct Cleanup(String);
+impl Drop for Cleanup {
+ fn drop(&mut self) {
+ let _ = docker(&["rm", "-f", &self.0]);
+ }
+}
+
+/// Serves `PAGE` on 8001, the test file on 8000 and the oversized one on 8002, all inside the
+/// container, as `attachment` downloads.
+fn serve_inside(container: &str, bytes: &[u8]) {
+ docker_stdin(&["exec", "-i", container, "sh", "-c", "cat > /tmp/src.bin"], bytes);
+ let scripts = [
+ ("page.sh", format!("printf 'HTTP/1.0 200 OK\\r\\nContent-Type: text/html\\r\\nContent-Length: {}\\r\\n\\r\\n'\nprintf '%s' \"{}\"\n", PAGE.len(), PAGE.replace('"', "\\\""))),
+ ("file.sh", format!("printf 'HTTP/1.0 200 OK\\r\\nContent-Type: application/octet-stream\\r\\nContent-Disposition: attachment; filename=\"clip.mp4\"\\r\\nContent-Length: {}\\r\\n\\r\\n'\ncat /tmp/src.bin\n", bytes.len())),
+ ("big.sh", format!("printf 'HTTP/1.0 200 OK\\r\\nContent-Type: application/octet-stream\\r\\nContent-Disposition: attachment; filename=\"big.mp4\"\\r\\nContent-Length: {BIG_BYTES}\\r\\n\\r\\n'\nhead -c {BIG_BYTES} /dev/zero\n")),
+ ];
+ for (name, body) in &scripts {
+ docker_stdin(&["exec", "-i", container, "sh", "-c", &format!("cat > /tmp/{name}")], body.as_bytes());
+ }
+ for (port, name) in [(8001, "page.sh"), (8000, "file.sh"), (8002, "big.sh")] {
+ docker(&["exec", "-d", container, "socat", &format!("TCP-LISTEN:{port},bind=127.0.0.1,reuseaddr,fork"), &format!("SYSTEM:sh /tmp/{name}")])
+ .expect("socat");
+ }
+}
+
+/// What's in the container's download folder right now (empty when it's gone).
+fn container_downloads(container: &str) -> Vec<String> {
+ docker(&["exec", container, "ls", "-A", "/tmp/kwnt-dl"])
+ .unwrap_or_default()
+ .lines()
+ .map(str::to_string)
+ .filter(|l| !l.is_empty())
+ .collect()
+}
+
+fn rpc(method: &str, params: Value) -> Value {
+ json!({"jsonrpc": "2.0", "id": 1, "method": method, "params": params})
+}
+
+async fn self_tool(app: &Router, token: &str, name: &str, args: Value) -> (Value, bool) {
+ let req = Request::builder()
+ .method("POST")
+ .uri("/api/mcp/self")
+ .header("content-type", "application/json")
+ .header("authorization", format!("Bearer {token}"))
+ .body(Body::from(rpc("tools/call", json!({"name": name, "arguments": args})).to_string()))
+ .unwrap();
+ let resp = app.clone().oneshot(req).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ let v: Value = serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap_or(Value::Null);
+ let is_error = v["result"]["isError"].as_bool().unwrap_or(false);
+ let text = v["result"]["content"][0]["text"].as_str().unwrap_or("");
+ (serde_json::from_str(text).unwrap_or(Value::Null), is_error)
+}
+
+/// Polls `browser_page` until `download.status` is terminal, or panics.
+async fn wait_for_download(app: &Router, token: &str) -> Value {
+ for _ in 0..200 {
+ let (page, is_error) = self_tool(app, token, "browser_page", json!({})).await;
+ assert!(!is_error, "{page}");
+ if let Some(status) = page["download"]["status"].as_str() {
+ if status != "in_progress" {
+ return page["download"].clone();
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(150)).await;
+ }
+ panic!("download never reached a terminal state");
+}
+
+async fn wait_until_empty(container: &str) -> Vec<String> {
+ for _ in 0..40 {
+ let left = container_downloads(container);
+ if left.is_empty() {
+ return left;
+ }
+ tokio::time::sleep(Duration::from_millis(250)).await;
+ }
+ container_downloads(container)
+}
+
+fn files_in(dir: &std::path::Path) -> Vec<String> {
+ std::fs::read_dir(dir)
+ .map(|d| d.flatten().map(|e| e.file_name().to_string_lossy().to_string()).collect())
+ .unwrap_or_default()
+}
+
+fn app_state(data_dir: &std::path::Path, db: Database, cdp_url: Option<String>) -> AppState {
+ let config = Config { data_dir: data_dir.to_path_buf(), browser_cdp_url: cdp_url, ..Config::default() };
+ AppState::new(config.clone(), db, ToolRunner::new(data_dir.join("workspace")), PermissionBroker::new(), LayaRouter::new(config.laya_url.clone()))
+}
+
+#[tokio::test]
+#[ignore = "needs Docker and the kwnt-browser:v3 image; starts a real container"]
+async fn a_vm_bots_download_from_the_browser_container_lands_in_its_own_folder() {
+ std::env::set_var(kwnt::sandbox::computer::MODE_ENV, "vm");
+ std::env::remove_var("KWNT_BROWSER_CDP_URL");
+ let tmp = tempfile::tempdir().unwrap();
+ let container = managed_container_name(tmp.path());
+ let _cleanup = Cleanup(container.clone());
+ let db = Database::init(tmp.path()).await.unwrap();
+ let state = app_state(tmp.path(), db, None);
+ let app = create_router(state.clone());
+ let kwants = state.db.list_kwants(&SpaceId::default_space()).await.unwrap();
+ let (bot_a, bot_b) = (&kwants[0], &kwants[1]);
+ let scope_a = SelfScope { kwant_id: bot_a.id.clone(), kwant_name: bot_a.name.clone(), sandbox_target: Some(bot_a.id.clone()), ..Default::default() };
+ let scope_b = SelfScope { kwant_id: bot_b.id.clone(), kwant_name: bot_b.name.clone(), sandbox_target: Some(bot_b.id.clone()), ..Default::default() };
+ let (_ctx_a, token_a) = mcp_runtime::prepare_turn_with_self(&state, bot_a, None, None, Some(scope_a)).await.unwrap();
+ let (_ctx_b, token_b) = mcp_runtime::prepare_turn_with_self(&state, bot_b, None, None, Some(scope_b)).await.unwrap();
+
+ // B's session first, so its own DevTools connection is listening before A's download begins
+ get_or_create_browser_controller(&state, &bot_b.id).await.expect("kwant starts its browser container");
+ assert_eq!(docker(&["inspect", "--format", "{{.State.Running}}", &container]).as_deref(), Some("true"));
+ let bytes = test_bytes();
+ serve_inside(&container, &bytes);
+ let (page, is_error) = self_tool(&app, &token_b, "browser_open", json!({"url": "http://127.0.0.1:8001/"})).await;
+ assert!(!is_error, "{page}");
+ let (page, is_error) = self_tool(&app, &token_a, "browser_open", json!({"url": "http://127.0.0.1:8001/"})).await;
+ assert!(!is_error, "{page}");
+
+ // 1. A's download lands in A's own folder, byte for byte, with the usual report
+ let (page, is_error) = self_tool(&app, &token_a, "browser_act", json!({"action": "click_text", "text": "Get the file"})).await;
+ assert!(!is_error, "{page}");
+ let report = wait_for_download(&app, &token_a).await;
+ eprintln!("A's report: {report}");
+ assert_eq!(report["status"], "done", "{report}");
+ assert_eq!(report["path"], "downloads/clip.mp4");
+ assert!(report["note"].as_str().unwrap().starts_with("Downloaded clip.mp4 (300.0 KB) to downloads/clip.mp4"), "{report}");
+ let (_, workspace_a) = resolve_sandbox_target_info(&state, &bot_a.id).await.expect("bot a workspace");
+ let landed = std::fs::read(workspace_a.join("downloads/clip.mp4")).expect("the file landed in A's folder");
+ assert_eq!(landed.len(), bytes.len());
+ assert!(landed == bytes, "the landed bytes match the served ones");
+ eprintln!("landed {} bytes at {}", landed.len(), workspace_a.join("downloads/clip.mp4").display());
+ assert_eq!(wait_until_empty(&container).await, Vec::<String>::new(), "the container copy is removed");
+ let staging = tmp.path().join("attachments").join("download-tmp");
+ assert_eq!(files_in(&staging), Vec::<String>::new(), "nothing is left in staging");
+
+ // 2. B, in the same container, gets nothing
+ let (_, workspace_b) = resolve_sandbox_target_info(&state, &bot_b.id).await.expect("bot b workspace");
+ assert_eq!(files_in(&workspace_b.join("downloads")), Vec::<String>::new(), "nothing landed in B's folder");
+ let (page_b, is_error) = self_tool(&app, &token_b, "browser_page", json!({})).await;
+ assert!(!is_error, "{page_b}");
+ assert!(page_b["download"].is_null(), "B's page never started a download: {page_b}");
+
+ // 3. Too big for the container: a plain failure, and nothing on the tmpfs
+ let (page, is_error) = self_tool(&app, &token_a, "browser_open", json!({"url": "http://127.0.0.1:8001/"})).await;
+ assert!(!is_error, "{page}");
+ let (page, is_error) = self_tool(&app, &token_a, "browser_act", json!({"action": "click_text", "text": "Get the big file"})).await;
+ assert!(!is_error, "{page}");
+ let report = wait_for_download(&app, &token_a).await;
+ eprintln!("A's big report: {report}");
+ assert_eq!(report["status"], "cancelled", "{report}");
+ assert!(report["note"].as_str().unwrap().contains("over 400 MB"), "{report}");
+ assert_eq!(wait_until_empty(&container).await, Vec::<String>::new(), "nothing is left on the tmpfs");
+ let tmp_used = docker(&["exec", &container, "du", "-sm", "/tmp"]).unwrap_or_default();
+ eprintln!("container /tmp after the big one: {tmp_used}");
+ assert_eq!(files_in(&workspace_a.join("downloads")), vec!["clip.mp4".to_string()]);
+ assert_eq!(files_in(&staging), Vec::<String>::new());
+
+ mcp_runtime::finish_turn(&state, &token_a).await;
+ mcp_runtime::finish_turn(&state, &token_b).await;
+
+ // 4. The same Chrome reached as a browser on another computer (KWNT_BROWSER_CDP_URL): kwant
+ // has no Docker access to it, so the download is refused with the clearer message
+ let binding = docker(&["port", &container, "9222/tcp"]).expect("port");
+ let port = binding.lines().next().and_then(|l| l.rsplit(':').next()).unwrap().to_string();
+ let other = tempfile::tempdir().unwrap();
+ let other_db = Database::init(other.path()).await.unwrap();
+ let remote = app_state(other.path(), other_db, Some(format!("http://127.0.0.1:{port}")));
+ let remote_app = create_router(remote.clone());
+ let bots = remote.db.list_kwants(&SpaceId::default_space()).await.unwrap();
+ let bot = &bots[0];
+ let scope = SelfScope { kwant_id: bot.id.clone(), kwant_name: bot.name.clone(), sandbox_target: Some(bot.id.clone()), ..Default::default() };
+ let (_ctx, token) = mcp_runtime::prepare_turn_with_self(&remote, bot, None, None, Some(scope)).await.unwrap();
+ let (page, is_error) = self_tool(&remote_app, &token, "browser_open", json!({"url": "http://127.0.0.1:8001/"})).await;
+ assert!(!is_error, "{page}");
+ let (page, is_error) = self_tool(&remote_app, &token, "browser_act", json!({"action": "click_text", "text": "Get the file"})).await;
+ assert!(!is_error, "{page}");
+ let report = wait_for_download(&remote_app, &token).await;
+ eprintln!("remote report: {report}");
+ assert_eq!(report["status"], "failed", "{report}");
+ assert!(report["note"].as_str().unwrap().contains("This browser runs on another computer"), "{report}");
+ assert!(!report["note"].as_str().unwrap().contains("for now"), "{report}");
+ let (_, workspace) = resolve_sandbox_target_info(&remote, &bot.id).await.expect("workspace");
+ assert_eq!(files_in(&workspace.join("downloads")), Vec::<String>::new());
+ assert_eq!(container_downloads(&container), Vec::<String>::new(), "a denied download writes nothing");
+ mcp_runtime::finish_turn(&remote, &token).await;
+ assert!(docker(&["inspect", &managed_container_name(other.path())]).is_none(), "an outside browser starts no container");
+}