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
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE IF NOT EXISTS "ai_usage" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"firm_id" uuid NOT NULL,
|
||||
"user_id" uuid,
|
||||
"feature" text NOT NULL,
|
||||
"model" text NOT NULL,
|
||||
"input_tokens" integer DEFAULT 0 NOT NULL,
|
||||
"output_tokens" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "stripe_events" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"type" text NOT NULL,
|
||||
"processed_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "firms" ALTER COLUMN "storage_bytes_used" SET DATA TYPE bigint;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "ai_usage" ADD CONSTRAINT "ai_usage_firm_id_firms_id_fk" FOREIGN KEY ("firm_id") REFERENCES "public"."firms"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "ai_usage_firm_idx" ON "ai_usage" USING btree ("firm_id","created_at");
|
||||
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,13 @@
|
||||
"when": 1777169307877,
|
||||
"tag": "0001_orange_jamie_braddock",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "7",
|
||||
"when": 1784308478719,
|
||||
"tag": "0002_daily_chronomancer",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -18,12 +18,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"drizzle-orm": "^0.36.4",
|
||||
"drizzle-orm": "^0.45.2",
|
||||
"pg": "^8.13.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/pg": "^8.11.10",
|
||||
"drizzle-kit": "^0.28.1",
|
||||
"drizzle-kit": "^0.31.10",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.6.3"
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { pgTable, uuid, text, timestamp, boolean, integer } from 'drizzle-orm/pg-core';
|
||||
import { pgTable, uuid, text, timestamp, boolean, bigint } from 'drizzle-orm/pg-core';
|
||||
|
||||
export const firms = pgTable('firms', {
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
@@ -7,7 +7,8 @@ export const firms = pgTable('firms', {
|
||||
.notNull()
|
||||
.default('starter'),
|
||||
watermarkEnabled: boolean('watermark_enabled').notNull().default(true),
|
||||
storageBytesUsed: integer('storage_bytes_used').notNull().default(0),
|
||||
// bigint (not integer): plan quotas reach 50 GB, well past the ~2.1 GB int4 ceiling.
|
||||
storageBytesUsed: bigint('storage_bytes_used', { mode: 'number' }).notNull().default(0),
|
||||
stripeCustomerId: text('stripe_customer_id'),
|
||||
stripeSubscriptionId: text('stripe_subscription_id'),
|
||||
trialEndsAt: timestamp('trial_ends_at', { withTimezone: true }),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { pgTable, uuid, text, timestamp, inet, index } from 'drizzle-orm/pg-core';
|
||||
import { pgTable, uuid, text, timestamp, inet, integer, index } from 'drizzle-orm/pg-core';
|
||||
import { firms } from './firms';
|
||||
|
||||
export const contactMessages = pgTable('contact_messages', {
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
@@ -23,3 +24,32 @@ export const toolUsage = pgTable(
|
||||
toolIdx: index('tool_usage_tool_idx').on(t.tool, t.createdAt),
|
||||
}),
|
||||
);
|
||||
|
||||
// Per-firm AI usage ledger — enables monthly token quotas and cost accounting.
|
||||
// One row per successful AI completion; aggregated over the current month for quota checks.
|
||||
export const aiUsage = pgTable(
|
||||
'ai_usage',
|
||||
{
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
firmId: uuid('firm_id')
|
||||
.notNull()
|
||||
.references(() => firms.id, { onDelete: 'cascade' }),
|
||||
userId: uuid('user_id'),
|
||||
feature: text('feature').notNull(), // 'case_summary' | 'document_summary' | 'polish'
|
||||
model: text('model').notNull(),
|
||||
inputTokens: integer('input_tokens').notNull().default(0),
|
||||
outputTokens: integer('output_tokens').notNull().default(0),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
},
|
||||
(t) => ({
|
||||
firmIdx: index('ai_usage_firm_idx').on(t.firmId, t.createdAt),
|
||||
}),
|
||||
);
|
||||
|
||||
// Stripe webhook idempotency ledger. The event id is the PK; an INSERT that hits the
|
||||
// unique constraint means we've already processed this event and can skip re-running side effects.
|
||||
export const stripeEvents = pgTable('stripe_events', {
|
||||
id: text('id').primaryKey(), // Stripe event.id
|
||||
type: text('type').notNull(),
|
||||
processedAt: timestamp('processed_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user