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:
+43
-3
@@ -12,7 +12,9 @@
|
||||
// appear in arguments or files other than the .env); server / defaults → the config's layers;
|
||||
// install → the config's install[product], else the variable's `from` filled with --var values.
|
||||
// Then --fallback, then the template's `default`. A required variable with no value stops
|
||||
// everything before any file is written (exit 3, the names on stderr).
|
||||
// everything before any file is written (exit 3, the names on stderr). So does a value the app
|
||||
// can't start with (VALUE_RULES, exit 4), e.g. TRUST_PROXY=TRUE, which crashes the Events backend
|
||||
// only after the long build.
|
||||
// --check: only says what would be added. Prints names only, never values.
|
||||
'use strict';
|
||||
|
||||
@@ -54,6 +56,33 @@ function keysIn(text) {
|
||||
return keys;
|
||||
}
|
||||
|
||||
// Values the apps check at startup. The rules live here rather than in the templates so they also
|
||||
// apply to releases whose templates predate them. Keep in step with checkEnvValue in
|
||||
// packages/contracts/src/agent.ts. Only for values that aren't secret: the message shows the value.
|
||||
const PROXY_NAME = /^(loopback|linklocal|uniquelocal)$/;
|
||||
const IPV4 = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
|
||||
const isIp = s => IPV4.test(s) || (s.includes(':') && /^[0-9A-Fa-f:.]+$/.test(s));
|
||||
function proxyEntry(s) {
|
||||
if (PROXY_NAME.test(s)) return true;
|
||||
const [ip, bits, extra] = s.split('/');
|
||||
if (extra !== undefined || !isIp(ip)) return false;
|
||||
return bits === undefined || /^\d{1,3}$/.test(bits) || IPV4.test(bits);
|
||||
}
|
||||
const VALUE_RULES = {
|
||||
// Events hands this to Express's "trust proxy" (apps/events/backend/src/app.js).
|
||||
TRUST_PROXY: {
|
||||
ok: v => v === 'true' || v === 'false' || /^\d+$/.test(v) || v.split(',').every(s => proxyEntry(s.trim())),
|
||||
hint: 'use loopback behind frp, a hop count such as 1, true or false (lowercase), or proxy IP addresses/CIDRs separated by commas',
|
||||
},
|
||||
};
|
||||
|
||||
/** Why the app can't use this value, or undefined when it can (or there's no rule for it). */
|
||||
function checkValue(name, value) {
|
||||
const rule = VALUE_RULES[name];
|
||||
if (!rule || rule.ok(String(value).trim())) return undefined;
|
||||
return `${name}=${JSON.stringify(String(value))} won't work: ${rule.hint}`;
|
||||
}
|
||||
|
||||
function fill(from, vars) {
|
||||
let missing = false;
|
||||
const value = from.replace(/\{([A-Za-z]+)\}/g, (_, k) => {
|
||||
@@ -94,6 +123,7 @@ function plan(opts) {
|
||||
const ctx = { config, vars: opts.vars, fallback: opts.fallback, product: opts.product || template.product };
|
||||
const files = [];
|
||||
const missing = [];
|
||||
const invalid = [];
|
||||
for (const [rel, variables] of Object.entries(template.files)) {
|
||||
if (rel.includes('..') || path.isAbsolute(rel)) throw new Error(`Bad file name in the template: ${rel}`);
|
||||
const file = path.join(opts.dir, rel);
|
||||
@@ -107,11 +137,17 @@ function plan(opts) {
|
||||
if (v.required) missing.push(`${v.name} (${v.layer}, ${rel})`);
|
||||
continue;
|
||||
}
|
||||
const problem = checkValue(v.name, value);
|
||||
if (problem) {
|
||||
const where = v.layer === 'server' ? "server, from the server's configuration on the Hub" : v.layer;
|
||||
invalid.push(`${problem} (${where}, ${rel})`);
|
||||
continue;
|
||||
}
|
||||
add.push({ name: v.name, value });
|
||||
}
|
||||
files.push({ rel, file, exists, add });
|
||||
}
|
||||
return { files, missing };
|
||||
return { files, missing, invalid: [...new Set(invalid)] };
|
||||
}
|
||||
|
||||
function write({ files }, release) {
|
||||
@@ -151,6 +187,10 @@ function main() {
|
||||
console.error(`Required settings with no value: ${result.missing.join(', ')}`);
|
||||
process.exit(3);
|
||||
}
|
||||
if (result.invalid.length) {
|
||||
console.error(`Settings the app can't start with:\n ${result.invalid.join('\n ')}`);
|
||||
process.exit(4);
|
||||
}
|
||||
for (const f of result.files) {
|
||||
if (!f.add.length) continue;
|
||||
const names = f.add.map(a => a.name).join(', ');
|
||||
@@ -161,4 +201,4 @@ function main() {
|
||||
|
||||
if (require.main === module) main();
|
||||
|
||||
module.exports = { parseArgs, keysIn, fill, line, plan, write };
|
||||
module.exports = { parseArgs, keysIn, fill, line, plan, write, checkValue };
|
||||
|
||||
@@ -54,6 +54,33 @@ envtpl() { node "$TPL_JS" --template "$TMP/template.json" --dir "$APP" --product
|
||||
[ ! -e "$APP/frontend/.env.production" ]
|
||||
}
|
||||
|
||||
@test "a value the app can't start with: nothing is written, the value and the fix are named" {
|
||||
printf '{ "server": { "TRUST_PROXY": "TRUE" } }\n' > "$TMP/bad.json"
|
||||
run envtpl --config "$TMP/bad.json" --release events@2.4.0
|
||||
[ "$status" -eq 4 ]
|
||||
[[ "$output" == *'TRUST_PROXY="TRUE" won'"'"'t work'* ]]
|
||||
[[ "$output" == *"loopback behind frp"* ]]
|
||||
[[ "$output" == *"server's configuration on the Hub"* ]]
|
||||
[ ! -e "$APP/backend/.env" ]
|
||||
[ ! -e "$APP/frontend/.env.production" ]
|
||||
}
|
||||
|
||||
@test "TRUST_PROXY: the values Events understands are accepted" {
|
||||
for v in loopback 1 true false 'loopback, 203.0.113.10' 10.0.0.0/8 '::1'; do
|
||||
rm -rf "$APP/backend/.env" "$APP/frontend/.env.production"
|
||||
printf '{ "server": { "TRUST_PROXY": "%s" } }\n' "$v" > "$TMP/ok.json"
|
||||
run envtpl --config "$TMP/ok.json"
|
||||
[ "$status" -eq 0 ] || { echo "$v: $output"; false; }
|
||||
done
|
||||
}
|
||||
|
||||
@test "deploy: a bad value already in a .env is left alone (only new variables are checked)" {
|
||||
printf 'DATABASE_URL=x\nPORT=1\nFRONTEND_URL=y\nTRUST_PROXY=TRUE\n' > "$APP/backend/.env"
|
||||
printf '{ "server": { "TRUST_PROXY": "TRUE" } }\n' > "$TMP/bad.json"
|
||||
run envtpl --config "$TMP/bad.json"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "--fallback fills a value no layer gives" {
|
||||
run envtpl --fallback TRUST_PROXY=1
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
Reference in New Issue
Block a user