Security hardening: deps, tenancy quotas, auth, deploy, webhooks
Addresses the findings from the platform security audit. Verified green: all-workspace typecheck, web build, 16 API unit tests, 23 e2e auth tests, and 0 high/critical production dependency vulnerabilities. Dependencies (High): - Bump drizzle-orm 0.36→0.45.2 (GHSA-gpj5-g38j-94v9 SQLi-via-identifier) and drizzle-kit→0.31.10; npm audit fix cleared fast-uri path-traversal and the react-router open-redirect. Remaining audit items are dev-only build tooling (esbuild/vite), not shipped at runtime. AI cost control + storage quota (new ai_usage table, migration 0002): - Per-firm monthly AI token budget enforced before each completion (429), with every completion recorded to an ai_usage ledger (lib/ai-usage.ts). - Enforce per-plan storage quota on upload (402) and maintain storage_bytes_used on upload/delete (lib/storage-quota.ts); widen the column int→bigint so 8GB/50GB plans don't overflow. Auth (defense-in-depth): - Constant-time login: verify against a dummy argon2 hash when the account doesn't exist, closing the timing/enumeration oracle (verifyPasswordSafe). - Enforce suspension on requireSuperadmin, /auth/me, /auth/resend-verification. Web: - Validate the post-login ?next= redirect to same-origin paths only (open-redirect / phishing). Deploy hardening: - docker-compose: memory/CPU limits so a spike can't OOM the Dokploy host. - .dockerignore: keep destructive one-off scripts (seed-demo, create-admin, migrate-storage) out of the runtime image; retain the cron scripts. - seed-demo.ts: hard-refuse NODE_ENV=production and the prod DB host. Webhooks / config: - Stripe idempotency via a stripe_events ledger (skip already-processed events; record only after successful processing so a transient failure still retries); make the plan-upgraded email non-blocking. - Rate-limit account export and invoice PDF; cap invoice item arrays at 200. - Require TURNSTILE_SECRET_KEY in production (bot protection no longer fails open on a forgotten key); don't load .env under NODE_ENV=test so the suite is hermetic. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
d1d96e4dd2
commit
304f7f30c3
@@ -1,4 +1,5 @@
|
||||
import argon2 from 'argon2';
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
const ARGON2_OPTIONS: argon2.Options = {
|
||||
type: argon2.argon2id,
|
||||
@@ -14,3 +15,24 @@ export function hashPassword(password: string): Promise<string> {
|
||||
export function verifyPassword(hash: string, password: string): Promise<boolean> {
|
||||
return argon2.verify(hash, password);
|
||||
}
|
||||
|
||||
// Precomputed dummy argon2id hash for constant-time login. When an account doesn't exist we
|
||||
// still run a full verify against this hash so the nonexistent-account path costs the same as a
|
||||
// real (failing) password check — closing the timing/enumeration oracle. Computed once at module
|
||||
// load from a random throwaway secret; the promise is cached so the hash cost is paid a single time.
|
||||
const dummyHashPromise: Promise<string> = hashPassword(crypto.randomBytes(32).toString('hex'));
|
||||
|
||||
// Verifies `password` against `hash` when present, otherwise against the dummy hash so the
|
||||
// account-exists and account-missing paths do equal argon2 work. Always resolves to a boolean and
|
||||
// never throws (a null/undefined or malformed hash simply resolves to false).
|
||||
export async function verifyPasswordSafe(
|
||||
hash: string | null | undefined,
|
||||
password: string,
|
||||
): Promise<boolean> {
|
||||
const target = hash ?? (await dummyHashPromise);
|
||||
try {
|
||||
return await argon2.verify(target, password);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ async function plugin(app: FastifyInstance) {
|
||||
|
||||
app.decorate('requireSuperadmin', async (req: FastifyRequest, reply: FastifyReply) => {
|
||||
if (!req.user) return reply.code(401).send({ error: 'unauthorized' });
|
||||
if (req.user.isSuspended) return reply.code(403).send({ error: 'account_suspended' });
|
||||
if (!req.user.isSuperadmin) return reply.code(403).send({ error: 'forbidden' });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user