fix(security): require auth on GET /api/orders and GET /api/admin/metrics
CI (SIAX Cloud) / security (push) Successful in 22s
Security Scan (SIAX Cloud) / security (push) Successful in 20s
CI (SIAX Cloud) / quality (push) Successful in 39s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / quality (pull_request) Successful in 40s

A live compliance audit against 0pt.app confirmed both endpoints returned
HTTP 200 with zero authentication -- the full order queue and financial
pipeline aggregates (deposit/pipeline totals) were readable by anyone.

Root cause: middleware.ts's matcher only covers /api/0pt/:path*, by design
("does NOT touch ... the unrelated payment/order APIs"), so nothing upstream
of these two handlers ever verified the caller.

Fix: both GET handlers now call the same fail-closed Zitadel verifier
/api/0pt/* relies on (resolveAuthContext, src/auth/context.ts) directly, via
a new thin wrapper (src/auth/require-route-auth.ts) that maps a rejected
decision to the same 401/403 JSON shape middleware.ts already returns. The
wrapper re-verifies the bearer token itself rather than trusting any
x-0pt-verified-* header on the request, since middleware never runs in front
of these routes to overwrite a client-supplied one.

POST /api/orders (anonymous draft/quote creation before the deposit-payment
step) is intentionally left as-is -- only the read paths that leaked data are
gated. Tracked as part of docs/compliance/BACKLOG.md P0-3.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-11 04:06:13 +02:00
co-authored by Claude Sonnet 5
parent 5f41047546
commit 15a0dbe3b1
4 changed files with 155 additions and 5 deletions
+50
View File
@@ -0,0 +1,50 @@
/**
* `0pt/auth/require-route-auth.ts` -- thin Next.js wrapper around
* `resolveAuthContext` (src/auth/context.ts) for API route handlers that sit
* OUTSIDE `middleware.ts`'s matcher (`/api/0pt/:path*` only -- see the
* comment at the top of middleware.ts: "does NOT touch ... the unrelated
* payment/order APIs").
*
* `/api/orders` (GET) and `/api/admin/*` are such routes: nothing upstream of
* the handler verifies the caller today, so the handler must call
* `resolveAuthContext` itself. Critically, it must NOT trust any
* `x-0pt-verified-*` header found on the incoming request as a shortcut --
* those headers are only trustworthy on routes middleware.ts actually runs in
* front of, where it overwrites them with its own verified values. A client
* hitting one of these unmatched routes directly controls every request
* header, including `x-0pt-verified-tenant`, so trusting it here would be a
* trivial bypass. This wrapper always re-verifies via `resolveAuthContext`
* (real Zitadel JWT verification, fail-closed by default -- see context.ts),
* exactly as middleware.ts does for `/api/0pt/*`.
*
* Mirrors middleware.ts's decision -> HTTP response mapping exactly, so a
* caller sees the identical 401/403 JSON shape whether the check ran in
* middleware or here.
*/
import { NextResponse, type NextRequest } from 'next/server';
import { resolveAuthContext, type OptRequestContext } from './context';
export type RouteAuthResult = { response: NextResponse } | { response?: undefined; ctx: OptRequestContext };
/**
* Enforce authentication for a route handler not covered by middleware.ts's
* matcher. Returns `{ response }` (send immediately) when unauthenticated or
* unauthorized, or `{ ctx }` (the verified request context) when the caller
* may proceed.
*/
export async function requireRouteAuth(req: NextRequest): Promise<RouteAuthResult> {
const decision = await resolveAuthContext(req.headers);
if (!decision.ok) {
return {
response: NextResponse.json(
{
error: decision.code,
message: decision.message,
...(decision.details ? { details: decision.details } : {}),
},
{ status: decision.status },
),
};
}
return { ctx: decision.ctx };
}