Skip to content

Commit 63bba28

Browse files
authored
test(doctor): use fixed OAuth sidecar vectors (#121986)
* test(doctor): use fixed OAuth sidecar vectors * test(doctor): preserve Linux warning assertion
1 parent 26d0158 commit 63bba28

6 files changed

Lines changed: 46 additions & 212 deletions

‎src/commands/doctor-auth-oauth-sidecar.test-support.ts‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎src/commands/doctor-auth-oauth-sidecar.test.ts‎

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Doctor OAuth sidecar tests cover encrypted sidecar detection and auth repair guidance.
2-
import { createCipheriv } from "node:crypto";
32
import fs from "node:fs";
43
import path from "node:path";
54
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -9,7 +8,6 @@ import {
98
type OpenClawTestState,
109
} from "../test-utils/openclaw-test-state.js";
1110
import { maybeRepairLegacyOAuthSidecarProfiles } from "./doctor-auth-oauth-sidecar.js";
12-
import { testing } from "./doctor-auth-oauth-sidecar.test-support.js";
1311
import type { DoctorPrompter } from "./doctor-prompter.js";
1412

1513
const states: OpenClawTestState[] = [];
@@ -54,34 +52,6 @@ function writeLegacyAuthProfiles(
5452
return state.writeJson(path.join("agents", agentId, "agent", "auth-profiles.json"), store);
5553
}
5654

57-
function encryptLegacySidecarMaterial(params: {
58-
ref: { source: "openclaw-credentials"; provider: "openai-codex"; id: string };
59-
profileId: string;
60-
provider: string;
61-
seed: string;
62-
material: Record<string, string>;
63-
}) {
64-
const iv = Buffer.alloc(12, 7);
65-
const cipher = createCipheriv("aes-256-gcm", testing.buildLegacyOAuthSecretKey(params.seed), iv);
66-
cipher.setAAD(
67-
testing.buildLegacyOAuthSecretAad({
68-
ref: params.ref,
69-
profileId: params.profileId,
70-
provider: params.provider,
71-
}),
72-
);
73-
const ciphertext = Buffer.concat([
74-
cipher.update(JSON.stringify(params.material), "utf8"),
75-
cipher.final(),
76-
]);
77-
return {
78-
algorithm: "aes-256-gcm",
79-
iv: iv.toString("base64url"),
80-
tag: cipher.getAuthTag().toString("base64url"),
81-
ciphertext: ciphertext.toString("base64url"),
82-
};
83-
}
84-
8555
afterEach(async () => {
8656
clearRuntimeAuthProfileStoreSnapshots();
8757
for (const state of states.splice(0)) {
@@ -126,17 +96,13 @@ describe("maybeRepairLegacyOAuthSidecarProfiles", () => {
12696
version: 1,
12797
profileId,
12898
provider: "openai-codex",
129-
encrypted: encryptLegacySidecarMaterial({
130-
ref,
131-
profileId,
132-
provider: "openai-codex",
133-
seed,
134-
material: {
135-
access: "access-token",
136-
refresh: "refresh-token",
137-
idToken: "id-token",
138-
},
139-
}),
99+
encrypted: {
100+
algorithm: "aes-256-gcm",
101+
iv: "BwcHBwcHBwcHBwcH",
102+
tag: "gSm_Lg58EVO-5wZGQlWHEA",
103+
ciphertext:
104+
"4qrZ4-zdgUdttB3gTUNORWdtO4gqLiFgTsilUX3-9RZiN2MLkCDdxQXQ2GfeqN1zi1qb9iURwK0sO0TJZfxO3zULMKNlRgUT",
105+
},
140106
},
141107
);
142108

@@ -231,16 +197,12 @@ describe("maybeRepairLegacyOAuthSidecarProfiles", () => {
231197
version: 1,
232198
profileId,
233199
provider: "openai-codex",
234-
encrypted: encryptLegacySidecarMaterial({
235-
ref,
236-
profileId,
237-
provider: "openai-codex",
238-
seed: "right-seed",
239-
material: {
240-
access: "access-token",
241-
refresh: "refresh-token",
242-
},
243-
}),
200+
encrypted: {
201+
algorithm: "aes-256-gcm",
202+
iv: "BwcHBwcHBwcHBwcH",
203+
tag: "ZHGhT2cekYFZCOxu8pP0KA",
204+
ciphertext: "OQPDJez2jSRH4FPxFNNkwEw7PDbClF6Ty6T2l4TLGvr6bdJfhK6VA6ccWwC1xlrR4ENA",
205+
},
244206
},
245207
);
246208

@@ -450,16 +412,13 @@ describe("maybeRepairLegacyOAuthSidecarProfiles", () => {
450412
version: 1,
451413
profileId,
452414
provider: "openai-codex",
453-
encrypted: encryptLegacySidecarMaterial({
454-
ref,
455-
profileId,
456-
provider: "openai-codex",
457-
seed,
458-
material: {
459-
access: "shared-access-token",
460-
refresh: "shared-refresh-token",
461-
},
462-
}),
415+
encrypted: {
416+
algorithm: "aes-256-gcm",
417+
iv: "BwcHBwcHBwcHBwcH",
418+
tag: "91XpNgcMQ-AVeo7NDnv11Q",
419+
ciphertext:
420+
"fVMsIFtJ0LX1ayciusBnyS7KulJU2dCAdkKU4yMLGYTVB-Gq0X_SvUqPTkAX_a1ZBIjGIC6nFH_3HvhWHMIX7Cs",
421+
},
463422
},
464423
);
465424

‎src/commands/doctor-auth-oauth-sidecar.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type { DoctorPrompter } from "./doctor-prompter.js";
1616
import {
1717
isLegacyOAuthRef,
1818
isLegacyOAuthSidecarPayload,
19-
legacyOAuthSidecarTestUtils,
2019
loadLegacyOAuthSidecarMaterial,
2120
resolveLegacyOAuthSidecarPath,
2221
type LegacyOAuthRef,
@@ -329,14 +328,3 @@ export async function maybeRepairLegacyOAuthSidecarProfiles(params: {
329328
}
330329
return result;
331330
}
332-
333-
const testing = {
334-
buildLegacyOAuthSecretAad: legacyOAuthSidecarTestUtils.buildLegacyOAuthSecretAad,
335-
buildLegacyOAuthSecretKey: legacyOAuthSidecarTestUtils.buildLegacyOAuthSecretKey,
336-
};
337-
338-
if (process.env.VITEST || process.env.NODE_ENV === "test") {
339-
(globalThis as Record<PropertyKey, unknown>)[
340-
Symbol.for("openclaw.doctorAuthOAuthSidecarTestApi")
341-
] = testing;
342-
}

‎src/commands/doctor/shared/legacy-oauth-sidecar.test-support.ts‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎src/commands/doctor/shared/legacy-oauth-sidecar.test.ts‎

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
createOpenClawTestState,
77
type OpenClawTestState,
88
} from "../../../test-utils/openclaw-test-state.js";
9-
import {
10-
legacyOAuthSidecarTestUtils,
11-
loadLegacyOAuthSidecarMaterial,
12-
} from "./legacy-oauth-sidecar.js";
13-
import { legacyOAuthSidecarInternalTestUtils } from "./legacy-oauth-sidecar.test-support.js";
9+
import { loadLegacyOAuthSidecarMaterial } from "./legacy-oauth-sidecar.js";
1410

1511
const states: OpenClawTestState[] = [];
1612

@@ -48,13 +44,12 @@ async function writeLegacySidecarThatNeedsKeychain(): Promise<{
4844
version: 1,
4945
profileId,
5046
provider: "openai-codex",
51-
encrypted: legacyOAuthSidecarTestUtils.encryptLegacyOAuthMaterial({
52-
ref,
53-
profileId,
54-
provider: "openai-codex",
55-
seed: "only-in-keychain",
56-
material: { access: "a", refresh: "b", idToken: "c" },
57-
}),
47+
encrypted: {
48+
algorithm: "aes-256-gcm",
49+
iv: "AQIDBAUGBwgJCgsM",
50+
tag: "G1t3MG1wjsZq17LOSqvu8w",
51+
ciphertext: "nkPkvPO-ZilcU9XIoVzMfskmxKVmknxIjFkNw3yLMhiP3d5--KdbiMub",
52+
},
5853
});
5954
return { state, ref, profileId };
6055
}
@@ -63,7 +58,6 @@ afterEach(async () => {
6358
for (const state of states.splice(0)) {
6459
await state.cleanup();
6560
}
66-
legacyOAuthSidecarInternalTestUtils.resetKeychainOnlyMigrationHint();
6761
});
6862

6963
describe("loadLegacyOAuthSidecarMaterial keychain-only headless warning", () => {
@@ -96,48 +90,32 @@ describe("loadLegacyOAuthSidecarMaterial keychain-only headless warning", () =>
9690
return env;
9791
}
9892

99-
it("emits a single doctor-pointer warning when only Keychain can decrypt and prompts are disabled", async () => {
93+
it("emits one doctor-pointer warning only on Darwin", async () => {
10094
const { state, ref, profileId } = await writeLegacySidecarThatNeedsKeychain();
10195
const env = envWithoutVitestSignals(state);
96+
const load = () =>
97+
loadLegacyOAuthSidecarMaterial({
98+
ref,
99+
profileId,
100+
provider: "openai-codex",
101+
allowKeychainPrompt: false,
102+
env,
103+
});
104+
105+
restorePlatform();
106+
restorePlatform = setPlatform("linux");
107+
expect(load()).toBeNull();
108+
expect(warnSpy).not.toHaveBeenCalled();
102109

103-
const firstAttempt = loadLegacyOAuthSidecarMaterial({
104-
ref,
105-
profileId,
106-
provider: "openai-codex",
107-
allowKeychainPrompt: false,
108-
env,
109-
});
110-
expect(firstAttempt).toBeNull();
110+
restorePlatform();
111+
restorePlatform = setPlatform("darwin");
112+
expect(load()).toBeNull();
111113
expect(warnSpy).toHaveBeenCalledTimes(1);
112114
const [firstMessage] = warnSpy.mock.calls[0] as [unknown];
113115
expect(String(firstMessage)).toContain("openclaw doctor --fix");
114116
expect(String(firstMessage)).toContain("macOS Keychain");
115117

116-
const secondAttempt = loadLegacyOAuthSidecarMaterial({
117-
ref,
118-
profileId,
119-
provider: "openai-codex",
120-
allowKeychainPrompt: false,
121-
env,
122-
});
123-
expect(secondAttempt).toBeNull();
118+
expect(load()).toBeNull();
124119
expect(warnSpy).toHaveBeenCalledTimes(1);
125120
});
126-
127-
it("does not emit the doctor-pointer warning on non-darwin platforms", async () => {
128-
restorePlatform();
129-
restorePlatform = setPlatform("linux");
130-
const { state, ref, profileId } = await writeLegacySidecarThatNeedsKeychain();
131-
const env = envWithoutVitestSignals(state);
132-
133-
const attempt = loadLegacyOAuthSidecarMaterial({
134-
ref,
135-
profileId,
136-
provider: "openai-codex",
137-
allowKeychainPrompt: false,
138-
env,
139-
});
140-
expect(attempt).toBeNull();
141-
expect(warnSpy).not.toHaveBeenCalled();
142-
});
143121
});

‎src/commands/doctor/shared/legacy-oauth-sidecar.ts‎

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Legacy OAuth sidecar reader for migrating encrypted auth-profile secret material.
22
import * as childProcess from "node:child_process";
3-
import { createCipheriv, createDecipheriv, hash } from "node:crypto";
3+
import { createDecipheriv, hash } from "node:crypto";
44
import fs from "node:fs";
55
import os from "node:os";
66
import path from "node:path";
@@ -113,38 +113,6 @@ function buildLegacyOAuthSecretKey(seed: string): Buffer {
113113
return hash("sha256", `openclaw:auth-profile-oauth:${seed}`, "buffer");
114114
}
115115

116-
function encryptLegacyOAuthMaterialForTest(params: {
117-
ref: LegacyOAuthRef;
118-
profileId: string;
119-
provider: string;
120-
seed: string;
121-
material: Record<string, string>;
122-
}): LegacyOAuthEncryptedPayload {
123-
const iv = Buffer.from("0102030405060708090a0b0c", "hex");
124-
const cipher = createCipheriv(
125-
LEGACY_OAUTH_SECRET_ALGORITHM,
126-
buildLegacyOAuthSecretKey(params.seed),
127-
iv,
128-
);
129-
cipher.setAAD(
130-
buildLegacyOAuthSecretAad({
131-
ref: params.ref,
132-
profileId: params.profileId,
133-
provider: params.provider,
134-
}),
135-
);
136-
const ciphertext = Buffer.concat([
137-
cipher.update(JSON.stringify(params.material), "utf8"),
138-
cipher.final(),
139-
]);
140-
return {
141-
algorithm: LEGACY_OAUTH_SECRET_ALGORITHM,
142-
iv: iv.toString("base64url"),
143-
tag: cipher.getAuthTag().toString("base64url"),
144-
ciphertext: ciphertext.toString("base64url"),
145-
};
146-
}
147-
148116
function uniquePaths(paths: Array<string | undefined>): string[] {
149117
return uniqueStrings(paths.filter((entry): entry is string => Boolean(entry)));
150118
}
@@ -342,18 +310,6 @@ function emitKeychainOnlyMigrationHintOnce(profileId: string): void {
342310
);
343311
}
344312

345-
const legacyOAuthSidecarInternalTestUtils = {
346-
resetKeychainOnlyMigrationHint(): void {
347-
keychainOnlyMigrationHintEmitted = false;
348-
},
349-
};
350-
351-
if (process.env.VITEST || process.env.NODE_ENV === "test") {
352-
(globalThis as Record<PropertyKey, unknown>)[
353-
Symbol.for("openclaw.legacyOAuthSidecarInternalTestApi")
354-
] = legacyOAuthSidecarInternalTestUtils;
355-
}
356-
357313
export function loadLegacyOAuthSidecarMaterial(params: {
358314
ref: LegacyOAuthRef;
359315
profileId: string;
@@ -386,9 +342,3 @@ export function loadLegacyOAuthSidecarMaterial(params: {
386342
}
387343
return normalizeLegacyOAuthSecretMaterial(raw);
388344
}
389-
390-
export const legacyOAuthSidecarTestUtils = {
391-
buildLegacyOAuthSecretAad,
392-
buildLegacyOAuthSecretKey,
393-
encryptLegacyOAuthMaterial: encryptLegacyOAuthMaterialForTest,
394-
};

0 commit comments

Comments
 (0)