diff --git a/src/billing.ts b/src/billing.ts index 66377b0032..508c10d97b 100644 --- a/src/billing.ts +++ b/src/billing.ts @@ -56,8 +56,8 @@ export function parseBillingSettings(value: unknown): BillingSettings | undefine if (value === undefined || value === null) return undefined; if (typeof value !== "object" || Array.isArray(value)) return undefined; const root = value as Record; + for (const k of Object.keys(root)) if (k !== "prices") return undefined; if (root["prices"] === undefined) { - for (const k of Object.keys(root)) if (k !== "prices") return undefined; return { prices: {} }; } if (root["prices"] === null || typeof root["prices"] !== "object" || Array.isArray(root["prices"])) return undefined; diff --git a/tests/billing-prices.test.ts b/tests/billing-prices.test.ts index a66aaba3a0..7d276fa2a9 100644 --- a/tests/billing-prices.test.ts +++ b/tests/billing-prices.test.ts @@ -79,6 +79,13 @@ test("#4899 parseBillingSettings: rejects every invalid shape loudly", () => { assert.equal(parseBillingSettings({ prices: { m: { input: "3" } } }), undefined, "string number"); }); +test("#7263 parseBillingSettings: rejects unknown root fields regardless of prices", () => { + for (const prices of [undefined, {}, { "claude-bill": { input: 3, cacheRead: 0.3 } }]) { + assert.equal(parseBillingSettings({ prices, cacheRead: 0.3 }), undefined, "misplaced price field"); + assert.equal(parseBillingSettings({ prices, price: {} }), undefined, "misspelled prices field"); + } +}); + // --------------------------------------------------------------------------- // resolveBillingPrice — overrides first (same name matching as the registry), // registry second, nothing third. Conservative fallbacks: a spend estimate diff --git a/tests/billing-web.test.ts b/tests/billing-web.test.ts index 3c9a05a01b..1d6226e66b 100644 --- a/tests/billing-web.test.ts +++ b/tests/billing-web.test.ts @@ -197,6 +197,40 @@ test("null billing prices: structured and whole-file saves reject without mutati } }); +test("#7263 unknown billing root field: load warns without rewriting config", (t) => { + resetState(); + const raw = JSON.stringify({ port: 9876, billing: { prices: { "claude-bill": { input: 6 } }, cacheRead: 0.3 } }); + const file = withRawConfig(t, raw); + setBillingOverrides({ "claude-bill": { input: 9 } }); + const warnings: string[] = []; + setLogCapture((level, message) => { if (level === "warn") warnings.push(message); }); + t.after(() => setLogCapture(null)); + + const options = loadOptions({}); + assert.equal(options.port, 9876, "other config remains available"); + assert.ok(warnings.some((message) => message.includes('ignoring invalid "billing" block'))); + assert.equal(resolveBillingPrice("claude-bill")?.source, "registry", "invalid block cannot apply or retain overrides"); + assert.equal(readFileSync(file, "utf8"), raw, "load preserves the complete invalid config file"); +}); + +test("#7263 unknown billing root field: both save paths preserve config and active prices", async (t) => { + resetState(); + const raw = JSON.stringify({ billing: { prices: { "claude-bill": { input: 9 } } } }); + const file = withRawConfig(t, raw); + loadOptions({}); + + for (const prices of [{}, { "claude-bill": { input: 6 } }]) { + const billing = { prices, cacheRead: 0.3 }; + for (const body of [{ billing }, { file: JSON.stringify({ billing }) }]) { + const result = await putConfig(body); + assert.equal(result.status, 400); + assert.match(String(result.payload.error), /invalid billing settings/); + assert.equal(readFileSync(file, "utf8"), raw, "rejection preserves the complete config file"); + assert.equal(resolveBillingPrice("claude-bill")?.input, 9, "rejection preserves active overrides"); + } + } +}); + test("#4899 web summaries: sessions carry costUsd/savedUsd/model; overview totals aggregate USD", async () => { resetState(); const priced = getSession("web-money-1", { protocol: "anthropic", upstreamOrigin: "http://api.somehost.com" });