mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-11 19:26:56 +00:00
865d792874
* feat(pricing): collapse to two tiers (Community + Admiral) Collapse Sencho's pricing from three tiers (Community / Skipper / Admiral) to two: a generous free Community tier and a single paid Admiral tier. The Skipper tier is removed. Now free in Community: auto-heal, auto-update, scheduled operations, webhooks, notification routing, Fleet Actions and bulk operations, SSO preset providers (Google / GitHub / Okta), unlimited users with admin and viewer roles, and deploy safety (atomic deploys, auto-rollback, and one-click rollback). Admiral (paid) is focused on running and governing a fleet: blueprints, Fleet Secrets, deploy enforcement, vulnerability report export, audit log, host console, private registries, mesh networking, node cordon, managed cloud backup, LDAP / Active Directory SSO, and the advanced RBAC roles (deployer, node-admin, auditor) with per-resource scoped assignments. Internally the license variant distinction is removed so tier is binary (community / paid). License validation still verifies the Lemon Squeezy store and product before granting paid status. Docs and the contributor guide are updated to the two-tier model. * docs(pricing): correct licensing page to two-tier pricing and tidy stale tier wording The licensing docs page kept the old Admiral pricing plus a Founder Lifetime column and an Enterprise paragraph after the two-tier collapse. Update it to $12/month or $99/year, drop the lifetime and Enterprise content, and link to the pricing page for current pricing. Also fix stale "Skipper" wording in CLA.md, SUPPORT.md, one test title, and three test comments. Historical CHANGELOG entries and the retired-Skipper license-guard test are intentionally left as-is. * docs: align licensing and SSO pages with the two-tier model Correct the SSO overview so the Google, GitHub, and Okta presets read as available on every tier, matching the provider table; only LDAP and Active Directory require Sencho Admiral. Remove the lifetime-plan references from the licensing, settings, and troubleshooting pages so they reflect subscription-only Admiral pricing. * fix(rbac): omit scoped permissions from /me on the Community tier Scoped role assignments only take effect on the paid tier, but GET /api/permissions/me returned them unconditionally, so a downgraded instance with leftover assignments rendered per-resource affordances the API then rejected with 403. The endpoint now mirrors the permission middleware and includes scoped permissions only on the paid tier. Adds a regression test covering the downgrade case. * docs: use custom-pricing wording on the contact page The two-tier model has no Enterprise tier; reword the contact page's enterprise pricing/deals to custom pricing/deals so it does not imply a tier that no longer exists.
427 lines
16 KiB
TypeScript
427 lines
16 KiB
TypeScript
/**
|
|
* Tests for the Host Console feature: environment sanitization, session limits,
|
|
* and console-token RBAC enforcement.
|
|
*/
|
|
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
|
|
import request from 'supertest';
|
|
import jwt from 'jsonwebtoken';
|
|
import bcrypt from 'bcrypt';
|
|
import * as path from 'path';
|
|
import { EventEmitter } from 'events';
|
|
import * as pty from 'node-pty';
|
|
import { setupTestDb, cleanupTestDb, TEST_USERNAME, TEST_JWT_SECRET } from './helpers/setupTestDb';
|
|
|
|
let tmpDir: string;
|
|
let app: import('express').Express;
|
|
|
|
beforeAll(async () => {
|
|
tmpDir = await setupTestDb();
|
|
// Mock LicenseService so paid-gated endpoints accept requests
|
|
const { LicenseService } = await import('../services/LicenseService');
|
|
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('paid');
|
|
({ app } = await import('../index'));
|
|
});
|
|
|
|
afterAll(() => {
|
|
vi.restoreAllMocks();
|
|
cleanupTestDb(tmpDir);
|
|
});
|
|
|
|
// ─── Environment Variable Sanitization ──────────────────────────────────────
|
|
|
|
describe('HostTerminalService.sanitizeEnv', () => {
|
|
let sanitizeEnv: (env: Record<string, string>) => Record<string, string>;
|
|
|
|
beforeAll(async () => {
|
|
const mod = await import('../services/HostTerminalService');
|
|
sanitizeEnv = mod.HostTerminalService.sanitizeEnv;
|
|
});
|
|
|
|
it('strips DATABASE_URL (explicit blocklist)', () => {
|
|
const result = sanitizeEnv({ DATABASE_URL: 'postgres://...', PATH: '/usr/bin' });
|
|
expect(result).not.toHaveProperty('DATABASE_URL');
|
|
expect(result).toHaveProperty('PATH', '/usr/bin');
|
|
});
|
|
|
|
it('strips REDIS_URL, MONGO_URI, AMQP_URL, DSN (explicit blocklist)', () => {
|
|
const result = sanitizeEnv({
|
|
REDIS_URL: 'redis://localhost',
|
|
MONGO_URI: 'mongodb://localhost',
|
|
AMQP_URL: 'amqp://localhost',
|
|
DSN: 'sentry://...',
|
|
HOME: '/home/user',
|
|
});
|
|
expect(result).not.toHaveProperty('REDIS_URL');
|
|
expect(result).not.toHaveProperty('MONGO_URI');
|
|
expect(result).not.toHaveProperty('AMQP_URL');
|
|
expect(result).not.toHaveProperty('DSN');
|
|
expect(result).toHaveProperty('HOME');
|
|
});
|
|
|
|
it('strips vars matching SECRET pattern', () => {
|
|
const result = sanitizeEnv({ JWT_SECRET: 'abc', APP_SECRET_KEY: 'xyz', LANG: 'en' });
|
|
expect(result).not.toHaveProperty('JWT_SECRET');
|
|
expect(result).not.toHaveProperty('APP_SECRET_KEY');
|
|
expect(result).toHaveProperty('LANG');
|
|
});
|
|
|
|
it('strips vars matching PASSWORD pattern', () => {
|
|
const result = sanitizeEnv({ DB_PASSWORD: 'pass', SMTP_PASSWORD: 'pass', USER: 'me' });
|
|
expect(result).not.toHaveProperty('DB_PASSWORD');
|
|
expect(result).not.toHaveProperty('SMTP_PASSWORD');
|
|
expect(result).toHaveProperty('USER');
|
|
});
|
|
|
|
it('strips vars matching TOKEN pattern', () => {
|
|
const result = sanitizeEnv({ API_TOKEN: '123', GITHUB_TOKEN: 'ghp_...', TERM: 'xterm' });
|
|
expect(result).not.toHaveProperty('API_TOKEN');
|
|
expect(result).not.toHaveProperty('GITHUB_TOKEN');
|
|
expect(result).toHaveProperty('TERM');
|
|
});
|
|
|
|
it('strips vars matching KEY pattern', () => {
|
|
const result = sanitizeEnv({ AWS_ACCESS_KEY_ID: 'AKIA...', ENCRYPTION_KEY: 'k', SHELL: '/bin/bash' });
|
|
expect(result).not.toHaveProperty('AWS_ACCESS_KEY_ID');
|
|
expect(result).not.toHaveProperty('ENCRYPTION_KEY');
|
|
expect(result).toHaveProperty('SHELL');
|
|
});
|
|
|
|
it('strips vars matching CREDENTIAL pattern', () => {
|
|
const result = sanitizeEnv({ GCP_CREDENTIAL: 'json...', PATH: '/usr/bin' });
|
|
expect(result).not.toHaveProperty('GCP_CREDENTIAL');
|
|
expect(result).toHaveProperty('PATH');
|
|
});
|
|
|
|
it('strips vars matching PRIVATE pattern', () => {
|
|
const result = sanitizeEnv({ SSH_PRIVATE_KEY: '-----BEGIN', PRIVATE_KEY_PEM: 'pem', HOSTNAME: 'box' });
|
|
expect(result).not.toHaveProperty('SSH_PRIVATE_KEY');
|
|
expect(result).not.toHaveProperty('PRIVATE_KEY_PEM');
|
|
expect(result).toHaveProperty('HOSTNAME');
|
|
});
|
|
|
|
it('strips vars matching AUTH pattern', () => {
|
|
const result = sanitizeEnv({ GITHUB_AUTH: 'token', OAUTH_CLIENT: 'id', COMPOSE_DIR: '/app' });
|
|
expect(result).not.toHaveProperty('GITHUB_AUTH');
|
|
expect(result).not.toHaveProperty('OAUTH_CLIENT');
|
|
expect(result).toHaveProperty('COMPOSE_DIR');
|
|
});
|
|
|
|
it('strips vars matching PASSPHRASE pattern', () => {
|
|
const result = sanitizeEnv({ GPG_PASSPHRASE: 'secret', PWD: '/home' });
|
|
expect(result).not.toHaveProperty('GPG_PASSPHRASE');
|
|
expect(result).toHaveProperty('PWD');
|
|
});
|
|
|
|
it('strips vars matching ENCRYPT pattern', () => {
|
|
const result = sanitizeEnv({ ENCRYPT_KEY: 'abc', ENCRYPTION_ALGO: 'aes', NODE_ENV: 'prod' });
|
|
expect(result).not.toHaveProperty('ENCRYPT_KEY');
|
|
expect(result).not.toHaveProperty('ENCRYPTION_ALGO');
|
|
expect(result).toHaveProperty('NODE_ENV');
|
|
});
|
|
|
|
it('strips vars matching SIGNING pattern', () => {
|
|
const result = sanitizeEnv({ SIGNING_KEY: 'key', JWT_SIGNING_SECRET: 's', LC_ALL: 'C' });
|
|
expect(result).not.toHaveProperty('SIGNING_KEY');
|
|
expect(result).not.toHaveProperty('JWT_SIGNING_SECRET');
|
|
expect(result).toHaveProperty('LC_ALL');
|
|
});
|
|
|
|
it('preserves safe environment variables', () => {
|
|
const safe = {
|
|
PATH: '/usr/bin',
|
|
HOME: '/home/user',
|
|
COMPOSE_DIR: '/app/compose',
|
|
NODE_ENV: 'production',
|
|
TERM: 'xterm-256color',
|
|
SHELL: '/bin/bash',
|
|
LANG: 'en_US.UTF-8',
|
|
};
|
|
const result = sanitizeEnv(safe);
|
|
expect(result).toEqual(safe);
|
|
});
|
|
|
|
it('pattern matching is case-insensitive', () => {
|
|
const result = sanitizeEnv({ my_secret: 'val', My_Password: 'val', PATH: '/usr/bin' });
|
|
expect(result).not.toHaveProperty('my_secret');
|
|
expect(result).not.toHaveProperty('My_Password');
|
|
expect(result).toHaveProperty('PATH');
|
|
});
|
|
});
|
|
|
|
// ─── Session Limit ──────────────────────────────────────────────────────────
|
|
|
|
describe('HostTerminalService session tracking', () => {
|
|
let HostTerminalService: typeof import('../services/HostTerminalService').HostTerminalService;
|
|
|
|
beforeAll(async () => {
|
|
const mod = await import('../services/HostTerminalService');
|
|
HostTerminalService = mod.HostTerminalService;
|
|
});
|
|
|
|
it('activeSessions map is accessible and starts empty', () => {
|
|
// Clear any leftover sessions
|
|
HostTerminalService.activeSessions.clear();
|
|
expect(HostTerminalService.activeSessions.size).toBe(0);
|
|
});
|
|
});
|
|
|
|
// ─── Stack-Path Resolution ──────────────────────────────────────────────────
|
|
|
|
describe('HostTerminalService.resolveConsoleDirectory', () => {
|
|
let resolveConsoleDirectory: (baseDir: string, stackParam: string | null) => string | null;
|
|
|
|
beforeAll(async () => {
|
|
const mod = await import('../services/HostTerminalService');
|
|
resolveConsoleDirectory = mod.HostTerminalService.resolveConsoleDirectory;
|
|
});
|
|
|
|
it('returns the base directory when no stack is given', () => {
|
|
expect(resolveConsoleDirectory('/srv/compose', null)).toBe(path.resolve('/srv/compose'));
|
|
});
|
|
|
|
it('resolves a stack subdirectory below the base', () => {
|
|
expect(resolveConsoleDirectory('/srv/compose', 'my-stack')).toBe(
|
|
path.resolve('/srv/compose', 'my-stack'),
|
|
);
|
|
});
|
|
|
|
it('rejects a sibling-prefix escape (../<base>-evil)', () => {
|
|
// The historical bug: a bare startsWith() matched `/srv/compose-evil`
|
|
// because it shares the `/srv/compose` prefix. The path.sep boundary
|
|
// must reject it.
|
|
expect(resolveConsoleDirectory('/srv/compose', '../compose-evil')).toBeNull();
|
|
});
|
|
|
|
it('rejects a parent-directory escape', () => {
|
|
expect(resolveConsoleDirectory('/srv/compose', '../..')).toBeNull();
|
|
});
|
|
|
|
it('rejects an absolute path outside the base', () => {
|
|
const outside = path.resolve('/etc');
|
|
if (outside !== path.resolve('/srv/compose')) {
|
|
expect(resolveConsoleDirectory('/srv/compose', outside)).toBeNull();
|
|
}
|
|
});
|
|
});
|
|
|
|
// ─── Session Audit Trail + Resize Validation ────────────────────────────────
|
|
|
|
interface FakeWs extends EventEmitter {
|
|
readyState: number;
|
|
send: ReturnType<typeof vi.fn>;
|
|
close: ReturnType<typeof vi.fn>;
|
|
ping: ReturnType<typeof vi.fn>;
|
|
terminate: ReturnType<typeof vi.fn>;
|
|
}
|
|
|
|
function makeFakeWs(): FakeWs {
|
|
const ws = new EventEmitter() as FakeWs;
|
|
ws.readyState = 1; // WebSocket.OPEN
|
|
ws.send = vi.fn();
|
|
ws.close = vi.fn();
|
|
ws.ping = vi.fn();
|
|
ws.terminate = vi.fn();
|
|
return ws;
|
|
}
|
|
|
|
function makeFakePty(pid = 4242) {
|
|
let exitCb: ((e: { exitCode: number; signal: number }) => void) | undefined;
|
|
return {
|
|
pid,
|
|
onData: vi.fn(),
|
|
onExit: vi.fn((cb: (e: { exitCode: number; signal: number }) => void) => { exitCb = cb; }),
|
|
write: vi.fn(),
|
|
resize: vi.fn(),
|
|
kill: vi.fn(),
|
|
triggerExit: (e: { exitCode: number; signal: number }) => exitCb?.(e),
|
|
};
|
|
}
|
|
|
|
describe('HostTerminalService.spawnTerminal', () => {
|
|
let HostTerminalService: typeof import('../services/HostTerminalService').HostTerminalService;
|
|
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
|
|
let insertSpy: ReturnType<typeof vi.spyOn>;
|
|
let spawnSpy: ReturnType<typeof vi.spyOn>;
|
|
const audit = { username: 'admin', nodeId: 7, ipAddress: '9.9.9.9' };
|
|
|
|
beforeAll(async () => {
|
|
HostTerminalService = (await import('../services/HostTerminalService')).HostTerminalService;
|
|
DatabaseService = (await import('../services/DatabaseService')).DatabaseService;
|
|
});
|
|
|
|
beforeEach(() => {
|
|
HostTerminalService.activeSessions.clear();
|
|
insertSpy = vi.spyOn(DatabaseService.getInstance(), 'insertAuditLog').mockImplementation(() => {});
|
|
spawnSpy = vi.spyOn(pty, 'spawn');
|
|
});
|
|
|
|
afterEach(() => {
|
|
insertSpy.mockRestore();
|
|
spawnSpy.mockRestore();
|
|
});
|
|
|
|
it('writes an open audit row when a session starts', () => {
|
|
spawnSpy.mockReturnValue(makeFakePty() as unknown as pty.IPty);
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
|
|
expect(insertSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
username: 'admin',
|
|
method: 'WS',
|
|
path: '/api/system/host-console',
|
|
status_code: 101,
|
|
node_id: 7,
|
|
ip_address: '9.9.9.9',
|
|
summary: 'Opened host console session',
|
|
}),
|
|
);
|
|
ws.emit('close'); // clean up the heartbeat interval
|
|
});
|
|
|
|
it('writes a close audit row with duration when the socket closes', () => {
|
|
spawnSpy.mockReturnValue(makeFakePty() as unknown as pty.IPty);
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
insertSpy.mockClear();
|
|
|
|
ws.emit('close');
|
|
|
|
expect(insertSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
method: 'WS',
|
|
path: '/api/system/host-console',
|
|
status_code: 200,
|
|
summary: expect.stringMatching(/^Closed host console session \(\d+ms\)$/),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('writes exactly one close row when the PTY exits and the socket then closes', () => {
|
|
const fakePty = makeFakePty();
|
|
spawnSpy.mockReturnValue(fakePty as unknown as pty.IPty);
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
insertSpy.mockClear();
|
|
|
|
// Both cleanup triggers fire (PTY exit, then the WS close it provokes); the
|
|
// `cleaned` guard must collapse them to a single close audit row.
|
|
fakePty.triggerExit({ exitCode: 0, signal: 0 });
|
|
ws.emit('close');
|
|
|
|
const closeRows = insertSpy.mock.calls.filter(
|
|
([entry]: [{ summary?: string }]) => entry?.summary?.startsWith('Closed host console session'),
|
|
);
|
|
expect(closeRows).toHaveLength(1);
|
|
});
|
|
|
|
it('applies a valid resize frame and rejects malformed ones', () => {
|
|
const fakePty = makeFakePty();
|
|
spawnSpy.mockReturnValue(fakePty as unknown as pty.IPty);
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: 120, rows: 40 })));
|
|
expect(fakePty.resize).toHaveBeenCalledWith(120, 40);
|
|
|
|
// Boundary: the max dimension is accepted, one above it is rejected.
|
|
fakePty.resize.mockClear();
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: 1000, rows: 1000 })));
|
|
expect(fakePty.resize).toHaveBeenCalledWith(1000, 1000);
|
|
|
|
fakePty.resize.mockClear();
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: 1001, rows: 40 })));
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: -1, rows: 40 })));
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: 99999, rows: 40 })));
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: '80', rows: 40 })));
|
|
expect(fakePty.resize).not.toHaveBeenCalled();
|
|
|
|
ws.emit('close');
|
|
});
|
|
|
|
it('forwards input frames to the PTY', () => {
|
|
const fakePty = makeFakePty();
|
|
spawnSpy.mockReturnValue(fakePty as unknown as pty.IPty);
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
|
|
ws.emit('message', Buffer.from(JSON.stringify({ type: 'input', payload: 'ls -la\n' })));
|
|
expect(fakePty.write).toHaveBeenCalledWith('ls -la\n');
|
|
|
|
ws.emit('close');
|
|
});
|
|
|
|
it('reports a shell-not-found error and closes when spawn throws ENOENT', () => {
|
|
spawnSpy.mockImplementation(() => { throw new Error('spawn sh ENOENT'); });
|
|
const ws = makeFakeWs();
|
|
HostTerminalService.spawnTerminal(ws as never, '/tmp', audit);
|
|
|
|
expect(ws.send).toHaveBeenCalledWith(expect.stringContaining('Shell not found'));
|
|
expect(ws.close).toHaveBeenCalled();
|
|
expect(HostTerminalService.activeSessions.size).toBe(0);
|
|
expect(insertSpy).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
// ─── Console Token RBAC ─────────────────────────────────────────────────────
|
|
|
|
describe('POST /api/system/console-token', () => {
|
|
it('returns 401 without authentication', async () => {
|
|
const res = await request(app).post('/api/system/console-token');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('returns 200 for admin user', async () => {
|
|
const token = jwt.sign({ username: TEST_USERNAME }, TEST_JWT_SECRET, { expiresIn: '1m' });
|
|
const res = await request(app)
|
|
.post('/api/system/console-token')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(typeof res.body.token).toBe('string');
|
|
});
|
|
|
|
it('returns 403 for non-admin user (viewer role)', async () => {
|
|
// Create a viewer user in the DB
|
|
const { DatabaseService } = await import('../services/DatabaseService');
|
|
const db = DatabaseService.getInstance();
|
|
const viewerHash = await bcrypt.hash('viewerpass', 1);
|
|
db.addUser({ username: 'viewer_test', password_hash: viewerHash, role: 'viewer' });
|
|
|
|
const token = jwt.sign({ username: 'viewer_test' }, TEST_JWT_SECRET, { expiresIn: '1m' });
|
|
const res = await request(app)
|
|
.post('/api/system/console-token')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('returns 403 for deployer role', async () => {
|
|
const { DatabaseService } = await import('../services/DatabaseService');
|
|
const db = DatabaseService.getInstance();
|
|
const deployerHash = await bcrypt.hash('deployerpass', 1);
|
|
db.addUser({ username: 'deployer_test', password_hash: deployerHash, role: 'deployer' });
|
|
|
|
const token = jwt.sign({ username: 'deployer_test' }, TEST_JWT_SECRET, { expiresIn: '1m' });
|
|
const res = await request(app)
|
|
.post('/api/system/console-token')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('returns 403 for API tokens', async () => {
|
|
// Create an API token via the admin endpoint first
|
|
const adminToken = jwt.sign({ username: TEST_USERNAME }, TEST_JWT_SECRET, { expiresIn: '1m' });
|
|
const createRes = await request(app)
|
|
.post('/api/api-tokens')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'test-console-blocked', scope: 'full-admin' });
|
|
expect(createRes.status).toBe(201);
|
|
const apiTokenValue = createRes.body.token;
|
|
|
|
const res = await request(app)
|
|
.post('/api/system/console-token')
|
|
.set('Authorization', `Bearer ${apiTokenValue}`);
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('SCOPE_DENIED');
|
|
});
|
|
});
|