diff --git a/app/src/mcp/mod.rs b/app/src/mcp/mod.rs index 4854cbc5..4eca5753 100644 --- a/app/src/mcp/mod.rs +++ b/app/src/mcp/mod.rs @@ -2241,13 +2241,21 @@ fn example_value(name: &str, kind: &str) -> serde_json::Value { type Service = StreamableHttpService; /// The MCP endpoint as a service: stateless, so any web process can answer any request. It answers only for our own -/// hosts (DNS rebinding protection): the app's host and localhost for development. +/// hosts (DNS rebinding protection): the MCP host (`VISUALCI_MCP_URL`, where clients connect), the other configured +/// hosts, the app's address and localhost for development. fn service() -> Service { static SERVICE: OnceLock = OnceLock::new(); SERVICE .get_or_init(|| { let mut hosts = vec!["localhost".to_string(), "127.0.0.1".to_string()]; - for url in [app_url(), laravel::config().app.url.clone()] { + let config = laravel::config(); + let configured = config.section::(); + let urls = [&configured.mcp, &configured.api, &configured.app, &configured.site] + .into_iter() + .flatten() + .cloned() + .chain([app_url(), config.app.url.clone()]); + for url in urls { if let Some(host) = url::Url::parse(&url).ok().and_then(|u| u.host_str().map(str::to_string)) { hosts.push(host); } diff --git a/app/tests/mcp_hosts.rs b/app/tests/mcp_hosts.rs new file mode 100644 index 00000000..548d44ea --- /dev/null +++ b/app/tests/mcp_hosts.rs @@ -0,0 +1,63 @@ +//! The MCP endpoint answers on the MCP host. rmcp refuses hosts it wasn't told about (DNS rebinding protection) with a +//! 403 "Host header is not allowed", so the host `VISUALCI_MCP_URL` names must be on its list. A file of its own: the +//! MCP service is built once per process, from the first test's settings. + +use app::models::{Organization, User}; +use laravel::db; +use laravel::model::create; +use laravel::testing::prelude::*; +use serde_json::json; + +fn hosts() -> Application { + app::testing::configure() + .with_envs([ + ("VISUALCI_SITE_URL", "https://visualci.test"), + ("VISUALCI_APP_URL", "https://app.visualci.test"), + ("VISUALCI_API_URL", "https://api.visualci.test"), + ("VISUALCI_MCP_URL", "https://mcp.visualci.test"), + ]) + .create() +} + +#[laravel::test(app = hosts)] +async fn mcp_answers_on_the_mcp_host(app: Application) -> laravel::Result<()> { + let email = format!("{}@example.com", laravel::str::uuid7()); + let user = create!(User { pid: laravel::str::uuid7(), name: "owner", email: email, staff: false }).await?; + let org = create!(Organization { pid: laravel::str::uuid7(), slug: "acme", name: "Acme" }).await?; + app::testing::fixtures::member(&org, &user, "admin").await?; + let token = format!("vcu_{}", laravel::str::uuid7().simple()); + let (hash, short, owner) = (app::models::token_hash(&token), token[..6].to_string(), user.id); + db::query!( + "INSERT INTO user_tokens (user_id, name, token_hash, token_prefix, created_at, updated_at) \ + VALUES ({owner}, 'agent', {hash}, {short}, now(), now())" + ) + .insert() + .await?; + + let initialize = json!({ + "jsonrpc": "2.0", "id": 1, "method": "initialize", + "params": { "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": { "name": "test", "version": "1" } } + }); + let response = app + .post("/mcp") + .header("host", "mcp.visualci.test") + .header("authorization", format!("Bearer {token}")) + // After the body: `json` sets `accept: application/json`, and MCP's Streamable HTTP wants both. + .json(initialize.clone()) + .header("accept", "application/json, text/event-stream") + .await; + response.assert_ok(); + let body = response.json().clone(); + assert!(body["result"]["serverInfo"].is_object(), "{body}"); + + // A host that isn't ours is still refused, by the host check before rmcp sees it. + let response = app + .post("/mcp") + .header("host", "evil.example.com") + .header("authorization", format!("Bearer {token}")) + .json(initialize) + .header("accept", "application/json, text/event-stream") + .await; + assert!(response.status().is_client_error(), "{}", response.status()); + Ok(()) +}