Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(federation): CORS preflight and the RFC 9728 resource identifier

ruv62609d77abf6
diff --git a/plugins/ruflo-chatgpt-federation/src/server.mjs b/plugins/ruflo-chatgpt-federation/src/server.mjs
index 3006a04b1b..3ad16f5d70 100644
--- a/plugins/ruflo-chatgpt-federation/src/server.mjs
+++ b/plugins/ruflo-chatgpt-federation/src/server.mjs
@@ -151,7 +151,21 @@ export function createPublisherService({ relay, keyPath, port } = {}) {
const server = createServer(async (req, res) => {
res.setHeader('x-content-type-options', 'nosniff');
res.setHeader('referrer-policy', 'no-referrer');
+ // A connector running in a browser preflights /mcp before it may POST to it,
+ // and cannot read WWW-Authenticate unless it is explicitly exposed — without
+ // that header it never learns where to authenticate, so the OAuth flow never
+ // starts. Origin is open because authority here comes from the bearer token,
+ // not from where the request was made.
+ res.setHeader('access-control-allow-origin', '*');
+ res.setHeader('access-control-expose-headers', 'www-authenticate, mcp-session-id, mcp-protocol-version');
const url = new URL(req.url, `http://${req.headers.host || 'x'}`);
+ if (req.method === 'OPTIONS') {
+ res.setHeader('access-control-allow-methods', 'GET, POST, OPTIONS');
+ res.setHeader('access-control-allow-headers',
+ 'content-type, authorization, x-caller-token, mcp-session-id, mcp-protocol-version, accept');
+ res.setHeader('access-control-max-age', '86400');
+ return res.writeHead(204).end();
+ }
if (url.pathname === '/health') return res.writeHead(200).end('ok');
if (url.pathname === '/' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'application/json' });
@@ -168,9 +182,14 @@ export function createPublisherService({ relay, keyPath, port } = {}) {
// because clients differ on which they probe.
if (url.pathname === '/.well-known/oauth-protected-resource'
|| url.pathname === '/.well-known/oauth-protected-resource/mcp') {
- res.writeHead(200, { 'content-type': 'application/json', 'access-control-allow-origin': '*' });
- return res.end(JSON.stringify(protectedResourceMetadata({
- resource: resourceUrl() || `https://${req.headers.host}`, issuer: OAUTH_ISSUER })));
+ // RFC 9728 §3: `resource` must be the resource identifier the client used.
+ // A client given `<base>/mcp` looks up the path-suffixed document and
+ // expects `<base>/mcp` back; answering with the bare origin is a mismatch
+ // it is entitled to reject, and some do — silently, during setup.
+ const base = resourceUrl() || `https://${req.headers.host}`;
+ const resource = url.pathname.endsWith('/mcp') ? `${base}/mcp` : base;
+ res.writeHead(200, { 'content-type': 'application/json' });
+ return res.end(JSON.stringify(protectedResourceMetadata({ resource, issuer: OAUTH_ISSUER })));
}
if (url.pathname === '/mcp') {
if (rateLimited(req)) return res.writeHead(429, { 'content-type': 'application/json' }).end('{"error":"rate limited"}');
diff --git a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
index b5dc196b26..c78b9bc412 100644
--- a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
+++ b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
@@ -88,12 +88,15 @@ test('discovery is served at both paths clients probe', async () => {
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- for (const path of ['/.well-known/oauth-protected-resource', '/.well-known/oauth-protected-resource/mcp']) {
+ for (const [path, expected] of [
+ ['/.well-known/oauth-protected-resource', 'https://cgf.example'],
+ ['/.well-known/oauth-protected-resource/mcp', 'https://cgf.example/mcp'],
+ ]) {
const r = await fetch(`http://127.0.0.1:${port}${path}`);
assert.equal(r.status, 200, path);
const m = await r.json();
assert.deepEqual(m.authorization_servers, [as.issuer]);
- assert.equal(m.resource, 'https://cgf.example');
+ assert.equal(m.resource, expected, path);
}
});
} finally { as.close(); }
@@ -303,3 +306,35 @@ test('ACCEPTANCE: session and other-client tokens get 401; ours succeeds with ex
});
} finally { as.close(); }
});
+
+test('a browser-origin connector can preflight /mcp', async () => {
+ // Without this the connector cannot POST at all, and the failure surfaces to
+ // the user as a generic "error creating connector".
+ const as = await fakeAuthServer();
+ try {
+ await withService(as, { required: false, publicUrl: 'https://cgf.example' }, async (port) => {
+ const r = await fetch(`http://127.0.0.1:${port}/mcp`, { method: 'OPTIONS',
+ headers: { origin: 'https://chatgpt.com', 'access-control-request-method': 'POST',
+ 'access-control-request-headers': 'content-type,authorization' } });
+ assert.equal(r.status, 204);
+ assert.equal(r.headers.get('access-control-allow-origin'), '*');
+ assert.match(r.headers.get('access-control-allow-methods') || '', /POST/);
+ assert.match(r.headers.get('access-control-allow-headers') || '', /authorization/);
+ // The client cannot start OAuth if it cannot read the challenge.
+ assert.match(r.headers.get('access-control-expose-headers') || '', /www-authenticate/);
+ });
+ } finally { as.close(); }
+});
+
+test('protected-resource metadata echoes the identifier the client asked about', async () => {
+ const as = await fakeAuthServer();
+ try {
+ await withService(as, { required: false, publicUrl: 'https://cgf.example' }, async (port) => {
+ const bare = await (await fetch(`http://127.0.0.1:${port}/.well-known/oauth-protected-resource`)).json();
+ assert.equal(bare.resource, 'https://cgf.example');
+ const suffixed = await (await fetch(`http://127.0.0.1:${port}/.well-known/oauth-protected-resource/mcp`)).json();
+ assert.equal(suffixed.resource, 'https://cgf.example/mcp',
+ 'a client given <base>/mcp must get <base>/mcp back, not the bare origin');
+ });
+ } finally { as.close(); }
+});