Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

fix: harden tiered app serving per adversarial review

Josh France457f7e2777ae
diff --git a/cli/src/backends/aws.ts b/cli/src/backends/aws.ts
index 2f1e9f717..89bc69e60 100644
--- a/cli/src/backends/aws.ts
+++ b/cli/src/backends/aws.ts
@@ -2592,8 +2592,9 @@ function awsCoreHostnames(config: QmConfig): string[] {
const apiHost = normalize(hostname, "apiUrl");
if (apiHost !== new URL(config.publicUrl).hostname.toLowerCase().replace(/\.$/, "")) hosts.push(apiHost);
}
- const apps = config.env.core?.AWS_DEPLOY_APPS_DOMAIN?.trim();
- if (apps) hosts.push(`*.${normalize(apps, "env.core.AWS_DEPLOY_APPS_DOMAIN")}`);
+ const apps = config.env.core?.DEPLOY_APPS_DOMAIN?.trim() || config.env.core?.AWS_DEPLOY_APPS_DOMAIN?.trim();
+ if (apps)
+ hosts.push(`*.${normalize(apps, "the apps domain (env.core.DEPLOY_APPS_DOMAIN or AWS_DEPLOY_APPS_DOMAIN)")}`);
return [...new Set(hosts)];
}
diff --git a/cli/test/aws.test.ts b/cli/test/aws.test.ts
index c9086f2cd..8fb0dde59 100644
--- a/cli/test/aws.test.ts
+++ b/cli/test/aws.test.ts
@@ -997,7 +997,7 @@ test("AWS portal ALB adopts pinned target groups and requires exactly the env-de
await run(
hostSplitConfig({ appsDomain: "*.apps.agent.acme.example" }),
undefined,
- /env\.core\.AWS_DEPLOY_APPS_DOMAIN .* does not derive a valid ALB host-header hostname/,
+ /env\.core\.DEPLOY_APPS_DOMAIN or AWS_DEPLOY_APPS_DOMAIN.* does not derive a valid ALB host-header hostname/,
);
await run(
hostSplitConfig(bothHosts),
diff --git a/docs/porter.md b/docs/porter.md
index 2073d7303..fb95bec40 100644
--- a/docs/porter.md
+++ b/docs/porter.md
@@ -106,8 +106,11 @@ domain you control (e.g. `apps.example.com`, with `*.apps.example.com` pointed a
instance) upgrades every app to its own origin — `https://<app>.apps.example.com/` — with
portal single sign-on, the request-access flow, and live editing; the portal derives its
cookie and returnTo settings from it, and boot probes the wildcard record and prints the
-exact DNS record to add when it is missing. The per-provider variables below remain as
-explicit overrides.
+exact DNS record to add when it is missing. Point the wildcard at the ingress that fronts
+core, never at Porter's sandbox ingress — the gate runs in core, so DNS that skips core
+skips the gate. `PORTER_DEPLOY_APPS_DOMAIN` is the separate, ungated mechanism: it
+registers each app's domain on Porter's own sandbox ingress and is deliberately NOT
+defaulted from `DEPLOY_APPS_DOMAIN`.
Published apps get their address from the cluster, not from qm. Declare the sandbox load
balancer with a root domain at cluster creation and **Porter names every published app
@@ -227,6 +230,10 @@ This is worth stating plainly because the failure it replaces was so confusing:
whose sandbox load balancer was attached _after_ creation, or whose root domain was never
delegated, has no usable zone for the DNS-01 challenge, so the certificate never issues and
the ingress serves nginx's self-signed _Kubernetes Ingress Controller Fake Certificate_.
+The remediation (confirmed with Porter support) is manual: create the dedicated hosted zone
+for the root domain yourself, add its `NS` delegation to the parent zone, move the wildcard
+record into it, then reconcile the cluster (the dashboard's infra **Update**, or re-POST the
+contract) so Porter actually creates the certificate issuer against the new zone.
Every client then fails certificate verification against an app that is otherwise serving
correctly — including the agent's own probe of the app it just published, which is why it
reports a gateway error it cannot explain. `kubectl get certificate -n porter-sandbox` tells
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index e7f9e19f7..9d02ae5d7 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -303,13 +303,37 @@ function hostOf(raw: string): string {
}
}
+function isMultiPartPublicSuffix(domain: string): boolean {
+ return [
+ "co.uk",
+ "org.uk",
+ "gov.uk",
+ "ac.uk",
+ "com.au",
+ "net.au",
+ "org.au",
+ "co.nz",
+ "co.jp",
+ "or.jp",
+ "ne.jp",
+ "com.br",
+ "com.mx",
+ "co.in",
+ "co.za",
+ "com.sg",
+ "com.hk",
+ "com.tw",
+ ].includes(domain);
+}
+
export function commonParentDomain(a: string, b: string): string | undefined {
const as = a.toLowerCase().split(".").filter(Boolean);
const bs = b.toLowerCase().split(".").filter(Boolean);
let n = 0;
while (n < as.length && n < bs.length && as[as.length - 1 - n] === bs[bs.length - 1 - n]) n++;
if (n < 2) return undefined;
- return as.slice(as.length - n).join(".");
+ const parent = as.slice(as.length - n).join(".");
+ return isMultiPartPublicSuffix(parent) ? undefined : parent;
}
export function hostIsWithinDomain(host: string, domain: string): boolean {
diff --git a/plugins/portal/src/proxy.ts b/plugins/portal/src/proxy.ts
index 89a9793c3..c7f73dc0a 100644
--- a/plugins/portal/src/proxy.ts
+++ b/plugins/portal/src/proxy.ts
@@ -4,7 +4,14 @@ import { mintPortalIdentity, PORTAL_IDENTITY_HEADER } from "../../chassis/src/po
const IDENTITY_TTL_MS = 60_000;
-const FORWARD_REQUEST_HEADERS = ["content-type", "accept", "accept-language", "user-agent", "accept-encoding"];
+const FORWARD_REQUEST_HEADERS = [
+ "content-type",
+ "accept",
+ "accept-language",
+ "user-agent",
+ "accept-encoding",
+ "sec-fetch-dest",
+];
const DROP_RESPONSE_HEADERS = new Set([
"connection",
diff --git a/plugins/portal/test/apps-domain-defaults.test.ts b/plugins/portal/test/apps-domain-defaults.test.ts
index 6bdaace83..ee7983608 100644
--- a/plugins/portal/test/apps-domain-defaults.test.ts
+++ b/plugins/portal/test/apps-domain-defaults.test.ts
@@ -34,6 +34,11 @@ test("commonParentDomain finds the deepest shared dot-suffix, never a bare TLD",
assert.equal(commonParentDomain("a.example.com", "b.other.net"), undefined);
assert.equal(commonParentDomain("a.example.com", "b.acme.com"), undefined, "a bare TLD is not a cookie domain");
assert.equal(commonParentDomain("", "apps.example.com"), undefined);
+ assert.equal(
+ commonParentDomain("portal.foo.co.uk", "apps.bar.co.uk"),
+ undefined,
+ "a shared public suffix like co.uk is not a cookie domain browsers accept",
+ );
});
test("DEPLOY_APPS_DOMAIN alone yields a working portal apps setup — cookie domain derived, boot clean", () => {
diff --git a/src/api/routes/deployments.ts b/src/api/routes/deployments.ts
index 50446884f..837584637 100644
--- a/src/api/routes/deployments.ts
+++ b/src/api/routes/deployments.ts
@@ -87,7 +87,7 @@ async function proxyDeployment(ctx: BaseCtx): Promise<void> {
}
}
const reach = await app.reachDeployment(id, principal);
- return proxyReach(ctx, reach, subPath, { sandboxHtml: true });
+ return proxyReach(ctx, reach, subPath, { sandbox: true });
}
function adminDeploymentProxyParts(pathname: string): { id: string; subPath: string } | null {
@@ -134,7 +134,7 @@ async function proxyAdminDeployment(ctx: BaseCtx): Promise<void> {
scopeLabel: deployment.ownerScopeId,
});
const reach = await app.reachDeployment(parts.id, "", { bypassAcl: true });
- return proxyReach(ctx, reach, parts.subPath, { sandboxHtml: true });
+ return proxyReach(ctx, reach, parts.subPath, { sandbox: true });
}
const GATEWAY_AUTH_HEADERS = [
@@ -192,7 +192,7 @@ const APP_SANDBOX_CSP = "sandbox allow-scripts allow-forms allow-popups allow-mo
function gatewaySafeResponseHeaders(
headers: Record<string, string | string[] | number | undefined>,
- sandboxHtml = false,
+ sandbox = false,
): Record<string, string | string[]> {
const normalized: Record<string, string | string[] | undefined> = {};
for (const [rawName, value] of Object.entries(headers)) {
@@ -208,7 +208,8 @@ function gatewaySafeResponseHeaders(
if (kept.length) out["set-cookie"] = kept;
else delete out["set-cookie"];
}
- if (sandboxHtml && String(out["content-type"] ?? "").includes("text/html")) {
+ delete out["clear-site-data"];
+ if (sandbox) {
const existing = out["content-security-policy"];
out["content-security-policy"] = existing
? [...(Array.isArray(existing) ? existing : [existing]), APP_SANDBOX_CSP]
@@ -279,7 +280,7 @@ function proxyReachHttp2(
subPath: string,
headers: Record<string, string | string[]>,
bufferedBody: Buffer | null,
- sandboxHtml: boolean,
+ sandbox: boolean,
): void {
const { req, res, deps, url, method } = ctx;
const { host, port, tls } = endpoint.endpoint;
@@ -384,7 +385,7 @@ function proxyReachHttp2(
markUpstreamUp(`${host}:${port}`);
armThrottleShield(`${host}:${port}`, Number(responseHeaders[":status"] ?? 0), up);
const status = Number(responseHeaders[":status"] ?? 502);
- const safeHeaders = gatewaySafeResponseHeaders(responseHeaders, sandboxHtml);
+ const safeHeaders = gatewaySafeResponseHeaders(responseHeaders, sandbox);
res.writeHead(status, safeHeaders);
up.pipe(res, { end: false });
});
@@ -474,7 +475,7 @@ async function proxyReach(
ctx: BaseCtx,
reach: Awaited<ReturnType<App["reachDeployment"]>>,
subPath: string,
- opts?: { sandboxHtml?: boolean },
+ opts?: { sandbox?: boolean },
): Promise<void> {
const { req, res, deps, url, method } = ctx;
if (res.destroyed) return;
@@ -520,7 +521,7 @@ async function proxyReach(
}
if (res.destroyed) return;
if (reach.endpoint.httpVersion === "2") {
- proxyReachHttp2(ctx, reach, subPath, headers, bufferedBody, opts?.sandboxHtml ?? false);
+ proxyReachHttp2(ctx, reach, subPath, headers, bufferedBody, opts?.sandbox ?? false);
return;
}
const htmlNav = wantsWarmingPage(req, method);
@@ -529,7 +530,7 @@ async function proxyReach(
markUpstreamUp(upstreamKey);
upRes.on("error", () => res.destroy());
armThrottleShield(upstreamKey, upRes.statusCode ?? 0, upRes);
- const headers = gatewaySafeResponseHeaders(upRes.headers, opts?.sandboxHtml ?? false);
+ const headers = gatewaySafeResponseHeaders(upRes.headers, opts?.sandbox ?? false);
res.writeHead(upRes.statusCode ?? 502, headers);
upRes.pipe(res);
});
diff --git a/src/config.ts b/src/config.ts
index 0b0d58704..2ee517c82 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -456,14 +456,16 @@ function deployAppsEnv(
env: NodeJS.ProcessEnv,
defaultLoginUrl: string | undefined,
): { deployAppsSessionSecret?: string; deployAppsLoginUrl?: string } {
- const secret = env.DEPLOY_APPS_SESSION_SECRET;
- const loginUrl = env.DEPLOY_APPS_LOGIN_URL ?? (secret ? defaultLoginUrl : undefined);
- if (loginUrl && !secret) {
+ const explicit = env.DEPLOY_APPS_SESSION_SECRET;
+ const shared = env.PORTAL_SESSION_SECRET;
+ const loginUrl = env.DEPLOY_APPS_LOGIN_URL ?? (explicit || shared ? defaultLoginUrl : undefined);
+ if (loginUrl && !explicit && !shared) {
throw new Error("DEPLOY_APPS_LOGIN_URL requires DEPLOY_APPS_SESSION_SECRET");
}
- if (secret && !loginUrl) {
+ if (explicit && !loginUrl) {
throw new Error("DEPLOY_APPS_SESSION_SECRET needs a sign-in address — set DEPLOY_APPS_LOGIN_URL or PUBLIC_WEB_URL");
}
+ const secret = explicit ?? (loginUrl ? shared : undefined);
if (!secret || !loginUrl) return {};
return { deployAppsSessionSecret: secret, deployAppsLoginUrl: loginUrl.replace(/\/$/, "") };
}
@@ -493,9 +495,18 @@ function sharedPlatformSuffixOf(domain: string): string | undefined {
return SHARED_PLATFORM_SUFFIXES.find((suffix) => host === suffix || host.endsWith(`.${suffix}`));
}
+const HOSTNAME_LABEL = "[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?";
+const HOSTNAME_RE = new RegExp(`^${HOSTNAME_LABEL}(?:\\.${HOSTNAME_LABEL})+$`);
+
function deployAppsDomainEnv(env: NodeJS.ProcessEnv, deployProvider: string): string | undefined {
- const canonical = env.DEPLOY_APPS_DOMAIN?.trim();
- if (canonical) {
+ const raw = env.DEPLOY_APPS_DOMAIN?.trim();
+ if (raw) {
+ const canonical = raw.toLowerCase().replace(/\.$/, "");
+ if (!HOSTNAME_RE.test(canonical)) {
+ throw new Error(
+ `DEPLOY_APPS_DOMAIN (${raw}) is not a plain DNS name — set it to a bare domain like apps.example.com (no scheme, port, path, or wildcard prefix).`,
+ );
+ }
const suffix = sharedPlatformSuffixOf(canonical);
if (suffix) {
throw new Error(
@@ -1145,13 +1156,12 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {
spritesSandbox: spritesSandboxEnv(env),
smolmachinesSandbox: smolmachinesSandboxEnv(env),
porterSandbox: porterSandboxEnv(env),
- porterDeploy: {
- ...porterDeployEnv(env),
- ...(deployAppsDomain && !env.PORTER_DEPLOY_APPS_DOMAIN ? { appsDomain: deployAppsDomain } : {}),
- },
+ porterDeploy: porterDeployEnv(env),
awsDeploy: {
...awsDeployEnv(env),
- ...(deployAppsDomain && !env.AWS_DEPLOY_APPS_DOMAIN ? { appsDomain: deployAppsDomain } : {}),
+ ...(env.DEPLOY_APPS_DOMAIN && deployAppsDomain && !env.AWS_DEPLOY_APPS_DOMAIN
+ ? { appsDomain: deployAppsDomain }
+ : {}),
},
...(deployAppsDomain ? { deployAppsDomain } : {}),
flyDeploy: flyDeployEnv(env),
diff --git a/src/deployment/secret-schema.ts b/src/deployment/secret-schema.ts
index 0f6781da7..4c18f9b7f 100644
--- a/src/deployment/secret-schema.ts
+++ b/src/deployment/secret-schema.ts
@@ -82,7 +82,10 @@ function isInvalidSecret(name: string, value: string | undefined): boolean {
const candidate = value?.trim();
if (!candidate || /^(replace-me|placeholder|changeme|todo)$/i.test(candidate)) return true;
return (
- (name === "CONNECTOR_SECRET_KEY" || name === "CORE_SIGNING_SECRET" || name === "SKILL_SIGNING_SECRET") &&
+ (name === "CONNECTOR_SECRET_KEY" ||
+ name === "CORE_SIGNING_SECRET" ||
+ name === "SKILL_SIGNING_SECRET" ||
+ name === "AWS_DEPLOY_GATE_SECRET") &&
!isStrongSigningSecret(candidate)
);
}
diff --git a/test/config.test.ts b/test/config.test.ts
index 77b2a350e..7445cca35 100644
--- a/test/config.test.ts
+++ b/test/config.test.ts
@@ -521,7 +521,11 @@ test("DEPLOY_APPS_DOMAIN is the one-var apps setup: it feeds the gate and defaul
AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
});
assert.equal(config.deployAppsDomain, "apps.example.com");
- assert.equal(config.porterDeploy.appsDomain, "apps.example.com");
+ assert.equal(
+ config.porterDeploy.appsDomain,
+ undefined,
+ "the gate domain must not be registered on Porter ingress — that would bypass the gate or loop the proxy",
+ );
assert.equal(config.awsDeploy.appsDomain, "apps.example.com");
const overridden = loadConfig({
DEPLOY_PROVIDER: "porter",
@@ -545,7 +549,8 @@ test("the active provider's own apps domain reaches the gate when DEPLOY_APPS_DO
PORTER_DEPLOY_APPS_DOMAIN: "apps.example.com",
});
assert.equal(porter.deployAppsDomain, "apps.example.com");
- assert.equal(porter.awsDeploy.appsDomain, "apps.example.com");
+ assert.equal(porter.awsDeploy.appsDomain, undefined);
+ assert.equal(porter.porterDeploy.appsDomain, "apps.example.com");
const aws = loadConfig({
AWS_DEPLOY_APPS_DOMAIN: "apps.example.com",
AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
@@ -564,6 +569,33 @@ test("DEPLOY_APPS_DOMAIN refuses shared platform domains that cannot carry per-a
);
});
+test("DEPLOY_APPS_DOMAIN must be a bare DNS name, normalized to lowercase without a trailing dot", () => {
+ const gate = { AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef" };
+ assert.equal(loadConfig({ DEPLOY_APPS_DOMAIN: "Apps.Example.COM.", ...gate }).deployAppsDomain, "apps.example.com");
+ assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "https://apps.example.com", ...gate }), /bare domain/);
+ assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "apps.example.com:[email protected]", ...gate }), /bare domain/);
+ assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "*.apps.example.com", ...gate }), /bare domain/);
+ assert.throws(() => loadConfig({ DEPLOY_APPS_DOMAIN: "myapp.fly.dev.", ...gate }), /shared platform domain/);
+});
+
+test("the portal session secret doubles as the deploy-apps viewer secret when a login URL exists", () => {
+ const derived = loadConfig({ PORTAL_SESSION_SECRET: "shared", PUBLIC_WEB_URL: "https://qm.example.com" });
+ assert.equal(derived.deployAppsSessionSecret, "shared");
+ assert.equal(derived.deployAppsLoginUrl, "https://qm.example.com");
+ const noUrl = loadConfig({ PORTAL_SESSION_SECRET: "shared" });
+ assert.equal(
+ noUrl.deployAppsSessionSecret,
+ undefined,
+ "no sign-in address means the fallback stays off, not a throw",
+ );
+ const explicit = loadConfig({
+ PORTAL_SESSION_SECRET: "shared",
+ DEPLOY_APPS_SESSION_SECRET: "own",
+ PUBLIC_WEB_URL: "https://qm.example.com",
+ });
+ assert.equal(explicit.deployAppsSessionSecret, "own");
+});
+
test("the deploy-apps sign-in address defaults to the public web URL", () => {
const derived = loadConfig({
DEPLOY_APPS_SESSION_SECRET: "s",
diff --git a/test/deploy-path-serving.test.ts b/test/deploy-path-serving.test.ts
index 39ce1aca6..e4b4af9c9 100644
--- a/test/deploy-path-serving.test.ts
+++ b/test/deploy-path-serving.test.ts
@@ -36,9 +36,10 @@ function httpGet(
async function fixture(
deps: Parameters<typeof createInsecureTestServer>[1],
upstreamContentType = "text/html; charset=utf-8",
+ upstreamHeaders: Record<string, string> = {},
) {
const upstream = createHttpServer((_req, res) => {
- res.writeHead(200, { "content-type": upstreamContentType });
+ res.writeHead(200, { "content-type": upstreamContentType, ...upstreamHeaders });
res.end("UPSTREAM OK");
});
upstream.listen(0);
@@ -92,12 +93,23 @@ test("/d/ path serving sandboxes proxied HTML so an app cannot act on the portal
}
});
-test("/d/ path serving leaves non-HTML responses unsandboxed", async () => {
- const f = await fixture({}, "application/json");
+test("/d/ path serving sandboxes every response — content-type spoofing cannot dodge it", async () => {
+ const f = await fixture({}, "Text/HTML; charset=utf-8");
try {
const data = await httpGet(f.port, "/d/mysite/api", { "x-as-principal": "[email protected]" });
assert.equal(data.status, 200);
- assert.equal(data.headers["content-security-policy"], undefined);
+ assert.match(String(data.headers["content-security-policy"]), /^sandbox /);
+ } finally {
+ await f.close();
+ }
+});
+
+test("/d/ path serving strips clear-site-data so an app cannot log its viewers out of the portal", async () => {
+ const f = await fixture({}, "text/html", { "clear-site-data": '"cookies", "storage"' });
+ try {
+ const page = await httpGet(f.port, "/d/mysite/", { "x-as-principal": "[email protected]" });
+ assert.equal(page.status, 200);
+ assert.equal(page.headers["clear-site-data"], undefined);
} finally {
await f.close();
}
diff --git a/test/secret-schema-drift.test.ts b/test/secret-schema-drift.test.ts
index 92a56fc91..b54fcc694 100644
--- a/test/secret-schema-drift.test.ts
+++ b/test/secret-schema-drift.test.ts
@@ -35,7 +35,15 @@ test("AWS deployment app domains reject a missing or placeholder gate secret", (
assert.deepEqual(
validateCoreSecretEnv({
AWS_DEPLOY_APPS_DOMAIN: "apps.example.com",
- AWS_DEPLOY_GATE_SECRET: "real-secret",
+ AWS_DEPLOY_GATE_SECRET: "short",
+ } as NodeJS.ProcessEnv),
+ ["AWS_DEPLOY_GATE_SECRET"],
+ "a guessable gate secret would let anyone forge owner tokens, so strength is enforced",
+ );
+ assert.deepEqual(
+ validateCoreSecretEnv({
+ AWS_DEPLOY_APPS_DOMAIN: "apps.example.com",
+ AWS_DEPLOY_GATE_SECRET: "0123456789abcdef0123456789abcdef",
} as NodeJS.ProcessEnv),
[],
);