fix: block invalid config startup
Co-authored-by: Muhammed Mukhthar CM <mukhtharcm@gmail.com>
This commit is contained in:
36
src/config/config.backup-rotation.test.ts
Normal file
36
src/config/config.backup-rotation.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { withTempHome } from "./test-helpers.js";
|
||||
import type { ClawdbotConfig } from "./types.js";
|
||||
|
||||
describe("config backup rotation", () => {
|
||||
it("keeps a 5-deep backup ring for config writes", async () => {
|
||||
await withTempHome(async () => {
|
||||
const { resolveConfigPath, writeConfigFile } = await import("./config.js");
|
||||
const configPath = resolveConfigPath();
|
||||
const buildConfig = (version: number): ClawdbotConfig =>
|
||||
({
|
||||
identity: { name: `v${version}` },
|
||||
}) as ClawdbotConfig;
|
||||
|
||||
for (let version = 0; version <= 6; version += 1) {
|
||||
await writeConfigFile(buildConfig(version));
|
||||
}
|
||||
|
||||
const readName = async (suffix = "") => {
|
||||
const raw = await fs.readFile(`${configPath}${suffix}`, "utf-8");
|
||||
return (JSON.parse(raw) as { identity?: { name?: string } }).identity?.name ?? null;
|
||||
};
|
||||
|
||||
await expect(readName()).resolves.toBe("v6");
|
||||
await expect(readName(".bak")).resolves.toBe("v5");
|
||||
await expect(readName(".bak.1")).resolves.toBe("v4");
|
||||
await expect(readName(".bak.2")).resolves.toBe("v3");
|
||||
await expect(readName(".bak.3")).resolves.toBe("v2");
|
||||
await expect(readName(".bak.4")).resolves.toBe("v1");
|
||||
await expect(fs.stat(`${configPath}.bak.5`)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -52,6 +52,8 @@ const SHELL_ENV_EXPECTED_KEYS = [
|
||||
"CLAWDBOT_GATEWAY_PASSWORD",
|
||||
];
|
||||
|
||||
const CONFIG_BACKUP_COUNT = 5;
|
||||
|
||||
export type ParseConfigJson5Result = { ok: true; parsed: unknown } | { ok: false; error: string };
|
||||
|
||||
function hashConfigRaw(raw: string | null): string {
|
||||
@@ -73,6 +75,53 @@ export function resolveConfigSnapshotHash(snapshot: {
|
||||
return hashConfigRaw(snapshot.raw);
|
||||
}
|
||||
|
||||
function coerceConfig(value: unknown): ClawdbotConfig {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return {};
|
||||
}
|
||||
return value as ClawdbotConfig;
|
||||
}
|
||||
|
||||
function rotateConfigBackupsSync(configPath: string, ioFs: typeof fs): void {
|
||||
if (CONFIG_BACKUP_COUNT <= 1) return;
|
||||
const backupBase = `${configPath}.bak`;
|
||||
const maxIndex = CONFIG_BACKUP_COUNT - 1;
|
||||
try {
|
||||
ioFs.unlinkSync(`${backupBase}.${maxIndex}`);
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
for (let index = maxIndex - 1; index >= 1; index -= 1) {
|
||||
try {
|
||||
ioFs.renameSync(`${backupBase}.${index}`, `${backupBase}.${index + 1}`);
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
try {
|
||||
ioFs.renameSync(backupBase, `${backupBase}.1`);
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
|
||||
async function rotateConfigBackups(configPath: string, ioFs: typeof fs.promises): Promise<void> {
|
||||
if (CONFIG_BACKUP_COUNT <= 1) return;
|
||||
const backupBase = `${configPath}.bak`;
|
||||
const maxIndex = CONFIG_BACKUP_COUNT - 1;
|
||||
await ioFs.unlink(`${backupBase}.${maxIndex}`).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
for (let index = maxIndex - 1; index >= 1; index -= 1) {
|
||||
await ioFs.rename(`${backupBase}.${index}`, `${backupBase}.${index + 1}`).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
}
|
||||
await ioFs.rename(backupBase, `${backupBase}.1`).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
}
|
||||
|
||||
export type ConfigIoDeps = {
|
||||
fs?: typeof fs;
|
||||
json5?: typeof JSON5;
|
||||
@@ -165,10 +214,13 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
|
||||
deps.fs.writeFileSync(tmp, json, { encoding: "utf-8", mode: 0o600 });
|
||||
|
||||
try {
|
||||
deps.fs.copyFileSync(configPath, `${configPath}.bak`);
|
||||
} catch {
|
||||
// best-effort
|
||||
if (deps.fs.existsSync(configPath)) {
|
||||
rotateConfigBackupsSync(configPath, deps.fs);
|
||||
try {
|
||||
deps.fs.copyFileSync(configPath, `${configPath}.bak`);
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -344,7 +396,7 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
raw,
|
||||
parsed: parsedRes.parsed,
|
||||
valid: false,
|
||||
config: {},
|
||||
config: coerceConfig(parsedRes.parsed),
|
||||
hash,
|
||||
issues: [{ path: "", message }],
|
||||
legacyIssues: [],
|
||||
@@ -366,7 +418,7 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
raw,
|
||||
parsed: parsedRes.parsed,
|
||||
valid: false,
|
||||
config: {},
|
||||
config: coerceConfig(resolved),
|
||||
hash,
|
||||
issues: [{ path: "", message }],
|
||||
legacyIssues: [],
|
||||
@@ -379,17 +431,13 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
|
||||
const validated = validateConfigObject(resolvedConfigRaw);
|
||||
if (!validated.ok) {
|
||||
const resolvedConfig =
|
||||
typeof resolvedConfigRaw === "object" && resolvedConfigRaw !== null
|
||||
? (resolvedConfigRaw as ClawdbotConfig)
|
||||
: {};
|
||||
return {
|
||||
path: configPath,
|
||||
exists: true,
|
||||
raw,
|
||||
parsed: parsedRes.parsed,
|
||||
valid: false,
|
||||
config: resolvedConfig,
|
||||
config: coerceConfig(resolvedConfigRaw),
|
||||
hash,
|
||||
issues: validated.issues,
|
||||
legacyIssues,
|
||||
@@ -450,9 +498,12 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
mode: 0o600,
|
||||
});
|
||||
|
||||
await deps.fs.promises.copyFile(configPath, `${configPath}.bak`).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
if (deps.fs.existsSync(configPath)) {
|
||||
await rotateConfigBackups(configPath, deps.fs.promises);
|
||||
await deps.fs.promises.copyFile(configPath, `${configPath}.bak`).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await deps.fs.promises.rename(tmp, configPath);
|
||||
|
||||
Reference in New Issue
Block a user