project-suspended.ts41 lines · main
| 1 | import { ForbiddenError } from '@briven/shared'; |
| 2 | import type { MiddlewareHandler } from 'hono'; |
| 3 | |
| 4 | import { getProjectSuspension } from '../services/abuse.js'; |
| 5 | |
| 6 | /** |
| 7 | * Gate state-changing project routes behind the suspension flag. Chain |
| 8 | * this AFTER `requireProjectAuth` so the project id is already resolved |
| 9 | * from the path param. Suspended projects 403 with code=project_suspended. |
| 10 | * |
| 11 | * Reads (GET/HEAD) are intentionally not gated — the operator + owner |
| 12 | * still need to inspect the project's state via the dashboard while a |
| 13 | * suspension is being investigated. The middleware short-circuits on |
| 14 | * GET/HEAD so it can be mounted globally at `/v1/projects/:id/*` without |
| 15 | * locking the dashboard out. |
| 16 | * |
| 17 | * Cache: not added in this version. At Phase 3 scale (~25 projects) |
| 18 | * the extra round-trip is in the single-digit ms and avoids a stale- |
| 19 | * cache window between suspend + the next invoke. Revisit when traffic |
| 20 | * justifies it. |
| 21 | */ |
| 22 | export const blockIfProjectSuspended = (): MiddlewareHandler => async (c, next) => { |
| 23 | const method = c.req.method; |
| 24 | if (method === 'GET' || method === 'HEAD' || method === 'OPTIONS') { |
| 25 | await next(); |
| 26 | return; |
| 27 | } |
| 28 | const projectId = c.req.param('id') ?? c.req.param('projectId'); |
| 29 | if (!projectId) { |
| 30 | await next(); |
| 31 | return; |
| 32 | } |
| 33 | const suspension = await getProjectSuspension(projectId); |
| 34 | if (suspension) { |
| 35 | throw new ForbiddenError( |
| 36 | 'project is suspended — contact support to appeal', |
| 37 | 'project_suspended', |
| 38 | ); |
| 39 | } |
| 40 | await next(); |
| 41 | }; |