File size: 13,937 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
import {
  ensureAuthProfileStore,
  findPersistedAuthProfileCredential,
  loadAuthProfileStoreForRuntime,
  resolveAuthProfileOrder,
  resolvePersistedAuthProfileOwnerAgentDir,
} from "../agents/auth-profiles.js";
import {
  clearRuntimeAuthProfileStoreSnapshots,
  setRuntimeAuthProfileStoreSnapshot,
} from "../agents/auth-profiles/runtime-snapshots.js";
import {
  inspectPersistedAuthProfileStoreRaw,
  readPersistedAuthProfileStoreRaw,
  writePersistedAuthProfileStoreRaw,
} from "../agents/auth-profiles/sqlite.js";
import type { RuntimeEnv } from "../runtime.js";
import { writeConfigMachineState } from "../state/config-machine-state-write.js";
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
import { agentExecCommand } from "./agent-exec.js";

const tempDirs = useAutoCleanupTempDirTracker(afterEach);

function createRuntime() {
  return { runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() } satisfies RuntimeEnv };
}

function successResult() {
  return {
    payloads: [{ text: "done" }],
    meta: { durationMs: 1, finalAssistantVisibleText: "done" },
  };
}

afterEach(() => {
  vi.restoreAllMocks();
  clearRuntimeAuthProfileStoreSnapshots();
});

