Skip to content

Commit df4920a

Browse files
committed
fix(auth): verify shared credential migration receipts
Attach expected credential fingerprints to both credential-store receipt variants so interrupted shared-store imports use the existing verification and archive recovery flow. State-only receipts retain their own verification. Regression coverage interrupts archival and removes the destination before retrying both legacy auth file formats against both SQLite destinations. The shared-store cases fail before this change; all 90 focused tests pass. Related to #134608, reported by @erameson. This preserves the existing non-replay policy for completed receipts; recovery of already-completed receipts requires a separate maintainer decision.
1 parent 113805d commit df4920a

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎src/commands/doctor-auth-flat-profiles.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ export async function maybeMigrateAuthProfileJsonStoresToSqlite(params: {
12841284
? hasAuthProfileState(coerceAuthProfileState(canonicalStore))
12851285
: false;
12861286
for (const receipt of sourceReceipts) {
1287-
if (receipt.targetTable === "auth_profile_store") {
1287+
if (receipt.targetTable !== "auth_profile_state") {
12881288
receipt.expectedProfileSha256 = expectedProfileSha256;
12891289
}
12901290
if (
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import fs from "node:fs";
2+
import { afterEach, expect, it, vi } from "vitest";
3+
import { loadPersistedSharedAuthProfileStore } from "../agents/auth-profiles/persisted.js";
4+
import { deletePersistedAuthProfileStoreRaw } from "../agents/auth-profiles/sqlite.js";
5+
import { writeConfigMachineState } from "../state/config-machine-state.js";
6+
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
7+
import {
8+
closeOpenClawStateDatabaseForTest,
9+
openOpenClawStateDatabase,
10+
} from "../state/openclaw-state-db.js";
11+
import {
12+
createOpenClawTestState,
13+
type OpenClawTestState,
14+
} from "../test-utils/openclaw-test-state.js";
15+
import { maybeMigrateAuthProfileJsonStoresToSqlite } from "./doctor-auth-flat-profiles.js";
16+
17+
let state: OpenClawTestState;
18+
19+
afterEach(async () => {
20+
vi.restoreAllMocks();
21+
closeOpenClawAgentDatabasesForTest();
22+
closeOpenClawStateDatabaseForTest();
23+
await state?.cleanup();
24+
});
25+
26+
it.each([
27+
["legacy-main", "auth-profiles.json"],
28+
["legacy-main", "auth.json"],
29+
["state-db", "auth-profiles.json"],
30+
["state-db", "auth.json"],
31+
] as const)("recovers %s credentials after interrupted %s archival", async (location, filename) => {
32+
state = await createOpenClawTestState({ layout: "state-only" });
33+
if (location === "state-db") {
34+
writeConfigMachineState("auth.sharedStore", { location }, { env: state.env });
35+
}
36+
const credential = { type: "api_key", provider: "openai", key: "synthetic-migration-key" };
37+
const source = await state.writeJson(
38+
`agents/main/agent/${filename}`,
39+
filename === "auth-profiles.json"
40+
? { version: 1, profiles: { "openai:default": credential } }
41+
: { openai: credential },
42+
);
43+
const sourceBytes = fs.readFileSync(source);
44+
const migrate = () =>
45+
maybeMigrateAuthProfileJsonStoresToSqlite({
46+
cfg: {},
47+
prompter: { confirmAutoFix: async () => true },
48+
env: state.env,
49+
});
50+
const rename = fs.renameSync;
51+
const interruptedRename = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
52+
rename(from, to);
53+
if (String(from) === source) {
54+
throw new Error("simulated interruption after archive rename");
55+
}
56+
});
57+
const interrupted = await migrate();
58+
interruptedRename.mockRestore();
59+
expect(interrupted.warnings).toEqual([
60+
expect.stringContaining("simulated interruption after archive rename"),
61+
]);
62+
expect(fs.existsSync(source)).toBe(false);
63+
const db = openOpenClawStateDatabase({ env: state.env }).db;
64+
expect(
65+
db.prepare("SELECT status FROM migration_sources WHERE source_path = ?").get(source),
66+
).toEqual({ status: "imported" });
67+
68+
// Model a lost destination after the import commit but before terminal completion.
69+
deletePersistedAuthProfileStoreRaw(location === "legacy-main" ? state.agentDir() : undefined);
70+
expect(loadPersistedSharedAuthProfileStore(state.env)?.profiles ?? {}).toEqual({});
71+
const repaired = await migrate();
72+
73+
expect(repaired.warnings).toEqual([]);
74+
expect(loadPersistedSharedAuthProfileStore(state.env)?.profiles).toEqual({
75+
"openai:default": credential,
76+
});
77+
expect(repaired.changes).toContain("Reset an interrupted auth migration receipt for retry.");
78+
const receipt = db
79+
.prepare("SELECT status, report_json FROM migration_sources WHERE source_path = ?")
80+
.get(source) as { status: string; report_json: string };
81+
expect(receipt.status).toBe("completed");
82+
expect(fs.readFileSync(JSON.parse(receipt.report_json).archivePath)).toEqual(sourceBytes);
83+
expect((await migrate()).changes).toEqual([]);
84+
});

0 commit comments

Comments
 (0)