Conversation
…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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.jsonuntouched. Build + Vitest stay greenwith zero env vars.
1. Security headers + CSP
Applied to every route via
next.config.tsheaders()(src/lib/security/headers.ts):default-src 'self',object-src 'none',base-uri 'self',form-action 'self',frame-ancestors 'none',upgrade-insecure-requests; enumeratedscript/style/img/font/connect/frame/worker/manifest-src(Supabase REST+WSS, Stripe API/JS, Base RPC, PWAblob:).payment=(self)), COOP: same-origin.script-srcallows'unsafe-inline', but the app ships no author inline scripts /dangerouslySetInnerHTML, and every other vector is locked down. Upgrade path documented inheaders.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_DISABLEDso CI + dev never throttle.POST /api/terminal/pinPOST /api/shop/auth/magic-linkPOST /api/ordersPOST /api/payments·/api/payments/refundTenant/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 explicittenant_id/location_idfilter (no un-scoped query found). The auth flows correctly use the user-scoped (RLS, anon-key + session) client instead.Authz fix:
POST /api/payments/refundpreviously had no auth (any caller could refund any payment) — now resolves the payment's tenant and requires membership;POST /api/connectnow requiresowner|manager.5. Input validation
readJsonBody256 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+buildgreen with zero env.Threat model:
plans/arrabbiata-71129-phase-7-security-hardening.md.🤖 Generated with Claude Code