Skip to content

hardening: security headers/CSP, rate limiting, audit logging, service-role audit - #13

Merged
snackman merged 1 commit into
mainfrom
hardening
Jun 6, 2026
Merged

hardening: security headers/CSP, rate limiting, audit logging, service-role audit#13
snackman merged 1 commit into
mainfrom
hardening

Conversation

@snackman

@snackman snackman commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Production security hardening (Phase 7)

Defense-in-depth on the multi-tenant SaaS POS, layered on top of the existing
RLS + session-gated auth without weakening any auth/money/RLS correctness.
No new npm dependency; package.json untouched. Build + Vitest stay green
with zero env vars.

1. Security headers + CSP

Applied to every route via next.config.ts headers() (src/lib/security/headers.ts):

  • CSP: default-src 'self', object-src 'none', base-uri 'self', form-action 'self', frame-ancestors 'none', upgrade-insecure-requests; enumerated script/style/img/font/connect/frame/worker/manifest-src (Supabase REST+WSS, Stripe API/JS, Base RPC, PWA blob:).
  • HSTS (2y, preload, subdomains), X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: strict-origin-when-cross-origin, Permissions-Policy (camera/mic/geo/usb denied, payment=(self)), COOP: same-origin.
  • No-nonce trade-off documented (middleware matches protected surfaces only, so a per-request nonce can't be applied uniformly): script-src allows 'unsafe-inline', but the app ships no author inline scripts / dangerouslySetInnerHTML, and every other vector is locked down. Upgrade path documented in headers.ts + PRODUCTION_READINESS §11a.

2. Rate limiting

Zero-dep in-memory fixed-window limiter (rate-limit.ts) + route guard (http.ts) returning 429 + Retry-After + RateLimit-*. No-op under tests / RATE_LIMIT_DISABLED so CI + dev never throttle.

Endpoint Limit / 60s Keys
POST /api/terminal/pin 8 IP
POST /api/shop/auth/magic-link 10 IP + email
POST /api/orders 60 IP
POST /api/payments · /api/payments/refund 30 IP

Tenant/platform login calls Supabase Auth directly from the browser (Supabase enforces its own auth limits) — noted in §11b.

3. Audit logging (broadened)

Tenant-scoped, fail-open (audit.ts) coverage beyond impersonation: auth_sign_in (per membership, in /auth/callback), staff_pin_switch, payment_refund/payment_void, menu_86, connect_change, subscription_change, tenant_go_live.

4. Service-role audit

Inventory (PRODUCTION_READINESS §11d): the service-role key is read only by the driver (src/lib/db/supabase.ts); every tenant-scoped query carries an explicit tenant_id/location_id filter (no un-scoped query found). The auth flows correctly use the user-scoped (RLS, anon-key + session) client instead.
Authz fix: POST /api/payments/refund previously had no auth (any caller could refund any payment) — now resolves the payment's tenant and requires membership; POST /api/connect now requires owner|manager.

5. Input validation

readJsonBody 256 KB cap (413), non-negative integer money guards, bounded id/email guards on the money + auth routes (validate.ts).

Tests / checks

New tests: rate limiter, HTTP guard/no-op, validators, CSP/headers, audit (incl. fail-open). 150 tests pass; typecheck + lint + build green with zero env.

Threat model: plans/arrabbiata-71129-phase-7-security-hardening.md.

🤖 Generated with Claude Code

…e-role audit

Production security hardening pass (Phase 7), layered on top of the existing
RLS + session-gated auth without weakening any auth/money/RLS correctness.
Zero new npm dependency; build + Vitest stay green with zero env vars.

- Security headers + CSP (src/lib/security/headers.ts, wired via next.config.ts
  headers()): strict static CSP (default-src 'self', object-src 'none',
  frame-ancestors 'none', enumerated connect/frame/script-src for Supabase +
  Stripe + Base RPC, upgrade-insecure-requests) + HSTS, X-Content-Type-Options,
  X-Frame-Options, Referrer-Policy, Permissions-Policy, COOP. No-nonce trade-off
  documented (middleware matches protected surfaces only).
- Rate limiting (src/lib/security/{rate-limit,http}.ts): zero-dep in-memory
  fixed-window limiter + route guard returning 429 + Retry-After. Applied to
  PIN, customer magic-link (per IP+email), order create, payment, refund.
  No-op under tests / RATE_LIMIT_DISABLED so CI + dev never throttle.
- Audit logging (src/lib/security/audit.ts + expanded AuditAction): tenant-
  scoped, fail-open coverage for staff/admin sign-in, PIN switch, refund/void,
  menu 86, Connect change, subscription change, tenant go-live.
- Service-role audit (PRODUCTION_READINESS §11d): inventory confirms the
  service-role key is used only by the driver, every tenant query carries an
  explicit tenant_id/location_id filter. Fixed an authz gap: the refund
  endpoint had NO auth (any caller could refund any payment) — now requires
  tenant membership; Connect start now requires owner|manager.
- Input validation (src/lib/security/validate.ts): 256 KB body cap (413),
  non-negative integer money guards, bounded id/email guards on the money +
  auth routes.
- Tests: rate limiter, HTTP guard/no-op, validators, CSP/headers, audit
  (incl. fail-open). 150 tests pass; typecheck + lint + build green, zero env.
- Threat model: plans/arrabbiata-71129-phase-7-security-hardening.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pos Ready Ready Preview, Comment Jun 6, 2026 6:58am

Request Review

@snackman
snackman marked this pull request as ready for review June 6, 2026 07:00
@snackman
snackman merged commit 7a6d5c1 into main Jun 6, 2026
5 checks passed
@snackman
snackman deleted the hardening branch June 6, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant