refactor(server): give every environment variable one config owner

Eight registerAs tokens existed while every consumer read the same values
through readEnv() instead, so the boot-stable layer the repo rules describe
was unused and nothing said which path owned a variable. FORCE_HTTPS, for
instance, was frozen by the middleware through the http token and read live
by the cookie code at the same time.

A variable is now owned by exactly one side. TRUST_PROXY and
HSTS_INCLUDE_SUBDOMAINS belong to httpConfig, which buildApp() already
injects into the pre-init middleware, and TREK_PLACE_PHOTO_DIR to the new
storageConfig, which the storage registry now injects instead of reading it
through RuntimeEnvService. Their derive functions moved to boot-derive.ts,
which deriveAll() never calls. The six tokens nothing injected are gone:
their values are needed before the container exists (config.ts, the
database, src/mcp, the SSRF guard) and stay on readEnv().

config-ownership.test.ts records the keys each side reads through a Proxy
and fails on a variable read through both, on a token nothing injects and
on a variable the schema does not validate. env-reference.test.ts holds
server/.env.example to the schema, which had drifted by 34 variables (SMTP,
WebAuthn, plugins, MCP session tuning, TREK_MANAGED and others); they are
documented now. jwt-verify.ts no longer cites a middleware file that is
gone.
This commit is contained in:
Maurice
2026-10-08 15:12:07 +02:00
parent 5c054863b9
commit ba1504d6e9
22 changed files with 389 additions and 142 deletions
+12 -7
View File
@@ -3,7 +3,9 @@ import compression from 'compression';
import cors from 'cors';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';
import { readEnv, type AppEnv } from '../app-config';
import type { ConfigType } from '@nestjs/config';
import { readEnv } from '../app-config';
import { httpConfig } from '../nest/app-config/tokens';
import { logDebug, logWarn, logError } from '../nest/audit/audit-log.logger';
import { isSameHostOrigin } from '../nest/common/same-origin';
@@ -124,7 +126,8 @@ export function routingCspOrigins(baseUrls: (string | null | undefined)[]): stri
export function applyGlobalMiddleware(
app: express.Application,
opts: {
http?: AppEnv['http'];
/** The boot-stable half (trust proxy, HSTS subdomains), from the `httpConfig` token. */
http?: ConfigType<typeof httpConfig>;
/**
* Extra origins the browser may talk to, from the instance's own settings — today the
* self-hosted routing engine (#1797). Read once at apply time like everything else
@@ -136,9 +139,11 @@ export function applyGlobalMiddleware(
): void {
// The whole pipeline is configured at APPLY time (the per-request closures
// capture these values), so a snapshot is the correct semantic. bootstrap
// threads in the DI-loaded httpConfig; direct callers fall back to an
// apply-time readEnv() — same values, same freeze point.
const { http = readEnv().http, extraConnectSrc = [] } = opts;
// threads in the DI-loaded httpConfig; a direct caller gets the same token
// factory evaluated now. The live half (origins, forced HTTPS) is read here
// too, once, so both halves freeze at the same point.
const { http = httpConfig(), extraConnectSrc = [] } = opts;
const liveHttp = readEnv().http;
const { nodeEnv, isProduction } = readEnv().app;
// Trust first proxy (nginx/Docker) for correct req.ip
@@ -161,7 +166,7 @@ export function applyGlobalMiddleware(
}),
);
const allowedOrigins = http.corsOrigins;
const allowedOrigins = liveHttp.corsOrigins;
// With ALLOWED_ORIGINS set, a request from anywhere else is refused outright.
// Two things keep that from hitting the instance's own pages (#2543): a request
@@ -186,7 +191,7 @@ export function applyGlobalMiddleware(
}
: { origin: isProduction ? false : true, credentials: true };
const shouldForceHttps = http.forceHttps;
const shouldForceHttps = liveHttp.forceHttps;
// HSTS is worth enabling any time we're serving production traffic,
// not only when FORCE_HTTPS is set. Self-hosters behind Traefik /
// Caddy / Cloudflare Tunnel typically leave FORCE_HTTPS unset (the