Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(serve): refuse cross-origin browser requests on an unauthenticated server
AkitaOnRailsa05533fdc593
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a8a8f6c9..f1b73019 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Fixed+- Fixed a web page in the operator's browser being able to change state on an+ unauthenticated loopback server (the default `127.0.0.1:49374`, no token).+ Browsers send `GET` and body-less `POST` requests cross-origin without a+ preflight, and the JSON-only extractors never see those: an `<img>` pointing+ at `GET /handoff?workspace=…&project=…` claimed the project's single-use+ handoff (and moved the anonymous active-project pointer), and a body-less+ `POST /admin/profile/rebuild` ran a profile pass, LLM calls included. With+ auth off, the machine and admin routes now refuse with 403 any request a+ browser marks as cross-site (`Sec-Fetch-Site`) or whose `Origin` is not the+ server's own host. Hooks, the CLI and MCP clients send neither header and+ are unaffected; the built-in web UI's same-origin calls still pass; servers+ with a bearer token are unchanged. A new end-to-end test drives every write+ route with each preflight-free content type, a DNS-rebinding `Host` and CORS+ preflights, and records the guard as row 12c in+ `docs/security-boundaries.md`. (#TBD)- Fixed the Jev choice reranker adapter silently fabricating scores from amalformed judge response: `docs/examples/jev-reranker-adapter/jev_rerank_shim_choice.py`defaulted missing or misnamed `probabilities` entries to `0.0`, so an HTTPdiff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex b0dd7113..9ace6f90 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -1732,9 +1732,8 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {&args.web_slug,))});- let router = machine+ let router = refuse_cross_origin_browser_requests(machine.merge(admin), auth_enabled).merge(healthz_router())- .merge(admin).merge(public_auth_router(auth_state.clone())).merge(session_auth_router(auth_state.clone())).merge(internal_auth_router(auth_state.clone()))@@ -2931,6 +2930,66 @@ async fn require_allowed_host((StatusCode::FORBIDDEN, "forbidden host\n").into_response()}+/// Refuse browser requests from a foreign page on a server with no credential+/// gate.+///+/// With auth off, the machine and admin routes trust whoever can reach the+/// port, and a page in the operator's browser can reach `127.0.0.1`. Browsers+/// send `GET` and body-less `POST` cross-origin without a preflight, and the+/// JSON extractors cannot refuse a request that has no body: `GET /handoff`+/// claims the single-use baton, `POST /admin/profile/rebuild` runs a profile+/// pass. Hooks, the CLI and MCP clients never send `Origin` or+/// `Sec-Fetch-Site`; the built-in web UI's same-origin calls into `/admin`+/// pass. With auth on, the bearer token (which a foreign page cannot attach)+/// and the session CSRF token are the gate instead.+fn refuse_cross_origin_browser_requests(router: axum::Router, auth_enabled: bool) -> axum::Router {+ if auth_enabled {+ return router;+ }+ router.layer(axum::middleware::from_fn(require_same_origin_browser))+}++async fn require_same_origin_browser(req: Request<Body>, next: Next) -> Response {+ if !is_cross_origin_browser_request(req.headers()) {+ return next.run(req).await;+ }+ tracing::warn!(+ path = req.uri().path(),+ origin = ?req.headers().get(header::ORIGIN),+ "rejected cross-origin browser request to an unauthenticated server"+ );+ (+ StatusCode::FORBIDDEN,+ "cross-origin browser request refused\n",+ )+ .into_response()+}++/// Whether a browser marks this request as issued by a page of another origin.+///+/// `Sec-Fetch-Site` is computed by the browser and decides when present. A+/// browser too old to send it still sends `Origin` on every cross-origin+/// `POST`, so that falls back to comparing `Origin` with `Host`; `Origin: null`+/// (sandboxed frames, some redirects) never matches.+fn is_cross_origin_browser_request(headers: &axum::http::HeaderMap) -> bool {+ if let Some(site) = headers.get("sec-fetch-site") {+ return !matches!(site.to_str(), Ok("same-origin" | "none"));+ }+ let Some(origin) = headers.get(header::ORIGIN) else {+ return false;+ };+ let host = headers.get(header::HOST).and_then(|h| h.to_str().ok());+ let authority = origin+ .to_str()+ .ok()+ .and_then(|origin| origin.split_once("://"))+ .map(|(_, authority)| authority);+ match (authority, host) {+ (Some(authority), Some(host)) => !authority.eq_ignore_ascii_case(host),+ _ => true,+ }+}+fn host_allowed(host: &str, allowed_hosts: &[String]) -> bool {allowed_hosts.iter().any(|allowed| {host.eq_ignore_ascii_case(allowed) || host_without_port(host).eq_ignore_ascii_case(allowed)@@ -4872,6 +4931,95 @@ mod tests {assert_eq!(host_without_port("[::1]:49374"), "::1");}+ fn browser_headers(pairs: &[(&'static str, &'static str)]) -> axum::http::HeaderMap {+ let mut headers = axum::http::HeaderMap::new();+ headers.insert(header::HOST, "127.0.0.1:49374".parse().unwrap());+ for (name, value) in pairs {+ headers.insert(*name, value.parse().unwrap());+ }+ headers+ }++ #[test]+ fn cross_origin_browser_request_detection() {+ for (pairs, cross) in [+ // Hooks, the CLI and MCP clients send neither header.+ (&[][..], false),+ (&[("sec-fetch-mode", "cors")][..], false),+ (&[("sec-fetch-site", "same-origin")][..], false),+ (&[("sec-fetch-site", "none")][..], false),+ (&[("origin", "http://127.0.0.1:49374")][..], false),+ (+ &[+ ("origin", "http://127.0.0.1:49374"),+ ("sec-fetch-site", "same-origin"),+ ][..],+ false,+ ),+ (&[("sec-fetch-site", "cross-site")][..], true),+ (&[("sec-fetch-site", "same-site")][..], true),+ (&[("origin", "https://evil.example")][..], true),+ (&[("origin", "http://localhost:49374")][..], true),+ (&[("origin", "null")][..], true),+ // Fetch metadata decides over a forged-looking Origin and vice versa.+ (+ &[+ ("origin", "http://127.0.0.1:49374"),+ ("sec-fetch-site", "cross-site"),+ ][..],+ true,+ ),+ ] {+ assert_eq!(+ is_cross_origin_browser_request(&browser_headers(pairs)),+ cross,+ "{pairs:?}"+ );+ }+ }++ #[tokio::test]+ async fn cross_origin_browser_guard_applies_only_without_auth() {+ use tower::ServiceExt as _;++ let send = |auth_enabled: bool, pairs: &'static [(&'static str, &'static str)]| async move {+ let app = refuse_cross_origin_browser_requests(+ axum::Router::new().route("/handoff", axum::routing::get(|| async { "claimed" })),+ auth_enabled,+ );+ let mut request = Request::builder()+ .uri("/handoff")+ .header(header::HOST, "127.0.0.1:49374");+ for (name, value) in pairs {+ request = request.header(*name, *value);+ }+ app.oneshot(request.body(Body::empty()).unwrap())+ .await+ .unwrap()+ .status()+ };+ // Attack: an `<img src>` from a foreign page, auth off.+ assert_eq!(+ send(false, &[("sec-fetch-site", "cross-site")]).await,+ StatusCode::FORBIDDEN+ );+ assert_eq!(+ send(false, &[("origin", "https://evil.example")]).await,+ StatusCode::FORBIDDEN+ );+ // Controls: a hook-style request, and the web UI's same-origin call.+ assert_eq!(send(false, &[]).await, StatusCode::OK);+ assert_eq!(+ send(false, &[("sec-fetch-site", "same-origin")]).await,+ StatusCode::OK+ );+ // With auth on, the bearer gate decides; this layer stays out of it.+ assert_eq!(+ send(true, &[("sec-fetch-site", "cross-site")]).await,+ StatusCode::OK+ );+ }+#[test]fn web_ui_dir_requires_enable_web() {let ui = TempDir::new().unwrap();diff --git a/crates/ai-memory-cli/tests/suite/cross_origin_e2e.rs b/crates/ai-memory-cli/tests/suite/cross_origin_e2e.rsnew file mode 100644index 00000000..02e5352d--- /dev/null+++ b/crates/ai-memory-cli/tests/suite/cross_origin_e2e.rs@@ -0,0 +1,605 @@+//! Adversarial end-to-end test: a web page the operator visits must not be+//! able to write to an unauthenticated loopback `ai-memory serve`.+//!+//! The documented default is `127.0.0.1:49374` with no bearer token. Any page+//! in the operator's browser can address that origin. Browsers send "simple"+//! requests cross-origin without a CORS preflight: `GET`, and `POST` whose+//! `Content-Type` is `text/plain`, `application/x-www-form-urlencoded` or+//! `multipart/form-data`, carrying no custom headers. The page cannot read the+//! response, but the server still acts on the request. DNS rebinding goes+//! further: the attacker's own hostname resolves to `127.0.0.1`, so the+//! request is same-origin for the browser and only the `Host` header gives it+//! away.+//!+//! Three server guards stand in the way (`docs/security-boundaries.md` row+//! 12c):+//!+//! 1. The browser-origin guard on the machine and admin routes+//! (`serve::refuse_cross_origin_browser_requests`) refuses a request a+//! browser marks as cross-site (`Sec-Fetch-Site`) or, from a browser too old+//! to send fetch metadata, one whose `Origin` names another host. Hooks,+//! the CLI and MCP clients never send either header.+//! 2. JSON-only extractors (axum `Json`, rmcp's `Content-Type` check) refuse+//! the three simple content types with 415, so a preflight-free `POST`+//! cannot deliver a body even past guard 1.+//! 3. The `Host` allowlist refuses a rebinding hostname with 403.+//!+//! Every attack carries a canary string. After the attacks the test scans the+//! whole data directory, SQLite and wiki alike, for it, and checks that the+//! destructive targets (a page, a project, a single-use handoff) survived. The+//! legitimate controls send the same requests the way hooks, the CLI and MCP+//! clients do (`application/json`, no `Origin`) and must succeed.++/// Spawns a server: seconds, not milliseconds, so the slow tier.+mod slow {+ use std::fs;+ use std::path::{Path, PathBuf};+ use std::time::{Duration, Instant};++ use reqwest::{Method, RequestBuilder, StatusCode};+ use serde_json::{Value, json};++ use crate::e2e_support::{hermetic, session_count, start_serve};++ const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");+ const WORKSPACE: &str = "a0-ws";+ /// The operator's real project: the target of the destructive attacks.+ const PROJECT: &str = "a0-project";+ /// A project only an attack would create.+ const EVIL_PROJECT: &str = "a0-evil-project";+ const PAGE: &str = "notes/keep-me.md";+ const CANARY: &str = "a0-cross-origin-canary-7f3c";+ const CONTROL: &str = "a0-legit-control-marker-91be";+ const HANDOFF_SUMMARY: &str = "a0 handoff baton that must survive";+ const EVIL_ORIGIN: &str = "https://evil.example";++ /// What a browser attaches to a request a foreign page issues. Real+ /// browsers send both headers; the single-header shapes cover a browser+ /// without fetch metadata (Origin only) and a no-cors `GET` such as an+ /// `<img>` tag, which carries fetch metadata but no `Origin`.+ #[derive(Clone, Copy, Debug)]+ enum Browser {+ Modern,+ LegacyOriginOnly,+ FetchMetadataOnly,+ }++ const BROWSERS: [Browser; 3] = [+ Browser::Modern,+ Browser::LegacyOriginOnly,+ Browser::FetchMetadataOnly,+ ];++ fn from_evil_page(req: RequestBuilder, browser: Browser) -> RequestBuilder {+ match browser {+ Browser::Modern => req+ .header("origin", EVIL_ORIGIN)+ .header("sec-fetch-site", "cross-site")+ .header("sec-fetch-mode", "no-cors"),+ Browser::LegacyOriginOnly => req.header("origin", EVIL_ORIGIN),+ Browser::FetchMetadataOnly => req+ .header("sec-fetch-site", "cross-site")+ .header("sec-fetch-mode", "no-cors"),+ }+ }++ /// The three content types a browser sends cross-origin without a preflight.+ const SIMPLE_CONTENT_TYPES: [&str; 3] = [+ "text/plain;charset=UTF-8",+ "application/x-www-form-urlencoded",+ "multipart/form-data; boundary=----a0boundary",+ ];++ fn tree_contains(root: &Path, needle: &str) -> Option<PathBuf> {+ let mut pending = vec![root.to_path_buf()];+ while let Some(dir) = pending.pop() {+ let Ok(entries) = fs::read_dir(&dir) else {+ continue;+ };+ for entry in entries.flatten() {+ let path = entry.path();+ if path.is_dir() {+ pending.push(path);+ } else if let Ok(bytes) = fs::read(&path)+ && bytes+ .windows(needle.len())+ .any(|window| window == needle.as_bytes())+ {+ return Some(path);+ }+ }+ }+ None+ }++ fn mcp_call(name: &str, arguments: Value) -> String {+ json!({+ "jsonrpc": "2.0",+ "id": 1,+ "method": "tools/call",+ "params": { "name": name, "arguments": arguments },+ })+ .to_string()+ }++ /// A legitimate MCP client call: JSON in, both accept types, no Origin.+ async fn legit_mcp(client: &reqwest::Client, base: &str, name: &str, args: Value) -> Value {+ let text = client+ .post(format!("{base}/mcp"))+ .header("content-type", "application/json")+ .header("accept", "application/json, text/event-stream")+ .body(mcp_call(name, args))+ .send()+ .await+ .expect("mcp request")+ .text()+ .await+ .expect("mcp body");+ let reply: Value = serde_json::from_str(&text)+ .unwrap_or_else(|e| panic!("non-JSON mcp reply {text}: {e}"));+ assert!(+ reply.get("error").is_none() && reply.pointer("/result/isError") != Some(&json!(true)),+ "legitimate MCP call {name} must succeed: {text}"+ );+ reply+ }++ /// One write a foreign page would attempt, with a JSON body that would do+ /// damage if the server parsed it.+ struct Attack {+ label: &'static str,+ method: Method,+ path: String,+ body: String,+ /// rmcp answers 406 before 415 without this; a page can send it, since+ /// `Accept` is a CORS-safelisted header.+ mcp_accept: bool,+ }++ fn write_attacks() -> Vec<Attack> {+ let post = |label, path: &str, body: Value| Attack {+ label,+ method: Method::POST,+ path: path.to_string(),+ body: body.to_string(),+ mcp_accept: false,+ };+ let hook_query = format!(+ "event=user-prompt-submit&agent=claude-code&workspace={WORKSPACE}&project={EVIL_PROJECT}"+ );+ let mut attacks = vec![+ post(+ "hook",+ &format!("/hook?{hook_query}"),+ json!({+ "session_id": "00000000-0000-4000-8000-00000000a0a0",+ "cwd": "/tmp/a0-evil",+ "prompt": CANARY,+ }),+ ),+ post(+ "hook batch",+ "/hook/batch",+ json!([{+ "url": format!("http://h/hook?{hook_query}"),+ "body": {+ "session_id": "00000000-0000-4000-8000-00000000a0a1",+ "cwd": "/tmp/a0-evil",+ "prompt": CANARY,+ },+ }]),+ ),+ post(+ "admin write-page",+ "/admin/write-page",+ json!({+ "workspace": WORKSPACE,+ "project": EVIL_PROJECT,+ "path": "notes/evil.md",+ "body": CANARY,+ }),+ ),+ post(+ "admin delete-page",+ "/admin/delete-page",+ json!({ "workspace": WORKSPACE, "project": PROJECT, "path": PAGE }),+ ),+ post(+ "admin purge-project",+ "/admin/purge-project",+ json!({ "workspace": WORKSPACE, "project": PROJECT, "confirm": true, "force": true }),+ ),+ post(+ "admin messages send",+ "/admin/messages/send",+ json!({+ "from_workspace": WORKSPACE,+ "from_project": PROJECT,+ "to_workspace": WORKSPACE,+ "to_project": PROJECT,+ "body": CANARY,+ }),+ ),+ post(+ "workstream prepare run",+ "/workstream/runs",+ json!({ "workspace": WORKSPACE, "project": EVIL_PROJECT, "note": CANARY }),+ ),+ ];+ for (label, name, args) in [+ (+ "mcp write_page",+ "memory_write_page",+ json!({+ "workspace": WORKSPACE,+ "project": EVIL_PROJECT,+ "path": "notes/evil-mcp.md",+ "body": CANARY,+ }),+ ),+ (+ "mcp delete_page",+ "memory_delete_page",+ json!({ "workspace": WORKSPACE, "project": PROJECT, "path": PAGE }),+ ),+ (+ "mcp message_send",+ "memory_message_send",+ json!({+ "workspace": WORKSPACE,+ "project": PROJECT,+ "to_workspace": WORKSPACE,+ "to_project": PROJECT,+ "body": CANARY,+ }),+ ),+ (+ "mcp handoff_accept",+ "memory_handoff_accept",+ json!({ "workspace": WORKSPACE, "project": PROJECT }),+ ),+ ] {+ attacks.push(Attack {+ label,+ method: Method::POST,+ path: "/mcp".to_string(),+ body: mcp_call(name, args),+ mcp_accept: true,+ });+ }+ attacks+ }++ /// Requests a page can issue with no body at all, so the JSON extractors+ /// never see them: only the browser-origin guard stops these.+ fn bodyless_attacks() -> Vec<Attack> {+ let bare = |label, method, path: String| Attack {+ label,+ method,+ path,+ body: String::new(),+ mcp_accept: false,+ };+ vec![+ // The session-start handoff fetch CLAIMS the single-use baton and+ // publishes the caller's active project: a state change on GET,+ // reachable from an `<img src>`.+ bare(+ "handoff claim",+ Method::GET,+ format!("/handoff?agent=claude-code&workspace={WORKSPACE}&project={PROJECT}"),+ ),+ bare(+ "admin profile rebuild",+ Method::POST,+ "/admin/profile/rebuild".to_string(),+ ),+ bare("admin backup", Method::POST, "/admin/backup".to_string()),+ ]+ }++ fn serve(data_dir: &Path, home: &Path, port: u16) -> std::process::Command {+ let mut cmd = hermetic(BIN);+ cmd.env("HOME", home)+ .env("USERPROFILE", home)+ .env("AI_MEMORY_HOME", home)+ .env("AI_MEMORY_DATA_DIR", data_dir)+ .env("AI_MEMORY_EMBEDDING_PROVIDER", "none")+ .env("AI_MEMORY_BACKFILL_ON_START", "false")+ .env("AI_MEMORY_AUTO_IMPROVE__SCHEDULER__ENABLED", "false")+ .current_dir(home)+ .args(["serve", "--transport", "http", "--bind"])+ .arg(format!("127.0.0.1:{port}"))+ .arg("--no-watcher");+ cmd+ }++ async fn open_handoffs(client: &reqwest::Client, base: &str) -> usize {+ let body: Value = client+ .get(format!("{base}/admin/handoffs"))+ .query(&[("workspace", WORKSPACE), ("project", PROJECT)])+ .send()+ .await+ .expect("handoffs request")+ .json()+ .await+ .expect("handoffs json");+ body["handoffs"].as_array().map_or(0, Vec::len)+ }++ async fn page_exists(client: &reqwest::Client, base: &str) -> bool {+ client+ .get(format!("{base}/admin/read-page"))+ .query(&[+ ("workspace", WORKSPACE),+ ("project", PROJECT),+ ("path", PAGE),+ ])+ .send()+ .await+ .expect("read-page request")+ .status()+ .is_success()+ }++ #[tokio::test]+ async fn browser_cross_origin_requests_cannot_write_to_an_unauthenticated_loopback_server() {+ let root = tempfile::tempdir().expect("tempdir");+ let data_dir = root.path().join("data");+ let home = root.path().join("home");+ fs::create_dir_all(&home).expect("home");+ let client = reqwest::Client::new();+ // The log lives outside the data dir so the canary scan sees only+ // what the server persisted.+ let (_server, base) = start_serve(&client, &root.path().join("serve.log"), |port| {+ serve(&data_dir, &home, port)+ })+ .await;+ let port = base.rsplit(':').next().expect("port").to_string();++ // Legitimate controls first: an MCP client (JSON, no Origin) writes the+ // page and the handoff the attacks will go after.+ legit_mcp(+ &client,+ &base,+ "memory_write_page",+ json!({+ "workspace": WORKSPACE,+ "project": PROJECT,+ "path": PAGE,+ "body": format!("Keep this page. {CONTROL}"),+ }),+ )+ .await;+ legit_mcp(+ &client,+ &base,+ "memory_handoff_begin",+ json!({+ "workspace": WORKSPACE,+ "project": PROJECT,+ "summary": HANDOFF_SUMMARY,+ "shared": true,+ }),+ )+ .await;+ assert!(page_exists(&client, &base).await, "control page must exist");+ assert_eq!(+ open_handoffs(&client, &base).await,+ 1,+ "control handoff must be open"+ );++ // Preflight-free simple requests carrying a damaging JSON body.+ for attack in write_attacks() {+ for content_type in SIMPLE_CONTENT_TYPES {+ for browser in BROWSERS {+ let mut req = client+ .request(attack.method.clone(), format!("{base}{}", attack.path))+ .header("content-type", content_type)+ .body(attack.body.clone());+ if attack.mcp_accept {+ req = req.header("accept", "application/json, text/event-stream");+ }+ let status = from_evil_page(req, browser)+ .send()+ .await+ .expect("attack request")+ .status();+ assert!(+ status.is_client_error(),+ "{} as {content_type} from {browser:?} must be refused, got {status}",+ attack.label+ );+ }+ }+ }++ // Bodyless requests: no content type for a JSON extractor to refuse.+ for attack in bodyless_attacks() {+ for browser in BROWSERS {+ let req = client.request(attack.method.clone(), format!("{base}{}", attack.path));+ let resp = from_evil_page(req, browser)+ .send()+ .await+ .expect("attack request");+ assert_eq!(+ resp.status(),+ StatusCode::FORBIDDEN,+ "{} from {browser:?} must be refused by the browser-origin guard",+ attack.label+ );+ }+ }++ // DNS rebinding: same-origin for the browser, foreign Host for us. The+ // request is a plain JSON one, so only the Host allowlist stops it.+ let rebound_host = format!("rebind.evil.example:{port}");+ for (path, body) in [+ (+ format!("/hook?event=session-start&agent=claude-code&workspace={WORKSPACE}&project={EVIL_PROJECT}"),+ json!({ "session_id": "00000000-0000-4000-8000-00000000a0a2", "cwd": "/tmp/a0", "prompt": CANARY }).to_string(),+ ),+ (+ "/admin/write-page".to_string(),+ json!({ "workspace": WORKSPACE, "project": EVIL_PROJECT, "path": "notes/rebind.md", "body": CANARY }).to_string(),+ ),+ (+ "/mcp".to_string(),+ mcp_call("memory_write_page", json!({ "workspace": WORKSPACE, "project": EVIL_PROJECT, "path": "notes/rebind-mcp.md", "body": CANARY })),+ ),+ ] {+ let status = client+ .post(format!("{base}{path}"))+ .header("host", &rebound_host)+ .header("content-type", "application/json")+ .header("accept", "application/json, text/event-stream")+ .body(body)+ .send()+ .await+ .expect("rebinding request")+ .status();+ assert_eq!(status, StatusCode::FORBIDDEN, "rebound Host on {path} must be refused");+ }+ let status = client+ .get(format!(+ "{base}/handoff?agent=claude-code&workspace={WORKSPACE}&project={PROJECT}"+ ))+ .header("host", &rebound_host)+ .send()+ .await+ .expect("rebinding handoff request")+ .status();+ assert_eq!(+ status,+ StatusCode::FORBIDDEN,+ "rebound Host on /handoff must be refused"+ );++ // A preflight must never come back permissive, so a non-simple request+ // (application/json, custom headers, DELETE) stays blocked in the browser.+ for path in [+ "/hook",+ "/mcp",+ "/admin/write-page",+ "/handoff",+ "/workstream/runs",+ ] {+ let resp = client+ .request(Method::OPTIONS, format!("{base}{path}"))+ .header("origin", EVIL_ORIGIN)+ .header("sec-fetch-site", "cross-site")+ .header("sec-fetch-mode", "cors")+ .header("access-control-request-method", "POST")+ .header("access-control-request-headers", "content-type")+ .send()+ .await+ .expect("preflight request");+ for name in [+ "access-control-allow-origin",+ "access-control-allow-credentials",+ "access-control-allow-headers",+ "access-control-allow-methods",+ ] {+ assert!(+ resp.headers().get(name).is_none(),+ "preflight to {path} must not answer {name}: {:?}",+ resp.headers().get(name)+ );+ }+ }++ // Legitimate hook: JSON, no Origin, accepted. Waiting for it to commit+ // also gives any wrongly accepted attack hook time to land.+ let status = client+ .post(format!("{base}/hook"))+ .query(&[+ ("event", "session-start"),+ ("agent", "claude-code"),+ ("workspace", WORKSPACE),+ ("project", PROJECT),+ ])+ .header("content-type", "application/json")+ .body(+ json!({+ "session_id": "00000000-0000-4000-8000-00000000a0ff",+ "cwd": "/tmp/a0-project",+ })+ .to_string(),+ )+ .send()+ .await+ .expect("legit hook")+ .status();+ assert_eq!(+ status,+ StatusCode::ACCEPTED,+ "legitimate hook must be accepted"+ );+ let deadline = Instant::now() + Duration::from_secs(10);+ while session_count(&client, &base, WORKSPACE, PROJECT).await == 0 {+ assert!(Instant::now() < deadline, "legitimate hook never committed");+ tokio::time::sleep(Duration::from_millis(50)).await;+ }++ // Nothing an attack carried was persisted, and every target survived.+ assert_eq!(+ session_count(&client, &base, WORKSPACE, EVIL_PROJECT).await,+ 0,+ "no attack hook may create a session"+ );+ assert!(+ page_exists(&client, &base).await,+ "the page must survive the delete/purge attacks"+ );+ assert_eq!(+ open_handoffs(&client, &base).await,+ 1,+ "the cross-origin handoff fetch must not claim the baton"+ );+ let projects = client+ .get(format!("{base}/admin/projects"))+ .send()+ .await+ .expect("projects request")+ .text()+ .await+ .expect("projects body");+ assert!(+ !projects.contains(EVIL_PROJECT),+ "no attack may create a project: {projects}"+ );+ assert!(+ tree_contains(&data_dir, CONTROL).is_some(),+ "the scan must see legitimately written content"+ );+ assert_eq!(+ tree_contains(&data_dir, CANARY),+ None,+ "an attack payload reached the store"+ );++ // The hook-style handoff fetch (no Origin) still claims the baton.+ let handoff = client+ .get(format!("{base}/handoff"))+ .query(&[+ ("agent", "claude-code"),+ ("workspace", WORKSPACE),+ ("project", PROJECT),+ ])+ .send()+ .await+ .expect("legit handoff fetch");+ assert_eq!(handoff.status(), StatusCode::OK);+ let text = handoff.text().await.expect("handoff body");+ assert!(+ text.contains(HANDOFF_SUMMARY),+ "legitimate fetch must receive the baton: {text}"+ );+ assert_eq!(+ open_handoffs(&client, &base).await,+ 0,+ "legitimate fetch claims it"+ );+ }+}diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rsindex 626b280d..56d655d3 100644--- a/crates/ai-memory-cli/tests/suite/main.rs+++ b/crates/ai-memory-cli/tests/suite/main.rs@@ -12,6 +12,7 @@ mod backfill_failures;mod backfill_member;mod backup_agents;mod completions;+mod cross_origin_e2e;mod doctor_e2e;mod e2e_support;mod external_capture;diff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex 4cf48719..471e0a3f 100644--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@ -75,6 +75,7 @@ means the boundary is not built yet.| 11i | OpenCode executable probing and generated-config cleanup (#1097) | `ai-memory-cli/src/config.rs` captures the full process environment once in typed `RuntimeEnv`; `run.rs` overlays explicit `run --env` values with platform key semantics and uses that same effective environment for executable resolution, probing, and launch without rereading process environment; `opencode_dialect.rs` invokes only direct argv `--version`, caps output and time, and rejects malformed/future majors. Automatic launch skips only OpenCode on probe failure; explicit OpenCode fails. `install_hooks.rs` quarantines the incompatible sibling before reading its exact ownership markers, restores unowned content with an atomic no-replace link, and never deletes or overwrites a concurrent replacement; `install_mcp.rs` owns the shared dual-schema endpoint/token inference used by both installers, rejects conflicting generated V1/V2 entries before mutation, and migrates an alternate-major entry only when endpoint, generated shape, and absent-or-Authorization-only headers match, preserving custom headers and ambiguous entries; auto-wire migrates MCP before hooks infer from that exact config path and skips hook mutation when transition preflight fails | `run.rs` `automatic_opencode_probe_failure_keeps_another_harness_available`, `opencode_executable_probe_selects_v2_for_launch_and_autowire`, effective-child-environment PATH casing controls, fake V1/V2/env-derived-major/malformed/future/timeout executable tests, `setup-agent` V1/V2 no-probe integration controls; `install_hooks.rs` `opencode_plugin_cleanup_removes_only_exact_owned_incompatible_sibling`, `opencode_plugin_cleanup_preserves_a_concurrent_user_replacement`, `opencode_plugin_restore_moves_quarantine_when_destination_is_free`, `opencode_plugin_restore_preserves_an_existing_replacement`, `opencode_plugin_restore_does_not_replace_a_file_created_at_restore_window`, `opencode_plugin_cleanup_does_not_escape_custom_target_directory`; `install_mcp.rs` `opencode_install_reuses_owned_endpoint_and_token_across_major_transitions`, `opencode_install_refuses_conflicting_owned_transition_entries`, `opencode_install_does_not_infer_from_a_user_owned_transition_entry`, `opencode_mcp_migration_moves_only_owned_alternate_shape`, `opencode_mcp_migration_preserves_ambiguous_user_entry`, `opencode_mcp_migration_preserves_custom_headers`, `opencode_mcp_migration_accepts_only_historical_generated_headers` | STRONG for Unix process probes and platform-neutral ownership gates; exact Windows executable resolution remains covered by `run.rs` resolver tests and requires the PR `windows` label || 12 | Network/auth posture | `config.rs` loopback `DEFAULT_BIND`; `serve.rs` `validate_http_exposure`, `require_allowed_host`; `auth.rs` `require_bearer`; `serve.rs` mounts `/identity` inside the host and machine-auth gates even with web disabled; `nix/nixos-module.nix` treats only literal `127.0.0.1`/`::1` as loopback, requires exactly one secret environment source for other binds, keeps secret auth and `llm_headers` values out of the Nix store, and brackets IPv6 in `ExecStart` | `serve.rs` host-guard (missing→400 / forged→403), non-loopback-requires-token; `auth.rs` wrong-token 401; `serve.rs` `machine_identity_remains_authenticated_with_web_disabled_and_expired_keys` (wrong and expired keys refused, valid machine key accepted, web remains disabled); `flake.nix` `nixos-module-eval` refuses `localhost` and `0.0.0.0` without a secret source, refuses two secret sources, `settings.auth` secrets, and `settings.llm_headers`, with literal IPv4/IPv6 loopback and one-source controls | STRONG || 12b | Managed-workstream provenance privacy, stable replay identity and server event namespace | `ai-memory-core/src/workstream.rs` `scrub_workstream_provenance`; `ai-memory-hooks/src/workstream.rs` `finish_run` reserved client-id check and `sanitize_events` before immutable segments, with `sanitize_events` and `append_boundary_events` scrubbing event/checkpoint/losses content *before* the 64 KiB cap so a cap-straddling secret is never cut into an unmatchable prefix (#1113); `ai-memory-store/src/workstream.rs` `finish_run` stable identity check and `stored_event`, scrubbing content with the explicitly supplied configured sanitizer before its 16 KiB store-boundary bound; no changes to the typed observation boundary | `store/tests/suite/workstream_provenance.rs` `workstream_provenance_is_bounded_scrubbed_and_idempotent_before_storage` (hostile and oversized source ids, unknown metadata, legitimate correlation, retry and distinct event ids), `workstream_provenance_legacy_reads_strip_secrets_and_unknown_metadata` (search and tail, malformed/oversized legacy JSON), `workstream_provenance_reused_event_id_retains_data_and_refuses_identity_change` (first indexed version retained, identity refusal and batch rollback), `workstream_provenance_replay_survives_sanitizer_changes` (new extra patterns and removal of a built-in-pattern allowlist exemption, with identity-refusal control), `workstream_provenance_replay_preserves_legacy_rows_above_bounds`, `workstream_provenance_finished_run_ignores_late_events`, `workstream_provenance_retry_with_marker_matching_pattern_is_idempotent`, `workstream_content_is_scrubbed_before_the_store_cap` (a credential straddling the 16 KiB store cap is redacted, not persisted as an unmatchable prefix; benign control verbatim; #1113); `hooks` `workstream_provenance_finish_scrubs_before_raw_segment_and_search` (custom sanitizer, immutable segment, SQL recovery and finished-run no-op), `workstream_provenance_client_cannot_use_server_event_ids` (reserved ids refused before raw writes, normal client event and server-generated checkpoint/loss controls), `finish_run_scrubs_event_content_before_the_byte_cap` and `finish_run_scrubs_boundary_events_before_the_byte_cap` (a credential straddling the 64 KiB event cap is redacted in the raw segment and never reaches the index; checkpoint/losses and benign controls; #1113); `cli/tests/suite/workstream_provenance.rs` (text/JSON and old server DTOs) | STRONG: privacy, stable-identity and reserved-namespace regressions fail with their guards bypassed; an isolated allowlist bypass fails on the unknown key, and bypassing the raw-id search authorization returns 200 where its test requires 403. No provenance is emitted in startup packets, and old raw segments are not rewritten |+| 12c | Browser cross-origin writes refused on an unauthenticated loopback server (a foreign page in the operator's browser, and DNS rebinding) | `serve.rs` `refuse_cross_origin_browser_requests` / `is_cross_origin_browser_request`, layered on the machine (`/mcp`, `/hook*`, `/handoff`, `/workstream/*`, `/identity`) and admin (`/admin/*`) routes when auth is off: `Sec-Fetch-Site` other than `same-origin`/`none` → 403, or, without fetch metadata, an `Origin` whose authority is not the `Host` → 403. Hooks, the CLI and MCP clients send neither header; the web UI's same-origin `/admin/pending-writes` calls pass. Behind it, axum `Json` and rmcp's `Content-Type: application/json` check refuse the preflight-free content types (`text/plain`, form-urlencoded, multipart) with 415; `require_allowed_host` (and rmcp `allowed_hosts` on `/mcp`) refuses a rebinding `Host`; machine/admin routes carry no CORS layer, so a preflight never comes back permissive | `cli/tests/suite/cross_origin_e2e.rs` `browser_cross_origin_requests_cannot_write_to_an_unauthenticated_loopback_server`, against the shipped `serve`: every write route (`/hook`, `/hook/batch`, MCP `memory_write_page`/`memory_delete_page`/`memory_message_send`/`memory_handoff_accept`, `/admin/write-page`/`delete-page`/`purge-project`/`messages/send`, `/workstream/runs`) × each simple content type × modern, Origin-only and fetch-metadata-only browser shapes → 4xx; body-less `GET /handoff` (which claims the single-use baton), `POST /admin/profile/rebuild` and `POST /admin/backup` → 403; a rebinding `Host` → 403; preflights answer no `Access-Control-Allow-*`. Then a canary scan of the whole data dir, page/project/handoff survival, and legitimate controls (JSON MCP calls, a JSON hook → 202, the hook-style `/handoff` fetch still claims the baton). `serve.rs` `cross_origin_browser_request_detection` (header table) and `cross_origin_browser_guard_applies_only_without_auth` | STRONG: with the guard removed, the cross-origin `GET /handoff` returns 200 and consumes the baton (the pre-fix behaviour); with the guard removed and `/hook` reading a raw body, a `text/plain` hook returns 202; with the host layer removed, a rebound `/hook` returns 202; a permissive `CorsLayer` fails the preflight assertion. With auth on, the bearer token and the session CSRF token are the gate and this layer is a no-op || 13 | Managed-run transcript attribution (concurrent launches in one checkout, invariant #16) | `ai-memory-store/src/workstream.rs` `link_native_session` stamps `native_session_linked_at` on its own run only, both `finish` updates drop the stamp when the session changes, `run_status` reports it; `ai-memory-cli/src/commands/run.rs` `resolve_native_session_after_run` takes a linked session only when `ai-memory-workstream` `native_session_in_checkout` holds it for this checkout (OpenCode by recorded directory), never falls back to a link it set aside, and turns an `AmbiguousNativeSession` into a warning with nothing imported; for Claude it moves the run's own session to a background session only when `ai-memory-workstream` `claude_continued_session` finds a `sessionKind: "bg"` record that names that exact session as its attached client and this checkout as `cwd` (#1050), and to a `/clear`ed session only when that session's own transcript records the `/clear` command and names the run's session as its launch `session_id` from this checkout or below it; `ai-memory-workstream/src/transcript.rs` `discover_crush` claims only the one top-level session created (or, with `--continue`, touched) during the run, and in a data directory outside the project only one that edited a file in it | `multi_session.rs` `a_session_linked_by_one_managed_run_is_not_another_runs`; `run.rs` `a_session_linked_during_the_run_wins_over_discovery` (concurrent newer session, another checkout's link refused, no fallback to it, unlinked control); `transcript.rs` `claude_continued_session_follows_only_this_checkouts_bg_records` (a concurrent launch's background session, another checkout's, and a non-background record refused; own attachment control); `transcript.rs` `claude_continued_session_follows_only_this_launchs_clear` (an uncleared transcript, a quoted `/clear`, another checkout's clear, a concurrent launch's clear, a subagent file, and a pre-run clear refused; own clear and the newest of two as controls); `run.rs` `a_claude_run_that_attached_to_a_background_session_follows_it`, `a_claude_run_that_was_cleared_follows_the_new_session`; `transcript.rs` `native_session_in_checkout_checks_the_opencode_directory`; `store/src/lib.rs` `managed_run_status_reports_a_link_made_during_the_run`; `run.rs` `ambiguous_crush_discovery_keeps_the_run`; `transcript.rs` `crush_discovery_claims_only_the_session_the_run_created`, `crush_discovery_in_a_shared_store_claims_only_an_edit_here` | PARTIAL: a run whose child links nothing still falls back to discovering the newest session in the checkout; Crush, which has no hooks, relies on that discovery alone and claims nothing when it is ambiguous || 13a | Managed Codex stale-daemon run recovery (#987; concurrent launches, project/worktree isolation, operator isolation) | `managed_runs.owner_user` (V71) uses the topology-aware `IdentityKey` owner stamp; `router.rs` resolves and authorizes the SessionStart repository before handling the untrusted run id; store `link_or_adopt_native_session` selects and links in one writer transaction and admits only the sole live, undelivered, never-linked Codex run in the exact workspace/project/checkout-cwd/owner bucket. An active supplied id must itself match every boundary; another harness, an ambiguity, or a second native session is refused | `multi_session.rs` `stale_codex_run_recovery_cannot_cross_project_owner_or_session_boundaries` (foreign owner, foreign checkout, and cross-project candidates refused; project-local controls and racing rebind), `stale_codex_run_recovery_fails_closed_on_ambiguity_and_active_mismatch` (two candidates and active foreign id refused, exact control); `workstream.rs` `prepare_stamps_the_operator_bucket_used_by_session_start_recovery` (HTTP actor stamp with foreign and legitimate controls); `router.rs` `managed_session_start_delivers_ledger_and_pending_handoff` supplies a finished stale id and proves the new run is linked and delivered | STRONG for SessionStart recovery. No candidate is selected from a run id's stale scope, another checkout, or another operator bucket; ambiguity deliberately falls back to no managed packet || 13b | Managed-run lease exclusivity and explicit recovery (one active run per workstream, invariant #16) | `ai-memory-store/src/workstream.rs` `prepare_run` expires lapsed leases and otherwise refuses an `active` run inside the writer transaction (`StoreError::WorkstreamBusy`). The explicit `force_unlock` path may expire and replace that row in the same transaction only when its durable `owner_user` equals the incoming actor's attribution; different operators remain isolated. `heartbeat` renews only `active` rows. `ai-memory-cli/src/commands/run.rs` normally uses `wait_out_held_lease`; `--force-unlock` instead asks once and fails immediately when refused. Neither path treats the informational `lease_owner` label as authority | `multi_session.rs` `force_unlock_replaces_only_the_same_operators_active_run` (foreign operator refused with live control, same operator succeeds, old run loses heartbeat, unattributed single-user control); hooks `workstream.rs` `force_unlock_cannot_evict_another_operator` exercises the HTTP actor boundary; CLI `run.rs` `force_unlock_asks_once_instead_of_waiting_out_the_lease`; existing controls `managed_workstream_batches_are_idempotent_and_release_the_lease`, `a_renewed_lease_is_reported_as_a_live_owner_not_taken_over`, `interactive_launch_waits_out_a_lapsing_lease_then_proceeds`, and `ctrl_c_aborts_the_held_lease_wait_immediately` | STRONG for exclusivity. The `lease_owner` label (`host:pid`) is informational only and not unique inside ai-jail (every jailed launcher reports `ai-sandbox:<ns-pid>`), so it must never become an ownership key. A shared unattributed/root context is intentionally one operator bucket; deployments needing person-level refusal must authenticate DB users |diff --git a/docs/security.md b/docs/security.mdindex 2b9ca03c..43488cb6 100644--- a/docs/security.md+++ b/docs/security.md@@ -55,6 +55,14 @@ HTTPS-only. Close or redirect direct HTTP access to that hostname.Non-loopback binds should also set `AI_MEMORY_ALLOWED_HOSTS` to guard againstDNS rebinding.+With no token (the loopback default), a web page open in your browser can+still address `127.0.0.1:49374`. The server refuses those requests on the+machine and admin routes: a request the browser marks as cross-site+(`Sec-Fetch-Site`), or one whose `Origin` is not the server's own host, gets a+403. Hooks, the CLI and MCP clients never send those headers. The `Host`+allowlist refuses DNS-rebinding hostnames, and the JSON-only routes refuse the+content types a browser can send without a CORS preflight.+Busy shared hook servers can also set `AI_MEMORY_HOOK_RATE_PER_SEC` (tokens persecond per actor/session source) and optionally `AI_MEMORY_HOOK_RATE_BURST` tobound one runaway session without blocking unrelated hook sources. Unset or `0`