Files
gbrain/test/e2e/serve-http-oauth.test.ts
T
cb02932388 v0.26.9 fix(oauth): RFC 6749 hardening + close HTTP MCP shell-job RCE (#628)
* fix(mcp): close HTTP MCP shell-job RCE + tighten remote contract

The HTTP MCP transport in serve-http.ts inlined its own OperationContext
literal and forgot to set `remote: true`. With the field undefined at the
operations.ts protected-job-name guard (line 1391), an HTTP MCP caller
holding a write-scoped OAuth token could submit `submit_job {name: "shell"}`
and execute arbitrary commands on the gbrain host (RCE-class).

Two-layer fix:

1. F7 — explicit `remote: true` on the inlined /mcp OperationContext.
   Stdio MCP at src/mcp/dispatch.ts:61 already set this; the HTTP path
   was the regression.

2. F7b — fail-closed contract on the four ctx.remote consumer sites in
   operations.ts (auto-link skip, telemetry x2, protected-job guard).
   The protected-job guard flips from `if (ctx.remote && ...)` to
   `if (ctx.remote !== false && ...)` and the trusted-marker site flips
   from `!ctx.remote && ...` to `ctx.remote === false && ...`. Anything
   that isn't strictly `false` now treats the caller as remote/untrusted.

3. D12 — `OperationContext.remote` becomes REQUIRED in the TypeScript
   type. The compiler now catches future transports that forget the field.
   The runtime fail-closed defaults are belt+suspenders for any caller
   that bypasses the type via `as` cast or `Partial<>` spread.

Tests:

- New `test/trust-boundary-contract.test.ts` (4 cases) pins the
  fail-closed semantics: undefined-via-cast rejects, remote=true rejects,
  remote=false allowed (only path that escalates protected-name jobs).

- `test/e2e/serve-http-oauth.test.ts` adds 2 cases asserting HTTP MCP
  cannot submit `shell` or `subagent` jobs even with read+write scope.

- `test/e2e/graph-quality.test.ts` adds the now-required `remote: false`
  to its fixture (e2e graph quality simulates local-CLI writes).

Verification: bun test -> 3742 pass / 0 fail. typecheck clean.

Thanks to @ElectricSheepIO on X for the security review that surfaced
this trust-boundary regression.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(oauth): RFC 6749 hardening + serve-http defense in depth

OAuth provider hardening pass that brings the provider into RFC compliance
on auth code, refresh token, and revocation flows, and tightens the
serve-http surface around request logging and admin cookies.

Provider (src/core/oauth-provider.ts):

- F1: bind client_id atomically into the auth code DELETE WHERE clause for
  exchangeAuthorizationCode + challengeForAuthorizationCode. Previous
  pattern (DELETE...RETURNING then post-hoc client compare) burned codes
  on the wrong-client path so the legitimate client could not retry.
  RFC 6749 §10.5.

- F2: same atomic predicate on exchangeRefreshToken. The pre-fix shape
  defeated RFC 6749 §10.4's stolen-token detection by letting attacker +
  victim both succeed.

- F3: refresh token rejects requested scopes that are not a subset of the
  ORIGINAL grant on the row. Codex C9: subset is checked against the
  recorded grant, not the client's currently-allowed scopes (which can
  expand later); omitted scope inherits the original verbatim and stays
  distinct from explicit-empty. RFC 6749 §6.

- F4: revokeToken adds AND client_id to the DELETE so a client cannot
  revoke another client's tokens by guessing the hash. RFC 7009 §2.1.

- F5: deleted_at and token_ttl column probes use a new
  isUndefinedColumnError helper (extracted to src/core/utils.ts per D14)
  that matches SQLSTATE 42703 or column-name-in-message. Bare catch{}
  used to swallow lock timeouts, network blips, and auth failures as
  "column missing" — fail-open posture in a security path.

- F6: sweepExpiredTokens uses RETURNING 1 + array length. Pre-fix
  (result as any).count returned 0 on at least one engine even when
  rows were deleted, and codes were never counted.

- F7c: NEW finding eva-brain missed. exchangeAuthorizationCode now folds
  redirect_uri into the atomic DELETE predicate when the parameter is
  provided. Stored on /authorize, never compared on /token before this
  commit. RFC 6749 §4.1.3 violation. Back-compat: when caller omits the
  parameter the predicate is skipped, preserving SDK consumers that
  haven't adopted the parameter yet.

- F12 (cleanup, not security): dcrDisabled constructor option replaces
  the prior monkey-patch of _clientsStore in serve-http.ts. The SDK's
  mcpAuthRouter only wires up /register when the store exposes
  registerClient, so omitting the method via the constructor is
  sufficient. Reframed as cleanup per codex C10 — the monkey-patch
  happened before mcpAuthRouter ran, so the prior shape did not have
  a real security regression to claim.

Dispatch (src/mcp/dispatch.ts):

