Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c134527509 | ||
|
|
1c87b82e70 |
+204
@@ -0,0 +1,204 @@
|
||||
# =============================================================================
|
||||
# 0pt (edge plane) -- environment inventory / example environment
|
||||
# =============================================================================
|
||||
# Copy to `.env` (gitignored) and fill in real values. Never commit real values.
|
||||
# Vercel env for the edge app; VPS .env (git-ignored) for infra/docker-compose.yml.
|
||||
# See docs/08-infrastructure.md for the canonical description of each integration.
|
||||
#
|
||||
# Two provenance markers are used below and they mean different things:
|
||||
# * "wired in code today" -- read via a `process.env.*` somewhere in
|
||||
# this repo right now (grep-verified).
|
||||
# * "documented target, not yet read" -- named in docs/08-infrastructure.md /
|
||||
# docs/02-architecture.md but with no
|
||||
# `process.env.*` read yet. Filling it in
|
||||
# unblocks the integration; it does not by
|
||||
# itself change behaviour.
|
||||
#
|
||||
# The app is designed to build and boot with all of these UNSET: lib/db.ts and
|
||||
# lib/stripe.ts are lazy + env-guarded, and the storage/payment/doc routes return
|
||||
# HTTP 501 ("not configured") until the relevant keys are provided.
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Core / public
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public base URL of this app (used for absolute links / callbacks).
|
||||
NEXT_PUBLIC_APP_URL=http://localhost:3000
|
||||
# Documented target (docs/08); no call site found reading it in this pass.
|
||||
# NOTE: api.0pt.app is a dead hostname (falls through to wildcard Vercel ->
|
||||
# DEPLOYMENT_NOT_FOUND). The app serves its own /api routes on the site
|
||||
# origin, so the honest base URL is the live origin itself.
|
||||
NEXT_PUBLIC_API_URL=https://0pt.app
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Database (Postgres) -- wired in code today: lib/db.ts, app/api/health
|
||||
# ---------------------------------------------------------------------------
|
||||
# Vercel Postgres sets POSTGRES_URL / POSTGRES_PRISMA_URL automatically when the
|
||||
# integration is attached. First one set wins, in this order (lib/db.ts):
|
||||
# DATABASE_URL -> POSTGRES_URL -> POSTGRES_PRISMA_URL.
|
||||
DATABASE_URL=postgres://user:password@localhost:5432/opt
|
||||
POSTGRES_URL=
|
||||
POSTGRES_PRISMA_URL=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Cache / queue (Redis) -- app/api/health only checks presence; no other call
|
||||
# site found reading it in this pass (worker queue usage, if any, lives in
|
||||
# services/api, outside this repo's scope).
|
||||
# ---------------------------------------------------------------------------
|
||||
REDIS_URL=redis://localhost:6379
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Auth -- session / dev-login / service-token secrets (wired in code today)
|
||||
# ---------------------------------------------------------------------------
|
||||
# Session-signing secret (generate a long random string).
|
||||
AUTH_SECRET=change-me-long-random-string
|
||||
# AUTH_URL is documented (docs/08) but not yet read by any route in this pass.
|
||||
AUTH_URL=https://0pt.app
|
||||
# Optional dev-only login shortcut key (leave UNSET in production).
|
||||
DEV_LOGIN_KEY=
|
||||
# Shared token for internal/service-to-service calls; sent as X-Internal-Token
|
||||
# on every engineFetch() call (lib/engine.ts).
|
||||
INTERNAL_TOKEN=change-me-internal-token
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Zitadel / OIDC Bearer verification (Wave 2 hardening) -- wired in code today
|
||||
# ---------------------------------------------------------------------------
|
||||
# 0pt verifies inbound Bearer tokens against the fleet Zitadel IdP
|
||||
# (src/auth/zitadel.ts, wired via middleware.ts). JWKS are discovered from
|
||||
# <issuer>/.well-known/openid-configuration, falling back to <issuer>/oauth/v2/keys.
|
||||
#
|
||||
# Fleet Zitadel issuer (OIDC). Default when unset: https://id-customers.siax.io
|
||||
ZITADEL_ISSUER=https://id-customers.siax.io
|
||||
# Enforcement is FAIL-CLOSED. Leave this UNSET (or set it to "true") and a
|
||||
# valid, verified Bearer token is REQUIRED on /api/0pt/* -- 401 on
|
||||
# missing/invalid, 403 when X-Tenant-Id disagrees with the token tenant.
|
||||
# ONLY the literal value "false" disables authentication, and that value is
|
||||
# REFUSED when NODE_ENV=production: the app throws at startup instead of
|
||||
# serving openly. Use "false" for local development only.
|
||||
AUTH_ENFORCE=true
|
||||
# Optional override for the claim key that carries the SIAX tenant id. When
|
||||
# unset, tenant derivation uses the documented precedence:
|
||||
# urn:zitadel:iam:org:id -> urn:zitadel:iam:user:resourceowner:id -> tenant_id -> tenant.
|
||||
ZITADEL_TENANT_CLAIM=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data engine bridge -- wired in code today: lib/engine.ts
|
||||
# ---------------------------------------------------------------------------
|
||||
# When set, edge route handlers defer to the FastAPI engine (services/api) as the
|
||||
# single source of truth (orders/estimates/catalog/ai-parse). When unset, the edge
|
||||
# computes locally / falls back to an honest heuristic -- see lib/engine.ts and
|
||||
# app/api/ai/parse.
|
||||
ENGINE_URL=http://localhost:8000
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SIAX platform clients
|
||||
# ---------------------------------------------------------------------------
|
||||
# WIRED FOR REAL since 2026-08-31: every factory in src/clients/* reads its
|
||||
# *_URL from process.env when no constructor baseUrl is given. Under
|
||||
# NODE_ENV=production an UNSET url is a hard boot failure
|
||||
# (SiaxError DEPENDENCY_UNAVAILABLE) — production never silently falls back
|
||||
# to the in-memory mocks (Master-DoD §3/§10). In dev/test, unset = documented
|
||||
# local mock/no-op behaviour.
|
||||
CL0UD_URL=
|
||||
N0D_URL=
|
||||
AUD0_URL=
|
||||
# Optional service bearer tokens for the platform clients (machine identity).
|
||||
# Values come from Infisical/secret refs — NEVER commit real values here.
|
||||
CL0UD_SERVICE_TOKEN=
|
||||
N0D_SERVICE_TOKEN=
|
||||
AUD0_SERVICE_TOKEN=
|
||||
ST0RE_SERVICE_TOKEN=
|
||||
# INF0 (AI OS) -- documented target, not yet read by any code in this repo.
|
||||
# Route AI-parsning / model calls through the gateway instead of raw provider keys.
|
||||
INF0_URL=
|
||||
# ST0RE_URL is the convention name for src/clients/st0re-client.ts /
|
||||
# st0re-lakehouse-client.ts and is read by the real-infra test suite
|
||||
# (tests/p0/*.realinfra.test.ts). The lakehouse write path was live-verified
|
||||
# against ST0RE on 2026-07-13 with this set; unset, the clients fall back to
|
||||
# their deterministic in-memory mocks.
|
||||
ST0RE_URL=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# P0 Data-OS backends (src/search, src/analytics, src/metadata, src/features)
|
||||
# ---------------------------------------------------------------------------
|
||||
# OpenSearch — search projections ONLY, never canonical truth (§42). Unset in
|
||||
# dev/test = search capability unavailable (typed error), never a fake.
|
||||
OPENSEARCH_URL=
|
||||
# ClickHouse — 0pt capability analytics (tenant-isolated metric events).
|
||||
# HAS NO CONNECTION to b0ts pipelines or quarantined market data (legal).
|
||||
CLICKHOUSE_URL=
|
||||
# "user:password" for ClickHouse basic auth; from secret refs only.
|
||||
CLICKHOUSE_AUTH=
|
||||
# OpenMetadata — metadata PROJECTION target (canonical stays in 0pt).
|
||||
OPENMETADATA_URL=
|
||||
OPENMETADATA_TOKEN=
|
||||
# Feast feature server (online features path).
|
||||
FEAST_SERVER_URL=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Payments (Stripe) -- wired in code today: app/api/payments/*,
|
||||
# app/api/webhooks/stripe
|
||||
# ---------------------------------------------------------------------------
|
||||
STRIPE_SECRET_KEY=sk_test_xxx
|
||||
STRIPE_WEBHOOK_SECRET=whsec_xxx
|
||||
# Documented target (docs/08); not yet read by any route in this pass.
|
||||
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Object storage -- Cloudflare R2 / S3-compatible
|
||||
# ---------------------------------------------------------------------------
|
||||
# R2_ENDPOINT / R2_BUCKET / R2_ACCESS_KEY_ID are read in code today; all three
|
||||
# are required to enable /api/uploads, otherwise it returns 501.
|
||||
# R2_ACCOUNT_ID and R2_SECRET_ACCESS_KEY are documented in docs/08 but were NOT
|
||||
# found via a process.env.* read in this pass -- verify before assuming the R2
|
||||
# client is fully wired.
|
||||
R2_ENDPOINT=https://<accountid>.r2.cloudflarestorage.com
|
||||
R2_BUCKET=opt-uploads
|
||||
R2_ACCESS_KEY_ID=change-me
|
||||
R2_ACCOUNT_ID=
|
||||
R2_SECRET_ACCESS_KEY=
|
||||
# Fallback local artifact store used when no object storage is configured
|
||||
# (src/adapters/local-blob-store.ts). Leave unset in production.
|
||||
OPT_LOCAL_BLOB_DIR=./.opt-blobs
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Document intelligence / OCR (Azure) -- wired in code today:
|
||||
# src/services/document-extraction-service.ts. Unset => extraction
|
||||
# "not configured".
|
||||
# ---------------------------------------------------------------------------
|
||||
DOC_INTELLIGENCE_ENDPOINT=https://<resource>.cognitiveservices.azure.com
|
||||
DOC_INTELLIGENCE_KEY=change-me
|
||||
DOC_INTELLIGENCE_API_VERSION=2024-11-30
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Customer DB-grant encryption -- wired in code today:
|
||||
# /api/connections/db-grant
|
||||
# ---------------------------------------------------------------------------
|
||||
# 32-byte key, AES-GCM. Encrypts customer-supplied read-only connection strings
|
||||
# at rest.
|
||||
GRANT_ENC_KEY=change-me-32-byte-key
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# External write-back targets
|
||||
# ---------------------------------------------------------------------------
|
||||
# Odoo base URL for the bills/write-back adapter.
|
||||
ODOO_BASE_URL=https://<your-odoo-host>
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Email -- Resend (documented target, docs/08; no Resend call site found in
|
||||
# this pass -- treat as not-yet-implemented, not just unconfigured)
|
||||
# ---------------------------------------------------------------------------
|
||||
RESEND_API_KEY=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Embedded analytics (documented target, docs/08; no call site found in this
|
||||
# pass)
|
||||
# ---------------------------------------------------------------------------
|
||||
METABASE_SITE_URL=
|
||||
METABASE_SECRET_KEY=
|
||||
NOCODB_BASE_URL=
|
||||
NOCODB_API_TOKEN=
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test-only (tests/p0 real-infra suite; not needed to run the app)
|
||||
# ---------------------------------------------------------------------------
|
||||
# ST0RE_TEST_TENANT_ID=
|
||||
Submodule .worktrees/feat-import-policy deleted from c134527509
@@ -5,6 +5,7 @@ import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mappin
|
||||
import { createAud0Client } from "@/src/clients/aud0-client";
|
||||
import { validateRows, defineMapping, type RowError } from "@siax/contracts/import-export";
|
||||
import { serviceById, type Flow } from "@/lib/services";
|
||||
import { IMPORT_DISPATCH_POLICY, evaluateImportDispatch } from "@/lib/import-dispatch-policy";
|
||||
import { ENTITY_TYPES, FREQUENCIES, type EntityType, type Frequency } from "@/lib/data";
|
||||
|
||||
// C8: CSV-import of orders — the symmetric counterpart of GET /api/orders/export
|
||||
@@ -26,8 +27,12 @@ import { ENTITY_TYPES, FREQUENCIES, type EntityType, type Frequency } from "@/li
|
||||
// boundary (durable outbox when DATABASE_URL is configured). Emission failure
|
||||
// is logged, never blocks the import (the outbox pattern owns delivery).
|
||||
|
||||
export const MAX_IMPORT_ROWS = 500;
|
||||
const MAX_CSV_CHARS = 1_000_000;
|
||||
// DES1GN ADR D3 (§31): taken härstammar från ImportDispatchPolicy — ett objekt,
|
||||
// en sanning. Inline-taket är oförändrat (500); rader > policy.maxRows (5000)
|
||||
// är N0D-dispatch-signalen (descriptor-gate: svarar 413 + hint tills N0D-workern
|
||||
// är verifierad — aldrig tyst trunkering, aldrig tyst dispatch).
|
||||
export const MAX_IMPORT_ROWS = IMPORT_DISPATCH_POLICY.maxInlineRows;
|
||||
const MAX_CSV_CHARS = IMPORT_DISPATCH_POLICY.maxBytes;
|
||||
const IMPORT_COLUMNS = ["flow", "service_type", "type", "volume", "frequency", "markets"] as const;
|
||||
const FLOWS: readonly Flow[] = ["catalog", "custom", "subscription"];
|
||||
const MAX_VOLUME = 1_000_000;
|
||||
@@ -187,15 +192,31 @@ export async function POST(req: NextRequest) {
|
||||
if (typeof csv !== "string" || csv.trim() === "") {
|
||||
return NextResponse.json({ error: { code: "bad_csv", message: "csv saknas eller är tom" } }, { status: 400 });
|
||||
}
|
||||
if (csv.length > MAX_CSV_CHARS) {
|
||||
return NextResponse.json({ error: { code: "csv_too_large", message: `CSV större än ${MAX_CSV_CHARS} tecken` } }, { status: 413 });
|
||||
const csvBytes = Buffer.byteLength(csv, "utf8");
|
||||
if (evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: 0 }).verdict === "byte_cap_exceeded") {
|
||||
return NextResponse.json({ error: { code: "csv_too_large", message: `CSV större än ${MAX_CSV_CHARS} bytes` } }, { status: 413 });
|
||||
}
|
||||
try {
|
||||
const { report, rows, headerError } = validateImport(csv);
|
||||
if (headerError) {
|
||||
return NextResponse.json({ error: { code: "bad_header", message: headerError }, report }, { status: 400 });
|
||||
}
|
||||
if (rows.length > MAX_IMPORT_ROWS) {
|
||||
const dispatchVerdict = evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: rows.length });
|
||||
if (dispatchVerdict.verdict === "dispatch_n0d") {
|
||||
// DES1GN D3-signalen: den här volymen ÄR ett N0D-jobb (0pt.import_execute_job).
|
||||
// Descriptor-gaten är stängd (N0D-worker UNVERIFIED) -> fail-closed 413 med hint.
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: "n0d_dispatch_required",
|
||||
message: `Import med ${rows.length} rader ska köras som N0D-jobb (0pt.import_execute_job); dispatch är ännu inte aktiverad`,
|
||||
hint: { capability_id: "0pt.import_execute", job_type: "0pt.import_execute_job", status: "UNVERIFIED" },
|
||||
},
|
||||
},
|
||||
{ status: 413 },
|
||||
);
|
||||
}
|
||||
if (dispatchVerdict.verdict === "row_cap_exceeded") {
|
||||
return NextResponse.json({ error: { code: "row_cap_exceeded", message: `Max ${MAX_IMPORT_ROWS} rader per import` } }, { status: 413 });
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// DES1GN ADR D3 (§31): bulk-import dispatch-policy — ETT objekt, en sanning.
|
||||
//
|
||||
// Import-routen (/api/orders/import) är inline-exekvering med hårt tak. Rader
|
||||
// över maxRows är DES1GN D3:s signal för N0D-dispatch (job_type
|
||||
// 0pt.import_execute_job / capability 0pt.import_execute i n0d:s
|
||||
// JOB_TYPE_REGISTRY). TILLS descriptor-gaten släpper (N0D-worker
|
||||
// UNVERIFIED) svarar routen 413 med n0d_dispatch_required + hint — aldrig
|
||||
// tyst trunkering, aldrig tyst dispatch.
|
||||
export const IMPORT_DISPATCH_POLICY = {
|
||||
/** Inline-tak i import-routen (oförändrat beteendekontrakt, 0pt#53). */
|
||||
maxInlineRows: 500,
|
||||
/** N0D-dispatch-signal: rader över detta är ett N0D-jobb, inget HTTP-anrop. */
|
||||
maxRows: 5000,
|
||||
/** Byte-tak för hela CSV-kroppen (UTF-8). */
|
||||
maxBytes: 1_000_000,
|
||||
/** Format-allowlist — endast RFC 4180 CSV i dag. */
|
||||
allowedFormats: ["csv"] as readonly string[],
|
||||
} as const;
|
||||
|
||||
export type ImportDispatchPolicy = typeof IMPORT_DISPATCH_POLICY;
|
||||
|
||||
export type ImportDispatchVerdict =
|
||||
| { verdict: "inline" }
|
||||
| { verdict: "unsupported_format"; format: string }
|
||||
| { verdict: "byte_cap_exceeded"; bytes: number; maxBytes: number }
|
||||
| { verdict: "row_cap_exceeded"; rows: number; maxInlineRows: number }
|
||||
| { verdict: "dispatch_n0d"; rows: number; maxRows: number };
|
||||
|
||||
/** Ren funktion — inga I/O, fullt deterministisk, testad per kombination. */
|
||||
export function evaluateImportDispatch(
|
||||
input: { format: string; bytes: number; rows: number },
|
||||
policy: ImportDispatchPolicy = IMPORT_DISPATCH_POLICY,
|
||||
): ImportDispatchVerdict {
|
||||
if (!policy.allowedFormats.includes(input.format)) {
|
||||
return { verdict: "unsupported_format", format: input.format };
|
||||
}
|
||||
if (input.bytes > policy.maxBytes) {
|
||||
return { verdict: "byte_cap_exceeded", bytes: input.bytes, maxBytes: policy.maxBytes };
|
||||
}
|
||||
if (input.rows > policy.maxRows) {
|
||||
return { verdict: "dispatch_n0d", rows: input.rows, maxRows: policy.maxRows };
|
||||
}
|
||||
if (input.rows > policy.maxInlineRows) {
|
||||
return { verdict: "row_cap_exceeded", rows: input.rows, maxInlineRows: policy.maxInlineRows };
|
||||
}
|
||||
return { verdict: "inline" };
|
||||
}
|
||||
Generated
+36
-36
@@ -1721,15 +1721,15 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/env": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/env/-/env-16.3.6.tgz",
|
||||
"integrity": "sha512-x9Vblze1EbtltQYnNH38xCPWU3TVfBd1eXqA3+w9+BTpedkkdNpAaltXlGQ/nsc1+E0mVTNrtcbX3GoO09zeLQ==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/env/-/env-16.3.4.tgz",
|
||||
"integrity": "sha512-cjWZnUUa6jZq2kFaNe/ZyJdZonOZ/QoN0Zka2nz/FLOrfx14pQuM9c5RaSVkWMqgdt4ksgPAMWPyHSs/CyV48Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@next/swc-darwin-arm64": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-16.3.6.tgz",
|
||||
"integrity": "sha512-E/7GEqaUkt8mk/T8v9lAnrhzR06kdq1ZBkC12F8tAMkdIadwNp3H1KqHynDHrpcTlGCUdq/qu6vUL2aYVyYBdw==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-16.3.4.tgz",
|
||||
"integrity": "sha512-iBr3I5LZNk5/bgl5//iTgD2tcym14MX0Xo7fD//u9dYAEgGzza1y9oywluPtf74YnOswVdH1908aK9xVz7zQTw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
@@ -1743,9 +1743,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-darwin-x64": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-16.3.6.tgz",
|
||||
"integrity": "sha512-yBE893/nDWTlaiBD1p+qgt7NUen4U5R6FXyH0s67Npq1S3E0cVSef1WIXC2xBRgQvwAvJq6DnS6Y6PrY0cy4Ew==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-16.3.4.tgz",
|
||||
"integrity": "sha512-2dpiSyl2Jw/NrBPaU2MAKGSa+2MR82pJIn4Sm5Rjr+gxAeuh0z158Su3Z2O8zn7UNNq+ej4bToed6RcRN/Lydg==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
@@ -1759,9 +1759,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-linux-arm64-gnu": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-16.3.6.tgz",
|
||||
"integrity": "sha512-KJDpjBqBPYlvkivmyrp+Qys6k/7ksbqGQvRVc6ZEGfR+cjQxx+nUkJaWmNZJsmoOrqYNbaXByF8wa0lBwDhB3Q==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-16.3.4.tgz",
|
||||
"integrity": "sha512-+t+U8HZT+fApePCS5h89CSH3datz29MkzyfCn+6fpsZBG/oiEOhINcb9rtkv6sdpToLGFn2e6146NzaKCXkqrA==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
@@ -1778,9 +1778,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-linux-arm64-musl": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-16.3.6.tgz",
|
||||
"integrity": "sha512-mqNg2K+hvWskSRb/QM+Ix412DvBsuSF0XV+frTSw5vmoucNnIlynFwKYew8D01bfATErMOM7Bujrf0BA5DRKFA==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-16.3.4.tgz",
|
||||
"integrity": "sha512-mx03GNs1ocQA5JQ4FxDMmIsNkdrZh8cuezKCrId28e5/gIPU/l7Kcy2+vmCCzdjnnmXJy+iOAu+7K0QppO6Urg==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
@@ -1797,9 +1797,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-linux-x64-gnu": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-16.3.6.tgz",
|
||||
"integrity": "sha512-nFncBNGAYouRHjRVaITs9beZRfhX4ssVwpnvPIAbkZVH6LtGoAVlH4bJ8Cnf9SOo9bsXgPFer/GdHtEE3JNOkw==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-16.3.4.tgz",
|
||||
"integrity": "sha512-YIhGY6fSMfha52bnVxnzc9zaVBzJg+cqQTOD8tXIBSx4fuv0pVMxQTE0PaS59YhnMOiYiG09IMwxJAf/CFm/Dw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
@@ -1816,9 +1816,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-linux-x64-musl": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-16.3.6.tgz",
|
||||
"integrity": "sha512-5Mf3cHDGR/Iz0ng2Bj3zUR3p5QS9YK3Hn2QiAfavFmyF48zwThAjpFoiTKNIcOHLYS4zEk+gzyJ/9deQ2ZB8yQ==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-16.3.4.tgz",
|
||||
"integrity": "sha512-+eaaX6axpDb0yF1GCpiERe6njplvdC+nks/fKfcHu3XPGRrald8P3/X7yv7QLdjA51knnxwl9pxdIJsg+w1L+Q==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
@@ -1835,9 +1835,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-win32-arm64-msvc": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-16.3.6.tgz",
|
||||
"integrity": "sha512-0jkJy0C2kbrJWTk4YLa3xk80pVBpx8FCHJym7CnUfDAXe/FWv5qT7SQJbR0KuemyxaEDlEx5WT4VQJoTW+/9Qw==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-16.3.4.tgz",
|
||||
"integrity": "sha512-0jcXW7Xs/uzICrmgV3MhDYDeRy++1CqnpDIerlPIqYO4bhzB4WNbX/aRnQclustsAyTkFKB0z6rbcjmNg5tR8A==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
@@ -1851,9 +1851,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@next/swc-win32-x64-msvc": {
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-16.3.6.tgz",
|
||||
"integrity": "sha512-/YXjI1e5OXcZ7YpxRwgP/1jAV/SBKTzeVKqN2mk7mLpcICsyn3Gl5+dIfDTJp70M0ccMhyMMRso4v6mPDCGepg==",
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-16.3.4.tgz",
|
||||
"integrity": "sha512-vvBzwu1pYQCp92maZCFCIw/XgOTMR5tur9GjakwIo2cmwRTMKajRZZDS9+e4KsUZWKu1E007WUeAFXRRjZeuzw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
@@ -6610,7 +6610,7 @@
|
||||
"integrity": "sha512-L+otWM/aQbYTx98aZhgEoMb4bZAXx1YVW4UMA/vuCyCoWG5HJyZUili8QAkqzrcC+5///tsz3s0M+SlyB5bLMw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@next/env": "16.3.6",
|
||||
"@next/env": "16.3.4",
|
||||
"@swc/helpers": "0.5.23",
|
||||
"baseline-browser-mapping": "^2.9.19",
|
||||
"caniuse-lite": "^1.0.30001579",
|
||||
@@ -6624,14 +6624,14 @@
|
||||
"node": ">=20.9.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@next/swc-darwin-arm64": "16.3.6",
|
||||
"@next/swc-darwin-x64": "16.3.6",
|
||||
"@next/swc-linux-arm64-gnu": "16.3.6",
|
||||
"@next/swc-linux-arm64-musl": "16.3.6",
|
||||
"@next/swc-linux-x64-gnu": "16.3.6",
|
||||
"@next/swc-linux-x64-musl": "16.3.6",
|
||||
"@next/swc-win32-arm64-msvc": "16.3.6",
|
||||
"@next/swc-win32-x64-msvc": "16.3.6",
|
||||
"@next/swc-darwin-arm64": "16.3.4",
|
||||
"@next/swc-darwin-x64": "16.3.4",
|
||||
"@next/swc-linux-arm64-gnu": "16.3.4",
|
||||
"@next/swc-linux-arm64-musl": "16.3.4",
|
||||
"@next/swc-linux-x64-gnu": "16.3.4",
|
||||
"@next/swc-linux-x64-musl": "16.3.4",
|
||||
"@next/swc-win32-arm64-msvc": "16.3.4",
|
||||
"@next/swc-win32-x64-msvc": "16.3.4",
|
||||
"sharp": "^0.35.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
+9
-3
@@ -55,10 +55,10 @@
|
||||
"@siax/charts": "^0.5.0",
|
||||
"@siax/collaboration": "0.3.0",
|
||||
"@siax/config": "^0.2.1",
|
||||
"@siax/contracts": "^0.11.1",
|
||||
"@siax/import-export": "^0.2.2",
|
||||
"@siax/contracts": "^0.10.0",
|
||||
"@siax/import-export": "^0.2.0",
|
||||
"@siax/tokens": "^0.1.2",
|
||||
"@siax/ui": "^0.15.1",
|
||||
"@siax/ui": "^0.14.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
@@ -99,5 +99,11 @@
|
||||
"typescript": "^5",
|
||||
"typescript-eslint": "^8.15.0",
|
||||
"vitest": "^3.2.7"
|
||||
},
|
||||
"pnpm": {
|
||||
"overrides": {
|
||||
"@siax/ui>@siax/contracts": "0.10.0",
|
||||
"@siax/ui>@siax/import-export": "0.2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+179
-167
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,70 @@
|
||||
"""The engine must not accept anonymous traffic when INTERNAL_TOKEN is set.
|
||||
|
||||
Background (2026-09-15): lib/engine.ts on the edge has always SENT
|
||||
`X-Internal-Token`, but the engine never verified it — a deployed engine would
|
||||
have accepted anyone who could reach the port. The gate is fail-closed:
|
||||
|
||||
* INTERNAL_TOKEN set -> every route except /health requires the EXACT
|
||||
token; wrong/missing = 401.
|
||||
* INTERNAL_TOKEN unset -> gate inert (local dev); production compose always
|
||||
sets it.
|
||||
|
||||
`/health` stays public on purpose: it is the liveness probe that
|
||||
`lib/health-checks.ts` calls from the edge, and it leaks nothing but booleans.
|
||||
|
||||
The token is read at module import (the same value the running process serves
|
||||
with), so each test reloads the module with the env it wants — and the
|
||||
teardown reloads it one more time with the env CLEARED so later test files in
|
||||
the same session never inherit a baked-in token.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def engine(monkeypatch):
|
||||
monkeypatch.setenv("INTERNAL_TOKEN", "s3cret")
|
||||
import orchestrator.main as main
|
||||
|
||||
importlib.reload(main)
|
||||
yield TestClient(main.app)
|
||||
os.environ.pop("INTERNAL_TOKEN", None)
|
||||
importlib.reload(main)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def engine_no_token(monkeypatch):
|
||||
monkeypatch.delenv("INTERNAL_TOKEN", raising=False)
|
||||
import orchestrator.main as main
|
||||
|
||||
importlib.reload(main)
|
||||
yield TestClient(main.app)
|
||||
os.environ.pop("INTERNAL_TOKEN", None)
|
||||
importlib.reload(main)
|
||||
|
||||
|
||||
def test_gate_off_when_internal_token_unset(engine_no_token):
|
||||
assert engine_no_token.get("/health").status_code == 200
|
||||
assert engine_no_token.get("/sources").status_code == 200
|
||||
|
||||
|
||||
def test_health_stays_public_but_sources_require_token(engine):
|
||||
# liveness probe is public by design
|
||||
assert engine.get("/health").status_code == 200
|
||||
# anonymous call to a data route -> 401 (was 200 before 2026-09-15)
|
||||
r = engine.get("/sources")
|
||||
assert r.status_code == 401
|
||||
assert r.json()["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
def test_wrong_token_is_401_and_correct_token_passes(engine):
|
||||
wrong = engine.get("/estimates?markets=SE", headers={"X-Internal-Token": "nope"})
|
||||
assert wrong.status_code == 401
|
||||
right = engine.get("/sources", headers={"X-Internal-Token": "s3cret"})
|
||||
assert right.status_code == 200
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "pe-sbom-0pt-f6a29da7e586",
|
||||
"repo": "0pt",
|
||||
"evidenceType": "sbom",
|
||||
"result": "pass",
|
||||
"verifiedAt": "2026-09-23T16:22:09Z",
|
||||
"verifiedBy": "syft (estate-kampanj issue #76, skannad vid deployad commit)",
|
||||
"proofRef": "siax/sbom/sbom-0pt-f6a29da7e586.json",
|
||||
"sourceSha": "f6a29da7e58601ce426181b11ea4959e90f07dcc",
|
||||
"tool": "syft",
|
||||
"sbomFormat": "cyclonedx-json"
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
# 0pt — cosign-signeringskedjan (2026-09-29)
|
||||
|
||||
Prod-imagen git.cloud.siax.io/sax3l/0pt:prod-2419b76eb2 (sha256:82bd3751669119e4cf806f9053d76463316157856a31144188902ee36cb7cba7) är cosign-signerad
|
||||
Prod-imagen git.cloud.siax.io/sax3l/0pt:prod-09b9cc80bf (sha256:8a1649c7418c8728e6793ff885814a266f055114f950e31ca9d7b252645dedd3) är cosign-signerad
|
||||
med nyckelparet vars pubkey ligger här. Privata nyckeln raderad efter signering.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEknnvwkO/YDVKCP7Nan6W8VoN2D2J
|
||||
bEx1cxYmhbhrPrc3VUAU8mAlkrk4yiFbe7TDPYuopW8A91qr7EMTzTdsQw==
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXES2u9EA2vFf6aQS5Hg/hsjudCJk
|
||||
mC1G/IFtBOzweyPlQD+A84UcYDJmxIR5Cfq5t8nvF3bY/ff18AhhK+WZmw==
|
||||
-----END PUBLIC KEY-----
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* `src/auth/internal-token.ts` -- verification for the ONE machine-to-machine
|
||||
* credential 0pt already uses for engine (Python) -> edge (Next.js) calls:
|
||||
* the `X-Internal-Token` header, checked against the `INTERNAL_TOKEN` env var
|
||||
* (the same shared secret `lib/engine.ts` and `lib/health-checks.ts` already
|
||||
* send on the opposite-direction edge -> engine calls).
|
||||
*
|
||||
* P014 FIX: `services/api/workers/quality_bridge.py` has always called
|
||||
* `POST /api/0pt/quality/bridge-run` with `X-Internal-Token`, never an
|
||||
* `Authorization: Bearer` header. But this route sits under
|
||||
* `/api/0pt/:path*`, which `middleware.ts` guards unconditionally with real
|
||||
* Zitadel Bearer verification -- and nothing anywhere checked
|
||||
* `X-Internal-Token` as an alternative credential. The result: in any
|
||||
* environment with the default `AUTH_ENFORCE` posture (enforced), the
|
||||
* Python engine's ONLY call into this bridge was rejected with 401 by
|
||||
* middleware before the route handler ever ran. `OPT_QUALITY_BRIDGE_ENABLED=true`
|
||||
* therefore could never actually succeed against a real deployment --
|
||||
* quality_bridge.py's fail-open design (see its own docstring) silently
|
||||
* absorbed the 401 as `status: "unavailable"`, so nothing outwardly broke,
|
||||
* but the whole point of building the bridge (an advisory second opinion
|
||||
* from the TS QualityService) was unreachable. Neither test suite caught
|
||||
* this: the TS test (tests/p0/quality-bridge.test.ts) calls the route's
|
||||
* POST function directly, bypassing middleware.ts entirely; the Python test
|
||||
* (services/api/tests/test_quality_bridge.py) mocks the HTTP layer and never
|
||||
* exercises the edge's real auth gate.
|
||||
*
|
||||
* This module verifies `X-Internal-Token` in constant time. middleware.ts
|
||||
* accepts it ONLY for the one route the engine actually calls
|
||||
* (`/api/0pt/quality/bridge-run`) -- not a blanket bypass for the rest of
|
||||
* `/api/0pt/*`. Fails closed: an unset/empty `INTERNAL_TOKEN` env var never
|
||||
* authenticates, even against an empty or missing header (no accidental
|
||||
* "unset equals unset, so let it through").
|
||||
*
|
||||
* Uses only Web APIs (`TextEncoder`), not `node:crypto` -- Next.js
|
||||
* middleware runs on the Edge runtime, which does not expose Node's
|
||||
* `crypto.timingSafeEqual`.
|
||||
*/
|
||||
|
||||
/** Minimal header accessor -- Web `Headers` and `NextRequest.headers` satisfy this. */
|
||||
export interface HeaderLookup {
|
||||
get(name: string): string | null | undefined;
|
||||
}
|
||||
|
||||
export const INTERNAL_TOKEN_HEADER = "x-internal-token";
|
||||
|
||||
/** Constant-time-ish byte comparison (Edge-runtime safe: no node:crypto). */
|
||||
function timingSafeEqualStr(a: string, b: string): boolean {
|
||||
const enc = new TextEncoder();
|
||||
const aBytes = enc.encode(a);
|
||||
const bBytes = enc.encode(b);
|
||||
const len = Math.max(aBytes.length, bBytes.length);
|
||||
let diff = aBytes.length ^ bBytes.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
diff |= (aBytes[i] ?? 0) ^ (bBytes[i] ?? 0);
|
||||
}
|
||||
return diff === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* True only when `X-Internal-Token` is present AND matches a non-empty
|
||||
* configured `INTERNAL_TOKEN`. Fails closed on every other combination
|
||||
* (missing header, missing/empty env var, mismatch).
|
||||
*/
|
||||
export function verifyInternalServiceToken(
|
||||
headers: HeaderLookup,
|
||||
env: { INTERNAL_TOKEN?: string } = process.env,
|
||||
): boolean {
|
||||
const expected = env.INTERNAL_TOKEN?.trim();
|
||||
if (!expected) return false;
|
||||
const provided = headers.get(INTERNAL_TOKEN_HEADER);
|
||||
if (!provided) return false;
|
||||
return timingSafeEqualStr(provided, expected);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* DES1GN ADR D3 (§31): ImportDispatchPolicy — ren policy-funktion, testad per
|
||||
* kombination (format × bytes × rows, gränsvärden inkluderade), plus route-
|
||||
* kopplingen: rows > policy.maxRows -> 413 n0d_dispatch_required med hint
|
||||
* (descriptor-gate stängd), inline-taket oförändrat.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { IMPORT_DISPATCH_POLICY, evaluateImportDispatch } from "@/lib/import-dispatch-policy";
|
||||
|
||||
describe("evaluateImportDispatch — verdict per kombination", () => {
|
||||
it("gränsvärde maxInlineRows: exakt 500 rader -> inline", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxInlineRows })).toEqual({ verdict: "inline" });
|
||||
});
|
||||
|
||||
it("maxInlineRows < rows <= maxRows -> row_cap_exceeded (inline-taket, oförändrat)", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxInlineRows + 1 })).toEqual({
|
||||
verdict: "row_cap_exceeded",
|
||||
rows: 501,
|
||||
maxInlineRows: 500,
|
||||
});
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxRows }).verdict).toBe("row_cap_exceeded");
|
||||
});
|
||||
|
||||
it("rows > maxRows -> dispatch_n0d (D3-signalen)", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxRows + 1 })).toEqual({
|
||||
verdict: "dispatch_n0d",
|
||||
rows: 5001,
|
||||
maxRows: 5000,
|
||||
});
|
||||
});
|
||||
|
||||
it("bytes > maxBytes -> byte_cap_exceeded, oavsett rows", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: IMPORT_DISPATCH_POLICY.maxBytes + 1, rows: 1 }).verdict).toBe("byte_cap_exceeded");
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: IMPORT_DISPATCH_POLICY.maxBytes, rows: 1 }).verdict).toBe("inline");
|
||||
});
|
||||
|
||||
it("format utanför allowlist -> unsupported_format", () => {
|
||||
expect(evaluateImportDispatch({ format: "xlsx", bytes: 10, rows: 1 })).toEqual({ verdict: "unsupported_format", format: "xlsx" });
|
||||
});
|
||||
|
||||
it("precedens: format slår bytes, bytes slår rows", () => {
|
||||
expect(evaluateImportDispatch({ format: "json", bytes: 9e9, rows: 9e9 }).verdict).toBe("unsupported_format");
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 9e9, rows: 9e9 }).verdict).toBe("byte_cap_exceeded");
|
||||
});
|
||||
});
|
||||
|
||||
describe("route-koppling: POST /api/orders/import respekterar policyn", () => {
|
||||
const { requireOrderOwnerMock, createOrderMock, ingestEventMock, mockOwner } = vi.hoisted(() => ({
|
||||
requireOrderOwnerMock: vi.fn(),
|
||||
createOrderMock: vi.fn(),
|
||||
ingestEventMock: vi.fn().mockResolvedValue({}),
|
||||
mockOwner: { owner: { tenantId: "tenant-test", subject: "buyer-test" } },
|
||||
}));
|
||||
const _unused = mockOwner;
|
||||
vi.mock("@/src/auth/require-order-owner", () => ({
|
||||
requireOrderOwner: (...args: unknown[]) => requireOrderOwnerMock(...args),
|
||||
}));
|
||||
vi.mock("@/lib/orders-store", () => ({ createOrder: (...args: unknown[]) => createOrderMock(...args) }));
|
||||
vi.mock("@/src/clients/aud0-client", () => ({ createAud0Client: () => ({ ingestEvent: ingestEventMock }) }));
|
||||
|
||||
let POST: typeof import("@/app/api/orders/import/route").POST;
|
||||
let MAX_IMPORT_ROWS: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
requireOrderOwnerMock.mockResolvedValue(mockOwner);
|
||||
({ POST, MAX_IMPORT_ROWS } = await import("@/app/api/orders/import/route"));
|
||||
});
|
||||
|
||||
function postReq(csv: string): NextRequest {
|
||||
return new NextRequest(new URL("/api/orders/import", "http://localhost"), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ mode: "validate", csv }),
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const line = "custom,icp,Bolag,10,Engångs,SE";
|
||||
|
||||
it("exakt maxInlineRows rader -> 200 (inline-beteende oförändrat)", async () => {
|
||||
const csv = ["flow,service_type,type,volume,frequency,markets", ...Array.from({ length: MAX_IMPORT_ROWS }, () => line)].join("\n");
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it("maxRows < rows -> 413 n0d_dispatch_required med UNVERIFIED-hint, ingen skrivning", async () => {
|
||||
const csv = ["flow,service_type,type,volume,frequency,markets", ...Array.from({ length: IMPORT_DISPATCH_POLICY.maxRows + 1 }, () => line)].join("\n");
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(413);
|
||||
const body = (await res.json()) as { error: { code: string; hint: { capability_id: string; job_type: string; status: string } } };
|
||||
expect(body.error.code).toBe("n0d_dispatch_required");
|
||||
expect(body.error.hint).toEqual({ capability_id: "0pt.import_execute", job_type: "0pt.import_execute_job", status: "UNVERIFIED" });
|
||||
expect(createOrderMock).not.toHaveBeenCalled();
|
||||
expect(ingestEventMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("CSV > maxBytes -> 413 csv_too_large", async () => {
|
||||
const csv = "flow,service_type,type,volume,frequency,markets\n" + "x".repeat(IMPORT_DISPATCH_POLICY.maxBytes);
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(413);
|
||||
const body = (await res.json()) as { error: { code: string } };
|
||||
expect(body.error.code).toBe("csv_too_large");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { verifyInternalServiceToken, INTERNAL_TOKEN_HEADER } from "@/src/auth/internal-token";
|
||||
|
||||
function headers(map: Record<string, string> = {}): { get(name: string): string | null } {
|
||||
const lower = new Map(Object.entries(map).map(([k, v]) => [k.toLowerCase(), v]));
|
||||
return { get: (name: string) => lower.get(name.toLowerCase()) ?? null };
|
||||
}
|
||||
|
||||
describe("verifyInternalServiceToken", () => {
|
||||
it("accepts a header that matches the configured INTERNAL_TOKEN exactly", () => {
|
||||
const ok = verifyInternalServiceToken(headers({ [INTERNAL_TOKEN_HEADER]: "s3cr3t" }), {
|
||||
INTERNAL_TOKEN: "s3cr3t",
|
||||
});
|
||||
expect(ok).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects a mismatched token", () => {
|
||||
const ok = verifyInternalServiceToken(headers({ [INTERNAL_TOKEN_HEADER]: "wrong" }), {
|
||||
INTERNAL_TOKEN: "s3cr3t",
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects a missing header even when INTERNAL_TOKEN is configured", () => {
|
||||
const ok = verifyInternalServiceToken(headers({}), { INTERNAL_TOKEN: "s3cr3t" });
|
||||
expect(ok).toBe(false);
|
||||
});
|
||||
|
||||
it("FAILS CLOSED: an unset INTERNAL_TOKEN never authenticates, even against an empty header value", () => {
|
||||
// Regression guard: quality_bridge.py sends `os.getenv("INTERNAL_TOKEN", "")`
|
||||
// -- if the edge's own INTERNAL_TOKEN were also unset, a naive `provided
|
||||
// === expected` comparison of two empty strings would wrongly authenticate.
|
||||
const ok = verifyInternalServiceToken(headers({ [INTERNAL_TOKEN_HEADER]: "" }), { INTERNAL_TOKEN: undefined });
|
||||
expect(ok).toBe(false);
|
||||
});
|
||||
|
||||
it("FAILS CLOSED: an unset INTERNAL_TOKEN never authenticates even a non-empty header", () => {
|
||||
const ok = verifyInternalServiceToken(headers({ [INTERNAL_TOKEN_HEADER]: "anything" }), {
|
||||
INTERNAL_TOKEN: undefined,
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects a whitespace-only configured token (trimmed to empty)", () => {
|
||||
const ok = verifyInternalServiceToken(headers({ [INTERNAL_TOKEN_HEADER]: " " }), { INTERNAL_TOKEN: " " });
|
||||
expect(ok).toBe(false);
|
||||
});
|
||||
|
||||
it("is case-sensitive on the header name lookup contract but tolerant of header-name casing via the HeaderLookup adapter", () => {
|
||||
const ok = verifyInternalServiceToken(headers({ "X-Internal-Token": "s3cr3t" }), { INTERNAL_TOKEN: "s3cr3t" });
|
||||
expect(ok).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* P014 regression test: before this pass, POST /api/0pt/quality/bridge-run
|
||||
* -- the ONE route the Python engine's quality_bridge.py actually calls,
|
||||
* using X-Internal-Token, never a Bearer token -- was rejected 401 by
|
||||
* middleware.ts in any environment enforcing auth (the default), because
|
||||
* middleware required a verified Zitadel Bearer token unconditionally on
|
||||
* every /api/0pt/* path. See src/auth/internal-token.ts's doc comment for
|
||||
* the full story. Neither existing test suite caught it: the TS bridge test
|
||||
* calls the route's POST function directly (bypassing middleware.ts
|
||||
* entirely) and the Python test mocks the HTTP layer.
|
||||
*
|
||||
* This test exercises the actual `middleware` function, not the route
|
||||
* handler, so it proves the fix at the layer where the bug lived.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { NextRequest } from 'next/server';
|
||||
import { middleware } from '../../middleware';
|
||||
|
||||
function req(path: string, headers: Record<string, string> = {}): NextRequest {
|
||||
return new NextRequest(new URL(path, 'http://localhost'), { headers });
|
||||
}
|
||||
|
||||
describe('middleware.ts: internal-token path for /api/0pt/quality/bridge-run', () => {
|
||||
const saved = { auth: process.env.AUTH_ENFORCE, internal: process.env.INTERNAL_TOKEN, node: process.env.NODE_ENV };
|
||||
|
||||
beforeEach(() => {
|
||||
delete process.env.AUTH_ENFORCE; // default posture: enforced
|
||||
process.env.INTERNAL_TOKEN = 'engine-shared-secret';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const [k, v] of Object.entries(saved)) {
|
||||
const envKey = k === 'auth' ? 'AUTH_ENFORCE' : k === 'internal' ? 'INTERNAL_TOKEN' : 'NODE_ENV';
|
||||
if (v === undefined) delete process.env[envKey];
|
||||
else process.env[envKey] = v;
|
||||
}
|
||||
});
|
||||
|
||||
it('REGRESSION: a valid X-Internal-Token lets the engine through with NO Bearer token (was: 401)', async () => {
|
||||
const res = await middleware(
|
||||
req('/api/0pt/quality/bridge-run', { 'x-internal-token': 'engine-shared-secret' }),
|
||||
);
|
||||
// NextResponse.next() carries no body/status override -- it is not an
|
||||
// error JSON response the way a rejection is.
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it('an invalid X-Internal-Token still falls through to the ordinary Bearer requirement (401, not a bypass)', async () => {
|
||||
const res = await middleware(req('/api/0pt/quality/bridge-run', { 'x-internal-token': 'wrong-secret' }));
|
||||
expect(res.status).toBe(401);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe('OPT_UNAUTHENTICATED');
|
||||
});
|
||||
|
||||
it('a valid X-Internal-Token on a DIFFERENT /api/0pt/* route does NOT bypass auth (not a blanket exemption)', async () => {
|
||||
const res = await middleware(req('/api/0pt/sources', { 'x-internal-token': 'engine-shared-secret' }));
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('no INTERNAL_TOKEN configured on the edge -- the internal-token path never activates, even with the right-looking header', async () => {
|
||||
delete process.env.INTERNAL_TOKEN;
|
||||
const res = await middleware(
|
||||
req('/api/0pt/quality/bridge-run', { 'x-internal-token': 'engine-shared-secret' }),
|
||||
);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('a real Bearer-authenticated caller can still reach bridge-run directly (not internal-token-only)', async () => {
|
||||
// No X-Internal-Token at all, no Authorization header either -> falls to
|
||||
// the normal Bearer requirement -> 401, proving this path is additive,
|
||||
// not a replacement for the existing Zitadel flow.
|
||||
const res = await middleware(req('/api/0pt/quality/bridge-run'));
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user