describe("agent exec stored auth", () => {
  it("skips external Codex CLI credentials under --auth-env-only", async () => {
    const codexHome = tempDirs.make("openclaw-agent-exec-codex-home-");
    await fs.writeFile(
      path.join(codexHome, "auth.json"),
      JSON.stringify({
        auth_mode: "chatgpt",
        tokens: { access_token: "test-access", refresh_token: "test-refresh" },
      }),
      "utf8",
    );
    const previousCodexHome = process.env.CODEX_HOME;
    const previousOpenAiApiKey = process.env.OPENAI_API_KEY;
    const previousDatabaseUrl = process.env.DATABASE_URL;
    process.env.CODEX_HOME = codexHome;
    process.env.OPENAI_API_KEY = "test-openai-key";
    process.env.DATABASE_URL = "postgres://test.invalid/database";
    const { runtime } = createRuntime();
    let profileIds: string[] = [];
    let runtimeProfileIds: string[] = [];
    let hostExecApiKey: string | undefined;
    let hostExecDatabaseUrl: string | undefined;
    try {
      const { withHostExecInheritedEnvOmitted } = await import("../infra/host-env-security.js");
      await withHostExecInheritedEnvOmitted(["DATABASE_URL"], () =>
        agentExecCommand("inspect", { authEnvOnly: true }, runtime, {
          runAgent: vi.fn(async () => {
            profileIds = Object.keys(
              ensureAuthProfileStore(undefined, {
                allowKeychainPrompt: false,
                externalCliProviderIds: ["openai"],
              }).profiles,
            );
            runtimeProfileIds = Object.keys(
              loadAuthProfileStoreForRuntime(undefined, {
                allowKeychainPrompt: false,
                externalCliProviderIds: ["openai"],
              }).profiles,
            );
            const { sanitizeHostExecEnv } = await import("../infra/host-env-security.js");
            const hostExecEnv = sanitizeHostExecEnv({ baseEnv: process.env });
            hostExecApiKey = hostExecEnv.OPENAI_API_KEY;
            hostExecDatabaseUrl = hostExecEnv.DATABASE_URL;
            return successResult();
          }),
        }),
      );
    } finally {
      if (previousCodexHome === undefined) {
        delete process.env.CODEX_HOME;
      } else {
        process.env.CODEX_HOME = previousCodexHome;
      }
      if (previousOpenAiApiKey === undefined) {
        delete process.env.OPENAI_API_KEY;
      } else {
        process.env.OPENAI_API_KEY = previousOpenAiApiKey;
      }
      if (previousDatabaseUrl === undefined) {
        delete process.env.DATABASE_URL;
      } else {
        process.env.DATABASE_URL = previousDatabaseUrl;
      }
    }

    expect(profileIds).toEqual([]);
    expect(runtimeProfileIds).toEqual([]);
    expect(hostExecApiKey).toBeUndefined();
    expect(hostExecDatabaseUrl).toBeUndefined();
  });

  it.each([false, true])(
    "reads portable shared credentials across temporary state (local override: %s)",
    async (localOverride) => {
      await withOpenClawTestState(
        { scenario: "minimal", env: { OPENAI_API_KEY: undefined } },
        async (state) => {
          writeConfigMachineState("auth.sharedStore", { location: "state-db" });
          const sharedStore = {
            version: 1,
            order: { openai: ["openai:token", "openai:shared", "openai:private"] },
            usageStats: {
              "openai:token": { disabledUntil: Date.now() + 3_600_000 },
              "openai:private": { lastUsed: 123 },
            },
            profiles: {
              "openai:shared": {
                type: "api_key" as const,
                provider: "openai",
                key: "shared-test-key",
              },
              "openai:token": {
                type: "token" as const,
                provider: "openai",
                token: "shared-test-token",
              },
              "openai:private": {
                type: "api_key" as const,
                provider: "openai",
                key: "private-test-key",
                copyToAgents: false,
              },
              "openai:oauth": {
                type: "oauth" as const,
                provider: "openai",
                access: "test-access",
                refresh: "test-refresh",
                expires: Date.now() + 60_000,
                copyToAgents: true,
              },
            },
          };
          writePersistedAuthProfileStoreRaw(sharedStore);
          if (localOverride) {
            writePersistedAuthProfileStoreRaw(
              {
                version: 1,
                profiles: {
                  "openai:shared": { type: "api_key", provider: "openai", key: "local-test-key" },
                },
              },
              state.agentDir(),
            );
          }
          setRuntimeAuthProfileStoreSnapshot(sharedStore, state.agentDir());
          const { runtime } = createRuntime();
          let resolvedKey: string | undefined;
          const result = await agentExecCommand("inspect", {}, runtime, {
            runAgent: async () => {
              expect(process.env.OPENCLAW_STATE_DIR).not.toBe(state.stateDir);
              const store = ensureAuthProfileStore(undefined, {
                externalCli: { mode: "none" },
                syncExternalCli: false,
              });
              expect(Object.keys(store.profiles).toSorted()).toEqual([
                "openai:shared",
                "openai:token",
              ]);
              expect(
                loadAuthProfileStoreForRuntime(undefined, {
                  externalCli: { mode: "none" },
                  syncExternalCli: false,
                }).profiles,
              ).toEqual(store.profiles);
              expect(findPersistedAuthProfileCredential({ profileId: "openai:token" })).toEqual(
                sharedStore.profiles["openai:token"],
              );
              expect(store).toMatchObject({
                runtimeLocalProfileIds: localOverride ? ["openai:shared"] : [],
              });
              expect(resolveAuthProfileOrder({ store, provider: "openai", cfg: {} })).toEqual([
                "openai:shared",
                "openai:token",
              ]);
              expect(store.usageStats).toEqual({
                "openai:token": sharedStore.usageStats["openai:token"],
              });
              const { resolveApiKeyForProfile, saveAuthProfileStore } =
                await import("../agents/auth-profiles.js");
              resolvedKey = (
                await resolveApiKeyForProfile({ store, profileId: "openai:shared", cfg: {} })
              )?.apiKey;
              expect(inspectPersistedAuthProfileStoreRaw(state.agentDir()).status).toBe(
                localOverride ? "readable" : "missing",
              );
              saveAuthProfileStore(store);
              return successResult();
            },
          });
          expect(result.envelope.error).toBeUndefined();
          expect(resolvedKey).toBe(localOverride ? "local-test-key" : "shared-test-key");
          expect(readPersistedAuthProfileStoreRaw()).toEqual(sharedStore);
          expect(readPersistedAuthProfileStoreRaw(state.agentDir())).toEqual({
            version: 1,
            profiles: localOverride
              ? {
                  "openai:shared": { type: "api_key", provider: "openai", key: "local-test-key" },
                }
              : {},
          });
        },
      );
    },
  );

  it("rejects an unreadable original shared store before entering temporary exec", async () => {
    await withOpenClawTestState({ scenario: "minimal", layout: "split" }, async (state) => {
      writeConfigMachineState("auth.sharedStore", { location: "state-db" });
      writePersistedAuthProfileStoreRaw({ version: 1, profiles: "invalid" });
      const { runtime } = createRuntime();
      const runAgent = vi.fn(async () => successResult());
      const result = await agentExecCommand("inspect", {}, runtime, { runAgent });
      expect(result.envelope.error?.message).toContain(
        path.join(state.stateDir, "state", "openclaw.sqlite"),
      );
      expect(result.envelope.error?.message).toContain("is unreadable; run openclaw doctor --fix");
      expect(runAgent).not.toHaveBeenCalled();
    });
  });

  it("reads stored credentials from the configured agent directory", async () => {
    const stateDir = tempDirs.make("openclaw-agent-exec-cfg-auth-");
    const customAgentDir = path.join(stateDir, "custom-home");
    await fs.mkdir(customAgentDir, { recursive: true });
    const seedPath = path.join(stateDir, "openclaw.json");
    await fs.writeFile(
      seedPath,
      JSON.stringify({
        agents: { entries: { main: { agentDir: customAgentDir } } },
      }),
      "utf8",
    );
    const { saveAuthProfileStore } = await import("../agents/auth-profiles.js");
    saveAuthProfileStore(
      {
        version: 1,
        profiles: { "openai:stored": { type: "api_key", provider: "openai", key: "test-key" } },
      },
      customAgentDir,
    );
    const { runtime } = createRuntime();
    let scopedProfileIds: string[] = [];

    await agentExecCommand("inspect", { config: seedPath }, runtime, {
      runAgent: vi.fn(async () => {
        scopedProfileIds = Object.keys(loadAuthProfileStoreForRuntime()?.profiles ?? {});
        return successResult();
      }),
    });

    // The run config strips agentDir to keep run state ephemeral, but credential
    // ownership must still follow the operator's configured directory.
    expect(scopedProfileIds).toContain("openai:stored");
  });

  it("blocks direct persisted credential reads under --auth-env-only", async () => {
    const normalStateDir = tempDirs.make("openclaw-agent-exec-hidden-auth-");
    const normalAgentDir = path.join(normalStateDir, "agents", "main", "agent");
    const previousStateDir = process.env.OPENCLAW_STATE_DIR;
    process.env.OPENCLAW_STATE_DIR = normalStateDir;
    const { saveAuthProfileStore } = await import("../agents/auth-profiles.js");
    saveAuthProfileStore(
      {
        version: 1,
        profiles: {
          "openai:stored": { type: "api_key", provider: "openai", key: "test-key" },
        },
      },
      normalAgentDir,
    );
    const { runtime } = createRuntime();
    let persistedCredential: unknown;
    let ownerAgentDir: string | undefined;
    try {
      await agentExecCommand("inspect", { authEnvOnly: true }, runtime, {
        runAgent: vi.fn(async () => {
          persistedCredential = findPersistedAuthProfileCredential({
            agentDir: normalAgentDir,
            profileId: "openai:stored",
          });
          ownerAgentDir = resolvePersistedAuthProfileOwnerAgentDir({
            agentDir: normalAgentDir,
            profileId: "openai:stored",
          });
          return successResult();
        }),
      });
    } finally {
      if (previousStateDir === undefined) {
        delete process.env.OPENCLAW_STATE_DIR;
      } else {
        process.env.OPENCLAW_STATE_DIR = previousStateDir;
      }
    }

    expect(persistedCredential).toBeUndefined();
    expect(ownerAgentDir).toBeUndefined();
  });

  it("uses the normal stored auth profile when auth-env-only is disabled", async () => {
    const normalStateDir = tempDirs.make("openclaw-agent-exec-normal-state-");
    const normalAgentDir = path.join(normalStateDir, "agents", "main", "agent");
    const previousStateDir = process.env.OPENCLAW_STATE_DIR;
    process.env.OPENCLAW_STATE_DIR = normalStateDir;
    const { saveAuthProfileStore } = await import("../agents/auth-profiles.js");
    saveAuthProfileStore(
      {
        version: 1,
        profiles: {
          "openai:stored": { type: "api_key", provider: "openai", key: "test-key" },
        },
      },
      normalAgentDir,
    );
    const { runtime } = createRuntime();
    let profileIds: string[] = [];
    try {
      await agentExecCommand("inspect", { authEnvOnly: false }, runtime, {
        runAgent: vi.fn(async () => {
          expect(process.env.OPENCLAW_STATE_DIR).not.toBe(normalStateDir);
          profileIds = Object.keys(
            ensureAuthProfileStore(undefined, {
              allowKeychainPrompt: false,
              syncExternalCli: false,
            }).profiles,
          );
          return successResult();
        }),
      });
    } finally {
      if (previousStateDir === undefined) {
        delete process.env.OPENCLAW_STATE_DIR;
      } else {
        process.env.OPENCLAW_STATE_DIR = previousStateDir;
      }
    }

    expect(profileIds).toContain("openai:stored");
  });
});