feat(engine): fail-closed X-Internal-Token-gate + commit-kedja (GIT_SHA) i services/api
masterplan-lock / check (push) Successful in 3s
masterplan-lock / check (pull_request) Successful in 10s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (push) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / integration (push) Successful in 25s
CI (SIAX Cloud) / quality (push) Successful in 35s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 40s
CI (SIAX Cloud) / integration (pull_request) Successful in 49s
CI (SIAX Cloud) / quality (pull_request) Successful in 56s
masterplan-lock / check (push) Successful in 3s
masterplan-lock / check (pull_request) Successful in 10s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (push) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / integration (push) Successful in 25s
CI (SIAX Cloud) / quality (push) Successful in 35s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 40s
CI (SIAX Cloud) / integration (pull_request) Successful in 49s
CI (SIAX Cloud) / quality (pull_request) Successful in 56s
- X-Internal-Token skickades av edgen men verifierades ALDRIG — gate som raw-ASGI-middleware: INTERNAL_TOKEN satt => alla routes utom /health kräver exakt token, 401 (constant-time) annars. Ospecificerad token = inert (dev). - Dockerfile: ARG GIT_SHA + OCI-label + /health.reporting revision - compose var redan skriven för INTERNAL_TOKEN — motorn hade bara aldrig deployats med den - tester: tests/test_internal_token_gate.py (3: off/public/401-pass) — hela services/api-sviten 62 passed, 2 skipped (dokumenterade known-gaps), 0 fail
This commit is contained in:
@@ -7,6 +7,7 @@ is the customer-facing edge; this service runs the actual pipeline.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hmac
|
||||
import os
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
@@ -15,6 +16,7 @@ from typing import Any
|
||||
import yaml
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from sources.connectors import opensanctions_screen, rdw_vehicle
|
||||
@@ -99,9 +101,58 @@ class OrderIn(BaseModel):
|
||||
config: dict[str, Any] = {}
|
||||
|
||||
|
||||
# ── internal-token gate (fail-closed) ────────────────────────────────────────
|
||||
# The edge sends `X-Internal-Token` on every engineFetch (lib/engine.ts), but
|
||||
# until 2026-09-15 this service never VERIFIED it — a deployed engine accepted
|
||||
# anonymous traffic from anyone who could reach the port. The gate below
|
||||
# enforces it when INTERNAL_TOKEN is set: every route except /health (the
|
||||
# liveness probe) must carry the exact token or gets 401. When INTERNAL_TOKEN
|
||||
# is unset the gate is inert (local dev / docker compose without auth wiring);
|
||||
# the production deployment ALWAYS sets it (see infra/docker-compose.yml).
|
||||
_INTERNAL_TOKEN = os.getenv("INTERNAL_TOKEN", "").strip()
|
||||
_PUBLIC_PATHS = {"/health", "/docs", "/openapi.json", "/redoc"}
|
||||
|
||||
|
||||
class _InternalTokenGate:
|
||||
"""Raw-ASGI middleware: constant-time X-Internal-Token check, 401 on
|
||||
missing/wrong token. Written as a plain ASGI class (not
|
||||
@app.middleware('http')) so behavior does not depend on the
|
||||
BaseHTTPMiddleware dispatch quirks of the installed starlette."""
|
||||
|
||||
def __init__(self, app) -> None:
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope["type"] == "http" and _INTERNAL_TOKEN and scope["path"] not in _PUBLIC_PATHS:
|
||||
headers = {}
|
||||
for k, v in scope.get("headers") or []:
|
||||
headers.setdefault(k.decode("latin-1").lower(), v.decode("latin-1"))
|
||||
supplied = headers.get("x-internal-token", "")
|
||||
if not hmac.compare_digest(supplied, _INTERNAL_TOKEN):
|
||||
resp = JSONResponse(
|
||||
status_code=401,
|
||||
content={"error": {"code": "unauthorized", "message": "missing or invalid X-Internal-Token"}},
|
||||
)
|
||||
await resp(scope, receive, send)
|
||||
return
|
||||
await self.app(scope, receive, send)
|
||||
|
||||
|
||||
app.add_middleware(_InternalTokenGate)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
return {"ok": True, "service": "0pt-data-engine", "db": bool(os.getenv("DATABASE_URL")), "sources": len(_SOURCES)}
|
||||
return {
|
||||
"ok": True,
|
||||
"service": "0pt-data-engine",
|
||||
"db": bool(os.getenv("DATABASE_URL")),
|
||||
"sources": len(_SOURCES),
|
||||
# Commit-kedjan: samma SHA som image-taggen/OCI-labeln (Dockerfile ARG
|
||||
# GIT_SHA). 'unknown' gör ett oprovenienserat image synligt istället
|
||||
# för tyst falskt — samma policy som 0pt-motor/scripts/verify-image.sh.
|
||||
"revision": os.getenv("GIT_SHA", "unknown"),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/sources")
|
||||
|
||||
Reference in New Issue
Block a user