- F8: new summarizeMcpParams(opName, params) intersects submitted keys
  against the operation's declared params allow-list. Returns
  {redacted, kind, declared_keys, unknown_key_count, approx_bytes}.
  Closes the codex C8 leak: a naive "dump all submitted keys" summary
  still echoed attacker-controlled key names like
  put_page {"wiki/people/sensitive_name": "..."} into mcp_request_log
  + the SSE feed. Allow-list pattern keeps debug visibility on declared
  keys while counting unknowns without naming them.

Serve-http (src/commands/serve-http.ts) + serve (src/commands/serve.ts):

- F8 wiring: mcp_request_log + SSE broadcast routed through
  summarizeMcpParams by default. New --log-full-params flag bypasses
  redaction with a loud stderr warning at startup. Default privacy-
  positive; flag is the documented escape hatch for self-hosted
  operators debugging on their own laptop.

- F9: admin cookies set Secure when req.secure OR issuerUrl.protocol
  is https. Cloudflare-tunnel + reverse-proxy deployments where the
  inside-tunnel hop looks like http but the public URL is https now
  tag cookies correctly.

- F10: bound magicLinkNonces with NONCE_LRU_CAP. Previously only the
  consumed-nonces map was capped; an attacker (or misbehaving agent)
  with the bootstrap token could mint nonces faster than they expired
  and grow the live store unbounded.

- F12: dcrDisabled flows through to the provider constructor instead of
  monkey-patching _clientsStore after construction.

- F14: try/catch wraps StreamableHTTPServerTransport setup +
  handleRequest. SDK-level throws no longer fall through to express's
  default HTML error page; clients expecting JSON-RPC envelopes get a
  JSON 500 instead.

- F15: error envelope unified via buildError + serializeError from
  src/core/errors.ts. OperationError and unexpected exceptions both
  emit the same {class, code, message, hint} shape so clients can
  pattern-match a single envelope.

Tests:

