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:
@@ -13,6 +13,7 @@ Events and the Hub, uses it, so the key fields are the same in every product's J
|
||||
| Sign-in return | — | The return-path check and OIDC `state` every product uses (ROADMAP-v3 §3) | `src/return-to.ts` |
|
||||
| Product link | `crosscode.link/1` | **Connect** under Settings → Linked products: the signed handshake that sets up a link's secret (`link.connect`, `link.connected`, `link.disconnect`) | `src/link.ts` |
|
||||
| Hub protocol v2 | `protocolVersion: 2` | Register, heartbeat (stats per product), linked products, update requests, AI, licence token | `src/hub.ts` (exported as `hub`) |
|
||||
| Env template | `crosscode.env-template/1` | Each product's `.env` variables by layer; `missingRequired` and, from 1.6.0, `checkEnvValue` / `invalidValues` (values an app can't start with, e.g. `TRUST_PROXY=TRUE`), which the Hub checks before a job | `src/agent.ts` (exported as `agent`) |
|
||||
| Signing | — | HMAC request signing for the Hub and for product links (Node only) | `src/signing.ts` (`@crosscode/contracts/signing`) |
|
||||
|
||||
## Using it
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@crosscode/contracts",
|
||||
"version": "1.5.0",
|
||||
"version": "1.6.0",
|
||||
"description": "The shared JSON formats used by every Cross Code product: zod schemas, JSON Schema, TypeScript types and sample payloads",
|
||||
"license": "UNLICENSED",
|
||||
"type": "commonjs",
|
||||
|
||||
@@ -318,3 +318,41 @@ export function missingRequired(template: EnvTemplate, layer: "server" | "defaul
|
||||
}
|
||||
return [...new Set(missing)];
|
||||
}
|
||||
|
||||
// Values the apps check at startup, so a bad one is refused when it's entered instead of
|
||||
// crashing the app after an install's build. Keep in step with VALUE_RULES in
|
||||
// scripts/lib/envtpl.cjs (which checks again on the server, for any release).
|
||||
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: string) => IPV4.test(s) || (s.includes(":") && /^[0-9A-Fa-f:.]+$/.test(s));
|
||||
function proxyEntry(s: string): boolean {
|
||||
if (PROXY_NAME.test(s)) return true;
|
||||
const [ip, bits, extra] = s.split("/");
|
||||
if (extra !== undefined || ip === undefined || !isIp(ip)) return false;
|
||||
return bits === undefined || /^\d{1,3}$/.test(bits) || IPV4.test(bits);
|
||||
}
|
||||
const ENV_VALUE_RULES: Record<string, { ok: (v: string) => boolean; hint: string }> = {
|
||||
// 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 an app can't start with this value (a sentence for the person who entered it), or null when it can. */
|
||||
export function checkEnvValue(name: string, value: string): string | null {
|
||||
const rule = ENV_VALUE_RULES[name];
|
||||
if (!rule || rule.ok(String(value).trim())) return null;
|
||||
return `${name} can't be "${value}". ${rule.hint}`;
|
||||
}
|
||||
|
||||
/** The problems with a layer's values ({ NAME: message }), empty when there are none (the Hub's check on save and before a job). */
|
||||
export function invalidValues(values: Record<string, string | undefined>): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const [name, value] of Object.entries(values)) {
|
||||
if (value === undefined || value === null || String(value) === "") continue;
|
||||
const problem = checkEnvValue(name, String(value));
|
||||
if (problem) out[name] = problem;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -171,4 +171,18 @@ describe("env templates", () => {
|
||||
expect(agent.missingRequired(template, "server", { TRUST_PROXY: "loopback" })).toEqual(["DB_HOST"]);
|
||||
expect(agent.missingRequired(template, "defaults", {})).toEqual(["HUB_URL"]);
|
||||
});
|
||||
|
||||
it("refuses a TRUST_PROXY the Events backend can't start with", () => {
|
||||
for (const ok of ["loopback", "1", "true", "false", "loopback, 203.0.113.10", "10.0.0.0/8", "::1", "fd00::/8", "192.168.1.0/255.255.255.0"]) {
|
||||
expect(agent.checkEnvValue("TRUST_PROXY", ok)).toBeNull();
|
||||
}
|
||||
for (const bad of ["TRUE", "True", "yes", "on", "300.1.1.1", "10.0.0.0/8/1", "loopback,"]) {
|
||||
expect(agent.checkEnvValue("TRUST_PROXY", bad)).toMatch(/^TRUST_PROXY can't be/);
|
||||
}
|
||||
expect(agent.checkEnvValue("DB_HOST", "anything")).toBeNull();
|
||||
expect(agent.invalidValues({ TRUST_PROXY: "TRUE", DB_HOST: "10.0.0.5", DB_PORT: undefined })).toEqual({
|
||||
TRUST_PROXY: expect.stringContaining("loopback"),
|
||||
});
|
||||
expect(agent.invalidValues({ TRUST_PROXY: "loopback" })).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user