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): bind the access token to the client_id, not the resource URL

ruv3bd12277c747
diff --git a/plugins/ruflo-chatgpt-federation/src/oauth.mjs b/plugins/ruflo-chatgpt-federation/src/oauth.mjs
index ce78e350b8..594180cadb 100644
--- a/plugins/ruflo-chatgpt-federation/src/oauth.mjs
+++ b/plugins/ruflo-chatgpt-federation/src/oauth.mjs
@@ -52,8 +52,17 @@ export function _resetJwksForTest() { jwks = null; jwksFor = null; }
* Verify an access token and return its granted scopes.
*
* Audience is checked, not merely parsed: a token minted for another Cognitum
- * resource must not act on the federation identity just because it is signed by
- * the same issuer. That is the whole point of RFC 8707.
+ * app must not act on the federation identity just because the same issuer
+ * signed it.
+ *
+ * The audience to expect is this connector's **client_id**, not its resource
+ * URL. `auth.cognitum.one` binds `aud` to the requesting OAuth client
+ * (services/identity/src/jwt.rs `issue_oauth_access_token`), which is
+ * client-audience binding rather than RFC 8707 resource binding. It closes the
+ * same confused-deputy hole here because this connector is the only resource
+ * its client_id is registered for β€” a one-client-one-resource assumption that
+ * is load-bearing, and the reason `federation:*` must never be added to another
+ * client's allowed_scopes.
*
* @returns {Promise<{ ok: true, scopes: string[], subject?: string } | { ok: false, error: string, description: string }>}
*/
diff --git a/plugins/ruflo-chatgpt-federation/src/server.mjs b/plugins/ruflo-chatgpt-federation/src/server.mjs
index 43217cde1b..3006a04b1b 100644
--- a/plugins/ruflo-chatgpt-federation/src/server.mjs
+++ b/plugins/ruflo-chatgpt-federation/src/server.mjs
@@ -58,6 +58,12 @@ function rateLimited(req, rate = 60) {
export function createPublisherService({ relay, keyPath, port } = {}) {
const RELAY = relay || process.env.CGF_RELAY_URL || 'wss://relay.ruv.io';
const signer = loadSigner(keyPath); // throws at boot if custody is wrong β€” by design
+ if (String(process.env.CGF_OAUTH_REQUIRED || '') === 'true' && !(process.env.CGF_OAUTH_CLIENT_ID || '').trim()) {
+ // Enforcing OAuth without an expected audience would accept any token this
+ // issuer ever minted, for any Cognitum app β€” strictly worse than the
+ // transitional caller token it replaces. Fail the deploy.
+ throw new Error('CGF_OAUTH_REQUIRED=true needs CGF_OAUTH_CLIENT_ID β€” refusing to enforce OAuth without an audience to bind to');
+ }
const text = (o) => ({ content: [{ type: 'text', text: JSON.stringify(o) }] });
// An OAuth caller must carry the scope; a legacy caller (no token, OAuth not yet
// mandatory) keeps the reads it has always had.
@@ -69,6 +75,10 @@ export function createPublisherService({ relay, keyPath, port } = {}) {
const OAUTH_ISSUER = process.env.CGF_OAUTH_ISSUER || 'https://auth.cognitum.one';
const OAUTH_JWKS = process.env.CGF_OAUTH_JWKS_URI || `${OAUTH_ISSUER}/.well-known/jwks.json`;
const OAUTH_REQUIRED = String(process.env.CGF_OAUTH_REQUIRED || '') === 'true';
+ // auth.cognitum.one sets `aud` to the requesting client_id, so THAT is what a
+ // token must be addressed to β€” not this service's URL. Absent, no audience is
+ // asserted, which is only acceptable while OAuth is not yet enforced.
+ const OAUTH_CLIENT_ID = (process.env.CGF_OAUTH_CLIENT_ID || '').trim();
const resourceUrl = () => (process.env.CGF_PUBLIC_URL || '').replace(/\/$/, '');
const prmUrl = () => `${resourceUrl()}/.well-known/oauth-protected-resource`;
@@ -82,7 +92,7 @@ export function createPublisherService({ relay, keyPath, port } = {}) {
const bearer = String(req?.headers?.authorization || '').match(/^Bearer\s+(.+)$/i)?.[1]?.trim();
if (bearer) {
const v = await verifyAccessToken(bearer, { issuer: OAUTH_ISSUER, jwksUri: OAUTH_JWKS,
- audience: resourceUrl() || undefined });
+ audience: OAUTH_CLIENT_ID || undefined });
if (!v.ok) return { mode: 'denied', ...v };
return { mode: 'oauth', scopes: v.scopes, subject: v.subject };
}
@@ -148,7 +158,7 @@ export function createPublisherService({ relay, keyPath, port } = {}) {
return res.end(JSON.stringify({ service: 'ruflo-chatgpt-federation', version: VERSION, mcp: '/mcp',
relay: RELAY, pubkey: signer.pubkey,
tools: ['federation_identity', 'channel_sync', 'channel_publish'],
- authorization: { type: 'oauth2', issuer: OAUTH_ISSUER,
+ authorization: { type: 'oauth2', issuer: OAUTH_ISSUER, clientId: OAUTH_CLIENT_ID || null,
scopes: [SCOPE_READ, SCOPE_PUBLISH],
protectedResourceMetadata: prmUrl(),
enforced: OAUTH_REQUIRED,
diff --git a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
index 75dc3fc075..42668f26f1 100644
--- a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
+++ b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
@@ -42,16 +42,19 @@ async function fakeAuthServer() {
return { issuer, jwksUri: `${issuer}/.well-known/jwks.json`, mint, mintRaw, close: () => srv.close() };
}
-async function withService(as, { required, publicUrl }, fn) {
+const CLIENT_ID = 'chatgpt-federation';
+
+async function withService(as, { required, publicUrl, clientId = CLIENT_ID }, fn) {
const prev = { ...process.env };
Object.assign(process.env, {
CGF_OAUTH_ISSUER: as.issuer, CGF_OAUTH_JWKS_URI: as.jwksUri,
CGF_PUBLIC_URL: publicUrl, CGF_OAUTH_REQUIRED: required ? 'true' : '',
+ CGF_OAUTH_CLIENT_ID: clientId,
});
const svc = createPublisherService({ relay: 'ws://127.0.0.1:1', keyPath });
const port = await svc.listen(0);
try { return await fn(port, svc); }
- finally { svc.server.close(); for (const k of ['CGF_OAUTH_ISSUER','CGF_OAUTH_JWKS_URI','CGF_PUBLIC_URL','CGF_OAUTH_REQUIRED']) { if (prev[k] === undefined) delete process.env[k]; else process.env[k] = prev[k]; } }
+ finally { svc.server.close(); for (const k of ['CGF_OAUTH_ISSUER','CGF_OAUTH_JWKS_URI','CGF_PUBLIC_URL','CGF_OAUTH_REQUIRED','CGF_OAUTH_CLIENT_ID']) { if (prev[k] === undefined) delete process.env[k]; else process.env[k] = prev[k]; } }
}
const call = (port, name, args = {}, headers = {}) =>
@@ -111,7 +114,7 @@ test('a token from another issuer is refused', async () => {
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- const tok = await as.mint({ scope: SCOPE_READ, audience: 'https://cgf.example', issuer: 'https://evil.example' });
+ const tok = await as.mint({ scope: SCOPE_READ, audience: CLIENT_ID, issuer: 'https://evil.example' });
const r = await call(port, 'federation_identity', {}, { authorization: `Bearer ${tok}` });
assert.equal(r.status, 401);
assert.match(r.wwwAuth, /error="invalid_token"/);
@@ -119,13 +122,14 @@ test('a token from another issuer is refused', async () => {
} finally { as.close(); }
});
-test('a token minted for a different resource is refused', async () => {
- // Same issuer, wrong audience. Without this check any Cognitum token would be
- // able to act on the federation identity β€” which is the point of RFC 8707.
+test('a token minted for a different client is refused', async () => {
+ // Same issuer, different client. auth.cognitum.one binds `aud` to the
+ // requesting client_id, so this is the check that stops another Cognitum app's
+ // token from acting as the federation identity.
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- const tok = await as.mint({ scope: SCOPE_PUBLISH, audience: 'https://some-other.cognitum.one' });
+ const tok = await as.mint({ scope: SCOPE_PUBLISH, audience: 'some-other-client' });
const r = await call(port, 'federation_identity', {}, { authorization: `Bearer ${tok}` });
assert.equal(r.status, 401);
});
@@ -136,7 +140,7 @@ test('an expired token is refused', async () => {
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- const tok = await as.mint({ scope: SCOPE_READ, audience: 'https://cgf.example', expSeconds: -3600 });
+ const tok = await as.mint({ scope: SCOPE_READ, audience: CLIENT_ID, expSeconds: -3600 });
const r = await call(port, 'federation_identity', {}, { authorization: `Bearer ${tok}` });
assert.equal(r.status, 401);
});
@@ -147,7 +151,7 @@ test('federation:read reads, but does not publish', async () => {
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port, svc) => {
- const tok = await as.mint({ scope: SCOPE_READ, audience: 'https://cgf.example' });
+ const tok = await as.mint({ scope: SCOPE_READ, audience: CLIENT_ID });
const auth = { authorization: `Bearer ${tok}` };
const id = await call(port, 'federation_identity', {}, auth);
@@ -165,7 +169,7 @@ test('a scopeless token cannot even read', async () => {
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- const tok = await as.mint({ scope: '', audience: 'https://cgf.example' });
+ const tok = await as.mint({ scope: '', audience: CLIENT_ID });
const r = await call(port, 'federation_identity', {}, { authorization: `Bearer ${tok}` });
assert.match(toolText(r.body), /lacks federation:read/);
});
@@ -176,7 +180,7 @@ test('federation:publish reaches the publish path (and fails only at the relay)'
const as = await fakeAuthServer();
try {
await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
- const tok = await as.mint({ scope: `${SCOPE_READ} ${SCOPE_PUBLISH}`, audience: 'https://cgf.example' });
+ const tok = await as.mint({ scope: `${SCOPE_READ} ${SCOPE_PUBLISH}`, audience: CLIENT_ID });
const r = await call(port, 'channel_publish',
{ channel: 'pub:announce', msgType: 'Status', payload: {} }, { authorization: `Bearer ${tok}` });
// Authorisation passed; the relay is unreachable in this test, which is how
@@ -248,3 +252,20 @@ test('a correctly-signed but unbound token is rejected AND says why', async () =
});
} finally { as.close(); }
});
+
+test('enforcing OAuth without an expected audience fails the deploy', async () => {
+ // Enforcing OAuth with no audience to bind to would accept ANY token this
+ // issuer ever minted, for any Cognitum app β€” strictly worse than the
+ // transitional caller token it replaces. It must not be a silent config gap.
+ const prev = { ...process.env };
+ try {
+ process.env.CGF_OAUTH_REQUIRED = 'true';
+ delete process.env.CGF_OAUTH_CLIENT_ID;
+ assert.throws(() => createPublisherService({ relay: 'ws://127.0.0.1:1', keyPath }),
+ /refusing to enforce OAuth without an audience to bind to/);
+ } finally {
+ for (const k of ['CGF_OAUTH_REQUIRED', 'CGF_OAUTH_CLIENT_ID']) {
+ if (prev[k] === undefined) delete process.env[k]; else process.env[k] = prev[k];
+ }
+ }
+});