mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
fix(server): tighten the guard, audit and token paths
Permissions cached an empty map before it had loaded, so a DB error during the load cached permissive defaults and every later check answered from that cache - fail-open, and it never expired. The rate limiter grew its per-IP map forever; the GC the legacy version had was dropped in the fold. Idempotency did a check-then-insert, so two concurrent duplicates both ran the handler. Audit rows recorded whatever X-Forwarded-For said, which the client writes, and the request-body debug logger redacted by exact key match - it missed refresh tokens, api keys and PKCE verifiers. FORCE_HTTPS built its redirect from the client's Host header. CSP asked for unsafe-eval on top of the wasm-unsafe-eval it actually needs. The OAuth deny path redirected to an unvalidated URI, ws-token minting had no rate limit at all, and PKCE was skipped when code_verifier was absent rather than refused. An MCP session resumed with a token whose scopes had since been reduced kept the capability set it was created with, and the session cap was checked before the async registration that increments it. Plugin frame containment was lexical, so a symlink out of the directory was never noticed, and instance config accepted keys the manifest never declared.
This commit is contained in:
@@ -250,6 +250,22 @@ describe('MCP session management', () => {
|
||||
expect(sessions.has(firstSessionId)).toBe(false); // the least-recently-active one made room
|
||||
});
|
||||
|
||||
it('MCP-006 — initializes that race past the cap check are trimmed when they register', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'mcp'").run();
|
||||
|
||||
const sessionsForUser = () => [...sessions.values()].filter((s) => s.userId === user.id).length;
|
||||
|
||||
for (let i = 0; i < 20; i++) await createSession(user.id);
|
||||
expect(sessionsForUser()).toBe(20);
|
||||
|
||||
// All five read the same pre-registration count, so all five pass the cap
|
||||
// check. Registration is where the overshoot has to be given back — nothing
|
||||
// else drops these entries before the TTL sweep, and each holds a server.
|
||||
await Promise.all([1, 2, 3, 4, 5].map(() => createSession(user.id)));
|
||||
expect(sessionsForUser()).toBe(20);
|
||||
});
|
||||
|
||||
it('MCP — session resumption with valid mcp-session-id', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'mcp'").run();
|
||||
@@ -433,6 +449,43 @@ describe('MCP transport parity pins (Nest-hosted /mcp)', () => {
|
||||
expect(res.headers['www-authenticate']).toBe(EXPECTED_CHALLENGE);
|
||||
});
|
||||
|
||||
it('MCP-P08b — a narrower token cannot resume a session that was created with wider scopes', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const created = oauthSvc.createOAuthClient(user.id, 'Scope Test Client', ['https://client.example.com/cb'], ['trips:read', 'trips:write']);
|
||||
const clientId = (created.client as { client_id: string }).client_id;
|
||||
const wide = oauthSvc.issueTokens(clientId, user.id, ['trips:read', 'trips:write'], null, MCP_AUDIENCE);
|
||||
const narrow = oauthSvc.issueTokens(clientId, user.id, ['trips:read'], null, MCP_AUDIENCE);
|
||||
const sessionId = await createSession(wide.access_token);
|
||||
|
||||
// The tool surface was registered from the wide set; resuming with the
|
||||
// narrow one would ride it.
|
||||
const res = await request(app)
|
||||
.post('/mcp')
|
||||
.set('Authorization', `Bearer ${narrow.access_token}`)
|
||||
.set('mcp-session-id', sessionId)
|
||||
.send({ jsonrpc: '2.0', method: 'tools/list', id: 2 });
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body).toEqual({ error: 'Session was created with different scopes' });
|
||||
expect(res.headers['www-authenticate']).toContain('error="insufficient_scope"');
|
||||
});
|
||||
|
||||
it('MCP-P08c — the same scopes in a different order still resume', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const created = oauthSvc.createOAuthClient(user.id, 'Scope Order Client', ['https://client.example.com/cb'], ['trips:read', 'trips:write']);
|
||||
const clientId = (created.client as { client_id: string }).client_id;
|
||||
const first = oauthSvc.issueTokens(clientId, user.id, ['trips:read', 'trips:write'], null, MCP_AUDIENCE);
|
||||
const reordered = oauthSvc.issueTokens(clientId, user.id, ['trips:write', 'trips:read'], null, MCP_AUDIENCE);
|
||||
const sessionId = await createSession(first.access_token);
|
||||
|
||||
const res = await request(app)
|
||||
.post('/mcp')
|
||||
.set('Authorization', `Bearer ${reordered.access_token}`)
|
||||
.set('mcp-session-id', sessionId)
|
||||
.set('Accept', 'application/json, text/event-stream')
|
||||
.send({ jsonrpc: '2.0', method: 'tools/list', id: 2 });
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it('MCP-P09 — a static-token session carries the deprecation notice on the first tool call only', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { rawToken } = createMcpToken(testDb, user.id);
|
||||
|
||||
@@ -820,22 +820,44 @@ describe('POST /api/oauth/authorize', () => {
|
||||
|
||||
it('OAUTH-030 — user denied returns redirect with error=access_denied', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const r = createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { challenge } = makePkce();
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/oauth/authorize')
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.send({
|
||||
approved: false,
|
||||
client_id: 'any',
|
||||
client_id: r.client!.client_id,
|
||||
redirect_uri: 'https://app.example.com/cb',
|
||||
scope: 'trips:read',
|
||||
code_challenge: 'c',
|
||||
code_challenge: challenge,
|
||||
code_challenge_method: 'S256',
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.redirect).toContain('error=access_denied');
|
||||
});
|
||||
|
||||
it('OAUTH-030b — a denial for an unregistered redirect_uri is refused, not redirected', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const r = createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { challenge } = makePkce();
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/oauth/authorize')
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.send({
|
||||
approved: false,
|
||||
client_id: r.client!.client_id,
|
||||
redirect_uri: 'https://attacker.example.com/cb',
|
||||
scope: 'trips:read',
|
||||
code_challenge: challenge,
|
||||
code_challenge_method: 'S256',
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toBe('invalid_redirect_uri');
|
||||
});
|
||||
|
||||
it('OAUTH-031 — invalid params returns 400', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { challenge } = makePkce();
|
||||
|
||||
Reference in New Issue
Block a user