← Back to all articles

Why Giving an AI Browser MCP Unrestricted File Upload Leaks Your Cookies — And How We Built Fail-Closed Native Uploads in Rust

How prompt-injected pages can exfiltrate storageState cookies through misconfigured browser MCP uploads, and how sessfork-mcp enforces native CDP DOM.setFileInputFiles, fail-closed --upload-root allowlists, unconditional credential denylists, and an empty-by-default upload-staging workflow.

🌐 阅读中文版本 →
#Rust#MCP#Security#Browser Automation
On this page

When an AI agent fills out web forms on your behalf—attaching PDFs, screenshots, or build archives—the naive solution is adding a file_upload tool that accepts any local path.

Without strict isolation, that single tool turns into a silent credential-exfiltration vector.

In sessfork-mcp (our pure-Rust, zero-Node.js, zero-Tokio CDP MCP server; commit 7543c1e), we just shipped native browser_file_upload (bringing the exposed toolset to 19 tools). Here is a breakdown of the three real failure modes in browser MCP file uploads, why we chose native CDP DOM.setFileInputFiles over JS DataTransfer, and two subtle P1 bugs caught by an independent diff review before shipping.


1. Three Real Risks in Browser MCP File Uploads

  1. Session state and upload roots sharing the same directory If a browser MCP server stores exported cookies or localStorage tokens inside --output-dir and allows uploads from that same root—or runs with --allow-unrestricted-file-access—a single prompt-injected webpage can instruct the agent to attach storageState JSON straight into an attacker-controlled upload form.
  2. Eager pre-upload leaks across tasks Even when an allowlist exists, pointing it at a broad historical folder means a multi-task agent can attach the wrong draft or archive. Many modern web forms upload files immediately on file-input change events, long before a human clicks the final Submit button.
  3. Path-existence oracles and directory leaks Returning distinct errors for “file not found” vs. “outside allowed directory” lets an untrusted prompt probe the host filesystem. Echoing absolute paths back in tool receipts leaks local usernames and project layout into LLM context.

2. Protocol Choice: Native CDP DOM.setFileInputFiles vs. JS DataTransfer

sessfork-mcp speaks standard CDP over OS pipes (--remote-debugging-pipe with lexmount/moli) or loopback WebSockets (--cdp-url with Chrome/Chromium). We benchmarked two upload mechanisms against a live local FormData endpoint:

Dimension JS Runtime.callFunctionOn + DataTransfer Native CDP DOM.setFileInputFiles
Memory overhead Reads file into Rust, base64-encodes over JSON-RPC, decodes into V8 heap Zero base64 bloat; passes canonical path after validation so the browser reads natively
Event fidelity Synthetic dispatchEvent(new Event('change')) produces isTrusted: false Fires native isTrusted: true input and change events
Large archives / video Bounded by JSON-RPC payload size & atob allocation Handles large files up to configurable --max-upload-mb
MIME & reset semantics Requires manual extension-to-MIME table Kernel infers MIME (image/png, application/zip, etc.); paths: [] clears cleanly

Testing directly against moli 1.1.27 confirmed full native support for DOM.setFileInputFiles (via both objectId and backendNodeId), exact binary FormData delivery, and Chromium-compatible UnableToSetFileInput errors when targeting non-file elements or sending multiple files to a single-file input.


3. Four Fail-Closed Guardrails in sessfork-mcp

All validation runs upfront before spawning or attaching to a browser process:

browser_file_upload(ref|selector, paths)
  │
  ├─► 1. Disabled by default: upload_roots empty? ──yes──► Reject immediately
  ├─► 2. Input shape: non-array / >20 files / empty / NUL? ──yes──► Reject
  ├─► 3. Canonicalize + single metadata() check
  │        (non-file / missing / outside return identical opaque error)
  ├─► 4. Unconditional credential denylist (evaluated BEFORE allowlist):
  │        .sessfork (case-insensitive on Win/macOS) / --output-dir / --storage-state? ──yes──► Reject
  └─► 5. Narrow --upload-root + configurable --max-upload-mb (default 100 MiB, 1..10240)
           ──► Strip Windows \\?\ verbatim prefix ──► CDP DOM.setFileInputFiles
           ──► Action Receipt prints basenames only (e.g. shot.png, poc.zip)

Key design details in storage.rs and browser.rs:

  • Denylist before allowlist: Even if an operator configures an overly broad --upload-root, any path containing .sessfork, inside --output-dir, or matching --storage-state is unconditionally blocked.
  • No size oracle on external files: File size (meta.len() > max_upload_bytes) is checked only after the file is proven to live inside an allowed --upload-root and outside all forbidden paths.
  • User-configurable size cap (--max-upload-mb): Defaults to 100 MiB and accepts 1..=10240 MiB (up to 10 GiB) across sessfork-mcp, sessfork-mcp setup, and Python’s mcp_setup.py. Passing 0 directly in BrowserConfig fails closed.
  • Basename-only receipts: Action Receipt logs only canon.file_name(), keeping local filesystem paths out of model context.

4. Two Real P1 Bugs Caught by Independent Diff Review

Before committing, we ran an independent adversarial review over the raw git diff (without sharing author assumptions). It caught two subtle cross-platform P1 bugs that unit tests on macOS alone had missed:

  1. Windows \\?\ verbatim prefix breaking CDP consumers On Windows, fs::canonicalize() prepends \\?\ (or \\?\UNC\). Passing that raw verbatim string straight into DOM.setFileInputFiles breaks Chromium/Electron path consumers. We reused strip_windows_verbatim_prefix right after canonicalized security checks and right before the CDP call.
  2. Python Sequence accepting a bare str and whitespace resolving to cwd() In build_managed_moli_mcp_entry(..., upload_roots: Optional[Sequence[Path]] = None), Python str and bytes implement Sequence. Passing upload_roots="/tmp/docs" would iterate character-by-character and emit --upload-root=/ (exposing /). Worse, passing "" or " " would cause Path(root).expanduser().resolve() to resolve to Path.cwd(). We added explicit isinstance(upload_roots, (str, bytes, os.PathLike)) rejection plus .strip() and NUL-byte checks with matching negative-control tests.

5. Operational Pattern: The Empty-by-Default upload-staging/ Outbox

Code guardrails pair best with a simple operational habit: instead of pointing --upload-root at a permanent archive directory, point it at a 0700 upload-staging/ directory that stays empty at rest.

  1. At rest, upload-staging/ contains zero files—a prompt-injected page has nothing to exfiltrate.
  2. Whenever a workflow needs to attach files, an automation step clears upload-staging/, copies only the current task’s approved files in, runs browser_file_upload, verifies the UI attachment list, and wipes upload-staging/ immediately afterward.

  • Rust CI (Ubuntu, macOS, Windows): cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings (0 warnings), cargo test --locked --no-fail-fast (48 passed).

  • Python suite: PYTHONPATH=src python3 -m unittest discover -s tests (52 passed).

  • Live E2E probe (sessfork-mcp + moli 1.1.27): Verified real binary PNG + ZIP FormData upload, isTrusted: true events, paths: [] reset, and fail-closed rejection on outside paths, .sessfork/state.json, non-file buttons (UnableToSetFileInput), and unconfigured instances.

  • GitHub (MIT): https://github.com/wasabihu/sessfork

  • Standalone Rust MCP Docs: https://github.com/wasabihu/sessfork/tree/main/rust/sessfork-mcp

Discussion & Feedback Join the discussion or share feedback on GitHub Discussions →