mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
feat(demo): the shared demo account keeps its keys, account and language
Every visitor of a demo instance signs in as the same account, so one visitor's API key, MCP token, OAuth client, passkey or new email address became everybody's. These writes now answer 403 for the demo account through refuseForDemoAccount in demo-write.ts: creating API keys, creating and deleting MCP tokens, registering, rotating and deleting OAuth clients, approving an app and ending a connected app, changing the name, address or picture, and enrolling, renaming or deleting passkeys. Password, 2FA, account deletion, sessions and the own AI connection were refused already. The demo account's language is fixed to English: a single settings write of the language answers unchanged, a bulk save drops it and keeps the rest. The MCP settings tool is held back for the demo account by the tool gate, as every write tool is. The admin of a demo instance and every real instance are untouched.
This commit is contained in:
@@ -12,11 +12,23 @@
|
||||
*/
|
||||
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { isDemoEmail } from '../../../src/nest/common/demo';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../../../src/nest/common/demo-write';
|
||||
import {
|
||||
DEMO_ACCOUNT_REFUSAL,
|
||||
DEMO_WRITE_ERROR,
|
||||
isDemoAccount,
|
||||
isDemoLockedSetting,
|
||||
isDemoWriteBlocked,
|
||||
refuseForDemoAccount,
|
||||
withoutDemoLockedSettings,
|
||||
} from '../../../src/nest/common/demo-write';
|
||||
import { DomainError } from '../../../src/nest/common/domain-error';
|
||||
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/nest/common/demo', () => ({ isDemoEmail: vi.fn(() => false) }));
|
||||
vi.mock('../../../src/nest/common/demo', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../../../src/nest/common/demo')>()),
|
||||
isDemoEmail: vi.fn(() => false),
|
||||
}));
|
||||
|
||||
const env = new RuntimeEnvService();
|
||||
|
||||
@@ -60,3 +72,50 @@ describe('isDemoWriteBlocked', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the demo account locks', () => {
|
||||
const demo = { email: 'demo@trek.app' };
|
||||
const other = { email: 'alice@example.com' };
|
||||
const onDemoInstance = () => {
|
||||
process.env.DEMO_MODE = 'true';
|
||||
vi.mocked(isDemoEmail).mockImplementation((email) => email === 'demo@trek.app');
|
||||
};
|
||||
|
||||
it('DEMO-WRITE-006: isDemoAccount is the demo address on a demo instance, and nothing else', () => {
|
||||
vi.mocked(isDemoEmail).mockImplementation((email) => email === 'demo@trek.app');
|
||||
expect(isDemoAccount(demo)).toBe(false);
|
||||
onDemoInstance();
|
||||
expect(isDemoAccount(demo)).toBe(true);
|
||||
expect(isDemoAccount(other)).toBe(false);
|
||||
expect(isDemoAccount(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('DEMO-WRITE-007: refuseForDemoAccount throws a 403 with the message for the demo account only', () => {
|
||||
onDemoInstance();
|
||||
expect(() => refuseForDemoAccount(other, DEMO_ACCOUNT_REFUSAL.apiKeys)).not.toThrow();
|
||||
let thrown: unknown;
|
||||
try {
|
||||
refuseForDemoAccount(demo, DEMO_ACCOUNT_REFUSAL.apiKeys);
|
||||
} catch (err) {
|
||||
thrown = err;
|
||||
}
|
||||
expect(thrown).toBeInstanceOf(DomainError);
|
||||
expect((thrown as DomainError).getStatus()).toBe(403);
|
||||
expect((thrown as DomainError).getResponse()).toEqual({ error: 'API keys cannot be created in demo mode.' });
|
||||
});
|
||||
|
||||
it('DEMO-WRITE-008: the language is the setting the demo account keeps', () => {
|
||||
onDemoInstance();
|
||||
expect(isDemoLockedSetting(demo, 'language')).toBe(true);
|
||||
expect(isDemoLockedSetting(demo, 'temperature_unit')).toBe(false);
|
||||
expect(isDemoLockedSetting(other, 'language')).toBe(false);
|
||||
});
|
||||
|
||||
it('DEMO-WRITE-009: a bulk save of the demo account loses the language and keeps the rest', () => {
|
||||
const settings = { language: 'de', distance_unit: 'imperial' };
|
||||
expect(withoutDemoLockedSettings(demo, settings)).toBe(settings);
|
||||
onDemoInstance();
|
||||
expect(withoutDemoLockedSettings(demo, settings)).toEqual({ distance_unit: 'imperial' });
|
||||
expect(withoutDemoLockedSettings(other, settings)).toBe(settings);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* The OAuth half of the MCP lock on a demo instance: the shared demo account
|
||||
* cannot register, rotate or delete an OAuth client, end a connected app, or
|
||||
* approve an app's request for access. Turning a request down stays possible.
|
||||
* Every refusal comes before the service is asked, so nothing is written.
|
||||
*/
|
||||
import { DEMO_ACCOUNT_REFUSAL } from '../../../src/nest/common/demo-write';
|
||||
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
|
||||
import { OauthApiController } from '../../../src/nest/oauth/oauth-api.controller';
|
||||
import type { OauthService } from '../../../src/nest/oauth/oauth.service';
|
||||
import type { User } from '../../../src/types';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
|
||||
import type { Request } from 'express';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
|
||||
|
||||
const demo = { id: 1, role: 'user', email: 'demo@trek.app' } as User;
|
||||
const other = { id: 2, role: 'user', email: 'alice@example.test' } as User;
|
||||
const req = { ip: '1.2.3.4', headers: {} } as unknown as Request;
|
||||
|
||||
function service() {
|
||||
return {
|
||||
mcpEnabled: vi.fn().mockResolvedValue(true),
|
||||
validateAuthorizeRequest: vi.fn().mockResolvedValue({ valid: true, scopes: ['trips:read'], resource: null }),
|
||||
saveConsent: vi.fn(),
|
||||
createAuthCode: vi.fn().mockResolvedValue('the_code'),
|
||||
createOAuthClient: vi.fn().mockResolvedValue({ client_id: 'c1', client_secret: 's' }),
|
||||
rotateOAuthClientSecret: vi.fn().mockResolvedValue({ client_secret: 's2' }),
|
||||
deleteOAuthClient: vi.fn().mockResolvedValue({}),
|
||||
revokeSession: vi.fn().mockResolvedValue({}),
|
||||
};
|
||||
}
|
||||
|
||||
let svc: ReturnType<typeof service>;
|
||||
const controller = () => new OauthApiController(svc as unknown as OauthService, new RateLimitService());
|
||||
|
||||
async function refusal(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
try {
|
||||
await fn();
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(HttpException);
|
||||
const e = err as HttpException;
|
||||
return { status: e.getStatus(), body: e.getResponse() };
|
||||
}
|
||||
throw new Error('expected a refusal');
|
||||
}
|
||||
|
||||
const consent = (approved: boolean) => ({
|
||||
client_id: 'c1',
|
||||
redirect_uri: 'https://app.example.test/cb',
|
||||
scope: 'trips:read',
|
||||
state: 'st',
|
||||
code_challenge: 'cc',
|
||||
code_challenge_method: 'S256' as const,
|
||||
approved,
|
||||
});
|
||||
|
||||
const MCP_REFUSAL = { status: 403, body: { error: DEMO_ACCOUNT_REFUSAL.mcp } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('DEMO_MODE', 'true');
|
||||
svc = service();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe('OAuth routes for the demo account', () => {
|
||||
it('OAUTH-DEMO-001: registering, rotating and deleting a client are refused before the service runs', async () => {
|
||||
expect(await refusal(() => controller().createClient(demo, { name: 'CLI', allowed_scopes: ['a'] }, req))).toEqual(
|
||||
MCP_REFUSAL,
|
||||
);
|
||||
expect(await refusal(() => controller().rotateClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => controller().deleteClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.createOAuthClient).not.toHaveBeenCalled();
|
||||
expect(svc.rotateOAuthClientSecret).not.toHaveBeenCalled();
|
||||
expect(svc.deleteOAuthClient).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-002: a connected app cannot be ended from the demo account', async () => {
|
||||
expect(await refusal(() => controller().revokeSession(demo, '7', req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.revokeSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-003: approving an app is refused, turning it down still redirects', async () => {
|
||||
expect(await refusal(() => controller().authorize(demo, consent(true), req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.saveConsent).not.toHaveBeenCalled();
|
||||
expect(svc.createAuthCode).not.toHaveBeenCalled();
|
||||
|
||||
const denied = await controller().authorize(demo, consent(false), req);
|
||||
expect((denied as { redirect: string }).redirect).toContain('error=access_denied');
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-004: every other account of the demo instance keeps the routes', async () => {
|
||||
expect(await controller().createClient(other, { name: 'CLI', allowed_scopes: ['a'] }, req)).toEqual({
|
||||
client_id: 'c1',
|
||||
client_secret: 's',
|
||||
});
|
||||
const approved = await controller().authorize(other, consent(true), req);
|
||||
expect((approved as { redirect: string }).redirect).toContain('code=the_code');
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-005: off a demo instance the demo address keeps them too', async () => {
|
||||
vi.stubEnv('DEMO_MODE', 'false');
|
||||
expect(await controller().deleteClient(demo, 'c1', req)).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user