Skip to content

Commit fe6fa89

Browse files
authored
fix(whatsapp): clear and migrate every Baileys credential class (#118610)
* fix(whatsapp): clear and migrate every auth credential class * test(whatsapp): await nullable legacy migration detector
1 parent 0dbf9b6 commit fe6fa89

5 files changed

Lines changed: 135 additions & 26 deletions

File tree

‎extensions/whatsapp/setup-entry.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Whatsapp tests cover setup entry plugin behavior.
2+
import fs from "node:fs";
3+
import os from "node:os";
4+
import path from "node:path";
25
import { describe, expect, it, vi } from "vitest";
36
import * as legacySessionSurfaceApi from "./legacy-session-surface-api.js";
47
import * as legacyStateMigrationsApi from "./legacy-state-migrations-api.js";
@@ -68,6 +71,57 @@ describe("whatsapp setup entry", () => {
6871
expect(legacySessionSurface.isLegacyGroupSessionKey).toBeTypeOf("function");
6972
});
7073

74+
it("plans migration for every Baileys auth category while preserving other shared-root files", async () => {
75+
const oauthDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-wa-legacy-migration-"));
76+
const authFiles = [
77+
"creds.json",
78+
"creds.json.bak",
79+
"pre-key-1.json",
80+
"session-contact.json",
81+
"sender-key-group.json",
82+
"sender-key-memory-group.json",
83+
"app-state-sync-key-contact.json",
84+
"app-state-sync-version-contact.json",
85+
"lid-mapping-15551234567.json",
86+
"device-list-15551234567.json",
87+
"tctoken-15551234567.json",
88+
"identity-key-15551234567.json",
89+
];
90+
91+
try {
92+
for (const file of [...authFiles, "oauth.json", "google-oauth.json", "notes.txt"]) {
93+
fs.writeFileSync(path.join(oauthDir, file), "{}", "utf-8");
94+
}
95+
fs.mkdirSync(path.join(oauthDir, "nested"));
96+
fs.writeFileSync(path.join(oauthDir, "nested", "session-keep.json"), "{}", "utf-8");
97+
fs.symlinkSync(path.join(oauthDir, "notes.txt"), path.join(oauthDir, "session-linked.json"));
98+
99+
const detectLegacyStateMigrations =
100+
setupEntry.loadLegacyStateMigrationDetector?.(setupEntryLoadOptions);
101+
if (!detectLegacyStateMigrations) {
102+
throw new Error("expected WhatsApp legacy state migration detector");
103+
}
104+
const migrations =
105+
(await detectLegacyStateMigrations({
106+
cfg: {},
107+
env: {},
108+
oauthDir,
109+
stateDir: oauthDir,
110+
})) ?? [];
111+
112+
expect(migrations.map((migration) => path.basename(migration.sourcePath)).toSorted()).toEqual(
113+
authFiles.toSorted(),
114+
);
115+
for (const migration of migrations) {
116+
expect(migration.targetPath).toBe(
117+
path.join(oauthDir, "whatsapp", "default", path.basename(migration.sourcePath)),
118+
);
119+
}
120+
} finally {
121+
fs.rmSync(oauthDir, { recursive: true, force: true });
122+
}
123+
});
124+
71125
it("loads the delegated setup wizard without importing runtime dependencies", async () => {
72126
const { whatsappSetupWizard } = await import("./src/setup-surface.js");
73127

‎extensions/whatsapp/src/auth-store.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,56 @@ describe("auth-store", () => {
351351
}
352352
});
353353

354+
it("clears every Baileys auth category from the shared legacy root without touching other files", async () => {
355+
const authDir = createTempAuthDir("openclaw-wa-auth-legacy-categories");
356+
const previousOAuthDir = hoisted.oauthDir;
357+
const authFiles = [
358+
"creds.json",
359+
"creds.json.bak",
360+
"pre-key-1.json",
361+
"session-contact.json",
362+
"sender-key-group.json",
363+
"sender-key-memory-group.json",
364+
"app-state-sync-key-contact.json",
365+
"app-state-sync-version-contact.json",
366+
"lid-mapping-15551234567.json",
367+
"device-list-15551234567.json",
368+
"tctoken-15551234567.json",
369+
"identity-key-15551234567.json",
370+
];
371+
const unrelatedFiles = ["oauth.json", "google-oauth.json", "notes.txt"];
372+
const nestedAuthFile = path.join(authDir, "nested", "session-keep.json");
373+
hoisted.oauthDir = authDir;
374+
375+
try {
376+
for (const file of [...authFiles, ...unrelatedFiles]) {
377+
fsSync.writeFileSync(path.join(authDir, file), "{}", "utf-8");
378+
}
379+
fsSync.mkdirSync(path.dirname(nestedAuthFile));
380+
fsSync.writeFileSync(nestedAuthFile, "keep", "utf-8");
381+
fsSync.symlinkSync(
382+
path.join(authDir, "notes.txt"),
383+
path.join(authDir, "session-linked.json"),
384+
);
385+
386+
await expect(logoutWeb({ authDir, isLegacyAuthDir: true })).resolves.toBe(true);
387+
388+
for (const file of authFiles) {
389+
expect(fsSync.existsSync(path.join(authDir, file)), file).toBe(false);
390+
}
391+
for (const file of unrelatedFiles) {
392+
expect(fsSync.existsSync(path.join(authDir, file)), file).toBe(true);
393+
}
394+
expect(fsSync.readFileSync(nestedAuthFile, "utf-8")).toBe("keep");
395+
expect(fsSync.lstatSync(path.join(authDir, "session-linked.json")).isSymbolicLink()).toBe(
396+
true,
397+
);
398+
} finally {
399+
hoisted.oauthDir = previousOAuthDir;
400+
fsSync.rmSync(authDir, { recursive: true, force: true });
401+
}
402+
});
403+
354404
it("clears auth state even when directory enumeration fails", async () => {
355405
await withOwnedOAuthAuthDir("openclaw-wa-auth-readdir", async (authDir) => {
356406
fsSync.writeFileSync(path.join(authDir, "creds.json"), "{}", "utf-8");

‎extensions/whatsapp/src/auth-store.ts‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { resolveOAuthDir } from "./auth-store.runtime.js";
1010
import {
1111
assertWebCredsPathRegularFileOrMissing,
1212
hasWebCredsSync,
13+
isWhatsAppBaileysAuthFileName,
1314
readWebCredsJsonRaw,
1415
readWebCredsJsonRawSync,
1516
resolveWebCredsBackupPath,
@@ -225,19 +226,6 @@ export async function readWebAuthSnapshotBestEffort(authDir: string = resolveDef
225226
} as const;
226227
}
227228

228-
function isBaileysAuthFileName(name: string): boolean {
229-
if (name === "oauth.json") {
230-
return false;
231-
}
232-
if (name === "creds.json" || name === "creds.json.bak") {
233-
return true;
234-
}
235-
if (!name.endsWith(".json")) {
236-
return false;
237-
}
238-
return /^(app-state-sync|session|sender-key|pre-key)-/.test(name);
239-
}
240-
241229
async function clearBaileysAuthFiles(
242230
authDir: string,
243231
beforeCredentialPersistence?: () => Promise<void>,
@@ -248,7 +236,7 @@ async function clearBaileysAuthFiles(
248236
}
249237
const entries = await fs.readdir(authDir, { withFileTypes: true });
250238
const credentialFiles = entries.filter(
251-
(entry) => entry.isFile() && isBaileysAuthFileName(entry.name),
239+
(entry) => entry.isFile() && isWhatsAppBaileysAuthFileName(entry.name),
252240
);
253241
if (credentialFiles.length === 0) {
254242
return;
@@ -273,7 +261,7 @@ async function shouldClearOnLogout(authDir: string, isLegacyAuthDir: boolean): P
273261
if (!entry.isFile()) {
274262
return false;
275263
}
276-
return isBaileysAuthFileName(entry.name);
264+
return isWhatsAppBaileysAuthFileName(entry.name);
277265
});
278266
}
279267
const credsStats = await fs.lstat(resolveWebCredsPath(authDir)).catch(() => null);

‎extensions/whatsapp/src/creds-files.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Whatsapp plugin module implements creds files behavior.
22
import path from "node:path";
3+
import type { SignalDataTypeMap } from "baileys";
34
import {
45
assertNoSymlinkParents,
56
assertNoSymlinkParentsSync,
@@ -9,6 +10,31 @@ import {
910
statRegularFileSync,
1011
} from "openclaw/plugin-sdk/security-runtime";
1112

13+
// The legacy OAuth root is shared; keep its exact WhatsApp namespaces aligned
14+
// with Baileys without importing the provider into setup discovery.
15+
const BAILEYS_SIGNAL_AUTH_CATEGORIES = {
16+
"app-state-sync-key": true,
17+
"app-state-sync-version": true,
18+
"device-list": true,
19+
"identity-key": true,
20+
"lid-mapping": true,
21+
"pre-key": true,
22+
"sender-key": true,
23+
"sender-key-memory": true,
24+
session: true,
25+
tctoken: true,
26+
} satisfies Record<keyof SignalDataTypeMap, true>;
27+
28+
export function isWhatsAppBaileysAuthFileName(name: string): boolean {
29+
if (name === "creds.json" || name === "creds.json.bak") {
30+
return true;
31+
}
32+
return (
33+
name.endsWith(".json") &&
34+
Object.keys(BAILEYS_SIGNAL_AUTH_CATEGORIES).some((category) => name.startsWith(`${category}-`))
35+
);
36+
}
37+
1238
export function resolveWebCredsPath(authDir: string): string {
1339
return path.join(authDir, "creds.json");
1440
}

‎extensions/whatsapp/src/state-migrations.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ import path from "node:path";
44
import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id";
55
import type { ChannelLegacyStateMigrationPlan } from "openclaw/plugin-sdk/channel-contract";
66
import { fileExists } from "openclaw/plugin-sdk/security-runtime";
7-
8-
function isLegacyWhatsAppAuthFile(name: string): boolean {
9-
if (name === "creds.json" || name === "creds.json.bak") {
10-
return true;
11-
}
12-
if (!name.endsWith(".json")) {
13-
return false;
14-
}
15-
return /^(app-state-sync|session|sender-key|pre-key)-/.test(name);
16-
}
7+
import { isWhatsAppBaileysAuthFileName } from "./creds-files.js";
178

189
export function detectWhatsAppLegacyStateMigrations(params: {
1910
oauthDir: string;
@@ -28,7 +19,7 @@ export function detectWhatsAppLegacyStateMigrations(params: {
2819
})();
2920

3021
return entries.flatMap((entry) => {
31-
if (!entry.isFile() || entry.name === "oauth.json" || !isLegacyWhatsAppAuthFile(entry.name)) {
22+
if (!entry.isFile() || !isWhatsAppBaileysAuthFileName(entry.name)) {
3223
return [];
3324
}
3425
const sourcePath = path.join(params.oauthDir, entry.name);

0 commit comments

Comments
 (0)