Skip to content

Commit 6c82346

Browse files
committed
fix(config): validate actual backup before recovery
1 parent 10b7a33 commit 6c82346

5 files changed

Lines changed: 462 additions & 704 deletions

File tree

‎config/max-lines-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,6 @@ src/config/config-misc.test.ts
644644
src/config/config.plugin-validation.test.ts
645645
src/config/env-preserve.ts
646646
src/config/io.observe-recovery.test.ts
647-
src/config/io.observe-recovery.ts
648647
src/config/io.write-config.test.ts
649648
src/config/io.write-prepare.test.ts
650649
src/config/io.write-prepare.ts

‎src/config/io.observe-recovery.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,92 @@ describe("config observe recovery", () => {
727727
});
728728
});
729729

730+
it.each(["async", "sync"] as const)(
731+
"%s recovery refuses a backup without gateway mode despite a stale healthy fingerprint",
732+
async (mode) => {
733+
await withSuiteHome(async (home) => {
734+
const { deps, configPath, auditPath } = makeDeps(home);
735+
const snapshot = await makeSnapshot(configPath, recoverableTelegramConfig);
736+
await promoteConfigSnapshotToLastKnownGood({ deps, snapshot, logger: deps.logger });
737+
await fsp.writeFile(
738+
`${configPath}.bak`,
739+
`${JSON.stringify({ meta: { lastTouchedVersion: "2026.4.22" } })}\n`,
740+
"utf-8",
741+
);
742+
const clobbered = await writeClobberedUpdateChannel(configPath);
743+
const input = { deps, configPath, ...clobbered };
744+
745+
const recovered =
746+
mode === "async"
747+
? await maybeRecoverSuspiciousConfigRead(input)
748+
: maybeRecoverSuspiciousConfigReadSync(input);
749+
750+
expect(recovered).toEqual(clobbered);
751+
await expect(fsp.readFile(configPath, "utf-8")).resolves.toBe(clobbered.raw);
752+
await expect(readObserveEvents(auditPath)).resolves.toEqual([]);
753+
});
754+
},
755+
);
756+
757+
it.each(["async", "sync"] as const)(
758+
"%s recovery uses canonical metadata fingerprints",
759+
async (mode) => {
760+
await withSuiteHome(async (home) => {
761+
const { deps, configPath } = makeDeps(home);
762+
const backup = { meta: { authoredBy: "operator" }, gateway: { mode: "local" } };
763+
await seedConfigBackup(configPath, backup);
764+
const clobbered = await writeConfigRaw(configPath, { gateway: { mode: "local" } });
765+
const input = { deps, configPath, ...clobbered };
766+
767+
const recovered =
768+
mode === "async"
769+
? await maybeRecoverSuspiciousConfigRead(input)
770+
: maybeRecoverSuspiciousConfigReadSync(input);
771+
772+
expect(recovered.parsed).toEqual(backup);
773+
});
774+
},
775+
);
776+
777+
it.each(["async", "sync"] as const)(
778+
"%s recovery tolerates an unreadable current config stat",
779+
async (mode) => {
780+
await withSuiteHome(async (home) => {
781+
const { deps, configPath } = makeDeps(home);
782+
await seedConfigBackup(configPath, recoverableTelegramConfig);
783+
const clobbered = await writeClobberedUpdateChannel(configPath);
784+
const statError = Object.assign(new Error("EACCES: stat denied"), { code: "EACCES" });
785+
const statDeps: ObserveRecoveryDeps = {
786+
...deps,
787+
fs: {
788+
...deps.fs,
789+
promises: {
790+
...deps.fs.promises,
791+
stat: ((target: fs.PathLike) =>
792+
target === configPath
793+
? Promise.reject(statError)
794+
: deps.fs.promises.stat(target)) as typeof fs.promises.stat,
795+
},
796+
statSync: ((target: fs.PathLike, options?: { throwIfNoEntry?: boolean }) => {
797+
if (target === configPath) {
798+
throw statError;
799+
}
800+
return deps.fs.statSync(target, options);
801+
}) as typeof fs.statSync,
802+
},
803+
};
804+
const input = { deps: statDeps, configPath, ...clobbered };
805+
806+
const recovered =
807+
mode === "async"
808+
? await maybeRecoverSuspiciousConfigRead(input)
809+
: maybeRecoverSuspiciousConfigReadSync(input);
810+
811+
expect((recovered.parsed as { gateway?: { mode?: string } }).gateway?.mode).toBe("local");
812+
});
813+
},
814+
);
815+
730816
it.each([
731817
{
732818
name: "records writeFile failure instead of falsely claiming restore succeeded",

0 commit comments

Comments
 (0)