Compare commits
3
Commits
main
..
mikrormm-dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4db0dc1e2 | ||
|
|
5b926d637b | ||
|
|
7a4979e0e1 |
@@ -145,3 +145,4 @@ vite.config.ts.timestamp-*
|
||||
# macOS
|
||||
.DS_Store
|
||||
site/
|
||||
.secrets-local/
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# CLAUDE.md — MikroRMM
|
||||
|
||||
Antes de alterar qualquer coisa neste projeto, leia nesta ordem:
|
||||
1. Este arquivo (`CLAUDE.md`)
|
||||
2. `docs/SISTEMA_ATUAL.md`
|
||||
3. `git status`
|
||||
4. `git log -5 --oneline`
|
||||
|
||||
## Regras permanentes
|
||||
|
||||
- Buscar causa-raiz antes de aplicar qualquer correção.
|
||||
- Não alterar código sem entender o fluxo afetado.
|
||||
- Preservar compatibilidade DEV/PROD (mesmo `docker-compose.yml` como base).
|
||||
- Nunca versionar secrets. `.env` e `.secrets-local/` ficam fora do Git.
|
||||
- Mudança de schema exige migration (`backend/src/db/migrate.ts`), nunca alteração manual do banco.
|
||||
- Testes focados primeiro (teste/módulo afetado), depois suíte completa como gate final.
|
||||
- Smoke real (`docker compose ps` + `curl https://localhost/api/health`) quando a mudança afetar runtime.
|
||||
- Atualizar `docs/SISTEMA_ATUAL.md` quando a mudança alterar arquitetura ou estado de verdade do sistema (não é changelog).
|
||||
- Não executar ações destrutivas em RouterBOARD sem missão explícita autorizando.
|
||||
- Não conectar clientes reais durante desenvolvimento.
|
||||
- Não considerar uma missão concluída com Git sujo (`git status` deve estar limpo, salvo arquivos intencionalmente não commitados e justificados).
|
||||
|
||||
## Ambiente local
|
||||
Ver seção 12 de `docs/SISTEMA_ATUAL.md`. Stack sobe com `docker compose up -d` a partir de `docker-compose.yml` (build local, não usar `docker-compose.ghcr.yml` em DEV).
|
||||
@@ -992,6 +992,41 @@ CREATE TABLE IF NOT EXISTS sites (
|
||||
-- devices in it. The delete endpoint refuses while devices remain.
|
||||
ALTER TABLE devices ADD COLUMN IF NOT EXISTS site_id INTEGER REFERENCES sites(id);
|
||||
CREATE INDEX IF NOT EXISTS idx_devices_site ON devices(site_id);
|
||||
|
||||
-- Mission 02 (SEC-001): server-side session revocation.
|
||||
--
|
||||
-- JWTs are stateless by design (no per-request DB write), but logout, a
|
||||
-- password change, disabling a compromised account, or an admin "sign this
|
||||
-- user out everywhere" action all need existing tokens to stop working
|
||||
-- without waiting out the 24h expiry. token_version is the mechanism: every
|
||||
-- signed JWT embeds the version current at sign time; requireAuth rejects a
|
||||
-- token whose embedded version no longer matches the user's row (see
|
||||
-- utils/sessionAuth.ts). Bumping the column is the "revoke" action.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS token_version INTEGER NOT NULL DEFAULT 0;
|
||||
-- A deactivated account must stop authenticating immediately, same path as
|
||||
-- token_version -- both are checked together in one cached lookup.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS active BOOLEAN NOT NULL DEFAULT true;
|
||||
|
||||
-- Mission 02 (SEC-002): explicit user <-> site authorization.
|
||||
--
|
||||
-- 'admin' is a global role and bypasses this table entirely (see
|
||||
-- middleware/site.ts). Every other role must hold an explicit row here for a
|
||||
-- site to be visible or writable -- no row means no access, not "unscoped".
|
||||
CREATE TABLE IF NOT EXISTS user_sites (
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
site_id INTEGER NOT NULL REFERENCES sites(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
PRIMARY KEY (user_id, site_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_sites_site ON user_sites(site_id);
|
||||
|
||||
-- Mission 02 (SEC-003): TLS trust-on-first-use pinning for the RouterOS
|
||||
-- API-SSL connection (see services/mikrotik/tlsTrust.ts). A device's first
|
||||
-- successful connection records the peer certificate's fingerprint; every
|
||||
-- later connection must present the same one, or the connection is refused
|
||||
-- until an admin explicitly re-trusts it (POST /api/devices/:id/tls/reset).
|
||||
ALTER TABLE devices ADD COLUMN IF NOT EXISTS tls_fingerprint VARCHAR(95);
|
||||
ALTER TABLE devices ADD COLUMN IF NOT EXISTS tls_mode VARCHAR(20) NOT NULL DEFAULT 'pinned';
|
||||
`;
|
||||
|
||||
const DEFAULT_SETTINGS = [
|
||||
@@ -1084,6 +1119,31 @@ export async function runMigrations(): Promise<void> {
|
||||
console.log('⚠️ Please change the default password after first login!');
|
||||
}
|
||||
|
||||
// user_sites backfill (SEC-002 migration), run exactly once. An existing
|
||||
// install had no concept of per-user site access -- every user could see
|
||||
// every site. Preserve that for accounts that predate this table by
|
||||
// granting every non-admin user access to every site that already exists
|
||||
// at the moment this runs. Gated by a settings flag rather than "has zero
|
||||
// rows" -- a user created *after* the backfill is meant to start with no
|
||||
// site access until explicitly granted, and would otherwise look
|
||||
// identical to a pre-existing, never-backfilled user on every later boot.
|
||||
const backfillFlag = await client.query(
|
||||
`SELECT 1 FROM app_settings WHERE key = 'user_sites_backfilled_v1'`
|
||||
);
|
||||
if (backfillFlag.rows.length === 0) {
|
||||
await client.query(
|
||||
`INSERT INTO user_sites (user_id, site_id)
|
||||
SELECT u.id, s.id FROM users u CROSS JOIN sites s
|
||||
WHERE u.role <> 'admin'
|
||||
ON CONFLICT DO NOTHING`
|
||||
);
|
||||
await client.query(
|
||||
`INSERT INTO app_settings (key, value) VALUES ('user_sites_backfilled_v1', 'true')
|
||||
ON CONFLICT (key) DO NOTHING`
|
||||
);
|
||||
console.log('user_sites backfilled for pre-existing non-admin users');
|
||||
}
|
||||
|
||||
console.log('Database migrations completed successfully');
|
||||
} finally {
|
||||
client.release();
|
||||
|
||||
+27
-5
@@ -28,6 +28,8 @@ import {
|
||||
import { netflowCollector } from './services/netflow/NetflowCollector';
|
||||
import { verifyToken, type AuthPayload } from './middleware/auth';
|
||||
import { rateLimitRedis } from './middleware/rateLimitRedis';
|
||||
import { getSessionState } from './utils/sessionAuth';
|
||||
import { userCanAccessDevice } from './middleware/site';
|
||||
import { initSecrets } from './utils/secrets';
|
||||
import { reencryptStaleCredentials } from './db/reencryptCredentials';
|
||||
import { reconcileStaleGuards } from './services/changeGuard/ChangeGuard';
|
||||
@@ -160,11 +162,24 @@ terminalNs.use((socket, next) => {
|
||||
if (!token) return next(new Error('No token'));
|
||||
try {
|
||||
const payload = verifyToken(token);
|
||||
if (!TERMINAL_ROLES.has(payload.role)) {
|
||||
return next(new Error('Console access denied for this role'));
|
||||
}
|
||||
socket.data.user = payload;
|
||||
next();
|
||||
// SEC-001: the terminal namespace authenticates over its own handshake,
|
||||
// outside the HTTP requireAuth pipeline, so it needs its own session
|
||||
// revocation check -- otherwise logout/deactivate/revoke-sessions would
|
||||
// not apply to an already-open or newly-opened console.
|
||||
getSessionState(payload.userId)
|
||||
.then((state) => {
|
||||
if (!state || !state.active || (payload.tokenVersion ?? 0) !== state.tokenVersion) {
|
||||
next(new Error('Session no longer valid'));
|
||||
return;
|
||||
}
|
||||
if (!TERMINAL_ROLES.has(state.role)) {
|
||||
next(new Error('Console access denied for this role'));
|
||||
return;
|
||||
}
|
||||
socket.data.user = { ...payload, role: state.role };
|
||||
next();
|
||||
})
|
||||
.catch(() => next(new Error('Could not verify session')));
|
||||
} catch {
|
||||
next(new Error('Invalid or expired token'));
|
||||
}
|
||||
@@ -189,6 +204,13 @@ terminalNs.on('connection', (socket) => {
|
||||
socket.emit('error', 'Too many terminal sessions started. Please wait a moment and try again.');
|
||||
return;
|
||||
}
|
||||
// SEC-002: a restricted user can't open a shell on a device outside
|
||||
// their authorized sites just by knowing/guessing its id.
|
||||
if (!(await userCanAccessDevice(user, deviceId))) {
|
||||
socket.emit('error', 'You do not have access to this device');
|
||||
return;
|
||||
}
|
||||
|
||||
const device = await queryOne<SshExecDevice>(
|
||||
`SELECT id, name, ip_address, ssh_port, ssh_username, ssh_password_encrypted,
|
||||
api_username, api_password_encrypted
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Regression coverage for findings recorded in docs/AUDITORIA_BASE_2GT.md.
|
||||
*
|
||||
* These tests prove the *current* upstream behavior with evidence, rather than
|
||||
* asserting an opinion. They are not fixes — see the audit report for
|
||||
* classification, impact and recommendation of each finding.
|
||||
*/
|
||||
import { AuthPayload } from '../auth';
|
||||
import { resolveSiteId } from '../../utils/siteScope';
|
||||
|
||||
// ── SEC-001: FIXED in Mission 02 ─────────────────────────────────────────────
|
||||
//
|
||||
// This finding ("a JWT could not be revoked before its 24h expiry") is now
|
||||
// covered by dedicated regression tests in middleware/__tests__/auth.test.ts
|
||||
// ("SEC-001 regression coverage": revoked-version, deactivated-account, and
|
||||
// deleted-user cases), backed by utils/sessionAuth.ts and the users.token_version
|
||||
// / users.active columns. See docs/AUDITORIA_BASE_2GT.md for status.
|
||||
|
||||
// ── SEC-002: "sites" are a display filter, not an authorization boundary ────
|
||||
//
|
||||
// resolveSiteId is the entire enforcement surface for site scoping: it turns
|
||||
// a client-supplied header/query value into a SQL predicate. It has no
|
||||
// knowledge of req.user and no lookup of which sites a user may access —
|
||||
// confirmed by its signature (raw value in, id-or-null out) and by grep: no
|
||||
// route file cross-references req.user with a site membership table (none
|
||||
// exists in the schema). Any authenticated user (any role) can omit the
|
||||
// header entirely to see every site's devices.
|
||||
describe('SEC-002: site scoping has no per-user authorization component', () => {
|
||||
it('resolves any syntactically valid site id with no user/session context ' +
|
||||
'available to the function at all', () => {
|
||||
// Note the signature: no user, no session, no membership check — just a
|
||||
// string. This is the one function that decides "which fleet subset",
|
||||
// and it never sees who is asking.
|
||||
expect(resolveSiteId('1')).toBe(1);
|
||||
expect(resolveSiteId('2')).toBe(2);
|
||||
expect(resolveSiteId(undefined)).toBeNull(); // unscoped = whole fleet
|
||||
expect(resolveSiteId('all')).toBeNull(); // explicit whole fleet
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,14 @@
|
||||
jest.mock('../../config/database');
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { signToken, verifyToken, requireAuth, requireAdmin, requireWrite, AuthPayload } from '../auth';
|
||||
import { queryOne } from '../../config/database';
|
||||
import { _resetSessionCacheForTest } from '../../utils/sessionAuth';
|
||||
|
||||
const TEST_PAYLOAD: AuthPayload = { userId: 1, username: 'alice', role: 'admin' };
|
||||
const mockedQueryOne = jest.mocked(queryOne);
|
||||
|
||||
const TEST_PAYLOAD: AuthPayload = { userId: 1, username: 'alice', role: 'admin', tokenVersion: 0 };
|
||||
|
||||
// Minimal Express mock helpers
|
||||
function mockReq(authHeader?: string): Request {
|
||||
@@ -16,6 +22,19 @@ function mockRes() {
|
||||
return res;
|
||||
}
|
||||
|
||||
/** requireAuth resolves its session check asynchronously; wait for `next`/`res.status`. */
|
||||
function waitForAuth(req: Request, res: Response, next: jest.Mock): Promise<void> {
|
||||
requireAuth(req, res, next as unknown as NextFunction);
|
||||
return new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
_resetSessionCacheForTest();
|
||||
// Default: session is current and active, matching TEST_PAYLOAD.
|
||||
mockedQueryOne.mockResolvedValue({ token_version: 0, active: true, role: 'admin' } as never);
|
||||
});
|
||||
|
||||
// ── signToken / verifyToken ─────────────────────────────────────────────────
|
||||
|
||||
describe('signToken / verifyToken', () => {
|
||||
@@ -25,6 +44,7 @@ describe('signToken / verifyToken', () => {
|
||||
expect(decoded.userId).toBe(TEST_PAYLOAD.userId);
|
||||
expect(decoded.username).toBe(TEST_PAYLOAD.username);
|
||||
expect(decoded.role).toBe(TEST_PAYLOAD.role);
|
||||
expect(decoded.tokenVersion).toBe(0);
|
||||
});
|
||||
|
||||
it('throws on an expired token', () => {
|
||||
@@ -69,16 +89,72 @@ describe('requireAuth', () => {
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls next and sets req.user for a valid token', () => {
|
||||
it('calls next and sets req.user for a valid, current-version token', async () => {
|
||||
const token = signToken(TEST_PAYLOAD);
|
||||
const req = mockReq(`Bearer ${token}`);
|
||||
const res = mockRes();
|
||||
const next = jest.fn() as unknown as NextFunction;
|
||||
requireAuth(req, res, next);
|
||||
const next = jest.fn();
|
||||
await waitForAuth(req, res, next);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.user?.userId).toBe(TEST_PAYLOAD.userId);
|
||||
expect(req.user?.role).toBe('admin');
|
||||
});
|
||||
|
||||
// ── SEC-001 regression coverage ───────────────────────────────────────────
|
||||
|
||||
it('rejects a token whose session was revoked (token_version bumped)', async () => {
|
||||
const token = signToken(TEST_PAYLOAD); // signed with tokenVersion: 0
|
||||
mockedQueryOne.mockResolvedValue({ token_version: 1, active: true, role: 'admin' } as never);
|
||||
|
||||
const req = mockReq(`Bearer ${token}`);
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
await waitForAuth(req, res, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
});
|
||||
|
||||
it('rejects a token for a deactivated account', async () => {
|
||||
const token = signToken(TEST_PAYLOAD);
|
||||
mockedQueryOne.mockResolvedValue({ token_version: 0, active: false, role: 'admin' } as never);
|
||||
|
||||
const req = mockReq(`Bearer ${token}`);
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
await waitForAuth(req, res, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
});
|
||||
|
||||
it('rejects a token for a user that no longer exists', async () => {
|
||||
const token = signToken(TEST_PAYLOAD);
|
||||
mockedQueryOne.mockResolvedValue(null as never);
|
||||
|
||||
const req = mockReq(`Bearer ${token}`);
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
await waitForAuth(req, res, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
});
|
||||
|
||||
it('uses the current DB role rather than the one embedded in the token', async () => {
|
||||
// Token was signed while the user was an operator; the account has since
|
||||
// been promoted to admin. The request must see the current role.
|
||||
const staleToken = signToken({ userId: 1, username: 'alice', role: 'operator', tokenVersion: 0 });
|
||||
mockedQueryOne.mockResolvedValue({ token_version: 0, active: true, role: 'admin' } as never);
|
||||
|
||||
const req = mockReq(`Bearer ${staleToken}`);
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
await waitForAuth(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.user?.role).toBe('admin');
|
||||
});
|
||||
});
|
||||
|
||||
// ── requireAdmin ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
jest.mock('../../config/database');
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { query, queryOne } from '../../config/database';
|
||||
import {
|
||||
siteContext, enforceSiteAccess, isDeviceAccessible, unauthorizedDeviceIds,
|
||||
invalidateUserSiteCache, deviceIdSiteGuard,
|
||||
} from '../site';
|
||||
import type { AuthPayload } from '../auth';
|
||||
|
||||
const mockedQuery = jest.mocked(query);
|
||||
const mockedQueryOne = jest.mocked(queryOne);
|
||||
|
||||
function mockReqRes(user: AuthPayload | undefined, siteIdHeader?: string) {
|
||||
const req = {
|
||||
headers: siteIdHeader !== undefined ? { 'x-site-id': siteIdHeader } : {},
|
||||
query: {},
|
||||
user,
|
||||
} as unknown as Request;
|
||||
const res = {} as Response;
|
||||
res.status = jest.fn().mockReturnValue(res);
|
||||
res.json = jest.fn().mockReturnValue(res);
|
||||
return { req, res };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
invalidateUserSiteCache(1);
|
||||
invalidateUserSiteCache(2);
|
||||
});
|
||||
|
||||
describe('SEC-002: enforceSiteAccess', () => {
|
||||
it('leaves admins unrestricted (siteScope mirrors the raw request)', async () => {
|
||||
const { req, res } = mockReqRes({ userId: 1, username: 'admin', role: 'admin' }, '5');
|
||||
siteContext(req, res, (() => {}) as NextFunction);
|
||||
const next = jest.fn();
|
||||
await enforceSiteAccess(req, res, next as unknown as NextFunction);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.siteScope).toBe(5);
|
||||
});
|
||||
|
||||
it('scopes a restricted user with no explicit site to their allowed sites', async () => {
|
||||
mockedQuery.mockResolvedValueOnce([{ site_id: 2 }, { site_id: 3 }] as never);
|
||||
const { req, res } = mockReqRes({ userId: 2, username: 'op', role: 'operator' });
|
||||
siteContext(req, res, (() => {}) as NextFunction);
|
||||
const next = jest.fn();
|
||||
await enforceSiteAccess(req, res, next as unknown as NextFunction);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.siteScope).toEqual([2, 3]);
|
||||
});
|
||||
|
||||
it('403s a restricted user requesting a site they are not a member of (IDOR)', async () => {
|
||||
mockedQuery.mockResolvedValueOnce([{ site_id: 2 }] as never); // only site 2
|
||||
const { req, res } = mockReqRes({ userId: 2, username: 'op', role: 'operator' }, '99');
|
||||
siteContext(req, res, (() => {}) as NextFunction);
|
||||
const next = jest.fn();
|
||||
await enforceSiteAccess(req, res, next as unknown as NextFunction);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
});
|
||||
|
||||
it('allows a restricted user requesting a site they are a member of', async () => {
|
||||
mockedQuery.mockResolvedValueOnce([{ site_id: 2 }] as never);
|
||||
const { req, res } = mockReqRes({ userId: 2, username: 'op', role: 'operator' }, '2');
|
||||
siteContext(req, res, (() => {}) as NextFunction);
|
||||
const next = jest.fn();
|
||||
await enforceSiteAccess(req, res, next as unknown as NextFunction);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.siteScope).toBe(2);
|
||||
});
|
||||
|
||||
it('scopes a viewer with zero site grants to nothing, not the whole fleet', async () => {
|
||||
mockedQuery.mockResolvedValueOnce([] as never);
|
||||
const { req, res } = mockReqRes({ userId: 2, username: 'v', role: 'viewer' });
|
||||
siteContext(req, res, (() => {}) as NextFunction);
|
||||
const next = jest.fn();
|
||||
await enforceSiteAccess(req, res, next as unknown as NextFunction);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req.siteScope).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SEC-002: isDeviceAccessible / deviceIdSiteGuard (cross-site device IDOR)', () => {
|
||||
it('denies a restricted user reaching a device in a site they do not belong to', async () => {
|
||||
const req = { siteScope: [2, 3], user: { userId: 2, role: 'operator' } } as unknown as Request;
|
||||
mockedQueryOne.mockResolvedValueOnce({ site_id: 99 } as never); // device belongs to site 99
|
||||
await expect(isDeviceAccessible(req, 42)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('allows a restricted user reaching a device in one of their sites', async () => {
|
||||
const req = { siteScope: [2, 3], user: { userId: 2, role: 'operator' } } as unknown as Request;
|
||||
mockedQueryOne.mockResolvedValueOnce({ site_id: 3 } as never);
|
||||
await expect(isDeviceAccessible(req, 42)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('never restricts an admin (siteScope null)', async () => {
|
||||
const req = { siteScope: null, user: { userId: 1, role: 'admin' } } as unknown as Request;
|
||||
await expect(isDeviceAccessible(req, 42)).resolves.toBe(true);
|
||||
expect(mockedQueryOne).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('deviceIdSiteGuard 403s the request end-to-end for a cross-site device id', async () => {
|
||||
const req = { siteScope: [2] } as unknown as Request;
|
||||
const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response;
|
||||
const next = jest.fn();
|
||||
mockedQueryOne.mockResolvedValueOnce({ site_id: 7 } as never); // not in [2]
|
||||
await deviceIdSiteGuard(req, res, next as unknown as NextFunction, '42');
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SEC-002: unauthorizedDeviceIds (bulk operations)', () => {
|
||||
it('flags device ids outside the caller scope', async () => {
|
||||
const req = { siteScope: [2] } as unknown as Request;
|
||||
mockedQuery.mockResolvedValueOnce([
|
||||
{ id: 1, site_id: 2 },
|
||||
{ id: 2, site_id: 9 },
|
||||
] as never);
|
||||
await expect(unauthorizedDeviceIds(req, [1, 2])).resolves.toEqual([2]);
|
||||
});
|
||||
|
||||
it('returns an empty list for an unrestricted (admin) scope', async () => {
|
||||
const req = { siteScope: null } as unknown as Request;
|
||||
await expect(unauthorizedDeviceIds(req, [1, 2, 3])).resolves.toEqual([]);
|
||||
expect(mockedQuery).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import jwt from 'jsonwebtoken';
|
||||
import { createHash } from 'crypto';
|
||||
import { query } from '../config/database';
|
||||
import { jwtSigningSecret, jwtVerifierSecrets } from '../utils/secrets';
|
||||
import { getSessionState } from '../utils/sessionAuth';
|
||||
|
||||
export interface AuthPayload {
|
||||
userId: number;
|
||||
@@ -10,6 +11,13 @@ export interface AuthPayload {
|
||||
role: string;
|
||||
/** Set when the request authenticated with an API token instead of a session. */
|
||||
tokenAuth?: boolean;
|
||||
/**
|
||||
* The user's token_version at the moment this JWT was signed (SEC-001).
|
||||
* Absent on tokens from before this field existed -- those are treated as
|
||||
* version 0, which is also where every account's counter starts, so
|
||||
* pre-existing sessions keep working until the next revoking action.
|
||||
*/
|
||||
tokenVersion?: number;
|
||||
}
|
||||
|
||||
declare global {
|
||||
@@ -90,6 +98,8 @@ export function requireAuth(req: Request, res: Response, next: NextFunction): vo
|
||||
const token = authHeader.slice(7);
|
||||
|
||||
if (token.startsWith('mtm_')) {
|
||||
// API tokens are revoked by deleting the row (see routes/automation.ts);
|
||||
// they carry no JWT-style version to check here.
|
||||
authenticateApiToken(token)
|
||||
.then((payload) => {
|
||||
if (!payload) { res.status(401).json({ error: 'Invalid or expired API token' }); return; }
|
||||
@@ -100,12 +110,41 @@ export function requireAuth(req: Request, res: Response, next: NextFunction): vo
|
||||
return;
|
||||
}
|
||||
|
||||
let payload: AuthPayload;
|
||||
try {
|
||||
req.user = verifyToken(token);
|
||||
next();
|
||||
payload = verifyToken(token);
|
||||
} catch {
|
||||
res.status(401).json({ error: 'Invalid or expired token' });
|
||||
return;
|
||||
}
|
||||
|
||||
// SEC-001: reject a token whose embedded session has since been revoked
|
||||
// (logout-everywhere, password change, TOTP disable, deactivation, or an
|
||||
// admin ending the session) rather than trusting the signature alone until
|
||||
// natural expiry. getSessionState is cached for a few seconds, so this adds
|
||||
// no DB round trip on the common path.
|
||||
getSessionState(payload.userId)
|
||||
.then((state) => {
|
||||
if (!state || !state.active) {
|
||||
res.status(401).json({ error: 'Session no longer valid' });
|
||||
return;
|
||||
}
|
||||
if ((payload.tokenVersion ?? 0) !== state.tokenVersion) {
|
||||
res.status(401).json({ error: 'Session was revoked' });
|
||||
return;
|
||||
}
|
||||
// The role in the DB is authoritative going forward -- a demotion/
|
||||
// promotion takes effect on the next request rather than waiting for
|
||||
// the token to expire, at the same cached-lookup cost as the version
|
||||
// check above.
|
||||
req.user = { ...payload, role: state.role };
|
||||
next();
|
||||
})
|
||||
.catch(() => {
|
||||
// Fail closed: if we can't confirm the session is still valid, don't
|
||||
// authenticate the request.
|
||||
res.status(401).json({ error: 'Could not verify session' });
|
||||
});
|
||||
}
|
||||
|
||||
export function requireAdmin(req: Request, res: Response, next: NextFunction): void {
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { resolveSiteId } from '../utils/siteScope';
|
||||
import { resolveSiteId, SiteScope } from '../utils/siteScope';
|
||||
import { queryOne, query } from '../config/database';
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||
namespace Express {
|
||||
interface Request {
|
||||
/** Active site, or null for the whole fleet (all-sites view). */
|
||||
/** Raw site id requested via header/query, before authorization. */
|
||||
siteId?: number | null;
|
||||
/**
|
||||
* The effective, authorized scope for this request (SEC-002):
|
||||
* `null` = unrestricted (admin/API token), a `number` = one authorized
|
||||
* site, `number[]` = every site a restricted user may see when no
|
||||
* explicit site was requested. Set by enforceSiteAccess.
|
||||
*/
|
||||
siteScope?: SiteScope;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,9 +22,11 @@ declare global {
|
||||
/**
|
||||
* Reads the active site from the X-Site-Id header (issue #130).
|
||||
*
|
||||
* An absent, empty or 'all' header means unscoped. That default is what keeps
|
||||
* every pre-existing API consumer -- scripts, API tokens, the docs' curl
|
||||
* examples -- behaving exactly as it did before sites existed.
|
||||
* An absent, empty or 'all' header means "no explicit selection". That
|
||||
* default is what keeps every pre-existing API consumer -- scripts, API
|
||||
* tokens, the docs' curl examples -- behaving exactly as it did before sites
|
||||
* existed, *for an admin*. For a restricted user it is narrowed to their
|
||||
* accessible sites by enforceSiteAccess below, not left unscoped.
|
||||
*
|
||||
* A query-string ?site_id= is also honoured, for links and for anything that
|
||||
* cannot set headers.
|
||||
@@ -28,7 +38,168 @@ export function siteContext(req: Request, _res: Response, next: NextFunction): v
|
||||
next();
|
||||
}
|
||||
|
||||
/** Convenience accessor so routes don't repeat the `?? null`. */
|
||||
export function activeSite(req: Request): number | null {
|
||||
return req.siteId ?? null;
|
||||
// Cached per user for a few seconds -- same trade-off as sessionAuth's cache:
|
||||
// a grant/revoke of site access takes a few seconds to propagate, in exchange
|
||||
// for not hitting the DB on every request of every route that scopes by site.
|
||||
const SITE_CACHE_TTL_MS = 5_000;
|
||||
const siteCache = new Map<number, { ids: number[]; cachedAt: number }>();
|
||||
|
||||
async function getUserSiteIds(userId: number): Promise<number[]> {
|
||||
const cached = siteCache.get(userId);
|
||||
if (cached && Date.now() - cached.cachedAt < SITE_CACHE_TTL_MS) return cached.ids;
|
||||
const rows = await query<{ site_id: number }>(`SELECT site_id FROM user_sites WHERE user_id = $1`, [userId]);
|
||||
const ids = rows.map((r) => r.site_id);
|
||||
siteCache.set(userId, { ids, cachedAt: Date.now() });
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** Test/admin-action seam: force the next read to hit the DB. */
|
||||
export function invalidateUserSiteCache(userId: number): void {
|
||||
siteCache.delete(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same authorization rule as isDeviceAccessible, usable outside an Express
|
||||
* request -- the Socket.IO terminal namespace authenticates over its own
|
||||
* handshake and has no `req` to hang a scope on.
|
||||
*/
|
||||
export async function userCanAccessDevice(
|
||||
user: { userId: number; role: string; tokenAuth?: boolean }, deviceId: number
|
||||
): Promise<boolean> {
|
||||
if (user.role === 'admin' || user.tokenAuth) return true;
|
||||
const device = await queryOne<{ site_id: number | null }>(`SELECT site_id FROM devices WHERE id = $1`, [deviceId]);
|
||||
if (!device || device.site_id === null) return true; // let the caller 404, or unassigned = visible
|
||||
const allowed = await getUserSiteIds(user.userId);
|
||||
return allowed.includes(device.site_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorize the site requested by siteContext against the authenticated user
|
||||
* (SEC-002). Must run after both requireAuth and siteContext.
|
||||
*
|
||||
* - admin and API-token callers are unrestricted (role model predates sites;
|
||||
* scoping tokens is tracked as residual work, see docs/AUDITORIA_BASE_2GT.md).
|
||||
* - a restricted user requesting a specific site must be a member of it, or
|
||||
* the request is refused (403) -- this is what closes the IDOR: sending
|
||||
* X-Site-Id for a site you don't belong to no longer works.
|
||||
* - a restricted user requesting no specific site ("all") is scoped to every
|
||||
* site they belong to, not the whole fleet.
|
||||
*/
|
||||
export async function enforceSiteAccess(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
const user = req.user;
|
||||
if (!user) { res.status(401).json({ error: 'No token provided' }); return; }
|
||||
|
||||
if (user.role === 'admin' || user.tokenAuth) {
|
||||
req.siteScope = req.siteId ?? null;
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const allowed = await getUserSiteIds(user.userId);
|
||||
if (req.siteId !== null && req.siteId !== undefined) {
|
||||
if (!allowed.includes(req.siteId)) {
|
||||
res.status(403).json({ error: 'You do not have access to this site' });
|
||||
return;
|
||||
}
|
||||
req.siteScope = req.siteId;
|
||||
} else {
|
||||
req.siteScope = allowed; // may be [] -- "no access to anything", not "all"
|
||||
}
|
||||
next();
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: `Could not resolve site access: ${(e as Error).message}` });
|
||||
}
|
||||
}
|
||||
|
||||
/** Convenience accessor so routes don't repeat the optional chaining. */
|
||||
export function activeSite(req: Request): SiteScope {
|
||||
return req.siteScope ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct-by-id guard for routes keyed on a specific device (SEC-002).
|
||||
*
|
||||
* Meant to be wired with `router.param('id', ...)` (or called explicitly for
|
||||
* a body/query-supplied deviceId, e.g. bulk commands and the terminal) so a
|
||||
* restricted user can't reach a device outside their sites by simply knowing
|
||||
* or guessing its numeric id -- the classic IDOR this mission calls out.
|
||||
*
|
||||
* Returns true if access is allowed (or the device doesn't exist -- the
|
||||
* handler's own 404 is the right response, not a 403 that would confirm the
|
||||
* id exists in someone else's site).
|
||||
*/
|
||||
export async function isDeviceAccessible(req: Request, deviceId: number): Promise<boolean> {
|
||||
const scope = activeSite(req);
|
||||
if (scope === null) return true; // unrestricted (admin/token), or explicit single site already checked below
|
||||
const device = await queryOne<{ site_id: number | null }>(`SELECT site_id FROM devices WHERE id = $1`, [deviceId]);
|
||||
if (!device) return true; // let the handler 404
|
||||
if (device.site_id === null) return true; // unassigned devices are visible to everyone (should not occur post-migration)
|
||||
return Array.isArray(scope) ? scope.includes(device.site_id) : scope === device.site_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Express `router.param('id', ...)` handler that 403s a request for a device
|
||||
* id outside the caller's authorized sites. Mount with:
|
||||
* router.param('id', deviceIdSiteGuard);
|
||||
* on any router whose `:id` is a device id.
|
||||
*/
|
||||
export async function deviceIdSiteGuard(
|
||||
req: Request, res: Response, next: NextFunction, idParam: string
|
||||
): Promise<void> {
|
||||
const id = parseInt(idParam, 10);
|
||||
if (Number.isNaN(id)) { next(); return; } // let the handler reject the malformed id
|
||||
try {
|
||||
if (!(await isDeviceAccessible(req, id))) {
|
||||
res.status(403).json({ error: 'You do not have access to this device' });
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: `Could not resolve device access: ${(e as Error).message}` });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk-operation guard (SEC-002): given a list of device ids supplied in a
|
||||
* request body (commands, bulk backup/sync, the terminal's single deviceId),
|
||||
* returns the subset the caller is NOT authorized for. An empty result means
|
||||
* every id is in scope. Used where router.param can't help because the ids
|
||||
* don't come from the URL.
|
||||
*/
|
||||
export async function unauthorizedDeviceIds(req: Request, deviceIds: number[]): Promise<number[]> {
|
||||
const scope = activeSite(req);
|
||||
if (scope === null || deviceIds.length === 0) return [];
|
||||
const rows = await query<{ id: number; site_id: number | null }>(
|
||||
`SELECT id, site_id FROM devices WHERE id = ANY($1::int[])`, [deviceIds]
|
||||
);
|
||||
const allowed = new Set(Array.isArray(scope) ? scope : [scope]);
|
||||
return rows.filter((r) => r.site_id !== null && !allowed.has(r.site_id)).map((r) => r.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same idea as deviceIdSiteGuard, for a route param that identifies a *row*
|
||||
* belonging to a device rather than the device itself (a backup, a config
|
||||
* snapshot, a command run...). `resolveDeviceId` looks up that row's owning
|
||||
* device id; returning null (row not found) lets the handler's own 404 win,
|
||||
* same reasoning as isDeviceAccessible.
|
||||
*/
|
||||
export function ownedByDeviceSiteGuard(
|
||||
resolveDeviceId: (rowId: number) => Promise<number | null>
|
||||
): (req: Request, res: Response, next: NextFunction, idParam: string) => Promise<void> {
|
||||
return async (req, res, next, idParam) => {
|
||||
const rowId = parseInt(idParam, 10);
|
||||
if (Number.isNaN(rowId)) { next(); return; }
|
||||
try {
|
||||
const deviceId = await resolveDeviceId(rowId);
|
||||
if (deviceId === null) { next(); return; } // handler will 404
|
||||
if (!(await isDeviceAccessible(req, deviceId))) {
|
||||
res.status(403).json({ error: 'You do not have access to this resource' });
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: `Could not resolve site access: ${(e as Error).message}` });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { signToken, signRawToken, verifyRawToken, requireAuth } from '../middlew
|
||||
import { getSecretsInfo } from '../utils/secrets';
|
||||
import { loginRateLimit, rateLimitRedis } from '../middleware/rateLimitRedis';
|
||||
import { validatePassword } from '../utils/passwordPolicy';
|
||||
import { bumpTokenVersion } from '../utils/sessionAuth';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -50,9 +51,14 @@ router.post('/login', loginRateLimit(), async (req: Request, res: Response) => {
|
||||
password_hash: string;
|
||||
role: string;
|
||||
totp_enabled: boolean;
|
||||
}>(`SELECT id, username, password_hash, role, totp_enabled FROM users WHERE username = $1`, [username]);
|
||||
active: boolean;
|
||||
token_version: number;
|
||||
}>(`SELECT id, username, password_hash, role, totp_enabled, active, token_version FROM users WHERE username = $1`, [username]);
|
||||
|
||||
if (!user || !(await bcrypt.compare(password, user.password_hash))) {
|
||||
// Same generic error for "no such user", "wrong password" and "deactivated"
|
||||
// -- distinguishing them would leak which usernames exist and which are
|
||||
// disabled.
|
||||
if (!user || !user.active || !(await bcrypt.compare(password, user.password_hash))) {
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
@@ -61,7 +67,7 @@ router.post('/login', loginRateLimit(), async (req: Request, res: Response) => {
|
||||
return res.json({ requires_totp: true, totp_token: totpToken });
|
||||
}
|
||||
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role });
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role, tokenVersion: user.token_version });
|
||||
return res.json({ token, user: { id: user.id, username: user.username, role: user.role } });
|
||||
});
|
||||
|
||||
@@ -81,11 +87,14 @@ router.post('/totp/verify', rateLimitRedis({ windowSec: 60, max: 5, keyPrefix: '
|
||||
return res.status(401).json({ error: 'Invalid token type' });
|
||||
}
|
||||
|
||||
const user = await queryOne<{ id: number; username: string; role: string; totp_secret: string | null; totp_enabled: boolean }>(
|
||||
`SELECT id, username, role, totp_secret, totp_enabled FROM users WHERE id = $1`,
|
||||
const user = await queryOne<{
|
||||
id: number; username: string; role: string; totp_secret: string | null; totp_enabled: boolean;
|
||||
active: boolean; token_version: number;
|
||||
}>(
|
||||
`SELECT id, username, role, totp_secret, totp_enabled, active, token_version FROM users WHERE id = $1`,
|
||||
[payload.userId]
|
||||
);
|
||||
if (!user || !user.totp_enabled || !user.totp_secret) {
|
||||
if (!user || !user.active || !user.totp_enabled || !user.totp_secret) {
|
||||
return res.status(401).json({ error: 'TOTP not configured' });
|
||||
}
|
||||
|
||||
@@ -95,7 +104,7 @@ router.post('/totp/verify', rateLimitRedis({ windowSec: 60, max: 5, keyPrefix: '
|
||||
return res.status(401).json({ error: 'Invalid TOTP code' });
|
||||
}
|
||||
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role });
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role, tokenVersion: user.token_version });
|
||||
return res.json({ token, user: { id: user.id, username: user.username, role: user.role } });
|
||||
});
|
||||
|
||||
@@ -165,6 +174,8 @@ router.post('/totp/disable', requireAuth, async (req: Request, res: Response) =>
|
||||
}
|
||||
|
||||
await query(`UPDATE users SET totp_enabled = false, totp_secret = NULL WHERE id = $1`, [req.user!.userId]);
|
||||
// SEC-001: disabling 2FA is a security-relevant auth change; force re-auth.
|
||||
await bumpTokenVersion(req.user!.userId);
|
||||
return res.json({ ok: true });
|
||||
});
|
||||
|
||||
@@ -181,8 +192,14 @@ router.get('/me', requireAuth, (req: Request, res: Response) => {
|
||||
res.json({ user: req.user });
|
||||
});
|
||||
|
||||
router.post('/logout', requireAuth, (_req: Request, res: Response) => {
|
||||
// JWT is stateless; client just discards token
|
||||
// Logout revokes the session server-side (SEC-001) by bumping token_version,
|
||||
// rather than relying on the client to discard the token. This invalidates
|
||||
// every token issued for the account, not just the one presented here --
|
||||
// there is no per-device/per-tab session identity to revoke selectively, and
|
||||
// "sign out everywhere" is an acceptable, documented trade for the added
|
||||
// safety (see docs/AUDITORIA_BASE_2GT.md, SEC-001).
|
||||
router.post('/logout', requireAuth, async (req: Request, res: Response) => {
|
||||
await bumpTokenVersion(req.user!.userId);
|
||||
res.json({ message: 'Logged out' });
|
||||
});
|
||||
|
||||
@@ -207,6 +224,10 @@ router.put('/password', requireAuth, async (req: Request, res: Response) => {
|
||||
|
||||
const hash = await bcrypt.hash(newPassword, 12);
|
||||
await query(`UPDATE users SET password_hash = $1 WHERE id = $2`, [hash, req.user!.userId]);
|
||||
// SEC-001: a password change is exactly the moment a session might be
|
||||
// compromised -- kill every existing token immediately rather than let
|
||||
// them ride out their 24h expiry.
|
||||
await bumpTokenVersion(req.user!.userId);
|
||||
return res.json({ message: 'Password updated' });
|
||||
});
|
||||
|
||||
|
||||
@@ -4,11 +4,22 @@ import * as path from 'path';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess, ownedByDeviceSiteGuard } from '../middleware/site';
|
||||
import { BackupService } from '../services/BackupService';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// SEC-002: :id/:fromId/:toId here are backup ids, not device ids -- resolve
|
||||
// each to its owning device before letting a handler run.
|
||||
const backupSiteGuard = ownedByDeviceSiteGuard(async (backupId) => {
|
||||
const row = await queryOne<{ device_id: number }>(`SELECT device_id FROM backups WHERE id = $1`, [backupId]);
|
||||
return row?.device_id ?? null;
|
||||
});
|
||||
router.param('id', backupSiteGuard);
|
||||
router.param('fromId', backupSiteGuard);
|
||||
router.param('toId', backupSiteGuard);
|
||||
|
||||
const backupService = new BackupService();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { query } from '../config/database';
|
||||
import { logSafe } from '../utils/logSafe';
|
||||
import { requireAuth, requireAdmin, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { PollerService } from '../services/PollerService';
|
||||
import { getQueryApi, bucket } from '../config/influxdb';
|
||||
import { fingerprintClient, DEVICE_CATEGORIES, DeviceCategory } from '../utils/clientFingerprint';
|
||||
@@ -23,6 +23,7 @@ function withCategory<T extends Record<string, unknown>>(row: T): T & { device_c
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
let pollerService: PollerService | null = null;
|
||||
export function setPollerService(p: PollerService): void { pollerService = p; }
|
||||
|
||||
@@ -9,12 +9,13 @@ import { Router } from 'express';
|
||||
import type { Request, Response } from 'express';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess, unauthorizedDeviceIds } from '../middleware/site';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { commandRunner } from '../services/CommandRunner';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
/** Commands that change how a device is reached, and so deserve a warning. */
|
||||
const RISKY_PATTERNS: { pattern: RegExp; why: string }[] = [
|
||||
@@ -46,6 +47,10 @@ router.post('/preview', async (req: Request, res: Response) => {
|
||||
if (!Array.isArray(device_ids) || device_ids.length === 0) {
|
||||
return res.status(400).json({ error: 'device_ids array is required' });
|
||||
}
|
||||
const forbidden = await unauthorizedDeviceIds(req, device_ids);
|
||||
if (forbidden.length) {
|
||||
return res.status(403).json({ error: 'One or more devices are outside your authorized sites', device_ids: forbidden });
|
||||
}
|
||||
|
||||
const size = Math.max(1, Math.min(50, Number(wave_size) || 1));
|
||||
const devices = await query<{ id: number; name: string; ip_address: string; status: string }>(
|
||||
@@ -85,6 +90,10 @@ router.post('/runs', requireWrite, async (req: Request, res: Response) => {
|
||||
if (!Array.isArray(device_ids) || device_ids.length === 0) {
|
||||
return res.status(400).json({ error: 'device_ids array is required' });
|
||||
}
|
||||
const forbidden = await unauthorizedDeviceIds(req, device_ids);
|
||||
if (forbidden.length) {
|
||||
return res.status(403).json({ error: 'One or more devices are outside your authorized sites', device_ids: forbidden });
|
||||
}
|
||||
|
||||
const size = Math.max(1, Math.min(50, Number(wave_size) || 1));
|
||||
const run = await queryOne<{ id: number }>(
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { enforceSiteAccess, deviceIdSiteGuard } from '../middleware/site';
|
||||
import { BackupService } from '../services/BackupService';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
router.param('deviceId', deviceIdSiteGuard);
|
||||
|
||||
const backupService = new BackupService();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireAdmin } from '../middleware/auth';
|
||||
import { encrypt } from '../utils/crypto';
|
||||
import { rateLimitRedis } from '../middleware/rateLimitRedis';
|
||||
import { validateRouterOsUsername } from '../utils/routerOsUsername';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
@@ -106,6 +107,8 @@ router.post('/', requireAdmin, presetMutationLimiter, async (req: Request, res:
|
||||
.status(400)
|
||||
.json({ error: 'name, api_username, and api_password are required' });
|
||||
}
|
||||
const sshUsernameError = validateRouterOsUsername(ssh_username);
|
||||
if (sshUsernameError) return res.status(400).json({ error: sshUsernameError });
|
||||
|
||||
const existing = await queryOne<{ id: number }>(
|
||||
`SELECT id FROM credential_presets WHERE name = $1`,
|
||||
@@ -184,6 +187,8 @@ router.put('/:id', requireAdmin, presetMutationLimiter, async (req: Request, res
|
||||
);
|
||||
if (clash) return res.status(409).json({ error: 'A preset with this name already exists' });
|
||||
}
|
||||
const sshUsernameError = validateRouterOsUsername(ssh_username);
|
||||
if (sshUsernameError) return res.status(400).json({ error: sshUsernameError });
|
||||
|
||||
const newApiPass = api_password ? encrypt(api_password) : existing.api_password_encrypted;
|
||||
let newSshPass: string | null = existing.ssh_password_encrypted;
|
||||
|
||||
@@ -2,7 +2,8 @@ import { Router, Request, Response } from 'express';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { Client as SshClient } from 'ssh2';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { requireAuth, requireWrite, requireAdmin } from '../middleware/auth';
|
||||
import { deviceIdSiteGuard } from '../middleware/site';
|
||||
import { encrypt, decrypt } from '../utils/crypto';
|
||||
import { RouterOSClient } from '../services/mikrotik/RouterOSClient';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
@@ -12,6 +13,7 @@ import { createDeviceFromBody, type CreateDeviceInput, type CreateDeviceContext
|
||||
import { parsePort } from '../utils/parsePort';
|
||||
import { safeConnectionError } from '../utils/safeClientError';
|
||||
import { detectLockoutRisk } from '../utils/firewallSafety';
|
||||
import { validateRouterOsUsername } from '../utils/routerOsUsername';
|
||||
import { withSafeApply, probeCapability, probeUndoCapability, type GuardDevice } from '../services/changeGuard/ChangeGuard';
|
||||
import { captureSnapshot, resolveManagementPath } from '../services/changeGuard/pathModel';
|
||||
import { analyzeChange, type PlannedChange } from '../services/changeGuard/analyzeChange';
|
||||
@@ -22,8 +24,8 @@ import { enqueueBulkAddJob, getBulkAddJobState } from '../services/DeviceBulkAdd
|
||||
import { logSafe } from '../utils/logSafe';
|
||||
import { DEVICE_BASE_COLUMNS } from '../services/deviceColumns';
|
||||
import { buildSegments, summarise, summariseBands } from '../utils/lteDwell';
|
||||
import { siteScopeDevices, andSite } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { siteScopeDevices, andSite, singleSiteOrNull } from '../utils/siteScope';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
|
||||
/** Postgres interval text to milliseconds, for the few windows we offer. */
|
||||
function intervalToMs(interval: string): number {
|
||||
@@ -70,6 +72,12 @@ async function loadCredentialPreset(
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
// SEC-002: every :id in this router is a device id -- refuse it up front if
|
||||
// the device belongs to a site the caller isn't authorized for, before any
|
||||
// handler runs. Closes the IDOR of reaching another site's device by guessing
|
||||
// or enumerating its numeric id.
|
||||
router.param('id', deviceIdSiteGuard);
|
||||
|
||||
let pollerService: PollerService | null = null;
|
||||
export function setPollerService(p: PollerService): void {
|
||||
@@ -285,7 +293,7 @@ router.post('/', requireWrite, async (req: Request, res: Response) => {
|
||||
const result = await createDeviceFromBody(req.body, pollerService, {
|
||||
requestingUserRole: req.user?.role,
|
||||
// A device added while viewing a site joins that site (issue #130).
|
||||
siteId: activeSite(req),
|
||||
siteId: singleSiteOrNull(activeSite(req)),
|
||||
});
|
||||
return res.status(result.status).json(result.body);
|
||||
});
|
||||
@@ -316,7 +324,7 @@ router.post('/bulk-add/jobs', requireWrite, async (req: Request, res: Response)
|
||||
BULK_ADD_META_TTL_SEC
|
||||
);
|
||||
await redis.set(`device-bulk-add:${jobId}:results`, '[]', 'EX', BULK_ADD_META_TTL_SEC);
|
||||
await enqueueBulkAddJob(jobId, items as CreateDeviceInput[], activeSite(req));
|
||||
await enqueueBulkAddJob(jobId, items as CreateDeviceInput[], singleSiteOrNull(activeSite(req)));
|
||||
return res.status(202).json({ job_id: jobId, total: items.length });
|
||||
});
|
||||
|
||||
@@ -450,6 +458,11 @@ router.put('/:id', requireWrite, async (req: Request, res: Response) => {
|
||||
const ssh_username = preset ? preset.ssh_username : req.body.ssh_username;
|
||||
const ssh_password = preset ? preset.ssh_password : req.body.ssh_password;
|
||||
|
||||
// SEC-006: reject an unsafe ssh_username before it can ever reach the
|
||||
// RouterOS CLI command SshKeyService builds from it.
|
||||
const sshUsernameError = validateRouterOsUsername(ssh_username);
|
||||
if (sshUsernameError) return res.status(400).json({ error: sshUsernameError });
|
||||
|
||||
// If the user is changing the IP (or port / username / password), verify the
|
||||
// RouterOS API is still reachable with the new values before persisting —
|
||||
// otherwise we could silently brick the device record.
|
||||
|
||||
@@ -2,10 +2,11 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeByNullableDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// GET /api/events
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Router, Request, Response } from 'express';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
import { firmwareOrchestrator } from '../services/FirmwareOrchestrator';
|
||||
import { clampConcurrency, MAX_WAVE_CONCURRENCY } from '../utils/concurrency';
|
||||
@@ -10,6 +10,7 @@ import { findUpstreamWithinSelection } from '../utils/rolloutTopology';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// GET /api/firmware/overview — fleet versions + latest rollout
|
||||
router.get('/overview', async (req: Request, res: Response) => {
|
||||
|
||||
@@ -2,11 +2,13 @@ import { Router, Request, Response } from 'express';
|
||||
import { getQueryApi, bucket } from '../config/influxdb';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByDevice, siteScopeByNullableDevice, clientSeriesTag } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { siteScopeDevices, siteScopeByDevice, siteScopeByNullableDevice, clientSeriesTag, singleSiteOrNull } from '../utils/siteScope';
|
||||
import { activeSite, enforceSiteAccess, deviceIdSiteGuard } from '../middleware/site';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
router.param('deviceId', deviceIdSiteGuard);
|
||||
|
||||
function rangeToFlux(range: string): string {
|
||||
const allowed = ['1h', '3h', '6h', '12h', '24h', '7d', '30d'];
|
||||
@@ -25,7 +27,15 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
|
||||
// the global series there is not a shortcut -- it keeps the chart's full
|
||||
// history, which the per-site series only accumulates from the upgrade
|
||||
// onward. That covers every single-site install.
|
||||
const siteId = activeSite(req);
|
||||
// clientSeriesTag needs one concrete site id or the true fleet-wide case.
|
||||
// SEC-002: a restricted user viewing several sites at once has neither --
|
||||
// there is no single dedup series for "these particular sites", and
|
||||
// falling back to `_global` would hand them the whole fleet's count. Such a
|
||||
// scope skips the dedup series entirely and goes straight to the per-device
|
||||
// fallback below, filtered to exactly the devices they may see.
|
||||
const scope = activeSite(req);
|
||||
const siteId = singleSiteOrNull(scope);
|
||||
const restrictedToMultipleSites = Array.isArray(scope);
|
||||
let seriesTag = '_global';
|
||||
if (siteId !== null) {
|
||||
const counts = await query<{ in_site: number; total: number }>(
|
||||
@@ -40,29 +50,31 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
|
||||
|
||||
// Use the global deduplicated metric (_global tag) written by DeviceCollector.
|
||||
// This avoids double-counting clients seen by multiple devices simultaneously.
|
||||
const fluxQuery = `
|
||||
from(bucket: "${bucket}")
|
||||
|> range(start: -${range})
|
||||
|> filter(fn: (r) => r._measurement == "client_counts")
|
||||
|> filter(fn: (r) => r._field == "total_clients")
|
||||
|> filter(fn: (r) => r.device_id == "${seriesTag}")
|
||||
|> aggregateWindow(every: 5m, fn: last, createEmpty: false)
|
||||
|> yield(name: "clients_over_time")
|
||||
`;
|
||||
|
||||
const rawPoints: { time: string; value: number }[] = [];
|
||||
try {
|
||||
await queryApi.collectRows(fluxQuery, (row, tableMeta) => {
|
||||
const time = tableMeta.get(row, '_time') as string;
|
||||
const value = tableMeta.get(row, '_value') as number;
|
||||
rawPoints.push({ time, value: Math.round(value) });
|
||||
});
|
||||
} catch {
|
||||
// InfluxDB might not have data yet
|
||||
if (!restrictedToMultipleSites) {
|
||||
const fluxQuery = `
|
||||
from(bucket: "${bucket}")
|
||||
|> range(start: -${range})
|
||||
|> filter(fn: (r) => r._measurement == "client_counts")
|
||||
|> filter(fn: (r) => r._field == "total_clients")
|
||||
|> filter(fn: (r) => r.device_id == "${seriesTag}")
|
||||
|> aggregateWindow(every: 5m, fn: last, createEmpty: false)
|
||||
|> yield(name: "clients_over_time")
|
||||
`;
|
||||
try {
|
||||
await queryApi.collectRows(fluxQuery, (row, tableMeta) => {
|
||||
const time = tableMeta.get(row, '_time') as string;
|
||||
const value = tableMeta.get(row, '_value') as number;
|
||||
rawPoints.push({ time, value: Math.round(value) });
|
||||
});
|
||||
} catch {
|
||||
// InfluxDB might not have data yet
|
||||
}
|
||||
}
|
||||
|
||||
// If no deduplicated series exists yet (first run, or a site whose history
|
||||
// predates the per-site series), fall back to max across real devices. max()
|
||||
// predates the per-site series), or the caller is scoped to several sites
|
||||
// at once, fall back to max across the exact devices they may see. max()
|
||||
// is better than sum() since the device with the most clients is the gateway
|
||||
// that sees everyone — summing would double-count clients visible from
|
||||
// multiple devices.
|
||||
@@ -72,12 +84,16 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
|
||||
// it matters: without it the fleet-wide fallback would fold the per-site
|
||||
// totals back in and count everyone twice.
|
||||
let deviceFilter: string | null = `|> filter(fn: (r) => not (r.device_id =~ /^_/))`;
|
||||
if (siteId !== null) {
|
||||
const ids = await query<{ id: number }>(
|
||||
`SELECT id FROM devices WHERE site_id = $1`, [siteId]
|
||||
);
|
||||
// An empty site has no series to fall back to, and must show an empty
|
||||
// chart rather than the fleet's (issue #130).
|
||||
if (scope !== null) {
|
||||
const scopePredicate = Array.isArray(scope) ? scope : [scope];
|
||||
const ids = scopePredicate.length
|
||||
? await query<{ id: number }>(
|
||||
`SELECT id FROM devices WHERE site_id = ANY($1::int[])`, [scopePredicate]
|
||||
)
|
||||
: [];
|
||||
// An empty site (or a viewer authorized for zero sites) has no series
|
||||
// to fall back to, and must show an empty chart rather than the fleet's
|
||||
// (issue #130 / SEC-002).
|
||||
deviceFilter = ids.length
|
||||
? `|> filter(fn: (r) => ${ids.map((d) => `r.device_id == "${d.id}"`).join(' or ')})`
|
||||
: null;
|
||||
|
||||
@@ -2,12 +2,13 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
import { netflowCollector } from '../services/netflow/NetflowCollector';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// ─── Helper ───────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { Issuer } from 'openid-client';
|
||||
import { requireAuth, requireAdmin, signToken } from '../middleware/auth';
|
||||
import { queryOne } from '../config/database';
|
||||
import { rateLimitRedis } from '../middleware/rateLimitRedis';
|
||||
import {
|
||||
loadOidcConfig, saveOidcConfig, maskedConfig, APP_ROLES, type AppRole, type OidcConfig,
|
||||
@@ -65,7 +66,14 @@ router.get(
|
||||
const params = req.query as Record<string, string>;
|
||||
if (params.error) throw new Error(params.error_description || params.error);
|
||||
const { user, returnTo } = await completeLogin(params);
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role });
|
||||
// SEC-001: SSO users go through the same revocable-session mechanism as
|
||||
// local accounts -- embed the current token_version so a "revoke
|
||||
// sessions" action or deactivation applies to them too.
|
||||
const current = await queryOne<{ token_version: number; active: boolean }>(
|
||||
`SELECT token_version, active FROM users WHERE id = $1`, [user.id]
|
||||
);
|
||||
if (!current || !current.active) throw new Error('Account is deactivated');
|
||||
const token = signToken({ userId: user.id, username: user.username, role: user.role, tokenVersion: current.token_version });
|
||||
const dest = safeReturnTo(returnTo);
|
||||
// Hand the token to the SPA via fragment (never sent to the server/logs).
|
||||
res.redirect(`/auth/callback#token=${encodeURIComponent(token)}&returnTo=${encodeURIComponent(dest)}`);
|
||||
|
||||
@@ -2,13 +2,14 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { getQueryApi } from '../config/influxdb';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByDevice, siteScopeByNullableDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { siteScopeDevices, siteScopeByDevice, siteScopeByNullableDevice, singleSiteOrNull, SiteScope } from '../utils/siteScope';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
import { BackupService, BackupDevice } from '../services/BackupService';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
const backupService = new BackupService();
|
||||
|
||||
@@ -454,7 +455,7 @@ async function influxGroupValues(flux: string): Promise<Map<string, number>> {
|
||||
return out;
|
||||
}
|
||||
|
||||
async function detectAnomalies(siteId: number | null): Promise<AttentionItem[]> {
|
||||
async function detectAnomalies(siteId: SiteScope): Promise<AttentionItem[]> {
|
||||
const items: AttentionItem[] = [];
|
||||
const hour = new Date().getUTCHours();
|
||||
const nameById = new Map<string, { id: number; name: string }>();
|
||||
@@ -556,7 +557,7 @@ async function detectAnomalies(siteId: number | null): Promise<AttentionItem[]>
|
||||
// who did what to the install -- they carry no device and so belong to no site,
|
||||
// and repeating "admin changed a setting" into every site would misattribute it.
|
||||
// Device-scoped history (config changes, events) is filtered normally.
|
||||
async function buildActivity(siteId: number | null) {
|
||||
async function buildActivity(siteId: SiteScope) {
|
||||
const cfgFilter = siteScopeByDevice(siteId, 'dc.device_id');
|
||||
const evtFilter = siteScopeByNullableDevice(siteId, 'e.device_id');
|
||||
const [configs, audits, events] = await Promise.all([
|
||||
|
||||
@@ -2,11 +2,12 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// GET /api/routers/lldp — LLDP enabled/disabled status per online router
|
||||
router.get('/lldp', async (req: Request, res: Response) => {
|
||||
|
||||
@@ -2,10 +2,11 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByNullableDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// GET /api/search?q=<term>
|
||||
router.get('/', async (req: Request, res: Response) => {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { requireAuth, requireAdmin } from '../middleware/auth';
|
||||
import { netflowCollector } from '../services/netflow/NetflowCollector';
|
||||
import { validatePassword } from '../utils/passwordPolicy';
|
||||
import { OIDC_SETTINGS_KEY } from '../services/oidc/oidcConfig';
|
||||
import { bumpTokenVersion } from '../utils/sessionAuth';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
@@ -47,7 +48,7 @@ router.put('/', requireAdmin, async (req: Request, res: Response) => {
|
||||
// GET /api/settings/users
|
||||
router.get('/users', requireAdmin, async (_req: Request, res: Response) => {
|
||||
const users = await query(
|
||||
`SELECT id, username, email, role, auth_provider, created_at FROM users ORDER BY username`
|
||||
`SELECT id, username, email, role, active, auth_provider, created_at FROM users ORDER BY username`
|
||||
);
|
||||
res.json(users);
|
||||
});
|
||||
@@ -82,10 +83,10 @@ router.post('/users', requireAdmin, async (req: Request, res: Response) => {
|
||||
return res.status(201).json(created);
|
||||
});
|
||||
|
||||
// PUT /api/settings/users/:id - update role and/or reset password
|
||||
// PUT /api/settings/users/:id - update role, active flag and/or reset password
|
||||
router.put('/users/:id', requireAdmin, async (req: Request, res: Response) => {
|
||||
const userId = parseInt(req.params.id);
|
||||
const { role, password } = req.body;
|
||||
const { role, password, active } = req.body as { role?: string; password?: string; active?: boolean };
|
||||
|
||||
const existing = await queryOne<{ id: number; role: string }>(
|
||||
`SELECT id, role FROM users WHERE id = $1`,
|
||||
@@ -97,14 +98,29 @@ router.put('/users/:id', requireAdmin, async (req: Request, res: Response) => {
|
||||
if (userId === req.user!.userId && role && role !== 'admin') {
|
||||
return res.status(400).json({ error: 'Cannot change your own role away from admin' });
|
||||
}
|
||||
// Cannot deactivate your own account (mirrors the self-delete guard below;
|
||||
// an admin locking themselves out would need another admin to undo it).
|
||||
if (userId === req.user!.userId && active === false) {
|
||||
return res.status(400).json({ error: 'Cannot deactivate your own account' });
|
||||
}
|
||||
|
||||
const validRoles = ['admin', 'operator', 'viewer'];
|
||||
if (role && !validRoles.includes(role)) {
|
||||
return res.status(400).json({ error: 'Invalid role' });
|
||||
}
|
||||
|
||||
// A role change or deactivation is exactly the case SEC-001 exists for:
|
||||
// the account's existing sessions must stop working immediately, not at
|
||||
// their natural 24h expiry.
|
||||
let mustRevoke = false;
|
||||
|
||||
if (role) {
|
||||
await query(`UPDATE users SET role = $1 WHERE id = $2`, [role, userId]);
|
||||
mustRevoke = true;
|
||||
}
|
||||
if (typeof active === 'boolean') {
|
||||
await query(`UPDATE users SET active = $1 WHERE id = $2`, [active, userId]);
|
||||
if (!active) mustRevoke = true;
|
||||
}
|
||||
if (password) {
|
||||
const pwError = validatePassword(password);
|
||||
@@ -113,15 +129,28 @@ router.put('/users/:id', requireAdmin, async (req: Request, res: Response) => {
|
||||
}
|
||||
const hash = await bcrypt.hash(password, 12);
|
||||
await query(`UPDATE users SET password_hash = $1 WHERE id = $2`, [hash, userId]);
|
||||
mustRevoke = true;
|
||||
}
|
||||
if (mustRevoke) await bumpTokenVersion(userId);
|
||||
|
||||
const updated = await queryOne(
|
||||
`SELECT id, username, role, created_at FROM users WHERE id = $1`,
|
||||
`SELECT id, username, role, active, created_at FROM users WHERE id = $1`,
|
||||
[userId]
|
||||
);
|
||||
return res.json(updated);
|
||||
});
|
||||
|
||||
// POST /api/settings/users/:id/revoke-sessions — end every session for a user
|
||||
// right now, without changing their password or role (SEC-001). Useful for
|
||||
// "this laptop was stolen" without also forcing an unrelated password reset.
|
||||
router.post('/users/:id/revoke-sessions', requireAdmin, async (req: Request, res: Response) => {
|
||||
const userId = parseInt(req.params.id);
|
||||
const existing = await queryOne<{ id: number }>(`SELECT id FROM users WHERE id = $1`, [userId]);
|
||||
if (!existing) return res.status(404).json({ error: 'User not found' });
|
||||
await bumpTokenVersion(userId);
|
||||
return res.json({ message: 'All sessions for this user have been revoked' });
|
||||
});
|
||||
|
||||
// DELETE /api/settings/users/:id
|
||||
router.delete('/users/:id', requireAdmin, async (req: Request, res: Response) => {
|
||||
if (parseInt(req.params.id) === req.user!.userId) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { requireAuth, requireWrite, requireAdmin } from '../middleware/auth';
|
||||
import { invalidateUserSiteCache } from '../middleware/site';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
@@ -36,8 +37,15 @@ function normaliseCoords(lat: unknown, lng: unknown): { lat: number | null; lng:
|
||||
return ok ? { lat: latN, lng: lngN } : { lat: null, lng: null };
|
||||
}
|
||||
|
||||
// GET /api/sites — list with device counts, for the selector and the all-sites table
|
||||
router.get('/', async (_req: Request, res: Response) => {
|
||||
// GET /api/sites — list with device counts, for the selector and the all-sites table.
|
||||
// SEC-002: a restricted (non-admin) caller only sees sites they're a member
|
||||
// of -- otherwise every viewer could enumerate every other client's site
|
||||
// names regardless of the X-Site-Id enforcement on device data itself.
|
||||
router.get('/', async (req: Request, res: Response) => {
|
||||
const restricted = req.user!.role !== 'admin' && !req.user!.tokenAuth;
|
||||
const memberFilter = restricted
|
||||
? `WHERE s.id IN (SELECT site_id FROM user_sites WHERE user_id = $1)`
|
||||
: '';
|
||||
const sites = await query<SiteRow & { device_count: string }>(
|
||||
`SELECT s.id, s.name, s.address,
|
||||
s.location_lat::float8 AS location_lat,
|
||||
@@ -47,8 +55,10 @@ router.get('/', async (_req: Request, res: Response) => {
|
||||
COUNT(d.id) FILTER (WHERE d.status = 'online')::text AS online_count
|
||||
FROM sites s
|
||||
LEFT JOIN devices d ON d.site_id = s.id
|
||||
${memberFilter}
|
||||
GROUP BY s.id
|
||||
ORDER BY s.is_default DESC, LOWER(s.name) ASC`
|
||||
ORDER BY s.is_default DESC, LOWER(s.name) ASC`,
|
||||
restricted ? [req.user!.userId] : []
|
||||
);
|
||||
res.json(sites.map((s) => ({
|
||||
...s,
|
||||
@@ -180,4 +190,62 @@ router.post('/:id/devices', requireWrite, async (req: Request, res: Response) =>
|
||||
res.json({ ok: true, moved: ids.length });
|
||||
});
|
||||
|
||||
// ─── SEC-002: site membership management (admin only) ───────────────────────
|
||||
|
||||
// GET /api/sites/:id/users — who currently has access to this site
|
||||
router.get('/:id/users', requireAdmin, async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id)) { res.status(400).json({ error: 'Invalid site id' }); return; }
|
||||
const users = await query<{ id: number; username: string; role: string }>(
|
||||
`SELECT u.id, u.username, u.role FROM user_sites us
|
||||
JOIN users u ON u.id = us.user_id
|
||||
WHERE us.site_id = $1
|
||||
ORDER BY u.username`,
|
||||
[id]
|
||||
);
|
||||
res.json(users);
|
||||
});
|
||||
|
||||
// POST /api/sites/:id/users — grant a user access to this site
|
||||
router.post('/:id/users', requireAdmin, async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const userId = parseInt(String((req.body as { userId?: unknown }).userId), 10);
|
||||
if (!Number.isInteger(id) || !Number.isInteger(userId)) {
|
||||
res.status(400).json({ error: 'Invalid site id or userId' });
|
||||
return;
|
||||
}
|
||||
const [site, user] = await Promise.all([
|
||||
queryOne<{ id: number }>(`SELECT id FROM sites WHERE id = $1`, [id]),
|
||||
queryOne<{ id: number; role: string }>(`SELECT id, role FROM users WHERE id = $1`, [userId]),
|
||||
]);
|
||||
if (!site) { res.status(404).json({ error: 'Site not found' }); return; }
|
||||
if (!user) { res.status(404).json({ error: 'User not found' }); return; }
|
||||
if (user.role === 'admin') {
|
||||
// Admins already see every site implicitly; a membership row for one
|
||||
// would be dead data and would misleadingly suggest their access is
|
||||
// scoped.
|
||||
res.status(400).json({ error: 'Admins already have access to every site' });
|
||||
return;
|
||||
}
|
||||
await query(
|
||||
`INSERT INTO user_sites (user_id, site_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
[userId, id]
|
||||
);
|
||||
invalidateUserSiteCache(userId);
|
||||
res.status(201).json({ ok: true });
|
||||
});
|
||||
|
||||
// DELETE /api/sites/:id/users/:userId — revoke a user's access to this site
|
||||
router.delete('/:id/users/:userId', requireAdmin, async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const userId = parseInt(req.params.userId, 10);
|
||||
if (!Number.isInteger(id) || !Number.isInteger(userId)) {
|
||||
res.status(400).json({ error: 'Invalid site id or userId' });
|
||||
return;
|
||||
}
|
||||
await query(`DELETE FROM user_sites WHERE site_id = $1 AND user_id = $2`, [id, userId]);
|
||||
invalidateUserSiteCache(userId);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -2,11 +2,12 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// GET /api/switches — all switch devices with port statistics
|
||||
router.get('/', async (req: Request, res: Response) => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Router, Request, Response } from 'express';
|
||||
import { query, queryOne } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { PollerService } from '../services/PollerService';
|
||||
import {
|
||||
buildTopology,
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
let pollerService: PollerService | null = null;
|
||||
export function setPollerService(p: PollerService): void {
|
||||
|
||||
@@ -2,13 +2,14 @@ import { Router, Request, Response } from 'express';
|
||||
import { query } from '../config/database';
|
||||
import { requireAuth, requireWrite } from '../middleware/auth';
|
||||
import { siteScopeDevices, siteScopeByDevice } from '../utils/siteScope';
|
||||
import { activeSite } from '../middleware/site';
|
||||
import { activeSite, enforceSiteAccess } from '../middleware/site';
|
||||
import { DeviceCollector, DeviceRow } from '../services/mikrotik/DeviceCollector';
|
||||
import { lookupVendor } from '../utils/oui';
|
||||
import { analyzeSpectrum, summarize, BAND_RANGES, parseChannelSpec } from '../utils/rfSpectrum';
|
||||
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
router.use(enforceSiteAccess);
|
||||
|
||||
// ─── Helper ───────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import { encrypt, decrypt } from '../utils/crypto';
|
||||
import {
|
||||
generateDeviceKeyPair, keyFileName, keyComment, isUsablePrivateKey, type KeyStatus,
|
||||
} from '../utils/sshKeys';
|
||||
import { isValidRouterOsUsername } from '../utils/routerOsUsername';
|
||||
import { RouterOSClient } from './mikrotik/RouterOSClient';
|
||||
|
||||
const CONNECT_TIMEOUT_MS = 20_000;
|
||||
@@ -173,6 +174,13 @@ export class SshKeyService {
|
||||
async deploy(target: SshTarget, opts: { rotate?: boolean } = {}): Promise<DeviceKeyRow> {
|
||||
const username = target.ssh_username?.trim();
|
||||
if (!username) throw new Error('No SSH username configured for this device');
|
||||
// SEC-006: username is interpolated unescaped into a RouterOS CLI command
|
||||
// below (`user=${username}`). Validating here, at the point of use, is
|
||||
// defense in depth even though device writes already reject an invalid
|
||||
// username before it reaches storage (routes/devices.ts, deviceCreation.ts).
|
||||
if (!isValidRouterOsUsername(username)) {
|
||||
throw new Error('Stored SSH username contains characters that are not safe to use — fix it in device settings before deploying a key');
|
||||
}
|
||||
|
||||
const existing = await this.getKey(target.id);
|
||||
const canUseExistingKey = existing && existing.status === 'verified';
|
||||
|
||||
@@ -2,6 +2,7 @@ import { query, queryOne } from '../config/database';
|
||||
import { encrypt, decrypt } from '../utils/crypto';
|
||||
import { parsePort } from '../utils/parsePort';
|
||||
import { safeConnectionError } from '../utils/safeClientError';
|
||||
import { validateRouterOsUsername } from '../utils/routerOsUsername';
|
||||
import { RouterOSClient } from './mikrotik/RouterOSClient';
|
||||
import type { PollerService } from './PollerService';
|
||||
import type { CredentialPresetRow } from '../routes/credentialPresets';
|
||||
@@ -112,6 +113,13 @@ export async function createDeviceFromBody(
|
||||
body: { error: 'name, ip_address, api_username, api_password are required' },
|
||||
};
|
||||
}
|
||||
// SEC-006: ssh_username is later interpolated unescaped into a RouterOS CLI
|
||||
// command (services/SshKeyService.ts). Reject an unsafe value at the point
|
||||
// it's first stored, not just where it happens to be used.
|
||||
const sshUsernameError = validateRouterOsUsername(ssh_username);
|
||||
if (sshUsernameError) {
|
||||
return { ok: false, status: 400, body: { error: sshUsernameError } };
|
||||
}
|
||||
|
||||
const testClient = new RouterOSClient(ip_address, api_port, api_username, api_password, 10_000);
|
||||
let detectedSerial: string | null;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { isValidRouterOsUsername, validateRouterOsUsername } from '../routerOsUsername';
|
||||
|
||||
describe('SEC-006: RouterOS SSH username allowlist', () => {
|
||||
it('accepts ordinary usernames', () => {
|
||||
for (const u of ['admin', 'svc-rmm', 'ops_user', 'user.name', 'a1', 'A']) {
|
||||
expect(isValidRouterOsUsername(u)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects command-injection payloads that rely on RouterOS token separators', () => {
|
||||
const payloads = [
|
||||
'admin;/system/reboot',
|
||||
'admin /system/reboot',
|
||||
'admin\n/system/reboot',
|
||||
'admin\t/system/reboot',
|
||||
'admin"; /system/reboot; "',
|
||||
'admin$(reboot)',
|
||||
'admin`reboot`',
|
||||
'admin/../root',
|
||||
'admin=x comment=y',
|
||||
'',
|
||||
' ',
|
||||
'a'.repeat(33), // over the length cap
|
||||
];
|
||||
for (const p of payloads) {
|
||||
expect(isValidRouterOsUsername(p)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('validateRouterOsUsername treats an absent value as fine (field is optional)', () => {
|
||||
expect(validateRouterOsUsername(undefined)).toBeNull();
|
||||
expect(validateRouterOsUsername(null)).toBeNull();
|
||||
expect(validateRouterOsUsername('')).toBeNull();
|
||||
});
|
||||
|
||||
it('validateRouterOsUsername rejects a non-string and an unsafe string with a message', () => {
|
||||
expect(validateRouterOsUsername(123)).toMatch(/string/i);
|
||||
expect(validateRouterOsUsername('admin; reboot')).toMatch(/letters, digits/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Positive-allowlist validation for a RouterOS SSH/API username (SEC-006).
|
||||
*
|
||||
* The username is stored by an operator and later interpolated directly into
|
||||
* RouterOS CLI commands sent over an SSH exec channel (see
|
||||
* services/SshKeyService.ts: `/user/ssh-keys/import ... user=${username}`).
|
||||
* Escaping after the fact is fragile — RouterOS's own command parser has its
|
||||
* own quoting rules, not shell rules, so a shell-style escape can still leave
|
||||
* a value that RouterOS itself parses as more than one token. Validating
|
||||
* against a known-safe character set before it is ever stored is simpler and
|
||||
* closes the injection regardless of where the value is later used.
|
||||
*
|
||||
* RouterOS itself limits usernames to letters, digits, and a handful of
|
||||
* punctuation characters, with no whitespace — this allowlist is deliberately
|
||||
* narrower than what the device might tolerate, because "narrower than
|
||||
* necessary but provably safe" is the right trade for a value that ends up
|
||||
* unescaped in a command line.
|
||||
*/
|
||||
const VALID_USERNAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,31}$/;
|
||||
|
||||
export function isValidRouterOsUsername(username: string): boolean {
|
||||
return VALID_USERNAME.test(username);
|
||||
}
|
||||
|
||||
/** Returns an error message if invalid, or null if the username is acceptable. */
|
||||
export function validateRouterOsUsername(username: unknown): string | null {
|
||||
if (username === null || username === undefined || username === '') return null; // optional field
|
||||
if (typeof username !== 'string') return 'Username must be a string';
|
||||
if (!isValidRouterOsUsername(username)) {
|
||||
return 'Username may only contain letters, digits, "." "_" "-", must start with a letter or digit, and be at most 32 characters';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Server-side JWT revocation (SEC-001).
|
||||
*
|
||||
* JWTs stay stateless for signing/verifying (no DB write on every request),
|
||||
* but every session must be killable before its natural expiry: logout,
|
||||
* a password change, disabling an account, or an admin "sign this user out
|
||||
* everywhere" action. The mechanism is a `token_version` column on `users`:
|
||||
* every signed token embeds the version current at sign time, and a request
|
||||
* is rejected the moment that version no longer matches the row -- bumping
|
||||
* the column *is* the revoke.
|
||||
*
|
||||
* Checking on every request would mean a DB round trip per authenticated
|
||||
* call. A short in-process cache (a few seconds) keeps the common case at
|
||||
* zero extra latency while capping how long a revoked token can keep working
|
||||
* to a small, documented window -- far better than the 24h it had before.
|
||||
*/
|
||||
import { query, queryOne } from '../config/database';
|
||||
|
||||
export interface SessionState {
|
||||
tokenVersion: number;
|
||||
active: boolean;
|
||||
role: string;
|
||||
}
|
||||
|
||||
interface CacheEntry {
|
||||
state: SessionState | null; // null = user no longer exists
|
||||
cachedAt: number;
|
||||
}
|
||||
|
||||
const CACHE_TTL_MS = 5_000;
|
||||
const cache = new Map<number, CacheEntry>();
|
||||
|
||||
async function fetchState(userId: number): Promise<SessionState | null> {
|
||||
const row = await queryOne<{ token_version: number; active: boolean; role: string }>(
|
||||
`SELECT token_version, active, role FROM users WHERE id = $1`,
|
||||
[userId]
|
||||
);
|
||||
return row ? { tokenVersion: row.token_version, active: row.active, role: row.role } : null;
|
||||
}
|
||||
|
||||
/** Current session state for a user, cached for a few seconds. */
|
||||
export async function getSessionState(userId: number): Promise<SessionState | null> {
|
||||
const cached = cache.get(userId);
|
||||
if (cached && Date.now() - cached.cachedAt < CACHE_TTL_MS) {
|
||||
return cached.state;
|
||||
}
|
||||
const state = await fetchState(userId);
|
||||
cache.set(userId, { state, cachedAt: Date.now() });
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bump a user's token_version, invalidating every JWT issued before now for
|
||||
* that account. Used by logout, password change, TOTP disable, account
|
||||
* deactivation, and the admin "revoke sessions" action.
|
||||
*
|
||||
* Returns the new version so callers that also sign a fresh token (e.g.
|
||||
* password-change-while-staying-logged-in flows, if ever added) can embed it.
|
||||
*/
|
||||
export async function bumpTokenVersion(userId: number): Promise<number> {
|
||||
const rows = await query<{ token_version: number }>(
|
||||
`UPDATE users SET token_version = token_version + 1 WHERE id = $1 RETURNING token_version`,
|
||||
[userId]
|
||||
);
|
||||
cache.delete(userId); // don't serve a stale version for the next CACHE_TTL_MS
|
||||
return rows[0]?.token_version ?? 0;
|
||||
}
|
||||
|
||||
/** Test seam: clear the cache between tests / after direct DB mutation in tests. */
|
||||
export function _resetSessionCacheForTest(): void {
|
||||
cache.clear();
|
||||
}
|
||||
@@ -1,22 +1,35 @@
|
||||
/**
|
||||
* Site scoping helpers (issue #130).
|
||||
* Site scoping helpers (issue #130), extended for per-user authorization in
|
||||
* Mission 02 (SEC-002).
|
||||
*
|
||||
* Every device-derived table hangs off devices(id), so a single site_id on
|
||||
* devices scopes clients, events, topology, backups and the rest transitively.
|
||||
* These helpers turn an active site into the SQL predicate that does it.
|
||||
* These helpers turn an *effective* site scope into the SQL predicate that
|
||||
* does it.
|
||||
*
|
||||
* The site id is inlined rather than parameterised, and that is a deliberate
|
||||
* A scope is one of:
|
||||
* - `null` unrestricted (the whole fleet) -- admins and API tokens.
|
||||
* - `number` exactly one site -- either an explicit selection the caller
|
||||
* has already been authorized for (middleware/site.ts), or a
|
||||
* restricted user's only accessible site.
|
||||
* - `number[]` a restricted user viewing "all sites" (no X-Site-Id sent):
|
||||
* scoped to every site they're a member of, which may be
|
||||
* empty -- meaning no access to anything, not "everything".
|
||||
*
|
||||
* The id(s) are inlined rather than parameterised, and that is a deliberate
|
||||
* choice worth justifying: the collection routes build their WHERE clauses by
|
||||
* incrementing a shared $n counter, so injecting a parameter mid-build would
|
||||
* mean renumbering every filter in ~23 routes -- exactly the kind of manual
|
||||
* bookkeeping that produces silent off-by-one bugs. Inlining is safe here
|
||||
* because resolveSiteId() below admits nothing but a positive integer; a value
|
||||
* that is not one becomes null (unscoped), never text that reaches SQL.
|
||||
* because every id passing through this module is validated as a positive
|
||||
* integer first (resolveSiteId, or read back from user_sites via a parameterised
|
||||
* query) -- never raw text reaching SQL.
|
||||
*/
|
||||
export type SiteScope = number | number[] | null;
|
||||
|
||||
/**
|
||||
* Normalise an untrusted site id (header, query string) into either a positive
|
||||
* integer or null, where null means "no scoping -- show the whole fleet".
|
||||
* integer or null, where null means "no explicit selection".
|
||||
*
|
||||
* 'all' is the explicit all-sites view. Anything unparseable is also treated as
|
||||
* unscoped rather than rejected, so an existing API consumer that knows nothing
|
||||
@@ -27,7 +40,7 @@ export function resolveSiteId(raw: unknown): number | null {
|
||||
const s = String(raw).trim();
|
||||
if (s === '' || s.toLowerCase() === 'all') return null;
|
||||
// Matched as a literal digit string rather than via Number(), deliberately.
|
||||
// Number() would accept '1e3', '0x1' and even Arabic-Indic '\u0661\u0662' as
|
||||
// Number() would accept '1e3', '0x1' and even Arabic-Indic '١٢' as
|
||||
// integers -- all harmless here, but it makes the safety of inlining rest on
|
||||
// coercion trivia. A digits-only regex makes it self-evident instead.
|
||||
if (!/^\d+$/.test(s)) return null;
|
||||
@@ -36,23 +49,41 @@ export function resolveSiteId(raw: unknown): number | null {
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapse a scope to a single concrete site id, or null. For an unrestricted
|
||||
* caller (null) or a restricted user viewing several sites at once (array),
|
||||
* there is no single "the site this belongs to" answer -- callers that need
|
||||
* one (e.g. "which site does a newly created device join") get null and fall
|
||||
* back to their own default-site behavior, same as an admin always has.
|
||||
*/
|
||||
export function singleSiteOrNull(scope: SiteScope): number | null {
|
||||
return typeof scope === 'number' ? scope : null;
|
||||
}
|
||||
|
||||
function idList(ids: number[]): string {
|
||||
// An empty IN () is invalid SQL and, more importantly, its intent ("match
|
||||
// nothing") is easy to get backwards. Spell it out instead.
|
||||
return ids.length ? `(${ids.join(',')})` : '(NULL)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Predicate for a query that selects from `devices` itself.
|
||||
* Returns null when unscoped, so callers can skip pushing a filter.
|
||||
*/
|
||||
export function siteScopeDevices(siteId: number | null, alias = ''): string | null {
|
||||
if (siteId === null) return null;
|
||||
export function siteScopeDevices(scope: SiteScope, alias = ''): string | null {
|
||||
if (scope === null) return null;
|
||||
const col = alias ? `${alias}.site_id` : 'site_id';
|
||||
return `${col} = ${siteId}`;
|
||||
return Array.isArray(scope) ? `${col} IN ${idList(scope)}` : `${col} = ${scope}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Predicate for a query on any table carrying a device_id.
|
||||
* `column` is the fully-qualified device id column, e.g. 'c.device_id'.
|
||||
*/
|
||||
export function siteScopeByDevice(siteId: number | null, column = 'device_id'): string | null {
|
||||
if (siteId === null) return null;
|
||||
return `${column} IN (SELECT id FROM devices WHERE site_id = ${siteId})`;
|
||||
export function siteScopeByDevice(scope: SiteScope, column = 'device_id'): string | null {
|
||||
if (scope === null) return null;
|
||||
const inner = Array.isArray(scope) ? `site_id IN ${idList(scope)}` : `site_id = ${scope}`;
|
||||
return `${column} IN (SELECT id FROM devices WHERE ${inner})`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,9 +92,10 @@ export function siteScopeByDevice(siteId: number | null, column = 'device_id'):
|
||||
* site is selected -- showing them in every site would misrepresent them as
|
||||
* that site's own.
|
||||
*/
|
||||
export function siteScopeByNullableDevice(siteId: number | null, column = 'device_id'): string | null {
|
||||
if (siteId === null) return null;
|
||||
return `(${column} IS NOT NULL AND ${column} IN (SELECT id FROM devices WHERE site_id = ${siteId}))`;
|
||||
export function siteScopeByNullableDevice(scope: SiteScope, column = 'device_id'): string | null {
|
||||
if (scope === null) return null;
|
||||
const inner = Array.isArray(scope) ? `site_id IN ${idList(scope)}` : `site_id = ${scope}`;
|
||||
return `(${column} IS NOT NULL AND ${column} IN (SELECT id FROM devices WHERE ${inner}))`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,6 +119,8 @@ export function andSite(base: string, predicate: string | null): string {
|
||||
*
|
||||
* When a site holds every device, its total *is* the global total -- an
|
||||
* identity, not an approximation -- so `_global` is both correct and complete.
|
||||
* Only meaningful for a single explicit site; a restricted multi-site scope
|
||||
* has no single tag and callers should fall back to per-site queries instead.
|
||||
*/
|
||||
export function clientSeriesTag(
|
||||
siteId: number | null,
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
# Auditoria Técnica e de Segurança — Base Upstream 2GT (MikroTik Manager)
|
||||
|
||||
- Commit auditado: `1a4b6350528b8c656ab7a84ee4806a8a86cf9932` (v0.24.5-beta)
|
||||
- Branch: `mikrormm-dev`, sobre bootstrap `7a4979e`
|
||||
- Escopo: código-fonte completo (backend, frontend, Docker), sem RouterBOARD real conectada
|
||||
- Método: leitura de código, grep dirigido, testes automatizados (jest/vitest), `npm audit`, análise de Dockerfiles/compose. Nenhuma credencial real foi usada ou impressa.
|
||||
|
||||
## Mapa factual da arquitetura
|
||||
|
||||
- **Frontend**: React 19 + Vite, SPA servida por nginx, autentica via Bearer JWT em `localStorage`.
|
||||
- **Backend**: Node/Express (`backend/src/index.ts`), monta ~28 routers sob `/api/*`, todos com `router.use(requireAuth)` no topo do arquivo exceto os endpoints públicos explícitos de `oidc.ts` (`/status`, `/login`, `/callback`).
|
||||
- **Autenticação**: `routes/auth.ts` + `middleware/auth.ts` — JWT HS256, bcryptjs, TOTP opcional (otpauth), OIDC opcional (openid-client).
|
||||
- **Autorização**: 3 papéis globais (`admin`/`operator`/`viewer`) checados em `middleware/auth.ts` (`requireAdmin`, `requireWrite`). Sem escopo por site/tenant.
|
||||
- **Banco**: PostgreSQL, schema em `db/migrate.ts` (migrations idempotentes, `CREATE TABLE IF NOT EXISTS` + `ALTER` incrementais), executado automaticamente no boot do backend.
|
||||
- **Redis/BullMQ**: filas de polling/automação; rate limiting (`middleware/rateLimitRedis.ts`) também usa Redis com fallback em memória.
|
||||
- **InfluxDB**: métricas de série temporal (`config/influxdb.ts`, consultado em `routes/metrics.ts`).
|
||||
- **Jobs/Scheduler/Polling**: `services/PollerService.ts`, `services/DeviceBulkAddWorker.ts`.
|
||||
- **Alertas**: `services/AlertService.ts` + `routes/alerts.ts`.
|
||||
- **RouterOS integration**: `services/mikrotik/RouterOSClient.ts` (API binário RouterOS, TCP/TLS), `services/mikrotik/DeviceCollector.ts`.
|
||||
- **SSH**: `services/sshExec.ts`, `services/SshKeyService.ts`, `services/BackupService.ts` (ssh2), terminal interativo via Socket.IO (`index.ts` namespace `/terminal`).
|
||||
- **API**: ~28 arquivos de rota, ~250 endpoints.
|
||||
- **SNMP**: não encontrada implementação SNMP no backend (ausente — não fazia parte do escopo upstream atual).
|
||||
- **NetFlow**: `services/netflow/NetflowCollector.ts`, coletor UDP na porta 2055, exposto no `docker-compose.yml`.
|
||||
- **Backups**: `services/BackupService.ts` + `routes/backups.ts`, arquivos `.rsc` (`/export compact`) em `backups_data` volume.
|
||||
- **Firmware upgrade**: `services/FirmwareOrchestrator.ts` + `routes/firmware.ts`, waves com concorrência limitada (`utils/concurrency.ts`, `MAX_WAVE_CONCURRENCY=20`).
|
||||
- **Terminal**: Socket.IO namespace `/terminal`, restrito a `admin`/`operator`, com auditoria e rate limit.
|
||||
- **Change Guard**: `services/changeGuard/ChangeGuard.ts` — dead-man-switch no próprio dispositivo (scheduler RouterOS que desfaz a mudança se a conexão não for reconfirmada).
|
||||
- **Auditoria**: `middleware/auditMiddleware.ts` (global) + tabela `audit_log`, consultável via `routes/auditLog.ts` (admin-only).
|
||||
|
||||
## Achados
|
||||
|
||||
### SEC-001 — Sessões JWT sem revogação server-side
|
||||
**Severidade:** MÉDIO
|
||||
**Componente:** `backend/src/middleware/auth.ts`, `routes/auth.ts`
|
||||
**Evidência:** `signToken`/`verifyToken` não consultam o banco; `POST /api/auth/logout` só responde OK sem invalidar nada ("JWT is stateless; client just discards token"); `PUT /api/auth/password` e `POST /api/auth/totp/disable` não alteram nenhum estado que `verifyToken` observe. Comprovado por teste automatizado (`middleware/__tests__/auditFindings.test.ts`, SEC-001).
|
||||
**Causa-raiz:** design stateless sem `jti`/versão de sessão nem lista de revogação.
|
||||
**Impacto:** um token vazado (XSS, log, dispositivo comprometido) continua válido por até 24h mesmo depois de o usuário trocar a senha, desabilitar TOTP ou ser removido/rebaixado por um admin — só uma rotação de `JWT_SECRET` (que derruba TODAS as sessões) resolve.
|
||||
**Exploração/condição:** requer vazamento prévio do token; não é explorável remotamente sem esse pré-requisito.
|
||||
**Recomendação:** adicionar `jti` + tabela de revogação (ou coluna `token_version` em `users`, checada em `requireAuth`), e reduzir `expiresIn` para algo como 1–4h com refresh token.
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-002 — "Sites" são um filtro de exibição, não um limite de autorização
|
||||
**Severidade:** ALTO (crítico para uso multi-cliente GTI; baixo para uso interno de um único tenant)
|
||||
**Componente:** `backend/src/utils/siteScope.ts`, `middleware/site.ts`, schema `users`
|
||||
**Evidência:** `siteContext` lê `X-Site-Id` do cliente sem checar identidade; `resolveSiteId` não recebe `req.user`; tabela `users` (`db/migrate.ts:6-12`) não tem nenhuma coluna/relação com `sites`. Comprovado por teste (`auditFindings.test.ts`, SEC-002) e por inspeção do schema.
|
||||
**Causa-raiz:** o recurso "sites" (#130) foi implementado como agrupamento operacional (dashboards por site), não como fronteira de tenant.
|
||||
**Impacto:** qualquer usuário autenticado — mesmo `viewer` — enxerga e (se `operator`/`admin`) opera sobre TODOS os dispositivos de TODOS os sites simplesmente omitindo o header `X-Site-Id`. Se a GTI pretende dar a um técnico/cliente acesso restrito a "seus" equipamentos, essa garantia **não existe hoje**.
|
||||
**Recomendação:** antes de onboarding com múltiplos clientes reais no mesmo backend, implementar RBAC por site (tabela `user_sites`, checagem no middleware, nunca confiar em header sem cruzar com o usuário).
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-003 — RouterOS API-SSL sem verificação de certificado
|
||||
**Severidade:** MÉDIO
|
||||
**Componente:** `backend/src/services/mikrotik/RouterOSClient.ts:97`
|
||||
**Evidência:** `tls.connect({ host, port, rejectUnauthorized: false }, ...)`.
|
||||
**Causa-raiz:** RouterOS normalmente usa certificado autoassinado; verificar CA exigiria gerenciamento de certificados por dispositivo, não implementado.
|
||||
**Impacto:** conexão à RouterBOARD é suscetível a MITM na rede entre backend e device — um atacante na mesma rede/segmento poderia interceptar credenciais RouterOS e comandos.
|
||||
**Recomendação:** oferecer *trust-on-first-use* com fixação de fingerprint por dispositivo (guardar o hash do cert na primeira conexão e alertar/rejeitar se mudar), em vez de desabilitar globalmente.
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-004 — Chaves de criptografia default nunca expiram como fallback de decrypt
|
||||
**Severidade:** MÉDIO
|
||||
**Componente:** `backend/src/utils/secrets.ts:154-161`, `utils/crypto.ts`
|
||||
**Evidência:** `encDecryptors` sempre inclui `KNOWN_DEFAULT_ENC` (as duas chaves públicas do repositório) na lista de chaves tentadas para decrypt, permanentemente, mesmo depois de anos de rotação.
|
||||
**Causa-raiz:** decisão deliberada de nunca quebrar dados antigos cifrados com a chave default (documentada no próprio código).
|
||||
**Impacto:** se uma instalação alguma vez rodou sem `ENCRYPTION_KEY` customizada (mesmo que temporariamente) e um `pg_dump` daquele período vazar, os segredos daquele período são decifráveis com uma chave pública e conhecida deste repositório — para sempre.
|
||||
**Recomendação:** aceitável como *tradeoff* de migração, mas documentar explicitamente no runbook de produção; considerar expurgar as chaves default do array de decrypt após confirmar (via `needsReencryption`) que nenhum registro as usa mais.
|
||||
**Status:** ACCEPTED (mitigação: `ENCRYPTION_KEY` DEV já é forte e aleatória desde a Missão 00; nunca esteve no default)
|
||||
|
||||
### SEC-005 — Change Guard cobre apenas um subconjunto pequeno das operações de escrita
|
||||
**Severidade:** MÉDIO (expectativa vs. realidade)
|
||||
**Componente:** `backend/src/routes/devices.ts` (único chamador de `withSafeApply`), `routes/commands.ts`, terminal Socket.IO
|
||||
**Evidência:** `grep -rl withSafeApply routes/` retorna **apenas** `devices.ts`, usado no fluxo de "preflight"/apply genérico de mudanças analisadas (`analyzeChange`). As dezenas de endpoints diretos de firewall, NAT, rotas, VLAN, bonds, endereços IP, interfaces (mesmo arquivo `devices.ts`) chamam a API RouterOS diretamente, sem snapshot nem dead-man-switch. `commands.ts` (execução em massa) e o terminal SSH interativo também não passam por `ChangeGuard`.
|
||||
**Causa-raiz:** Change Guard foi construído para o fluxo guiado de "mudança arriscada analisada", não como interceptador universal de toda escrita.
|
||||
**Impacto:** um operador pode remover uma rota, uma regra de firewall ou uma interface via endpoint direto (ou via terminal/comando em massa) e perder conectividade com o dispositivo sem qualquer rede de segurança automática.
|
||||
**O que o Change Guard REALMENTE garante:** para o fluxo que passa por `withSafeApply`, garante rollback automático se a nova conexão de verificação falhar, com um restore point salvo no próprio dispositivo antes da mudança.
|
||||
**O que ele NÃO garante:** proteção em firewall/NAT/rotas/VLAN/interfaces editados diretamente, em `commands.ts` (bulk), no terminal interativo, em `configHistory.ts` rollback, ou contra falha de hardware/queda de energia do device.
|
||||
**Recomendação:** documentar essa cobertura parcial para os operadores GTI e avaliar estender `withSafeApply` aos endpoints de firewall/rotas/VLAN mais usados antes de operar em RBs de produção.
|
||||
**Status:** OPEN (documentação atualizada nesta auditoria)
|
||||
|
||||
### SEC-006 — Injeção de comando RouterOS via `ssh_username` não sanitizado
|
||||
**Severidade:** BAIXO
|
||||
**Componente:** `backend/src/services/SshKeyService.ts:196-204`
|
||||
**Evidência:** `` `/user/ssh-keys/import public-key-file=${fileName} user=${username}` `` e `` `/file/remove ${fileName}` `` interpolam `username`/`fileName` sem escaping. `fileName` é gerado internamente (seguro); `username` vem de `target.ssh_username`, campo configurável por qualquer usuário `operator`/`admin` ao cadastrar o dispositivo.
|
||||
**Causa-raiz:** ausência de validação/allowlist de caracteres para `ssh_username`.
|
||||
**Impacto:** um operador que já tem permissão de configurar credenciais de um dispositivo (logo, já tem acesso legítimo a ele) poderia usar um `ssh_username` malformado para injetar comandos RouterOS adicionais na mesma sessão SSH. Não é escalação de privilégio (o ator já controla o dispositivo), mas quebra o princípio de defesa em profundidade.
|
||||
**Recomendação:** validar `ssh_username` contra o alfabeto aceito pelo RouterOS para nomes de usuário antes de interpolar em qualquer comando.
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-007 — `nginx:alpine` sem tag de versão e demais imagens sem pinning por digest
|
||||
**Severidade:** BAIXO (reprodutibilidade/supply chain)
|
||||
**Componente:** `nginx/Dockerfile:12`, `docker-compose.yml`
|
||||
**Evidência:** `FROM nginx:alpine` (sem versão nenhuma); `FROM node:26-alpine`; `postgres:15-alpine`, `redis:7-alpine`, `influxdb:2.7` — nenhuma imagem fixada por digest sha256.
|
||||
**Impacto:** dois builds em datas diferentes podem produzir imagens de base diferentes (patches de SO, ou até uma nova major do nginx), reduzindo a reprodutibilidade entre DEV e PROD e dificultando auditoria de supply chain.
|
||||
**Recomendação:** fixar ao menos a versão do nginx (`nginx:1.27-alpine`) e considerar pinning por digest em PROD.
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-008 — Dockerfiles usam `npm install` em vez de `npm ci`
|
||||
**Severidade:** BAIXO
|
||||
**Componente:** `backend/Dockerfile`, `nginx/Dockerfile` (estágio frontend-builder)
|
||||
**Evidência:** ambos os estágios de build rodam `npm install` em vez de `npm ci`, apesar de existir `package-lock.json` no repositório.
|
||||
**Impacto:** `npm install` pode resolver versões ligeiramente diferentes das travadas no lockfile em rebuilds futuros (dentro dos ranges semver), reduzindo reprodutibilidade bit-a-bit do build.
|
||||
**Recomendação:** trocar para `npm ci` nos dois Dockerfiles.
|
||||
**Status:** OPEN
|
||||
|
||||
### SEC-009 — `qs`/`body-parser`/`express` — DoS moderado (transitivo)
|
||||
**Severidade:** BAIXO
|
||||
**Componente:** dependência transitiva `qs` (via `express@4.22.2` → `body-parser`)
|
||||
**Evidência:** `npm audit --omit=dev` no backend reporta 3 vulnerabilidades moderadas (GHSA-x5fp-wj9c-mxmx, GHSA-4mjr-xmp4-gh2g) — bypass de limite de array / DoS via `isBuffer` manipulado.
|
||||
**Contexto de exploração:** a API já tem rate limit global (120 req/min/IP, `rateLimitRedis`), reduzindo o valor prático do DoS. Correção disponível via `npm audit fix` dentro do range semver do Express 4.x.
|
||||
**Recomendação:** aplicar o fix na próxima janela de manutenção de dependências (fora do escopo desta missão — "não atualizar dependências" foi respeitado aqui).
|
||||
**Status:** OPEN (não corrigido nesta missão, por instrução explícita da missão)
|
||||
|
||||
### SEC-010 — Rate limit de login é só por IP, sem lockout por conta
|
||||
**Severidade:** BAIXO/INFORMATIVO
|
||||
**Componente:** `backend/src/middleware/rateLimitRedis.ts` (`loginRateLimit`)
|
||||
**Evidência:** chave `rl:login:ip:${ip}`; nenhuma chave por `username`.
|
||||
**Impacto:** um atacante distribuído (múltiplos IPs/proxies) não é throttled por conta — mas a mensagem de erro é genérica ("Invalid credentials") em ambos os casos (usuário inexistente ou senha errada), então não há enumeração de usuários.
|
||||
**Recomendação:** considerar um limite adicional por `username` além do IP.
|
||||
**Status:** ACCEPTED (mitigação parcial já existe; risco baixo)
|
||||
|
||||
### SEC-011 — Política de senha mínima aceitável, sem verificação de senha vazada
|
||||
**Severidade:** INFORMATIVO
|
||||
**Componente:** `backend/src/utils/passwordPolicy.ts`
|
||||
**Evidência:** mínimo 10 caracteres, 1 letra, 1 número; sem checagem contra listas de senhas vazadas (ex. HaveIBeenPwned) nem bloqueio de senhas triviais como `Password123`.
|
||||
**Recomendação:** aceitável para baseline; considerar reforço futuro.
|
||||
**Status:** ACCEPTED
|
||||
|
||||
### SEC-012 — Criptografia de credenciais RouterOS (AES-256-GCM) — bem desenhada
|
||||
**Severidade:** INFORMATIVO (achado positivo)
|
||||
**Componente:** `backend/src/utils/crypto.ts`, `utils/secrets.ts`
|
||||
**Evidência:** AES-256-GCM com IV aleatório de 16 bytes por operação, tag de autenticação verificada (`setAuthTag`/`getAuthTag`), chave derivada via SHA-256 quando não tem exatamente 32 bytes, chave nunca persistida no banco (arquivo `secrets.json` fora do Postgres, modo `0o600`), suporte a rotação com múltiplas chaves de decrypt. Design correto e documentado no próprio código.
|
||||
**Status:** FALSE_POSITIVE (não é um achado negativo — registrado como ponto forte confirmado)
|
||||
|
||||
### SEC-013 — Backups: nomes de arquivo sanitizados corretamente, sem path traversal encontrado
|
||||
**Severidade:** INFORMATIVO
|
||||
**Componente:** `backend/src/services/BackupService.ts:73-74`
|
||||
**Evidência:** `filename = device.name.replace(/[^a-z0-9]/gi, '_')`; `file_path` sempre construído com `path.join(BACKUPS_DIR, String(device.id), filename)`, `device.id` é `SERIAL` numérico. Endpoints de download/preview/restore buscam `file_path` do banco por `id` numérico, nunca aceitam path do cliente.
|
||||
**Status:** FALSE_POSITIVE
|
||||
|
||||
### SEC-014 — RBAC de backend (rotas) consistente com o esperado
|
||||
**Severidade:** INFORMATIVO
|
||||
**Evidência:** todas as 28 rotas verificadas têm `router.use(requireAuth)` (ou, no caso de `oidc.ts`, `requireAuth`/`requireAdmin` por rota, corretamente deixando só `/status`, `/login`, `/callback` públicos). Endpoints administrativos sensíveis (`users`, `oidc/config`, `audit-log`, `cert/regenerate`, `credential-presets`, `maintenance-windows`) usam `requireAdmin`. Autoproteção contra auto-rebaixamento/auto-exclusão de admin implementada em `routes/settings.ts`.
|
||||
**Status:** FALSE_POSITIVE (sem achado — RBAC de rota está correto)
|
||||
|
||||
## Achados por severidade
|
||||
- Crítico: 0
|
||||
- Alto: 1 (SEC-002)
|
||||
- Médio: 4 (SEC-001, SEC-003, SEC-004, SEC-005)
|
||||
- Baixo: 5 (SEC-006, SEC-007, SEC-008, SEC-009, SEC-010)
|
||||
- Informativo: 5 (SEC-011 a SEC-014, incl. 2 achados positivos)
|
||||
|
||||
## Principais 10 achados
|
||||
1. SEC-002 — Sites não são fronteira de autorização (ALTO para multi-tenant).
|
||||
2. SEC-005 — Change Guard cobre só um subconjunto pequeno de operações de escrita.
|
||||
3. SEC-001 — Sessões JWT sem revogação server-side.
|
||||
4. SEC-003 — RouterOS API-SSL sem verificação de certificado (`rejectUnauthorized: false`).
|
||||
5. SEC-004 — Chaves de criptografia default como fallback de decrypt permanente.
|
||||
6. SEC-006 — Injeção potencial de comando RouterOS via `ssh_username`.
|
||||
7. SEC-009 — DoS moderado transitivo em `qs`/`express` (não corrigido por instrução da missão).
|
||||
8. SEC-007/SEC-008 — Supply chain: imagens sem pinning, `npm install` em vez de `npm ci`.
|
||||
9. SEC-010 — Rate limit de login só por IP.
|
||||
10. SEC-012/SEC-013/SEC-014 — Pontos fortes confirmados: criptografia de credenciais, sanitização de path de backup, RBAC de rota consistente.
|
||||
|
||||
## Credenciais RouterOS
|
||||
**Avaliação: aceitável com ressalva.** Armazenamento em repouso é sólido (AES-256-GCM, IV único, tag autenticada, chave fora do banco, rotação suportada). A ressalva é em trânsito: TLS sem verificação de certificado (SEC-003) expõe a credencial a MITM na rede local. Não há exposição das credenciais em API, logs (`logSafe`/`safeClientError` fazem redação deliberada) ou frontend (`maskedConfig` para OIDC, sem exposição de `*_encrypted` observada nas rotas revisadas).
|
||||
|
||||
## RBAC
|
||||
**Avaliação: seguro dentro de um único tenant, inseguro para multi-cliente.** A separação admin/operator/viewer é aplicada corretamente no backend (não só na UI) e resiste a manipulação direta de endpoint. A falha estrutural é SEC-002: não existe isolamento por site/cliente — inaceitável se a GTI planeja hospedar múltiplos clientes finais na mesma instância antes de implementar `user_sites`.
|
||||
|
||||
## Operações remotas
|
||||
**Avaliação: seguro com uma ressalva de TLS.** Terminal SSH interativo tem controle de papel (admin/operator), auditoria e rate limit por usuário. RouterOS API usa protocolo binário estruturado (não concatenação de string), reduzindo risco de injeção clássica. A ressalva é SEC-003 (TLS sem verificação).
|
||||
|
||||
## Change Guard
|
||||
**O que realmente protege:** mudanças que passam pelo fluxo de "preflight + apply analisado" em `devices.ts`, com snapshot no dispositivo e rollback automático via scheduler RouterOS se a verificação de conectividade pós-mudança falhar.
|
||||
**O que NÃO protege:** a maioria das edições diretas de firewall/NAT/rotas/VLAN/interfaces, execução de comandos em massa (`commands.ts`) e o terminal interativo — nenhum desses passa por `withSafeApply`.
|
||||
|
||||
## Backups
|
||||
**Estado: seguro para o escopo atual.** Nomes de arquivo sanitizados, paths nunca vêm do cliente, autorização de escrita (`requireWrite`) presente em criar/restaurar/excluir. Sem criptografia em repouso do arquivo `.rsc` em si (fica em texto plano no volume `backups_data`) — aceitável para DEV, a avaliar antes de integrar com SFTP GTI (fora do escopo desta missão).
|
||||
|
||||
## Docker
|
||||
**Estado: funcional, com dívida de reprodutibilidade.** Non-root user no backend, capability mínima necessária (`NET_RAW` só para arp-scan/ping), healthchecks presentes. Pontos a melhorar: pinning de imagens (SEC-007) e `npm ci` (SEC-008).
|
||||
|
||||
## Dependências
|
||||
**Estado: aceitável.** Backend: 3 vulnerabilidades moderadas transitivas (SEC-009), sem crítico/alto. Frontend: 0 vulnerabilidades em dependências de produção. Nenhuma atualização automática foi aplicada, conforme instrução da missão.
|
||||
|
||||
## Testes
|
||||
```
|
||||
BACKEND TESTS: 545/545 (543 originais + 2 de auditoria, SEC-001/SEC-002)
|
||||
FRONTEND TESTS: 56/56
|
||||
TYPECHECK: PASS
|
||||
LINT: PASS (0 erros; 270 warnings backend / 108 frontend, security/detect-object-injection e afins — nenhum classificado como vulnerabilidade real nesta auditoria; ver observação abaixo)
|
||||
BUILD: PASS
|
||||
```
|
||||
|
||||
**Nota sobre os warnings do `eslint-plugin-security`:** os 378 warnings (270+108) foram amostrados por arquivo (`rfSpectrum.ts`, `serverArp.ts`, `secrets.ts`, `wifiChannels.ts`, `topologyLayout.ts` entre outros). Nenhum ponto analisado usa índice vindo diretamente de entrada HTTP não validada para acessar um objeto sensível (a maioria é iteração sobre arrays/enums internos, falso positivo típico da regra `detect-object-injection`). Classificação: **falso positivo majoritário** — não foi encontrada uma cadeia de exploração real amostrando os arquivos mais citados pela ferramenta. Não corrigidos mecanicamente, conforme instrução da missão.
|
||||
|
||||
## Primeira RB GTI read-only
|
||||
**AUTORIZADA PARA PRIMEIRA RB GTI READ-ONLY: SIM**
|
||||
|
||||
Para uma **única** RouterBOARD, em modo **read-only** (sem escrita, sem Change Guard necessário, sem terminal), com um único usuário/tenant, a base é segura o suficiente: credenciais cifradas corretamente, RBAC de rota correto, auditoria presente. As ressalvas (TLS sem verificação — SEC-003, sessões sem revogação — SEC-001) são aceitáveis para um piloto controlado e monitorado.
|
||||
|
||||
## Correções obrigatórias antes da primeira RB **de escrita** ou multi-cliente
|
||||
1. **SEC-002** — implementar RBAC por site antes de conectar RBs de mais de um cliente final na mesma instância.
|
||||
2. **SEC-005** — decidir explicitamente (e documentar para os operadores GTI) quais operações de escrita ficam sem rede de segurança do Change Guard, ou estender `withSafeApply` a firewall/rotas antes de liberar escrita irrestrita em produção.
|
||||
3. **SEC-003** — avaliar fixação de fingerprint TLS antes de operar RBs fora de uma rede de gestão confiável.
|
||||
|
||||
## Documentação operacional
|
||||
`docs/SISTEMA_ATUAL.md` foi atualizado (seções 11 e 22) para refletir SEC-001, SEC-002, SEC-003 e SEC-005 como fatos comprovados sobre o sistema atual.
|
||||
|
||||
## Git
|
||||
- Commit desta missão: ver `git log -1` após o commit de auditoria (arquivos: este relatório, teste `middleware/__tests__/auditFindings.test.ts`, atualização de `docs/SISTEMA_ATUAL.md`).
|
||||
- Nenhuma refatoração ampla foi feita; nenhuma correção de vulnerabilidade foi aplicada ao código de produção (todos os achados ficam como `OPEN`/`ACCEPTED`, conforme regra da missão — só se corrige o que é necessário para provar/criar o teste).
|
||||
- Worktree limpo ao final.
|
||||
|
||||
## VEREDITO FINAL
|
||||
|
||||
# APROVADO COM CORREÇÕES OBRIGATÓRIAS
|
||||
|
||||
A arquitetura é sólida e o nível de engenharia é notavelmente maduro para uma base open source (comentários de causa-raiz, testes extensivos, defesa em profundidade em vários pontos, Change Guard bem desenhado onde é usado). Não há achado CRÍTICO. Mas existe um achado ALTO (SEC-002) que é estrutural para o modelo de negócio pretendido pela GTI (múltiplos clientes) e um MÉDIO (SEC-005) que muda a expectativa operacional sobre o que o Change Guard cobre. Nenhum dos dois inviabiliza a base — ambos são corrigíveis sem trocar de projeto — mas devem ser resolvidos antes de qualquer RB de escrita ou multi-cliente real.
|
||||
|
||||
## Próxima missão recomendada
|
||||
**MISSÃO 02 — RBAC por site (`user_sites`) + extensão do Change Guard aos endpoints de firewall/rotas mais usados**, antes de iniciar qualquer integração GTI (SFTP, GLPI, Slack) ou conectar a primeira RouterBOARD real em modo de escrita.
|
||||
@@ -0,0 +1,136 @@
|
||||
# MikroRMM — Sistema Atual
|
||||
|
||||
## 1. Objetivo do sistema
|
||||
Plataforma de gestão e monitoramento de dispositivos MikroTik (RouterOS): inventário de clientes/dispositivos, polling de métricas, backups, automação, alertas, change guard, firmware rollout, topologia de rede.
|
||||
|
||||
## 2. Base upstream
|
||||
- Projeto: MikroTik Manager (2GT Media Group LLC)
|
||||
- Repositório: https://github.com/2GT-Media-Group-LLC/mikrotik-manager
|
||||
- Licença: ver `LICENSE` na raiz do repo
|
||||
- Remote local: `origin` → https://github.com/2GT-Media-Group-LLC/mikrotik-manager.git
|
||||
- Commit baseline: `1a4b6350528b8c656ab7a84ee4806a8a86cf9932` (branch `main`, tag semântica v0.24.5-beta)
|
||||
- Branch de trabalho MikroRMM: `mikrormm-dev`
|
||||
|
||||
## 3. Arquitetura atual
|
||||
Stack Docker Compose com 5 serviços: `nginx` (proxy/reverse proxy + estáticos do frontend), `backend` (API Node/Express + poller + coletor NetFlow), `postgres` (dados relacionais), `redis` (filas BullMQ), `influxdb` (métricas de série temporal).
|
||||
|
||||
## 4. Estrutura do repositório
|
||||
- `backend/` — API Node.js/TypeScript (Express, BullMQ, Socket.IO, SSH2)
|
||||
- `frontend/` — SPA React 19 + Vite + TailwindCSS
|
||||
- `nginx/` — Dockerfile, `nginx.conf`, `entrypoint.sh` (gera cert self-signed se ausente)
|
||||
- `docs/` — documentação (mkdocs)
|
||||
- `scripts/` — utilitários e fixtures de seed
|
||||
|
||||
## 5. Containers e serviços
|
||||
| Serviço | Imagem | Porta host (DEV) | Healthcheck |
|
||||
|----------|-----------------------|------------------------|-------------|
|
||||
| nginx | build local | 80, 443 | nenhum definido no compose |
|
||||
| backend | build local | 2055/udp (NetFlow) | `GET /api/health` |
|
||||
| postgres | postgres:15-alpine | interno | `pg_isready` |
|
||||
| redis | redis:7-alpine | interno | `redis-cli ping` |
|
||||
| influxdb | influxdb:2.7 | interno | `influx ping` |
|
||||
|
||||
## 6. Banco de dados
|
||||
PostgreSQL 15, banco `mikrotik_manager`, usuário `mikrotik`. Migrations em `backend/src/db/migrate.ts`, executadas automaticamente no boot do backend (idempotente — cria usuário admin default apenas se a tabela `users` estiver vazia).
|
||||
|
||||
## 7. Redis / filas
|
||||
BullMQ para jobs de polling/automação. `maxmemory` configurável via `REDIS_MAXMEMORY` (default 512mb), política `noeviction` (ver comentário no `docker-compose.yml`).
|
||||
|
||||
## 8. InfluxDB / métricas
|
||||
InfluxDB 2.7, org/bucket configuráveis via env (`INFLUXDB_ORG`, `INFLUXDB_BUCKET`), token de admin gerado no setup inicial.
|
||||
|
||||
## 9. Frontend
|
||||
React 19 + Vite + TailwindCSS, servido como estático pelo nginx. Build gera chunk principal >500KB (aviso do Vite, não bloqueante).
|
||||
|
||||
## 10. Backend
|
||||
Node.js/TypeScript, Express, autenticação JWT, TOTP opcional, OIDC opcional, coletor NetFlow/IPFIX na porta 2055/udp, backups, SSH (ssh2) para RouterOS.
|
||||
|
||||
## 11. Autenticação
|
||||
JWT (`JWT_SECRET`, HS256, expiração 24h), senha com bcryptjs, suporte a TOTP e OIDC. Usuário admin default criado na migration inicial (`admin/admin`) — **a senha padrão DEVE ser trocada imediatamente após o primeiro login** (feito nesta instalação DEV; ver seção 12 do CLAUDE.md).
|
||||
|
||||
RBAC: 3 papéis globais (`admin`/`operator`/`viewer`), aplicados corretamente no backend em toda rota (ver `docs/AUDITORIA_BASE_2GT.md`). **Não existe isolamento por site/cliente** — o filtro `X-Site-Id` é apenas de exibição; qualquer usuário autenticado enxerga todos os dispositivos de todos os sites se omitir o header (achado SEC-002). Sessões JWT não têm revogação server-side: trocar senha, desabilitar TOTP ou remover um usuário não invalida tokens já emitidos até expirarem (achado SEC-001).
|
||||
|
||||
## 12. Ambiente DEV
|
||||
- Diretório: `D:\PROJETOS\MIKROTIK RMM\mikrormm` (Windows 11, Git Bash/MINGW64)
|
||||
- Docker: 29.6.2 / Compose v5.3.1
|
||||
- Compose usado: `docker-compose.yml` (build local a partir do código-fonte, não `docker-compose.ghcr.yml`)
|
||||
- Portas publicadas: 80 (HTTP), 443 (HTTPS self-signed), 2055/udp (NetFlow)
|
||||
- `.env` local com secrets DEV gerados aleatoriamente (nunca versionado)
|
||||
- `TZ=America/Bahia`
|
||||
|
||||
## 13. Ambiente PROD planejado
|
||||
Infraestrutura Docker dedicada da GTI. Deve manter paridade com DEV (mesmo `docker-compose.yml` como base, variando apenas `.env`/secrets/domínio/CORS). Não implementado nesta missão.
|
||||
|
||||
## 14. Variáveis de ambiente
|
||||
Somente nomes e finalidade (ver `.env.example` para a lista completa):
|
||||
- `HTTP_PORT`, `HTTPS_PORT` — portas publicadas do nginx
|
||||
- `TZ` — timezone dos containers
|
||||
- `CORS_ORIGIN` — origens permitidas pela API
|
||||
- `DB_PASSWORD` — senha do PostgreSQL
|
||||
- `JWT_SECRET` — segredo de assinatura JWT
|
||||
- `ENCRYPTION_KEY` — chave de criptografia de senhas de dispositivos (32 bytes)
|
||||
- `INFLUXDB_TOKEN`, `INFLUXDB_ORG`, `INFLUXDB_BUCKET`, `INFLUXDB_ADMIN_PASSWORD` — InfluxDB
|
||||
- `POLLER_*` — tuning do poller de dispositivos
|
||||
- `REDIS_MAXMEMORY` — teto de memória do Redis
|
||||
|
||||
## 15. Como iniciar
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 16. Como parar
|
||||
```bash
|
||||
docker compose down
|
||||
```
|
||||
|
||||
## 17. Como rebuildar
|
||||
```bash
|
||||
docker compose build
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 18. Como verificar saúde
|
||||
```bash
|
||||
docker compose ps
|
||||
curl -k https://localhost/api/health
|
||||
```
|
||||
|
||||
## 19. Como executar testes
|
||||
```bash
|
||||
# backend
|
||||
cd backend && npm ci && npm test && npm run lint && npx tsc --noEmit
|
||||
# frontend
|
||||
cd frontend && npm ci && npm test -- --run && npm run lint && npm run build
|
||||
```
|
||||
|
||||
## 20. Persistência e volumes
|
||||
Volumes nomeados: `postgres_data`, `redis_data`, `influxdb_data`, `backups_data`, `certs_data`, `app_data`. Persistência comprovada via `docker compose restart` nesta missão.
|
||||
|
||||
## 21. Backup do próprio MikroRMM
|
||||
Backend expõe rotas de backup de dispositivos (`backups_data`); backup do próprio stack MikroRMM (dump de `postgres_data`/`influxdb_data`) ainda não documentado/procedimentado — pendente de missão futura.
|
||||
|
||||
## 22. Segurança atual
|
||||
- `.env` fora do Git (confirmado via `git check-ignore`)
|
||||
- Secrets DEV gerados aleatoriamente (DB_PASSWORD 32c, JWT_SECRET 64c hex, ENCRYPTION_KEY 32c, INFLUXDB_TOKEN 64c hex, INFLUXDB_ADMIN_PASSWORD 24c)
|
||||
- Senha padrão do admin alterada (ver `.secrets-local/dev-admin-password.txt`, fora do Git)
|
||||
- Certificado HTTPS self-signed gerado pelo nginx (`nginx/entrypoint.sh`) — adequado só para DEV
|
||||
- Lint de segurança (`eslint-plugin-security`) roda no baseline com 270 (backend) + 108 (frontend) warnings, 0 erros — auditados na Missão 01 (`docs/AUDITORIA_BASE_2GT.md`), majoritariamente falso positivo
|
||||
- Credenciais RouterOS cifradas em repouso com AES-256-GCM, chave fora do banco (`SECRETS_DIR/secrets.json`), IV único por operação, tag autenticada — design confirmado correto na auditoria
|
||||
- Conexão RouterOS API-SSL usa `rejectUnauthorized: false` (sem verificação de certificado) — suscetível a MITM na rede local (achado SEC-003, OPEN)
|
||||
- Change Guard (dead-man-switch) só cobre o fluxo de "preflight + apply analisado" em `devices.ts`; a maioria das edições diretas de firewall/rotas/VLAN, comandos em massa e o terminal interativo não passam por ele (achado SEC-005, OPEN)
|
||||
- Ver `docs/AUDITORIA_BASE_2GT.md` para a lista completa de achados, severidades e status
|
||||
|
||||
## 23. Integrações atuais
|
||||
Nenhuma integração externa real conectada (GLPI, Slack, SFTP, RouterBOARD real) — fora do escopo desta missão.
|
||||
|
||||
## 24. Funcionalidades atualmente disponíveis
|
||||
Conforme upstream v0.24.5-beta: gestão de clientes/dispositivos, polling, backups, automação, alertas, change guard, firmware rollout, topologia, NetFlow, OIDC, TOTP. Ver `README.md` do upstream para detalhes completos.
|
||||
|
||||
## 25. Funcionalidades GTI ainda não implementadas
|
||||
Integração GLPI, SFTP, Slack, onboarding customizado, renomeação para "MikroRMM", RouterBOARD real conectada.
|
||||
|
||||
## 26. Procedimento para mudanças com IA
|
||||
Ver `CLAUDE.md` na raiz do projeto.
|
||||
|
||||
## 27. Prompt/missão operacional atual
|
||||
**Missão 00 — Preparar Ambiente Dev Local** (concluída): bootstrap e baseline apenas. Clonar upstream, subir stack local via build de código-fonte, validar saúde/login/testes, documentar, manter Git limpo, zero secrets versionados. Proibido nesta missão: renomear o produto, redesenhar UI, implementar GLPI/SFTP/Slack, alterar autenticação/schema/Change Guard, conectar RouterBOARD real, corrigir achados de auditoria. Próxima missão recomendada: **Auditoria Técnica e de Segurança da Base Upstream**.
|
||||
Reference in New Issue
Block a user