Refuse a TRUST_PROXY the apps can't start with; Accounts reads loopback
A server configuration with TRUST_PROXY=TRUE made an Events install build for minutes, then fail: Express threw "invalid IP address: TRUE" at startup. - scripts/lib/envtpl.cjs: VALUE_RULES checked before any .env is written (exit 4, naming the value and the fix), for every release. - Contracts 1.6.0: agent.checkEnvValue / agent.invalidValues, the same rules for the Hub. - Accounts 1.0.1: TRUST_PROXY read as Events reads it; loopback used to turn proxy trust off. - Docs: CHANGELOGs, WHATS-NEW, HANDOVER, AGENT-1.0-PLAN, ROLLOUT-1.0, READMEs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -24,7 +24,7 @@ import { appVersion } from './lib/version.js';
|
||||
export function createApp(ctx: Ctx, provider: Provider) {
|
||||
const app = express();
|
||||
app.disable('x-powered-by');
|
||||
if (ctx.config.trustProxy) app.set('trust proxy', 1);
|
||||
if (ctx.config.trustProxySetting !== false) app.set('trust proxy', ctx.config.trustProxySetting);
|
||||
|
||||
app.use((_req, res, next) => {
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
|
||||
@@ -9,8 +9,10 @@ export type Config = {
|
||||
/** Encrypts secrets at rest and signs cookies. Generated on the server, never sent anywhere. */
|
||||
secret: string;
|
||||
production: boolean;
|
||||
/** Behind nginx/frp: trust X-Forwarded-* (as Events' TRUST_PROXY). */
|
||||
/** Behind nginx/frp: trust X-Forwarded-* (oidc-provider's `proxy`). */
|
||||
trustProxy: boolean;
|
||||
/** Express's "trust proxy" (TRUST_PROXY, read as Events reads it), or false when off. */
|
||||
trustProxySetting: TrustProxy;
|
||||
hubUrl: string;
|
||||
hubLicenceKey: string | null;
|
||||
hubPublicKey: string | null;
|
||||
@@ -25,17 +27,37 @@ export type Config = {
|
||||
|
||||
const bool = (v: string | undefined, def: boolean) => (v === undefined || v === '' ? def : String(v).toLowerCase() === 'true');
|
||||
|
||||
export type TrustProxy = false | number | string | string[];
|
||||
|
||||
/**
|
||||
* TRUST_PROXY as Express's "trust proxy": `loopback` (behind frp, the default the Hub suggests),
|
||||
* a hop count, IP addresses/CIDRs separated by commas, `true` (one hop: never "trust everyone") or
|
||||
* `false`. Unset: one hop in production, off otherwise. Before 1.0.1 anything but `true` meant
|
||||
* off, so `loopback` turned it off.
|
||||
*/
|
||||
export function trustProxyFrom(v: string | undefined, production: boolean): TrustProxy {
|
||||
const raw = (v ?? '').trim();
|
||||
if (raw === '') return production ? 1 : false;
|
||||
const lower = raw.toLowerCase();
|
||||
if (lower === 'true') return 1;
|
||||
if (lower === 'false') return false;
|
||||
if (/^\d+$/.test(raw)) return Number(raw) || false;
|
||||
return raw.includes(',') ? raw.split(',').map((s) => s.trim()).filter(Boolean) : raw;
|
||||
}
|
||||
|
||||
export function loadConfig(env: Record<string, string | undefined> = process.env): Config {
|
||||
const publicUrl = String(env.ACCOUNTS_URL || env.PUBLIC_URL || 'http://localhost:3200').replace(/\/+$/, '');
|
||||
const production = env.NODE_ENV === 'production';
|
||||
const secret = env.ACCOUNTS_SECRET || '';
|
||||
const trustProxySetting = trustProxyFrom(env.TRUST_PROXY, production);
|
||||
if (production && secret.length < 32) throw new Error('ACCOUNTS_SECRET must be set (at least 32 characters) in production.');
|
||||
return {
|
||||
publicUrl,
|
||||
port: Number(env.PORT || 5200),
|
||||
secret: secret || 'development-only-secret-change-me-0123456789',
|
||||
production,
|
||||
trustProxy: bool(env.TRUST_PROXY, production),
|
||||
trustProxy: trustProxySetting !== false,
|
||||
trustProxySetting,
|
||||
hubUrl: String(env.HUB_URL || 'https://hub.crosscode.co.za').replace(/\/+$/, ''),
|
||||
hubLicenceKey: env.HUB_LICENCE_KEY?.trim() || null,
|
||||
hubPublicKey: env.HUB_PUBLIC_KEY?.trim() || null,
|
||||
|
||||
Reference in New Issue
Block a user