Skip to content

Commit a9485ea

Browse files
committed
app/: MCP answers on VISUALCI_MCP_URL's host: rmcp's allowed hosts are every configured host
Production MCP answered 403 "Host header is not allowed": rmcp's DNS-rebinding guard was given localhost and APP_URL, never the MCP host. tests/mcp_hosts.rs posts initialize with Host: mcp.visualci.test.
1 parent c6e6ca3 commit a9485ea

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎app/src/mcp/mod.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,13 +2241,21 @@ fn example_value(name: &str, kind: &str) -> serde_json::Value {
22412241
type Service = StreamableHttpService<VisualCi, LocalSessionManager>;
22422242

22432243
/// The MCP endpoint as a service: stateless, so any web process can answer any request. It answers only for our own
2244-
/// hosts (DNS rebinding protection): the app's host and localhost for development.
2244+
/// hosts (DNS rebinding protection): the MCP host (`VISUALCI_MCP_URL`, where clients connect), the other configured
2245+
/// hosts, the app's address and localhost for development.
22452246
fn service() -> Service {
22462247
static SERVICE: OnceLock<Service> = OnceLock::new();
22472248
SERVICE
22482249
.get_or_init(|| {
22492250
let mut hosts = vec!["localhost".to_string(), "127.0.0.1".to_string()];
2250-
for url in [app_url(), laravel::config().app.url.clone()] {
2251+
let config = laravel::config();
2252+
let configured = config.section::<crate::http::hosts::HostUrls>();
2253+
let urls = [&configured.mcp, &configured.api, &configured.app, &configured.site]
2254+
.into_iter()
2255+
.flatten()
2256+
.cloned()
2257+
.chain([app_url(), config.app.url.clone()]);
2258+
for url in urls {
22512259
if let Some(host) = url::Url::parse(&url).ok().and_then(|u| u.host_str().map(str::to_string)) {
22522260
hosts.push(host);
22532261
}

‎app/tests/mcp_hosts.rs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! The MCP endpoint answers on the MCP host. rmcp refuses hosts it wasn't told about (DNS rebinding protection) with a
2+
//! 403 "Host header is not allowed", so the host `VISUALCI_MCP_URL` names must be on its list. A file of its own: the
3+
//! MCP service is built once per process, from the first test's settings.
4+
5+
use app::models::{Organization, User};
6+
use laravel::db;
7+
use laravel::model::create;
8+
use laravel::testing::prelude::*;
9+
use serde_json::json;
10+
11+
fn hosts() -> Application {
12+
app::testing::configure()
13+
.with_envs([
14+
("VISUALCI_SITE_URL", "https://visualci.test"),
15+
("VISUALCI_APP_URL", "https://app.visualci.test"),
16+
("VISUALCI_API_URL", "https://api.visualci.test"),
17+
("VISUALCI_MCP_URL", "https://mcp.visualci.test"),
18+
])
19+
.create()
20+
}
21+
22+
#[laravel::test(app = hosts)]
23+
async fn mcp_answers_on_the_mcp_host(app: Application) -> laravel::Result<()> {
24+
let email = format!("{}@example.com", laravel::str::uuid7());
25+
let user = create!(User { pid: laravel::str::uuid7(), name: "owner", email: email, staff: false }).await?;
26+
let org = create!(Organization { pid: laravel::str::uuid7(), slug: "acme", name: "Acme" }).await?;
27+
app::testing::fixtures::member(&org, &user, "admin").await?;
28+
let token = format!("vcu_{}", laravel::str::uuid7().simple());
29+
let (hash, short, owner) = (app::models::token_hash(&token), token[..6].to_string(), user.id);
30+
db::query!(
31+
"INSERT INTO user_tokens (user_id, name, token_hash, token_prefix, created_at, updated_at) \
32+
VALUES ({owner}, 'agent', {hash}, {short}, now(), now())"
33+
)
34+
.insert()
35+
.await?;
36+
37+
let initialize = json!({
38+
"jsonrpc": "2.0", "id": 1, "method": "initialize",
39+
"params": { "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": { "name": "test", "version": "1" } }
40+
});
41+
let response = app
42+
.post("/mcp")
43+
.header("host", "mcp.visualci.test")
44+
.header("authorization", format!("Bearer {token}"))
45+
// After the body: `json` sets `accept: application/json`, and MCP's Streamable HTTP wants both.
46+
.json(initialize.clone())
47+
.header("accept", "application/json, text/event-stream")
48+
.await;
49+
response.assert_ok();
50+
let body = response.json().clone();
51+
assert!(body["result"]["serverInfo"].is_object(), "{body}");
52+
53+
// A host that isn't ours is still refused, by the host check before rmcp sees it.
54+
let response = app
55+
.post("/mcp")
56+
.header("host", "evil.example.com")
57+
.header("authorization", format!("Bearer {token}"))
58+
.json(initialize)
59+
.header("accept", "application/json, text/event-stream")
60+
.await;
61+
assert!(response.status().is_client_error(), "{}", response.status());
62+
Ok(())
63+
}

0 commit comments

Comments
 (0)