Files
Bohdan Triapitsyn 75978cf188 fix(mcp): reliable OAuth across runtimes and honest pre-restart UI
MCP authorization was broken in several stacked ways. The browser return
leg landed on the SPA behind the auth gate, so the system browser saw a
login page instead of finishing; the pending-context store silently
saved nothing because its route had no JSON body parser; and the
callback-URL config write started deferring behind Apply & Restart, so
authorization ran against a runtime without the URL and dead-ended on
OpenCode's loopback listener.

The return leg is now completed entirely server-side by an
unauthenticated GET /mcp/oauth/callback that only forwards a code whose
state matches a parked context. Desktop with the local server and VS
Code switch to OpenCode's native flow over its fixed loopback port —
no config writes or restarts at all, with a one-time cleanup of the
previously written callback URL — and its completion signal drives the
page instead of blind status polling. Remote, hosted-web, and mobile
keep the server-callback flow, applying a queued callback-URL write
immediately since authorization cannot wait for a manual restart.

Also: a server queued behind Apply & Restart now shows an Awaiting
restart badge and explanation instead of connect/reauthorize buttons
that can only fail, and Reauthorize is offered only while the server is
actually connected.
2026-08-10 20:23:45 +03:00

112 lines
4.3 KiB
JavaScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import express from 'express';
import request from 'supertest';
import { registerOpenCodeRoutes } from './routes.js';
// No global body parser on purpose: the real server parses JSON per-route, so
// these tests must fail if the pending route loses its own parser again.
const createApp = (overrides = {}) => {
const app = express();
const dependencies = {
buildOpenCodeUrl: (path) => `http://opencode.local${path}`,
getOpenCodeAuthHeaders: () => ({ 'x-opencode-auth': 'test' }),
...overrides,
};
registerOpenCodeRoutes(app, dependencies);
return { app, dependencies };
};
const queuePending = (app, { state, name, directory = null, origin = null }) =>
request(app)
.post('/api/mcp/auth/pending')
.send({ state, name, directory, origin })
.expect(200);
afterEach(() => {
vi.unstubAllGlobals();
});
describe('MCP OAuth browser callback route', () => {
it('completes authorization server-side for a parked state and clears it', async () => {
const upstreamFetch = vi.fn(async () => new Response(JSON.stringify({ success: true }), { status: 200 }));
vi.stubGlobal('fetch', upstreamFetch);
const { app } = createApp();
await queuePending(app, { state: 'state-1', name: 'linear', directory: '/projects/demo', origin: 'desktop' });
const response = await request(app)
.get('/mcp/oauth/callback')
.query({ state: 'state-1', code: 'auth-code', server: 'linear' })
.expect(200);
expect(upstreamFetch).toHaveBeenCalledTimes(1);
const [url, init] = upstreamFetch.mock.calls[0];
expect(String(url)).toBe('http://opencode.local/mcp/linear/auth/callback?directory=%2Fprojects%2Fdemo');
expect(init.method).toBe('POST');
expect(JSON.parse(init.body)).toEqual({ code: 'auth-code' });
expect(init.headers['x-opencode-auth']).toBe('test');
expect(response.text).toContain('Authorization Complete');
// Started from the desktop shell: the page hands control back via deep link.
expect(response.text).toContain('openchamber://focus/mcp-auth');
await request(app).get('/api/mcp/auth/pending').query({ state: 'state-1' }).expect(404);
});
it('never forwards a code whose state is unknown', async () => {
const upstreamFetch = vi.fn();
vi.stubGlobal('fetch', upstreamFetch);
const { app } = createApp();
const response = await request(app)
.get('/mcp/oauth/callback')
.query({ state: 'forged', code: 'attacker-code', server: 'linear' })
.expect(400);
expect(upstreamFetch).not.toHaveBeenCalled();
expect(response.text).toContain('Authorization Failed');
});
it('omits the desktop deep link for flows started outside the desktop shell', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response('{}', { status: 200 })));
const { app } = createApp();
await queuePending(app, { state: 'state-web', name: 'linear' });
const response = await request(app)
.get('/mcp/oauth/callback')
.query({ state: 'state-web', code: 'auth-code' })
.expect(200);
expect(response.text).not.toContain('openchamber://');
});
it('reports a provider error without contacting OpenCode', async () => {
const upstreamFetch = vi.fn();
vi.stubGlobal('fetch', upstreamFetch);
const { app } = createApp();
await queuePending(app, { state: 'state-2', name: 'linear' });
const response = await request(app)
.get('/mcp/oauth/callback')
.query({ state: 'state-2', error: 'access_denied', error_description: 'User <denied> access' })
.expect(400);
expect(upstreamFetch).not.toHaveBeenCalled();
// Interpolated provider text is escaped, not rendered as markup.
expect(response.text).toContain('User &lt;denied&gt; access');
await request(app).get('/api/mcp/auth/pending').query({ state: 'state-2' }).expect(404);
});
it('surfaces an OpenCode rejection as a failed page', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ error: 'invalid code' }), { status: 400 })));
const { app } = createApp();
await queuePending(app, { state: 'state-3', name: 'linear' });
const response = await request(app)
.get('/mcp/oauth/callback')
.query({ state: 'state-3', code: 'stale-code' })
.expect(502);
expect(response.text).toContain('invalid code');
});
});