diff --git a/changelog.d/fixed-proxy-mcp-endpoints.md b/changelog.d/fixed-proxy-mcp-endpoints.md new file mode 100644 index 0000000000..dd3678e08a --- /dev/null +++ b/changelog.d/fixed-proxy-mcp-endpoints.md @@ -0,0 +1 @@ +- Standalone deployments (Docker Compose, Podman Quadlet, LXC): the hive's two MCP endpoints now work through the bundled gateway on port 3001. The dashboard auth proxy answered `401 Unauthorized` to the task MCP (`POST /api/contribute/mcp`) before the hive could check the caller's own credential, so hub-launched agents and remote contributors could not use it whenever their task MCP URL went through the gateway (`dashboard.public_url` or a hub-assigned dashboard URL). It also answered 401 to anonymous calls to the public knowledge MCP (`POST /mcp/knowledge`) and had no route for that path at all, so the endpoint was unreachable even after the owner switched it on. The proxy now forwards exactly these two paths to the hive: the hive still verifies the task MCP caller's launch token or lease, and still serves public knowledge only while the owner's switch is on. A request let through this way gets no gateway credential (`X-Hive-Internal`), other methods on `/mcp/knowledge` are answered `405`, and every other unauthenticated write is refused as before. Kubernetes deployments and the default `http://localhost:3002` task MCP URL were not affected. diff --git a/src/proxy/api_internal_header.test.js b/src/proxy/api_internal_header.test.js index c85e6ac9ad..e66c99e00b 100644 --- a/src/proxy/api_internal_header.test.js +++ b/src/proxy/api_internal_header.test.js @@ -18,6 +18,9 @@ // 4. anonymous POST → 401 at the proxy (unchanged) // 5. a client-forged X-Hive-Internal never reaches the Go API // 6. hosted mode (no token): header absent, reads still pass through +// 7. the task MCP and the public knowledge MCP, whose callers the Go API +// authenticates itself, are forwarded WITHOUT X-Hive-Internal instead of +// being answered 401 here; their sibling paths are still refused import { createServer, request as httpRequest } from 'http'; import { strict as assert } from 'assert'; @@ -41,6 +44,7 @@ function startMockGoApi() { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ url: req.url, + authorization: req.headers['authorization'] ?? null, internal: req.headers['x-hive-internal'] ?? null, user: req.headers['x-hive-user'] ?? null, role: req.headers['x-hive-role'] ?? null, @@ -94,7 +98,7 @@ function req(method, path, headers = {}) { (res) => { let body = ''; res.on('data', (c) => { body += c; }); - res.on('end', () => resolve({ status: res.statusCode, body })); + res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, body })); }, ); r.on('error', reject); @@ -208,6 +212,94 @@ async function main() { assert.equal(JSON.parse(res.body).internal, null); }); + // ── MCP endpoints the Go API authenticates itself ─────────────────────── + // Their callers do not hold the dashboard token: the task MCP verifies a + // per-launch token (?token=) or a lease bearer, and the public knowledge MCP + // is anonymous while the owner's switch is on. The mock Go API answers 200 + // to whatever reaches it, so a 401 or 405 below was produced by the proxy. + const LEASE_TOKEN = 'hive_mcp_v1.test-payload.test-signature'; + + await check('task MCP POST with a ?token= launch token is forwarded WITHOUT X-Hive-Internal', async () => { + const res = await req('POST', `/api/contribute/mcp?task_id=t&repo=o%2Fr&number=7&token=${LEASE_TOKEN}`); + assert.equal(res.status, 200); + const seen = JSON.parse(res.body); + assert.equal(seen.internal, null); + // The Go handler reads the token and the task scope from the query string. + const forwarded = new URL(seen.url, 'http://go.test'); + assert.equal(forwarded.pathname, '/api/contribute/mcp'); + assert.equal(forwarded.searchParams.get('token'), LEASE_TOKEN); + assert.equal(forwarded.searchParams.get('repo'), 'o/r'); + }); + + await check('task MCP POST with a lease bearer is forwarded WITHOUT X-Hive-Internal', async () => { + const res = await req('POST', '/api/contribute/mcp', { Authorization: `Bearer ${LEASE_TOKEN}` }); + assert.equal(res.status, 200); + const seen = JSON.parse(res.body); + assert.equal(seen.internal, null); + // The Go handler verifies the bearer, so it must arrive untouched. + assert.equal(seen.authorization, `Bearer ${LEASE_TOKEN}`); + }); + + await check('the dashboard token in a task MCP ?token= never earns X-Hive-Internal', async () => { + // Only the Authorization header can mark a request as gateway-authenticated; + // the Go handler refuses a dashboard token sent in the query string itself. + const res = await req('POST', `/api/contribute/mcp?token=${TOKEN}`); + assert.equal(res.status, 200); + assert.equal(JSON.parse(res.body).internal, null); + }); + + for (const p of ['/api/contribute/mcpx', '/api/contribute/mcp/', '/api/contribute/mcp/extra']) { + await check(`anonymous POST ${p} is still 401 (exact match, not prefix)`, async () => { + const res = await req('POST', p); + assert.equal(res.status, 401); + }); + } + + await check('anonymous POST /mcp/knowledge is forwarded with no trust headers', async () => { + const res = await req('POST', '/mcp/knowledge'); + assert.equal(res.status, 200); + const seen = JSON.parse(res.body); + assert.equal(seen.url, '/mcp/knowledge'); + assert.equal(seen.internal, null); + assert.equal(seen.user, null); + assert.equal(seen.role, null); + }); + + await check('client-forged trust headers on POST /mcp/knowledge never reach the Go API', async () => { + const res = await req('POST', '/mcp/knowledge', { + 'X-Hive-Internal': TOKEN, + 'X-Hive-User': 'attacker', + 'X-Hive-Role': 'owner', + }); + assert.equal(res.status, 200); + const seen = JSON.parse(res.body); + assert.equal(seen.internal, null); + assert.equal(seen.user, null); + assert.equal(seen.role, null); + }); + + await check('POST /mcp/knowledge with the dashboard token still gets no X-Hive-Internal', async () => { + // The endpoint is public: the proxy vouches for nobody on it. + const res = await req('POST', '/mcp/knowledge', { Authorization: `Bearer ${TOKEN}` }); + assert.equal(res.status, 200); + assert.equal(JSON.parse(res.body).internal, null); + }); + + for (const method of ['GET', 'PUT', 'DELETE']) { + await check(`${method} /mcp/knowledge is answered 405 with Allow: POST, not forwarded`, async () => { + const res = await req(method, '/mcp/knowledge'); + assert.equal(res.status, 405); + assert.equal(res.headers.allow, 'POST'); + }); + } + + for (const p of ['/mcp/knowledgex', '/mcp/knowledge/', '/mcp/knowledge/extra', '/mcp/other']) { + await check(`anonymous POST ${p} is still 401 (exact match, not prefix)`, async () => { + const res = await req('POST', p); + assert.equal(res.status, 401); + }); + } + await stopProxy(proxy); // ── hosted mode: no shared token exists at all ────────────────────────── diff --git a/src/proxy/server.js b/src/proxy/server.js index 8cb2c42c00..f45c56ce86 100644 --- a/src/proxy/server.js +++ b/src/proxy/server.js @@ -1010,7 +1010,21 @@ const PUBLIC_POST_PATHS = ['/api/contribute/register', '/api/contribute/reissue- // Device-flow sign-in runs before the visitor holds any credential. Matched // EXACTLY (query string aside), never by prefix, so a sibling route is not // opened by accident. /poll only completes a flow for the holder of its flow_id. -const PUBLIC_EXACT_POST_PATHS = ['/api/gh-user-auth/start', '/api/gh-user-auth/poll']; +// +// The hive's two MCP endpoints authenticate their callers in the Go API. Those +// callers do not hold the dashboard token, so a 401 here would refuse everyone +// the endpoints exist for: +// /api/contribute/mcp task MCP. The Go handler takes a per-launch token +// (?token=, hub-launched agents) or a lease bearer +// (remote contributors) and refuses everything else. +// /mcp/knowledge public knowledge MCP. Anonymous by design; the Go API +// serves it only while the owner's switch is on. +// Neither credential is the dashboard token and bearerTokenMatches never reads +// the query string, so hiveTokenVerified stays false: no X-Hive-Internal. +const PUBLIC_EXACT_POST_PATHS = [ + '/api/gh-user-auth/start', '/api/gh-user-auth/poll', + '/api/contribute/mcp', '/mcp/knowledge', +]; app.use((req, res, next) => { // SECURITY (CWE-306): decide ONCE, for every method, whether this request // actually presented the shared dashboard token. apiProxy injects the @@ -1126,6 +1140,39 @@ app.use('/gh-setup', createProxyMiddleware({ }, })); +// Public read-only knowledge MCP (src/docs/public-knowledge-mcp.md). The Go +// API serves POST /mcp/knowledge to anyone while the owner's "Public read-only +// MCP" switch is on and refuses it otherwise. This route was missing here: an +// anonymous POST got the token gate's 401, a POST with the dashboard token a +// 404, and a GET the dashboard page, so the endpoint never worked through the +// gateway. The proxy vouches for nobody on this route: X-Hive-Internal is +// never added, not even for a request that presented the dashboard token, and +// client-supplied identity headers are dropped. +const knowledgeMcpProxy = createProxyMiddleware({ + target: GO_API_URL, + changeOrigin: true, + on: { + proxyReq(proxyReq) { + proxyReq.removeHeader('X-Hive-Internal'); + proxyReq.removeHeader('X-Hive-User'); + proxyReq.removeHeader('X-Hive-Role'); + }, + error(err, req, res) { + console.error(`[knowledge-mcp-proxy] ${req.method} ${redactURL(req.url)} → ${err.message}`); + if (res.writeHead) { + res.writeHead(502, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Go API unavailable', detail: err.message })); + } + }, + }, +}); +app.post('/mcp/knowledge', knowledgeMcpProxy); +// POST only, like the Go route: there is no SSE stream to GET. +app.all('/mcp/knowledge', (_req, res) => { + res.setHeader('Allow', 'POST'); + res.status(405).type('text/plain').send('Method Not Allowed'); +}); + function stripSensitiveTerminalQuery(rawUrl) { try { const u = new URL(rawUrl, 'http://hive.local');