fix(recovery): restore source removed by production documentation merge

This commit is contained in:
2026-11-10 03:42:10 +01:00
parent 585bdd4b44
commit 3d2fc6f913
957 changed files with 115754 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
/**
* src/http/siax-error-response.ts
* =================================
* Maps a thrown error to a NextResponse for the source-registry routes
* (app/api/0pt/sources/*). SourceRegistryService throws real `SiaxError`
* instances (src/platform/errors.ts) with a pinned `.httpStatus`/`.code` —
* this honors that mapping directly instead of re-deriving status codes from
* regex-matching `error.message`, the ad-hoc pattern most other 0pt routes
* use (app/api/0pt/documents/[documentId]/extraction/route.ts etc.) because
* their services still throw plain `Error`. SourceService/SourceRegistryService's
* lower-level plain Errors ("already exists", "not found") still fall back
* to that same regex convention here, for consistency with the rest of the
* repo's error surface.
*/
import { NextResponse } from "next/server";
import { SiaxError } from "../platform/errors";
export function siaxErrorResponse(error: unknown): NextResponse {
if (error instanceof SiaxError) {
return NextResponse.json(error.toJSON(), { status: error.httpStatus });
}
const message = error instanceof Error ? error.message : "Unknown error";
const status = /already exists/i.test(message) ? 409 : /not found/i.test(message) ? 404 : 400;
return NextResponse.json({ error: message }, { status });
}
+78
View File
@@ -0,0 +1,78 @@
import { NextResponse, type NextRequest } from "next/server";
import { readServiceTenantScope } from "@/src/auth/internal-service-tenant";
export const VERIFIED_TENANT_HEADER = "x-0pt-verified-tenant";
export function assertTenantMatchesVerified(
request: NextRequest,
tenantId: string | null | undefined,
): NextResponse | null {
const verified = request.headers.get(VERIFIED_TENANT_HEADER);
// Internal service principal: tenant authority is the SERVER-CONFIGURED
// allowlist middleware installed (never a client header). The scope header is
// present even when the allowlist is empty, which means "no tenant
// authority" -> fail closed. Routes that do not take a tenant never call
// this guard, so evidence dispatch / ER / search definitions keep working.
if (request.headers.get("x-0pt-verified-service-tenants") !== null) {
if (!tenantId) return null; // handlers already 400 on a missing tenant_id separately
const scope = readServiceTenantScope(request.headers);
if (!scope.includes(tenantId)) {
return NextResponse.json(
{
error: "OPT_ACCESS_DENIED",
message: "tenant_id is outside the internal service principal's configured tenant scope",
details: { requested_tenant: tenantId, service_tenant_scope: scope },
},
{ status: 403 },
);
}
return null;
}
if (!verified) return null;
if (!tenantId) return null;
if (tenantId !== verified) {
return NextResponse.json(
{
error: "OPT_ACCESS_DENIED",
message: "tenant_id disagrees with the authenticated token's tenant",
details: { requested_tenant: tenantId, verified_tenant: verified },
},
{ status: 403 },
);
}
return null;
}
export function resolveDocumentActor(
request: NextRequest,
requestedActor: string | undefined,
): { actorId?: string; response?: NextResponse } {
const verified = request.headers.get('x-0pt-verified-sub');
if (verified && requestedActor && requestedActor !== verified) {
return {
response: NextResponse.json(
{ error: 'OPT_ACCESS_DENIED', message: 'actor_id disagrees with the authenticated token subject' },
{ status: 403 },
),
};
}
return { actorId: verified || requestedActor };
}
export function assertWorkloadCapabilities(
request: NextRequest,
required: readonly string[],
): NextResponse | null {
if (!required.length) return null;
const raw = request.headers.get('x-0pt-verified-capabilities');
if (raw === null) return null; // not a workload-capability-scoped principal
const caps = raw ? raw.split(',').map((s) => s.trim()).filter(Boolean) : [];
const missing = required.filter((r) => !caps.includes(r) && !caps.includes('*'));
if (missing.length) {
return NextResponse.json(
{ error: 'OPT_ACCESS_DENIED', message: 'missing required workload capability', details: { missing } },
{ status: 403 },
);
}
return null;
}
+57
View File
@@ -0,0 +1,57 @@
/**
* Verified request scope for customer-facing 0pt routes.
*
* Middleware (`middleware.ts`) performs real Zitadel Bearer verification and
* forwards the verified subject/tenant only when a principal was verified.
* Customer routes must NOT fall back to client-supplied tenant/actor values:
* that fallback exists only for the explicit local-development mode, and even
* there this helper requires an explicit dev tenant header.
*/
import { NextResponse, type NextRequest } from 'next/server';
import { VERIFIED_TENANT_HEADER } from './tenant-guard';
export const VERIFIED_SUBJECT_HEADER = 'x-0pt-verified-sub';
/** Dev-only header (AUTH_ENFORCE=false) so local flows still have a scope. */
export const DEV_TENANT_HEADER = 'x-0pt-dev-tenant';
export const DEV_SUBJECT_HEADER = 'x-0pt-dev-subject';
const idPattern = /^[\x21-\x7e]{1,256}$/;
export interface VerifiedScope {
tenantId: string;
actorId: string;
}
function devMode(): boolean {
return process.env.AUTH_ENFORCE === 'false' && process.env.NODE_ENV !== 'production';
}
export function resolveVerifiedScope(
request: NextRequest,
): { scope: VerifiedScope } | { response: NextResponse } {
const tenantId = request.headers.get(VERIFIED_TENANT_HEADER);
const actorId = request.headers.get(VERIFIED_SUBJECT_HEADER);
if (tenantId && actorId && idPattern.test(tenantId) && idPattern.test(actorId)) {
return { scope: { tenantId, actorId } };
}
if (devMode()) {
const devTenant = request.headers.get(DEV_TENANT_HEADER);
const devActor = request.headers.get(DEV_SUBJECT_HEADER);
if (devTenant && devActor && idPattern.test(devTenant) && idPattern.test(devActor)) {
return { scope: { tenantId: devTenant, actorId: devActor } };
}
}
return {
response: NextResponse.json(
{
error: 'OPT_UNAUTHENTICATED',
message: 'A verified tenant and subject are required',
},
{ status: 401 },
),
};
}
export function isValidScopeId(value: unknown): value is string {
return typeof value === 'string' && idPattern.test(value);
}