diff --git a/src/bootstrap/load-runtime-config.ts b/src/bootstrap/load-runtime-config.ts index 5cd5f40f..9cfc3a36 100644 --- a/src/bootstrap/load-runtime-config.ts +++ b/src/bootstrap/load-runtime-config.ts @@ -116,16 +116,18 @@ export async function loadRuntimeConfig( const validated = validateRuntimeConfig(rawConfig); if (!validated.success) { + // Q-6.9-10. Only a value that passed a check reaches the safe error. The + // declared schema version did when it selected a parser; nothing else in + // a rejected document did (RELEASE_ID could be 64 KB of anything). + const versionAccepted = !validated.issues.some( + (issue) => issue.path === "CONFIG_SCHEMA_VERSION", + ); throw new BootConfigError("CONFIG_SCHEMA_INVALID", { buildId: buildConfig.buildId, configSchemaVersion: - typeof rawConfig.CONFIG_SCHEMA_VERSION === "string" + versionAccepted && typeof rawConfig.CONFIG_SCHEMA_VERSION === "string" ? rawConfig.CONFIG_SCHEMA_VERSION : undefined, - releaseId: - typeof rawConfig.RELEASE_ID === "string" - ? rawConfig.RELEASE_ID - : undefined, }); } diff --git a/tests/runtime-schema/runtime-config.test.ts b/tests/runtime-schema/runtime-config.test.ts index c277ace9..b7b71af0 100644 --- a/tests/runtime-schema/runtime-config.test.ts +++ b/tests/runtime-schema/runtime-config.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it } from "vitest"; -import { loadRuntimeConfig } from "../../src/bootstrap/load-runtime-config.ts"; +import { + BootConfigError, + loadRuntimeConfig, +} from "../../src/bootstrap/load-runtime-config.ts"; import { validateRuntimeConfig } from "../../src/bootstrap/runtime-config-schema.ts"; import { assertSafeConfigNames } from "../../src/contracts/env.ts"; @@ -228,6 +231,43 @@ describe("runtime configuration boundary", () => { expect(result.validationDurationMs).toBe(0); }); + it.each([ + [ + "an invalid field", + { ...validConfig, REQUEST_TIMEOUT_MS: -1, RELEASE_ID: "r".repeat(10_000) }, + { configSchemaVersion: "2.0" }, + ], + [ + "an unsupported version", + { ...validConfig, CONFIG_SCHEMA_VERSION: "v".repeat(10_000), RELEASE_ID: "release-a" }, + {}, + ], + ])( + "puts only validated config values into the safe error for %s (Q-6.9-10)", + async (_case, document, expected) => { + const failure = await loadRuntimeConfig({ + buildConfig: { + buildId: "build-a", + commitSha: "local", + routerBasePath: "/", + runtimeConfigUrl: "/config.json", + }, + fetcher: async () => + new Response(JSON.stringify(document), { + headers: { "content-type": "application/json" }, + }), + }).catch((error: unknown) => error); + + expect(failure).toBeInstanceOf(BootConfigError); + const safe = (failure as BootConfigError).safe; + expect(safe.code).toBe("CONFIG_SCHEMA_INVALID"); + expect({ + configSchemaVersion: safe.configSchemaVersion, + releaseId: safe.releaseId, + }).toEqual({ configSchemaVersion: undefined, releaseId: undefined, ...expected }); + }, + ); + it("returns only safe boot fields on failure", async () => { await expect( loadRuntimeConfig({