From 280bd4ffdb1c20ee439f71b9715e7a723a0047fa Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 06:50:38 +0800 Subject: [PATCH 1/3] fix: reject invalid global cacheKeepalive block at config load (#4200) --- src/config.ts | 12 ++ ...0-global-cachekeepalive-validation.test.ts | 122 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 tests/issue4200-global-cachekeepalive-validation.test.ts diff --git a/src/config.ts b/src/config.ts index cf008299e1..3127f31e1b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2255,6 +2255,18 @@ export function loadConfigFile(): FileConfig { warnInertResignPassthrough(obj); warnInertStripImages(obj); warnInertRemovedCompressKeys(obj); + // #4200: the global cacheKeepalive block gets the same strict #1909 + // validation as route-level / per-model entries (parseRouteEntry) — + // previously it was only KNOWN_TOP_LEVEL_KEYS-listed, so a typo'd + // block loaded clean at root while the identical typo under + // providers. rejected, and resolveCacheKeepalive silently + // dropped the bad fields to defaults. Validation only: well-formed + // blocks pass through as-is. null means "absent" (the web save + // contract, src/web/api.ts) and is skipped; anything else invalid + // throws into the catch below — loud error naming the field, file + // treated as unreadable, exactly like a malformed file. + const globalKeepalive = obj.cacheKeepalive; + if (globalKeepalive !== undefined && globalKeepalive !== null) parseCacheKeepaliveSettings(globalKeepalive); value = obj as FileConfig; } else { value = {}; diff --git a/tests/issue4200-global-cachekeepalive-validation.test.ts b/tests/issue4200-global-cachekeepalive-validation.test.ts new file mode 100644 index 0000000000..6d30d72edb --- /dev/null +++ b/tests/issue4200-global-cachekeepalive-validation.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import test from "node:test"; +import type { IncomingMessage, ServerResponse } from "node:http"; +import { Readable } from "node:stream"; + +process.env.NODE_ENV = "test"; + +import { loadConfigFile, parseRouteEntry } from "../src/config.ts"; +import { resolveCacheKeepalive } from "../src/cache-keepalive.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { handleConfigPut } from "../src/web/api.js"; + +// #4200: the GLOBAL cacheKeepalive block at the config root was only listed in +// KNOWN_TOP_LEVEL_KEYS — loadConfigFile never ran the strict #1909 parser on +// it, so a typo'd block loaded clean at root while the identical typo under +// providers. rejected, and resolveCacheKeepalive silently dropped the bad +// fields to defaults. These pin the fix: an invalid global block fails loudly +// at load (error naming the field + file treated as unreadable, like a +// malformed file), a valid one passes through as-is, and null stays "absent". + +const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-cfg-keepalive-")); +const savedConfigEnv = process.env.BILI_CONFIG_FILE; +const KEEPALIVE_ENV_KEYS = [ + "BILI_CACHE_KEEPALIVE", + "BILI_CACHE_KEEPALIVE_TTL_FACTOR", + "BILI_CACHE_KEEPALIVE_MAX_DURATION_MS", + "BILI_CACHE_KEEPALIVE_MAX_PINGS", + "BILI_CACHE_KEEPALIVE_ASSUME_TTL", + "BILI_CACHE_KEEPALIVE_ARM_WITHOUT_BREAKPOINTS", + "BILI_CACHE_KEEPALIVE_INTERVAL_MS", +]; +const savedKeepaliveEnv: Record = {}; + +test.before(() => { + for (const k of KEEPALIVE_ENV_KEYS) { savedKeepaliveEnv[k] = process.env[k]; delete process.env[k]; } +}); + +test.after(() => { + setLogCapture(null); + if (savedConfigEnv === undefined) delete process.env.BILI_CONFIG_FILE; + else process.env.BILI_CONFIG_FILE = savedConfigEnv; + for (const k of KEEPALIVE_ENV_KEYS) { + if (savedKeepaliveEnv[k] === undefined) delete process.env[k]; + else process.env[k] = savedKeepaliveEnv[k]; + } + fs.rmSync(root, { recursive: true, force: true }); +}); + +function pointAt(name: string): string { + const f = path.join(root, name); + process.env.BILI_CONFIG_FILE = f; + return f; +} + +test("#4200: typo'd global cacheKeepalive fails loudly at load (field named, file unreadable)", () => { + const f = pointAt("bad-global.json"); + const seen: string[] = []; + setLogCapture((_level, msg) => { if (msg.includes("failed to parse")) seen.push(msg); }); + try { + // The exact repro from the issue: the value that rejects route-level. + fs.writeFileSync(f, JSON.stringify({ port: 9001, cacheKeepalive: { ttlFactor: "high" } })); + for (let i = 0; i < 5; i++) assert.deepEqual(loadConfigFile(), {}); + assert.equal(seen.length, 1, `first corruption logs once across 5 calls (got ${seen.length})`); + assert.match(seen[0], /failed to parse .*cacheKeepalive\.ttlFactor must be a number in \(0, 1\]/); + // Fixing the file clears the failure without further logging. + fs.writeFileSync(f, JSON.stringify({ port: 9002 })); + assert.equal(loadConfigFile().port, 9002); + loadConfigFile(); + assert.equal(seen.length, 1, "valid rewrite logs nothing more"); + } finally { + setLogCapture(null); + } +}); + +test("#4200: unknown key in the global block is rejected too", () => { + const f = pointAt("unknown-key.json"); + const seen: string[] = []; + setLogCapture((_level, msg) => { seen.push(msg); }); + try { + fs.writeFileSync(f, JSON.stringify({ cacheKeepalive: { bogusKey: 1 } })); + assert.deepEqual(loadConfigFile(), {}); + const failed = seen.find((m) => m.includes("failed to parse")); + assert.ok(failed, "one failed-to-parse line"); + assert.match(failed, /unknown key "bogusKey"/); + } finally { + setLogCapture(null); + } +}); + +test("#4200: the identical typo still rejects at route level (symmetry pinned)", () => { + assert.throws(() => parseRouteEntry({ cacheKeepalive: { ttlFactor: "high" } }), /cacheKeepalive\.ttlFactor/); +}); + +test("#4200: a valid global block passes through as-is and reaches the resolver", () => { + const f = pointAt("valid-global.json"); + fs.writeFileSync(f, JSON.stringify({ port: 9003, cacheKeepalive: { enabled: true, ttlFactor: 0.5, assumeTtl: "1h" } })); + const cfg = loadConfigFile(); + assert.equal(cfg.port, 9003); + assert.deepEqual(cfg.cacheKeepalive, { enabled: true, ttlFactor: 0.5, assumeTtl: "1h" }, "raw object preserved verbatim"); + const r = resolveCacheKeepalive(undefined, undefined); + assert.equal(r.enabled, true); + assert.equal(r.ttlFactor, 0.5); + assert.equal(r.assumeTtl, "1h"); +}); + +test("#4200: null global block means absent (web save contract) — loads clean", () => { + const f = pointAt("null-global.json"); + const seen: string[] = []; + setLogCapture((_level, msg) => { seen.push(msg); }); + try { + fs.writeFileSync(f, JSON.stringify({ port: 9004, cacheKeepalive: null })); + const cfg = loadConfigFile(); + assert.equal(cfg.port, 9004); + assert.equal(cfg.cacheKeepalive, null); + assert.equal(seen.filter((m) => m.includes("failed to parse")).length, 0, "null block logs nothing"); + } finally { + setLogCapture(null); + } +}); From 6cce12f24a5cfe561e0d608b43dc5c183fc2b0fb Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 06:51:39 +0800 Subject: [PATCH 2/3] fix: return 400 instead of unhandled rejection for invalid global cacheKeepalive in web whole-file save (#4200 follow-up) --- src/web/api.ts | 11 +++++- ...0-global-cachekeepalive-validation.test.ts | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/web/api.ts b/src/web/api.ts index c815163fe8..b5c74b2cb3 100644 --- a/src/web/api.ts +++ b/src/web/api.ts @@ -234,7 +234,16 @@ export async function handleConfigPut( } if (next.upstreamProxyMode !== undefined && (typeof next.upstreamProxyMode !== "string" || !["auto", "manual", "direct"].includes(next.upstreamProxyMode))) return sendError(res, 400, "upstreamProxyMode must be auto, manual, or direct"); if (next.compress !== undefined && next.compress !== null && parseCompressSettings(next.compress) === undefined) return sendError(res, 400, "invalid compress settings"); - if (next.cacheKeepalive !== undefined && next.cacheKeepalive !== null && parseCacheKeepaliveSettings(next.cacheKeepalive) === undefined) return sendError(res, 400, "invalid cacheKeepalive settings"); + if (next.cacheKeepalive !== undefined && next.cacheKeepalive !== null) { + // #4200 follow-up: the strict parser THROWS on invalid values — catch it so a bad + // whole-file save gets the same clean 400 as every other field instead of an + // unhandled rejection that leaves the client's PUT with no response at all. + try { + if (parseCacheKeepaliveSettings(next.cacheKeepalive) === undefined) return sendError(res, 400, "invalid cacheKeepalive settings"); + } catch (error) { + return sendError(res, 400, String(error)); + } + } // The chain references named recipes — validate the expansion against // the providers table BEING SAVED (not the on-disk one), so a save // cannot strand the config on a dangling reference. Only enabled diff --git a/tests/issue4200-global-cachekeepalive-validation.test.ts b/tests/issue4200-global-cachekeepalive-validation.test.ts index 6d30d72edb..79cd56005d 100644 --- a/tests/issue4200-global-cachekeepalive-validation.test.ts +++ b/tests/issue4200-global-cachekeepalive-validation.test.ts @@ -120,3 +120,37 @@ test("#4200: null global block means absent (web save contract) — loads clean" setLogCapture(null); } }); + +// ---- web whole-file save facet (#4200 follow-up) --------------------------- + +async function putWholeFile(rawText: string): Promise<{ status: number; payload: Record }> { + const req = new Readable({ read() {} }); + req.push(Buffer.from(JSON.stringify({ file: rawText }))); + req.push(null); + let status = 0; + let payloadText = ""; + const res = { + writeHead(s: number) { status = s; return this; }, + end(b?: string | Buffer) { payloadText = typeof b === "string" ? b : String(b ?? ""); }, + }; + await handleConfigPut(req as unknown as IncomingMessage, res as unknown as ServerResponse, undefined, 8787); + return { status, payload: JSON.parse(payloadText) as Record }; +} + +test("#4200 web: invalid global block in a whole-file save gets a clean 400 naming the field", async () => { + const f = pointAt("web-bad-global.json"); + fs.writeFileSync(f, "{}"); + const r = await putWholeFile(JSON.stringify({ cacheKeepalive: { ttlFactor: "high" } })); + assert.equal(r.status, 400); + assert.match(String(r.payload.error), /cacheKeepalive\.ttlFactor must be a number/); + assert.equal(fs.readFileSync(f, "utf8"), "{}", "the file is never written"); +}); + +test("#4200 web: valid global block still saves (no over-strictening)", async () => { + const f = pointAt("web-valid-global.json"); + fs.writeFileSync(f, "{}"); + const r = await putWholeFile(JSON.stringify({ cacheKeepalive: { enabled: true, ttlFactor: 0.5 } })); + assert.equal(r.status, 200); + const saved = JSON.parse(fs.readFileSync(f, "utf8")) as Record; + assert.deepEqual(saved.cacheKeepalive, { enabled: true, ttlFactor: 0.5 }); +}); From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 3/3] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": {