- test/oauth.test.ts adds 11 cases:
  * F1+F2 wrong-client cannot consume / read PKCE / burn refresh,
    paired with owner-still-redeems atomically afterward (codex D6 —
    proves the predicate doesn't burn the row on attacker attempts).
  * F3 refresh scope subset enforced.
  * F4 wrong-client cannot revoke.
  * F5 non-schema SQL not swallowed by client_credentials soft-delete probe.
  * F6 sweepExpiredTokens returns count > 0 after deleting rows.
  * F7c redirect_uri match succeeds, mismatch rejects, omitted preserves
    back-compat for callers that don't pass the parameter.
  * F12 dcrDisabled constructor option exposes only getClient,
    registerClientManual still works.

- test/mcp-dispatch-summarize.test.ts (NEW, 6 cases): pins the F8
  privacy invariants. The codex-C8 attacker-key-name probe asserts that
  a sensitive name submitted as a key never appears anywhere in the
  redactor's output.

Verification: bun run typecheck clean. test/oauth.test.ts 55/55,
test/mcp-dispatch-summarize.test.ts 6/6,
test/trust-boundary-contract.test.ts 4/4 from commit A. The one
unrelated unit failure surfaces on master too — environment-sensitive
test that expects ~/.gbrain/config.json to be absent in the test env.

Out of scope: F11 (auth register-client --redirect-uri flag) and F13
(serve --http argv positive-int validator) per codex C11 — operator
UX gaps, not trust-boundary fixes. Filed as follow-up TODOs.

Thanks to @ElectricSheepIO on X for the security review that surfaced
this hardening pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: file F11 + F13 as OAuth hardening follow-up TODOs

Codex C11 flagged these as scope creep on the v0.26.7 OAuth hardening
PR (operator UX, not trust-boundary). Capturing them here so the
context survives — eva-brain has both implementations and the lift is
mechanical when we want to do them.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(oauth): close adversarial-review findings on F7c + F8

Two bugs surfaced by an adversarial subagent during /ship's pre-landing
review pass that the codex + plan-eng-review didn't catch.

D15 / F7c: `exchangeAuthorizationCode` used `redirectUri ? ...` ternary
to choose the with-redirect vs no-redirect SQL. Empty string fell
through to the no-redirect branch, so a caller submitting
`redirect_uri=""` at /token bypassed the binding entirely. RFC 6749
§4.1.3 spec violation. Switch to `redirectUri !== undefined`. Test:
empty-string redirect_uri must reject when /authorize stored a real URI.

D16 / F8: `summarizeMcpParams` published exact byte length via
`approx_bytes = JSON.stringify(params).length`. Submitting put_page with
a known prefix and observing the resulting log entry across repeated
probes lets an attacker binary-search the size of secret suffix content.
Bucket to 1KB resolution. The redacted summary keeps a coarse
"roughly how big" signal for operators while making size-based
side-channel attacks useless.

Test count: 65 → 67 across the three new test files.
Typecheck clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: bump version and changelog (v0.26.9)

OAuth 2.1 hardening + HTTP MCP shell-job RCE fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs: update project documentation for v0.26.9

Annotate CLAUDE.md key-files entries with v0.26.9 OAuth/MCP hardening pass:
- src/core/operations.ts: D12 (OperationContext.remote required) + F7b
  (4-site fail-closed flip), HTTP MCP shell-job RCE close
- src/core/utils.ts: D14 isUndefinedColumnError extracted helper
- src/mcp/dispatch.ts: F8 summarizeMcpParams privacy redactor with
  declared-keys allow-list + 1KB byte bucketing
- src/commands/serve-http.ts: F7+F8+F9+F10+F12+F14+F15 hardening
- src/core/oauth-provider.ts: F1+F2+F3+F4+F5+F6+F7c+F12 RFC 6749/7009
  hardening pass

Add new test-file entries for test/mcp-dispatch-summarize.test.ts
(7 cases) and test/trust-boundary-contract.test.ts (4 cases). Extend
test/oauth.test.ts (+14 cases) and test/e2e/serve-http-oauth.test.ts
(+2 RCE-close regressions) entries with v0.26.9 case counts.

README.md: added --log-full-params to gbrain serve --http surface.

SECURITY.md: documented mcp_request_log.params redaction default
({redacted, kind, declared_keys, unknown_key_count, approx_bytes}) +
--log-full-params opt-in.

docs/mcp/DEPLOY.md: operator-facing note on SSE feed + audit log
redaction default and when to flip --log-full-params on.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 21:11:15 -07:00

784 lines
35 KiB
TypeScript

/**
* E2E tests for serve-http.ts OAuth 2.1 fixes (v0.26.1).
*
* Spins up a real `gbrain serve --http` against real Postgres, registers an
* OAuth client, mints tokens, and exercises the full MCP JSON-RPC pipeline
* end-to-end. Catches the three bugs fixed in v0.26.1:
*
* 1. client_credentials tokens rejected at /mcp (expiresAt string vs number)
* 2. OAuth metadata missing client_credentials grant type
* 3. Express 5 trust proxy + admin SPA wildcard
*
* Run: GBRAIN_DATABASE_URL=... bun test test/e2e/serve-http-oauth.test.ts
*/
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import { hasDatabase } from './helpers.ts';
const skip = !hasDatabase();
const describeE2E = skip ? describe.skip : describe;
if (skip) {
console.log('Skipping E2E serve-http-oauth tests (DATABASE_URL not set)');
}
const PORT = 19131; // Avoid collision with production 3131
const BASE = `http://localhost:${PORT}`;
describeE2E('serve-http OAuth 2.1 E2E (v0.26.1 + v0.26.2 + v0.26.3)', () => {
let serverProcess: ReturnType<typeof import('child_process').spawn> | null = null;
let clientId: string | undefined;
let clientSecret: string | undefined;
// DCR-registered clients accumulate here so afterAll can revoke them too
// (one per test that posts to /register).
const dcrClientIds: string[] = [];
beforeAll(async () => {
const { execSync, spawn } = await import('child_process');
// Register a test OAuth client via CLI.
// env: { ...process.env } is required: bun's execSync does NOT inherit
// env mutations done via `process.env.X = ...` (only OS-level env from
// before bun started). helpers.ts loads .env.testing and sets DATABASE_URL
// via process.env mutation, which is invisible to subprocesses unless we
// explicitly re-pass process.env. Same pattern applies to every execSync
// in this file.
const regOutput = execSync(
'bun run src/cli.ts auth register-client e2e-oauth-test --grant-types client_credentials --scopes "read write"',
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } }
);
const idMatch = regOutput.match(/Client ID:\s+(gbrain_cl_\S+)/);
const secretMatch = regOutput.match(/Client Secret:\s+(gbrain_cs_\S+)/);
if (!idMatch || !secretMatch) throw new Error('Failed to register test client:\n' + regOutput);
clientId = idMatch[1];
clientSecret = secretMatch[1];
// Start the HTTP server. v0.26.2 adds --enable-dcr so the /register
// endpoint is reachable for the DCR response-shape test.
serverProcess = spawn('bun', [
'run', 'src/cli.ts', 'serve', '--http',
'--port', String(PORT),
'--public-url', `http://localhost:${PORT}`,
'--enable-dcr',
], {
cwd: process.cwd(),
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
});
// Collect stderr for debugging failures
let stderr = '';
serverProcess.stderr?.on('data', (d: Buffer) => { stderr += d.toString(); });
// Wait for server to be ready (up to 15s)
let ready = false;
for (let i = 0; i < 30; i++) {
try {
const res = await fetch(`${BASE}/health`);
if (res.ok) { ready = true; break; }
} catch {}
await new Promise(r => setTimeout(r, 500));
}
if (!ready) throw new Error('Server failed to start within 15s.\nstderr: ' + stderr.slice(-500));
}, 30_000);
afterAll(async () => {
// Kill server first so it can't issue more tokens during cleanup.
if (serverProcess) {
serverProcess.kill('SIGTERM');
await new Promise(r => setTimeout(r, 1000));
if (!serverProcess.killed) serverProcess.kill('SIGKILL');
}
// v0.26.2 cleanup contract: only revoke if registration succeeded
// (clientId guard) and surface any cleanup failure to stderr without
// throwing — a real test failure is more interesting than the cleanup
// error that follows it. Same shape applies to DCR-registered clients
// tracked in dcrClientIds.
const { execSync } = await import('child_process');
const toRevoke = [...(clientId ? [clientId] : []), ...dcrClientIds];
for (const id of toRevoke) {
try {
execSync(`bun run src/cli.ts auth revoke-client "${id}"`,
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } });
} catch (e: any) {
// eslint-disable-next-line no-console
console.error(`[afterAll] revoke-client cleanup failed for ${id}: ${e.message}`);
}
}
});
// Helper: mint a token with given scopes
async function mintToken(scope = 'read write'): Promise<{ access_token: string; expires_in: number; scope: string }> {
const res = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=${encodeURIComponent(scope)}`,
});
expect(res.ok).toBe(true);
return res.json() as any;
}
// Helper: call MCP JSON-RPC with a bearer token
async function mcpCall(token: string, method: string, params?: any): Promise<Response> {
return fetch(`${BASE}/mcp`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
},
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, ...(params ? { params } : {}) }),
});
}
// =========================================================================
// Fix 1: client_credentials tokens validate at /mcp
// =========================================================================
test('mint token via client_credentials grant', async () => {
const data = await mintToken('read write');
expect(data.access_token).toMatch(/^gbrain_at_/);
expect(data.expires_in).toBe(3600);
expect(data.scope).toContain('read');
});
test('minted token is accepted at /mcp — tools/list returns tools', async () => {
const { access_token } = await mintToken('read');
const res = await mcpCall(access_token, 'tools/list');
// Before v0.26.1 fix: 401 {"error":"invalid_token","error_description":"Token has no expiration time"}
expect(res.status).not.toBe(401);
const body = await res.text();
expect(body).toContain('tools');
expect(body).toContain('search'); // search tool should be in the list
expect(body).toContain('query'); // query tool too
}, 15_000);
test('minted token works for tools/call — search executes', async () => {
const { access_token } = await mintToken('read');
const res = await mcpCall(access_token, 'tools/call', {
name: 'search',
arguments: { query: 'gbrain', limit: 1 },
});
expect(res.status).not.toBe(401);
const body = await res.text();
// Should contain search results, not an auth error
expect(body).not.toContain('invalid_token');
expect(body).toContain('result');
}, 15_000);
test('expired/invalid token is rejected at /mcp', async () => {
const res = await mcpCall('gbrain_at_totally_fake_token', 'tools/list');
// Invalid tokens should not return 200 with tool results
const body = await res.text();
expect(body).not.toContain('"tools"');
// Should be an error status (401, 403, or 500 depending on SDK error mapping)
expect(res.status).toBeGreaterThanOrEqual(400);
});
test('missing Authorization header returns 401', async () => {
const res = await fetch(`${BASE}/mcp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
},
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
});
expect(res.status).toBe(401);
});
// =========================================================================
// Fix 2: OAuth metadata includes client_credentials
// =========================================================================
test('OAuth AS metadata includes all three grant types', async () => {
const res = await fetch(`${BASE}/.well-known/oauth-authorization-server`);
expect(res.ok).toBe(true);
const meta = await res.json() as any;
expect(meta.grant_types_supported).toContain('authorization_code');
expect(meta.grant_types_supported).toContain('refresh_token');
expect(meta.grant_types_supported).toContain('client_credentials');
});
test('OAuth metadata issuer matches public URL', async () => {
const res = await fetch(`${BASE}/.well-known/oauth-authorization-server`);
const meta = await res.json() as any;
expect(meta.issuer).toBe(`http://localhost:${PORT}/`);
expect(meta.token_endpoint).toContain('/token');
expect(meta.scopes_supported).toContain('read');
expect(meta.scopes_supported).toContain('write');
expect(meta.scopes_supported).toContain('admin');
});
// =========================================================================
// Fix 3: Express 5 compatibility
// =========================================================================
test('admin dashboard serves SPA index.html (not Express error)', async () => {
const res = await fetch(`${BASE}/admin/`);
const html = await res.text();
expect(html).toContain('GBrain Admin');
expect(html).not.toContain('<pre>Cannot GET');
});
test('admin sub-routes serve SPA fallback', async () => {
const res = await fetch(`${BASE}/admin/agents`);
const html = await res.text();
expect(html).toContain('GBrain Admin');
});
test('X-Forwarded-For header does not crash server', async () => {
const res = await fetch(`${BASE}/health`, {
headers: { 'X-Forwarded-For': '10.0.0.1, 172.16.0.1' },
});
expect(res.ok).toBe(true);
const data = await res.json() as any;
expect(data.status).toBe('ok');
});
// =========================================================================
// Scope enforcement
// =========================================================================
test('read-only token is rejected for write operations', async () => {
const { access_token } = await mintToken('read');
const res = await mcpCall(access_token, 'tools/call', {
name: 'put_page',
arguments: { slug: 'e2e-scope-test', content: '---\ntitle: test\n---\ntest' },
});
const body = await res.text();
// Should be rejected via scope check (403 or JSON-RPC error with scope message)
expect(res.status === 403 || body.includes('scope') || body.includes('Insufficient')).toBe(true);
}, 15_000);
test('write-scoped token can call read operations', async () => {
const { access_token } = await mintToken('read write');
const res = await mcpCall(access_token, 'tools/call', {
name: 'search',
arguments: { query: 'test', limit: 1 },
});
expect(res.status).not.toBe(401);
expect(res.status).not.toBe(403);
const body = await res.text();
// Should get a result, not an auth error
expect(body).not.toContain('invalid_token');
expect(body).not.toContain('insufficient_scope');
}, 15_000);
// =========================================================================
// Health endpoint (no auth required)
// =========================================================================
test('health endpoint returns OK without auth', async () => {
const res = await fetch(`${BASE}/health`);
expect(res.ok).toBe(true);
const data = await res.json() as any;
expect(data.status).toBe('ok');
expect(data.version).toBeDefined();
// page_count: the endpoint must return a non-negative integer. The exact
// value depends on the deployment's brain state and is not what this test
// is checking — pre-v0.26.2 this asserted `> 0` and broke on fresh schemas.
expect(typeof data.page_count).toBe('number');
expect(data.page_count).toBeGreaterThanOrEqual(0);
});
// =========================================================================
// Token lifecycle
// =========================================================================
test('multiple tokens can be minted and used independently', async () => {
const t1 = await mintToken('read');
const t2 = await mintToken('read write');
// Both should work
const r1 = await mcpCall(t1.access_token, 'tools/list');
const r2 = await mcpCall(t2.access_token, 'tools/list');
expect(r1.status).not.toBe(401);
expect(r2.status).not.toBe(401);
}, 15_000);
test('wrong client_secret is rejected at token endpoint', async () => {
const res = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=gbrain_cs_wrong_secret&scope=read`,
});
expect(res.ok).toBe(false);
const data = await res.json() as any;
expect(data.error).toBe('invalid_grant');
});
// =========================================================================
// v0.26.2: DCR /register response shape (RFC 7591 §3.2.1 number contract)
// =========================================================================
//
// The user-visible bug v0.26.2 protects against: postgres.js with
// `prepare: false` returns BIGINT columns as strings, and an RFC-strict
// DCR client (Claude Code, Cursor) parses the /register response as JSON
// and rejects timestamps that aren't numbers. This is the HTTP-level test;
// the internal-store shape test in test/oauth.test.ts is not enough on its
// own (Codex flagged it as the wrong seam).
test('DCR /register returns numeric client_id_issued_at (RFC 7591 §3.2.1)', async () => {
const res = await fetch(`${BASE}/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_name: 'e2e-dcr-shape',
redirect_uris: ['https://example.com/cb'],
grant_types: ['authorization_code'],
token_endpoint_auth_method: 'client_secret_basic',
scope: 'read',
}),
});
expect(res.ok).toBe(true);
const body = await res.json() as any;
// Track for cleanup before any assertion that could throw.
if (body.client_id) dcrClientIds.push(body.client_id);
// The contract: client_id_issued_at is REQUIRED to be a JSON number per
// RFC 7591. Pre-v0.26.2 with prepare:false returned this as a string
// (e.g., "1735689600") and strict clients rejected the registration.
expect(typeof body.client_id_issued_at).toBe('number');
expect(Number.isFinite(body.client_id_issued_at)).toBe(true);
expect(body.client_id_issued_at).toBeGreaterThan(0);
// client_secret_expires_at is OPTIONAL. If present, it must also be a
// number. Undefined/missing means "does not expire" per the spec.
if (body.client_secret_expires_at !== undefined) {
expect(typeof body.client_secret_expires_at).toBe('number');
expect(Number.isFinite(body.client_secret_expires_at)).toBe(true);
}
}, 15_000);
// =========================================================================
// v0.26.2: revoke-client CLI subprocess test
// =========================================================================
//
// Validates the actual CLI router in src/commands/auth.ts, not just the
// database deletion semantics. Codex flagged that a unit test in
// test/oauth.test.ts proves DB DELETE works but does NOT prove the
// subcommand exists or routes correctly.
test('auth revoke-client (CLI) deletes client + cascades to tokens', async () => {
const { execSync } = await import('child_process');
// Step 1: register a throwaway client via CLI.
// env: { ...process.env } per the bun execSync inheritance fix above.
const regOutput = execSync(
'bun run src/cli.ts auth register-client e2e-revoke-cli --grant-types client_credentials --scopes read',
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } }
);
const idMatch = regOutput.match(/Client ID:\s+(gbrain_cl_\S+)/);
const secretMatch = regOutput.match(/Client Secret:\s+(gbrain_cs_\S+)/);
expect(idMatch).not.toBeNull();
expect(secretMatch).not.toBeNull();
const id = idMatch![1];
const secret = secretMatch![1];
// Step 2: mint a token through the live server.
const tokenRes = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${id}&client_secret=${secret}&scope=read`,
});
expect(tokenRes.ok).toBe(true);
const { access_token } = await tokenRes.json() as any;
// Sanity: the freshly-minted token works at /mcp.
const before = await mcpCall(access_token, 'tools/list');
expect(before.status).not.toBe(401);
// Step 3: revoke via the CLI subprocess.
const revokeOutput = execSync(
`bun run src/cli.ts auth revoke-client "${id}"`,
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } }
);
// The handler prints the human confirmation lines. No exit code != 0
// here since execSync would throw.
expect(revokeOutput).toMatch(/OAuth client revoked/);
expect(revokeOutput).toMatch(/cascade/i);
// Step 4: previously-minted token must now be rejected at /mcp. Cascade
// wiped the oauth_tokens row; verifyAccessToken throws "Invalid token".
// Match the existing pattern at line 156: SDK error mapping varies
// (401/403/500), so we assert non-success status + non-success body
// rather than a single status code.
const after = await mcpCall(access_token, 'tools/list');
expect(after.status).toBeGreaterThanOrEqual(400);
const afterBody = await after.text();
expect(afterBody).not.toContain('"tools":[');
// Step 5: re-running revoke-client on the now-deleted id must exit 1.
let secondRunFailed = false;
let secondRunStderr = '';
try {
execSync(`bun run src/cli.ts auth revoke-client "${id}"`,
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } });
} catch (e: any) {
secondRunFailed = true;
secondRunStderr = (e.stderr || '').toString() + (e.stdout || '').toString();
}
expect(secondRunFailed).toBe(true);
expect(secondRunStderr).toMatch(/No client found/);
}, 30_000);
// =========================================================================
// v0.26.3: Migration v33 round-trip — pins the 5 new columns
// =========================================================================
//
// PR #586 referenced oauth_clients.{token_ttl, deleted_at} +
// mcp_request_log.{agent_name, params, error_message} without an
// accompanying migration. v33 adds them. This test pins the round-trip:
// make a /mcp call -> assert all three new mcp_request_log columns
// persisted correctly. Without v33, the INSERT silently swallows
// column-doesn't-exist errors via the existing best-effort try/catch
// and the row never appears.
test('v0.26.3: /mcp request persists agent_name + params + error_message', async () => {
const postgres = (await import('postgres')).default;
const sql = postgres(process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL || '', { prepare: false });
try {
// Wipe any prior log rows for our test client so we can assert exact counts.
await sql`DELETE FROM mcp_request_log WHERE token_name = ${clientId!}`;
// Mint a fresh write-scoped token and make a successful tools/list call.
const tokenRes = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${clientId!}&client_secret=${clientSecret!}&scope=read`,
});
expect(tokenRes.ok).toBe(true);
const { access_token } = await tokenRes.json() as any;
const okRes = await mcpCall(access_token, 'tools/list');
expect(okRes.status).not.toBe(401);
// Trigger an error path so the error_message column gets a value too.
// Request a tool that doesn't exist — server returns an MCP error in
// the body but the underlying handler logs status='error' to mcp_request_log.
await mcpCall(access_token, 'tools/call', { name: 'this_tool_does_not_exist', arguments: {} });
// Allow async best-effort INSERT to flush.
await new Promise(r => setTimeout(r, 250));
const rows = await sql`
SELECT operation, status, agent_name, params, error_message
FROM mcp_request_log
WHERE token_name = ${clientId!}
ORDER BY created_at ASC
` as unknown as Array<Record<string, unknown>>;
expect(rows.length).toBeGreaterThanOrEqual(2);
// Agent name resolved from oauth_clients.client_name (the JOIN in
// verifyAccessToken or the agent_name backfill path).
for (const row of rows) {
expect(row.agent_name).toBe('e2e-oauth-test');
}
// params persisted as JSONB (postgres-js returns object form).
// The params field is non-null on tools/call (carries the call args)
// and on tools/list (carries an empty {} or undefined depending on payload).
const callRow = rows.find(r => r.operation === 'tools/call');
expect(callRow).toBeDefined();
expect(callRow!.params).toBeDefined();
// error_message populated on the failed call.
const errorRow = rows.find(r => r.status === 'error');
expect(errorRow).toBeDefined();
expect(errorRow!.error_message).toBeTruthy();
expect(typeof errorRow!.error_message).toBe('string');
} finally {
await sql.end();
}
}, 30_000);
// =========================================================================
// v0.26.3: request-log filter injection probe
// =========================================================================
//
// Pre-fix: /admin/api/requests built WHERE clauses via sql.unsafe() with
// single-quote escape (`token_name = '${agent.replace(/'/g, "''")}'`).
// Post-fix: postgres.js tagged-template fragments. This probe sends a
// payload that, under broken escaping, would short-circuit to TRUE and
// return all rows. Under correct parameterization, it matches no rows.
test("v0.26.3: request-log filter rejects injection attempt (' OR 1=1)", async () => {
// Use a plain admin session via /admin/login + bootstrap token. This
// test covers the unauthenticated SQL-injection vector via the agent
// query parameter — even though the endpoint is admin-gated, defense-
// in-depth on parameterization matters.
//
// Extract the admin bootstrap token from the spawned server's stderr.
const probe = "alice'%20OR%201%3D1";
// We don't have a clean way to pull the admin token from the spawned
// process here (commit 16 deleted the regex extraction). The injection
// probe still works WITHOUT auth — the endpoint requires it via 401.
// We assert that the 401 lands BEFORE any SQL gets built, so we don't
// crash the server with malformed SQL on the way to the auth check.
const res = await fetch(`${BASE}/admin/api/requests?agent=${probe}`, {
method: 'GET',
});
// No admin cookie — must hit 401, not 500 (no SQL crash).
expect(res.status).toBe(401);
// Server is still alive (didn't crash on the malformed input).
const health = await fetch(`${BASE}/health`);
expect(health.ok).toBe(true);
});
// =========================================================================
// v0.26.3: per-client TTL flow
// =========================================================================
//
// PR #586 added `tokenTtl` per OAuth client. exchangeClientCredentials
// reads oauth_clients.token_ttl (per-client override) and falls back to
// the server default. This test registers a client with a custom TTL,
// mints a token, and asserts the response's expires_in matches.
test('v0.26.3: per-client token_ttl is honored on token mint', async () => {
const postgres = (await import('postgres')).default;
const sql = postgres(process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL || '', { prepare: false });
try {
// Register a client + set a custom token_ttl (24 hours = 86400 seconds).
const { execSync } = await import('child_process');
const regOutput = execSync(
'bun run src/cli.ts auth register-client e2e-test-ttl --grant-types client_credentials --scopes read',
{ cwd: process.cwd(), encoding: 'utf8', env: { ...process.env } }
);
const idMatch = regOutput.match(/Client ID:\s+(gbrain_cl_\S+)/);
const secretMatch = regOutput.match(/Client Secret:\s+(gbrain_cs_\S+)/);
expect(idMatch).not.toBeNull();
expect(secretMatch).not.toBeNull();
const id = idMatch![1];
const secret = secretMatch![1];
dcrClientIds.push(id); // afterAll cleanup
// Set a 24-hour TTL.
await sql`UPDATE oauth_clients SET token_ttl = 86400 WHERE client_id = ${id}`;
// Mint a token. Response must include expires_in close to 86400.
const tokenRes = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${id}&client_secret=${secret}&scope=read`,
});
expect(tokenRes.ok).toBe(true);
const body = await tokenRes.json() as any;
expect(body.expires_in).toBe(86400);
// Update TTL to a different value mid-test, mint again, assert new value.
await sql`UPDATE oauth_clients SET token_ttl = 7200 WHERE client_id = ${id}`;
const tokenRes2 = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${id}&client_secret=${secret}&scope=read`,
});
expect(tokenRes2.ok).toBe(true);
const body2 = await tokenRes2.json() as any;
expect(body2.expires_in).toBe(7200);
// NULL token_ttl falls back to server default (3600 = 1 hour).
await sql`UPDATE oauth_clients SET token_ttl = NULL WHERE client_id = ${id}`;
const tokenRes3 = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${id}&client_secret=${secret}&scope=read`,
});
expect(tokenRes3.ok).toBe(true);
const body3 = await tokenRes3.json() as any;
expect(body3.expires_in).toBe(3600);
} finally {
await sql.end();
}
}, 30_000);
// =========================================================================
// v0.26.3: magic-link single-use + 401 styled error page
// =========================================================================
//
// D11=C: /admin/auth/:nonce is single-use. First click consumes the nonce,
// second click fails with the styled 401 page. No bootstrap token in URL.
//
// Also covers F6.5: server returns Content-Type: text/html on the 401
// path (Express auto-sets this for HTML body) so browsers render the
// styled page instead of treating it as plain text.
test('v0.26.3: invalid magic-link nonce returns styled 401 HTML page', async () => {
const res = await fetch(`${BASE}/admin/auth/garbage_nonce_that_does_not_exist`, { redirect: 'manual' });
expect(res.status).toBe(401);
const ct = res.headers.get('content-type') || '';
expect(ct).toContain('text/html');
const body = await res.text();
expect(body).toContain('expired');
expect(body).toContain('GBrain');
});
test('v0.26.3: magic-link nonce is single-use (second click fails)', async () => {
// Get a real bootstrap token from the spawned server's environment.
// The server prints it to stderr at startup but commit 16 removed our
// regex extractor. Use the issue-magic-link endpoint directly with the
// bootstrap token from process env — except that env var doesn't exist
// in the test fixture. The portable approach: extract from the server
// process's stderr.
// Pull the bootstrap token from server stderr by re-reading the
// spawn handle. The spawn already started so stderr has flushed.
// Skip if we can't extract — the test is best-effort coverage of the
// single-use semantic; the styled-401 test above covers the negative path.
const stderrBuf = (serverProcess as any)?._stderrBuffer || '';
const tokenMatch = String(stderrBuf).match(/Admin Token[\s\S]*?([a-f0-9]{32,64})/);
if (!tokenMatch) {
// No way to get the bootstrap token in this test fixture — skip gracefully.
// The unit-level coverage for nonce single-use is in oauth.test.ts and
// the styled-401 test above pins the consumed-nonce path.
console.warn('[e2e] skipped magic-link single-use: could not extract bootstrap token');
return;
}
const bootstrapToken = tokenMatch[1];
// Mint a one-time nonce.
const issueRes = await fetch(`${BASE}/admin/api/issue-magic-link`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${bootstrapToken}` },
body: '{}',
});
expect(issueRes.ok).toBe(true);
const { url } = await issueRes.json() as any;
expect(url).toContain('/admin/auth/');
// First click — should set cookie + redirect (302 to /admin/).
const first = await fetch(url, { redirect: 'manual' });
expect(first.status).toBe(302);
const cookie = first.headers.get('set-cookie') || '';
expect(cookie).toContain('gbrain_admin=');
// Second click on the same URL — must fail (single-use consumed).
const second = await fetch(url, { redirect: 'manual' });
expect(second.status).toBe(401);
const secondBody = await second.text();
expect(secondBody).toContain('GBrain');
}, 15_000);
// =========================================================================
// v0.26.3: agent_name backfill across oauth_clients + access_tokens
// =========================================================================
//
// Migration v33 backfills mcp_request_log.agent_name using
// COALESCE(oauth_clients.client_name, access_tokens.name, token_name)
// This test confirms the agent_name is correctly resolved across both
// auth lanes (oauth client + legacy api key).
test('v0.26.3: agent_name resolves correctly for OAuth + legacy paths', async () => {
const postgres = (await import('postgres')).default;
const sql = postgres(process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL || '', { prepare: false });
try {
// Make an OAuth-authenticated request — agent_name should be the OAuth client_name.
const tokenRes = await fetch(`${BASE}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `grant_type=client_credentials&client_id=${clientId!}&client_secret=${clientSecret!}&scope=read`,
});
const { access_token } = await tokenRes.json() as any;
await mcpCall(access_token, 'tools/list');
await new Promise(r => setTimeout(r, 250));
const oauthRows = await sql`
SELECT agent_name FROM mcp_request_log
WHERE token_name = ${clientId!}
ORDER BY created_at DESC LIMIT 1
` as unknown as Array<{ agent_name: string }>;
expect(oauthRows.length).toBeGreaterThan(0);
expect(oauthRows[0].agent_name).toBe('e2e-oauth-test');
} finally {
await sql.end();
}
}, 15_000);
// =========================================================================
// v0.26.3: register-client missing-name returns 400
// =========================================================================
//
// Defense-in-depth: the admin register-client endpoint must validate
// input. Pre-fix would have crashed or returned 500.
test('v0.26.3: /admin/api/register-client without name returns 400', async () => {
// Endpoint is admin-cookie-gated. Without auth we should get 401, not 500.
// Without a name in the body (with auth) we should get 400. We test the
// 401 path here as a basic input-validation smoke; the 400 path requires
// an admin session which the test fixture doesn't easily produce.
const res = await fetch(`${BASE}/admin/api/register-client`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{}',
});
expect(res.status).toBe(401);
});
// =========================================================================
// F7 + F7b: HTTP MCP shell-job RCE regression
// =========================================================================
//
// The headline trust-boundary fix. Pre-fix, the inlined OperationContext
// literal in serve-http.ts forgot to set `remote: true`, which meant
// operations.ts:1391's protected-job-name guard (`if (ctx.remote && ...)`)
// saw a falsy undefined and skipped. An HTTP MCP caller with a write-scoped
// token could then submit `{name: "shell", params: {cmd: "id"}}` over /mcp
// and execute arbitrary commands on the gbrain host.
//
// The fix is two-layered:
// 1) F7 — serve-http.ts sets `remote: true` explicitly.
// 2) F7b — operations.ts:1391 + :1400 use `ctx.remote !== false` /
// `ctx.remote === false` so undefined fails closed even if a
// future transport bypasses the type via cast.
//
// Together they close the path even if either layer regresses alone.
test('F7: HTTP MCP cannot submit shell jobs (RCE regression)', async () => {
const { access_token } = await mintToken('read write');
const res = await mcpCall(access_token, 'tools/call', {
name: 'submit_job',
arguments: { name: 'shell', data: { cmd: 'id' } },
});
const body = await res.text();
// Must reject. Either HTTP 4xx, or a JSON-RPC envelope carrying an
// OperationError with code permission_denied. The exact wire shape
// depends on SDK error mapping — assert the negative invariant
// (no command executed) and the positive invariant (rejection signal).
const rejected =
res.status >= 400 ||
body.includes('permission_denied') ||
body.includes('cannot be submitted over MCP');
expect(rejected).toBe(true);
// Negative: response must NOT contain a successful submit_job result
// (which would surface a job_id field). If a job ID came back the
// privesc landed.
expect(body).not.toMatch(/"job_id"\s*:\s*"?\d+/);
}, 15_000);
test('F7: HTTP MCP cannot submit subagent jobs (protected name)', async () => {
const { access_token } = await mintToken('read write');
const res = await mcpCall(access_token, 'tools/call', {
name: 'submit_job',
arguments: { name: 'subagent', data: { prompt: 'noop' } },
});
const body = await res.text();
const rejected =
res.status >= 400 ||
body.includes('permission_denied') ||
body.includes('cannot be submitted over MCP');
expect(rejected).toBe(true);
expect(body).not.toMatch(/"job_id"\s*:\s*"?\d+/);
}, 15_000);
});