fix(security): self-healing secrets + auth/SSRF/header hardening (v0.16.10-beta)
Follow-up to the /terminal authorization fix: a full-codebase security review surfaced several more issues, fixed here. All changes are designed to roll out with no interruption to existing deployments — no forced migration, no manual re-key, no boot failure. Self-healing secret management (utils/secrets.ts): - JWT_SECRET / ENCRYPTION_KEY auto-generate to strong values on first boot and persist to the app_data volume (0600, outside the DB) when not set to a strong value in the environment. Replaces the prior behavior of running on the public repo defaults. - decrypt() tries the current key plus every legacy/default key, so existing device credentials keep decrypting after a key change; a startup sweep re-encrypts stale rows forward (db/reencryptCredentials.ts). - verifyToken() accepts the current signing secret plus prior *strong* secrets (never the public defaults), so rotation doesn't invalidate live sessions; sessions signed with a default secret require one re-login. - Dockerfile pre-creates /app/data as the app user so the non-root process can persist to a fresh named volume; app_data volume added to both compose files; compose secret defaults changed to empty (auto-gen). Authorization / input: - Default Socket.IO namespace now requires a valid JWT (was unauthenticated and broadcasting fleet-activity events); frontend sends the token and drops the shared socket on logout. - Webhook delivery blocks SSRF to loopback/private/link-local/CGNAT ranges and pins the request to the validated IP (WebhookService.ts). - Rate limit added to /auth/totp/verify; global mutation backstop on /api. - Password policy (>=10 chars, letter+digit) enforced on set/change/create. - system.ts used verifyToken as middleware (always 500); now requireAuth. Hardening: - CSP + HSTS/nosniff/frame-deny headers in nginx, scoped to allow the OpenStreetMap tile/geocoding hosts the maps use. Docs/tests: - README secret section rewritten for the self-healing model. - New unit tests for secret resolution, persistence, rotation, and legacy-key decryption (97 tests pass). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
27560fa1d6
commit
9e962939ea
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { createHash } from 'crypto';
|
||||
import { query } from '../config/database';
|
||||
import { jwtSigningSecret, jwtVerifierSecrets } from '../utils/secrets';
|
||||
|
||||
export interface AuthPayload {
|
||||
userId: number;
|
||||
@@ -20,14 +21,42 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'changeme';
|
||||
|
||||
export function signToken(payload: AuthPayload): string {
|
||||
return jwt.sign(payload, JWT_SECRET, { expiresIn: '24h' });
|
||||
return jwt.sign(payload, jwtSigningSecret(), { expiresIn: '24h' });
|
||||
}
|
||||
|
||||
export function verifyToken(token: string): AuthPayload {
|
||||
return jwt.verify(token, JWT_SECRET) as AuthPayload;
|
||||
// Accept the current signing secret plus any prior strong secret, so a secret
|
||||
// rotation doesn't invalidate sessions already issued under the previous one.
|
||||
const secrets = jwtVerifierSecrets();
|
||||
let lastErr: unknown;
|
||||
for (const secret of secrets) {
|
||||
try {
|
||||
return jwt.verify(token, secret) as AuthPayload;
|
||||
} catch (e) {
|
||||
lastErr = e;
|
||||
}
|
||||
}
|
||||
throw lastErr instanceof Error ? lastErr : new Error('invalid token');
|
||||
}
|
||||
|
||||
/** Sign a short-lived token (e.g. the partial 2FA token) with the current secret. */
|
||||
export function signRawToken(payload: object, options: jwt.SignOptions): string {
|
||||
return jwt.sign(payload, jwtSigningSecret(), options);
|
||||
}
|
||||
|
||||
/** Verify a token that was signed with signRawToken, honoring rotated secrets. */
|
||||
export function verifyRawToken<T>(token: string): T {
|
||||
const secrets = jwtVerifierSecrets();
|
||||
let lastErr: unknown;
|
||||
for (const secret of secrets) {
|
||||
try {
|
||||
return jwt.verify(token, secret) as T;
|
||||
} catch (e) {
|
||||
lastErr = e;
|
||||
}
|
||||
}
|
||||
throw lastErr instanceof Error ? lastErr : new Error('invalid token');
|
||||
}
|
||||
|
||||
// API tokens ("mtm_…") are hashed at rest; scope maps onto the existing role
|
||||
|
||||
Reference in New Issue
Block a user