mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-27 22:15:33 +00:00
* v0.41.3.0 fix(security/mcp): OAuth CORS lockdown, pre-register without DCR, validator surface
Three expanded cherry-picks plus codex-surfaced live-CORS fix, parser
rewrite, atomicity fix, DCR validator gate, SECURITY.md reconciliation.
What ships
- gbrain auth register-client gets --redirect-uri (repeatable) and
--token-endpoint-auth-method flags so the SECURITY.md-recommended
"pre-register without --enable-dcr" path actually works for claude.ai
and ChatGPT custom connectors.
- ALLOWED_TOKEN_ENDPOINT_AUTH_METHODS = {client_secret_post,
client_secret_basic, none} validator gates all three registration
entry points (CLI, admin endpoint, DCR /register) so --enable-dcr is
no longer the looser path.
- Live Express OAuth server (/mcp, /token, /authorize, /register,
/revoke) was using default-wide-open cors() middleware — every
origin could complete a token exchange from a logged-in operator's
browser. Now default-deny; allowlist via GBRAIN_HTTP_CORS_ORIGIN.
- GBRAIN_HTTP_TRUST_PROXY env var on Express server with the same
semantics as the legacy bearer transport already had. Default
'loopback' preserved. SECURITY.md doc rewritten to match reality
(was lying that trust proxy was "disabled by default" while code
hardcoded 'loopback').
- Admin endpoint registration now atomic — INSERT-then-UPDATE for
public clients replaced with single INSERT via the new
registerClientManual(..., tokenEndpointAuthMethod) parameter (codex
outside-voice F4 catch).
- Legacy transport corsHeaders + corsPreflightHeaders consolidated
into one function gated on the allowlist for BOTH Allow-Origin and
Allow-Methods/Headers (codex F1; #983 thematically).
Surfaced by D7 codex outside-voice review on the v0.41.3 plan:
F1 (live Express CORS wide-open), F2 (indexOf parser couldn't do
repeatable flags), F3 (client_secret_basic missing from validator),
F4 (admin endpoint INSERT-then-UPDATE atomicity), F5 (DCR path
bypassed validator), F6 (env var already existed on legacy transport),
F7 (SECURITY.md vs impl doc disagreement).
Tests: 183 directly-touched cases green. Three new test files
(test/serve-http-trust-proxy.test.ts, test/serve-http-cors.test.ts,
test/auth-register-client-args.test.ts) + 18 new oauth.test.ts cases
+ 4 IRON RULE CORS preflight regressions.
Plan: ~/.claude/plans/system-instruction-you-are-working-wise-piglet.md
(D1-D11 captured, codex outside-voice integrated, GSTACK REVIEW REPORT
verdict CLEARED).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(test): audit-writer readRecent calendar-boundary flake
writer.log() uses real `new Date()` for filename computation, but the
test mocked `now` to 2026-05-22. When CI runs on a date in a different
ISO week (e.g. 2026-05-25 W22 vs the mocked W21), log() writes to one
file but readRecent(now) reads a different one — zero events overlap,
expect(2).toBe(0) fails.
Fix: write events directly to the file matching the test's mocked
`now` via writer.computeFilename(now), same pattern the cross-week
straddle test (line 234+) already used for the previous-week event.
Pre-existing test bug, surfaced when CI rolled past the week boundary
the original author wrote against. Not introduced by v0.41.3.0; fix
included here because /ship found it.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
120 lines
6.3 KiB
TypeScript
120 lines
6.3 KiB
TypeScript
/**
|
|
* Structural assertions for fix-wave fixes whose behavior is best verified
|
|
* at source-shape level rather than via a runtime harness:
|
|
*
|
|
* - #1125 query drain cache writes — assert cli.ts awaits the drain after
|
|
* the query op completes.
|
|
* - #1090 admin embed — assert the two-tier resolution (cwd path + embedded
|
|
* manifest fallback) is in serve-http.ts and consumes ADMIN_ASSETS.
|
|
* - #1077 admin register-client PKCE — assert the admin endpoint honors
|
|
* grantTypes / redirectUris / tokenEndpointAuthMethod from the body.
|
|
* - #1100 PGLite phaseASchema — assert the v0.11.0 orchestrator routes
|
|
* in-process when the engine is pglite (not via execSync subprocess).
|
|
* - #1124 query no-expand — assert the parseOpArgs negation logic exists.
|
|
*
|
|
* Source-grep regression tests are the right tool when the rule is "this
|
|
* specific line shape must stay present"; a behavioral test would either
|
|
* duplicate what an E2E covers or require heavy mocking that hides the
|
|
* regression behind a test seam.
|
|
*/
|
|
import { describe, test, expect } from 'bun:test';
|
|
import { readFileSync } from 'fs';
|
|
|
|
describe('v0.36.1.x #1125 — query drain cache writes before CLI exit', () => {
|
|
test("cli.ts awaits awaitPendingSearchCacheWrites for the 'query' op", () => {
|
|
const src = readFileSync('src/cli.ts', 'utf8');
|
|
// Sequence: 'query' op match → import the drain → await
|
|
expect(src).toMatch(/op\.name\s*===\s*'query'[\s\S]{0,200}awaitPendingSearchCacheWrites/);
|
|
expect(src).toMatch(/await\s+awaitPendingSearchCacheWrites\(\)/);
|
|
});
|
|
|
|
test('hybrid.ts exports the drain helper + trackCacheWrite', () => {
|
|
const src = readFileSync('src/core/search/hybrid.ts', 'utf8');
|
|
expect(src).toMatch(/export async function awaitPendingSearchCacheWrites/);
|
|
expect(src).toMatch(/pendingCacheWrites\.add\(promise\)/);
|
|
expect(src).toMatch(/trackCacheWrite\(/);
|
|
});
|
|
});
|
|
|
|
describe('v0.36.1.x #1090 — admin embed two-tier resolution', () => {
|
|
test('serve-http.ts uses ADMIN_ASSETS manifest when admin/dist is not next to cwd', () => {
|
|
const src = readFileSync('src/commands/serve-http.ts', 'utf8');
|
|
expect(src).toMatch(/import\(['"]\.\.\/admin-embedded/);
|
|
expect(src).toMatch(/ADMIN_ASSETS/);
|
|
expect(src).toMatch(/ADMIN_INDEX_HTML/);
|
|
// Two-tier: dev path (cwd-relative admin/dist) AND embedded manifest fallback
|
|
expect(src).toMatch(/useDevPath/);
|
|
});
|
|
|
|
test('src/admin-embedded.ts is auto-generated with file: imports', () => {
|
|
const src = readFileSync('src/admin-embedded.ts', 'utf8');
|
|
expect(src).toMatch(/AUTO-GENERATED/);
|
|
expect(src).toMatch(/with \{ type: 'file' \}/);
|
|
expect(src).toMatch(/export const ADMIN_ASSETS/);
|
|
expect(src).toMatch(/export const ADMIN_INDEX_HTML/);
|
|
});
|
|
|
|
test('build script + CI guard exist', () => {
|
|
const buildSrc = readFileSync('scripts/build-admin-embedded.ts', 'utf8');
|
|
expect(buildSrc).toMatch(/walk\(DIST/);
|
|
expect(buildSrc).toMatch(/with \{ type: 'file' \}/);
|
|
const guard = readFileSync('scripts/check-admin-embedded.sh', 'utf8');
|
|
expect(guard).toMatch(/git diff --exit-code -- src\/admin-embedded\.ts/);
|
|
});
|
|
});
|
|
|
|
describe('v0.36.1.x #1077 — admin register-client supports PKCE public clients', () => {
|
|
test('admin endpoint reads grantTypes / redirectUris / tokenEndpointAuthMethod from request body', () => {
|
|
const src = readFileSync('src/commands/serve-http.ts', 'utf8');
|
|
// The destructure must surface name / tokenTtl / grantTypes /
|
|
// redirectUris / tokenEndpointAuthMethod from req.body. v0.39.3.0
|
|
// WARN-9 (PR #1308) moved `scopes` to a separate read line that
|
|
// accepts BOTH `scopes` (admin SPA) AND `scope` (OAuth wire singular)
|
|
// via `?? `, so this regex no longer requires `scopes` in the inline
|
|
// destructure — it's separately covered by the scope-source check
|
|
// below.
|
|
expect(src).toMatch(/const\s+\{\s*name,\s*(?:[^}]*?,\s*)?tokenTtl,\s*grantTypes,\s*redirectUris,\s*tokenEndpointAuthMethod\s*\}\s*=\s*req\.body/);
|
|
// v0.39.3.0 WARN-9: the route must still read a `scope`/`scopes` field
|
|
// (under either name) from req.body. Pin the fallback pattern so the
|
|
// PKCE-fix regression contract stays load-bearing.
|
|
expect(src).toMatch(/req\.body[^;]*scopes\s*\?\?\s*[^;]*scope\b/);
|
|
// v0.41.3 (T4 atomicity fix, codex F4): admin endpoint now validates
|
|
// tokenEndpointAuthMethod via the shared validator and passes it to
|
|
// registerClientManual as a positional arg. Pre-v0.41.3 the route did
|
|
// INSERT (confidential) → UPDATE (NULL out secret_hash) for the 'none'
|
|
// case, which left a confidential row stranded if the UPDATE failed.
|
|
// Atomic now: one INSERT writes the correct shape; no post-insert
|
|
// UPDATE block (the regex deliberately asserts the post-insert UPDATE
|
|
// is GONE).
|
|
expect(src).toMatch(/validateTokenEndpointAuthMethod\(tokenEndpointAuthMethod\)/);
|
|
expect(src).toMatch(/registerClientManual\([^)]*validatedAuthMethod[^)]*\)/);
|
|
// Regression guard: post-insert UPDATE flipping client_secret_hash to
|
|
// NULL based on a runtime check is exactly the non-atomic pattern T4
|
|
// killed. Re-introducing it brings back codex F4.
|
|
expect(src).not.toMatch(/UPDATE oauth_clients SET client_secret_hash = NULL, token_endpoint_auth_method = 'none'/);
|
|
});
|
|
});
|
|
|
|
describe('v0.36.1.x #1100 — PGLite v0.11.0 phaseASchema routes in-process', () => {
|
|
test('phaseASchema branches on pglite and calls initSchema directly', () => {
|
|
const src = readFileSync('src/commands/migrations/v0_11_0.ts', 'utf8');
|
|
expect(src).toMatch(/cfg\?\.engine\s*===\s*'pglite'/);
|
|
expect(src).toMatch(/eng\.initSchema\(\)/);
|
|
expect(src).toMatch(/await\s+phaseASchema/);
|
|
});
|
|
|
|
test('apply-migrations skips pre-flight schema-version probe on PGLite', () => {
|
|
const src = readFileSync('src/commands/apply-migrations.ts', 'utf8');
|
|
expect(src).toMatch(/skipPreflight\s*=\s*cfg\.engine\s*===\s*'pglite'/);
|
|
});
|
|
});
|
|
|
|
describe('v0.36.1.x #1124 — query --no-expand actually negates expand', () => {
|
|
test("cli.ts parseOpArgs handles --no-<key> as boolean negation", () => {
|
|
const src = readFileSync('src/cli.ts', 'utf8');
|
|
expect(src).toMatch(/arg\.startsWith\(['"]--no-['"]\)/);
|
|
expect(src).toMatch(/positiveDef\?\.type\s*===\s*'boolean'/);
|
|
expect(src).toMatch(/params\[positiveKey\]\s*=\s*false/);
|
|
});
|
|
});
|