Skip to content

Commit a018257

Browse files
vincentkocsteipete
authored andcommitted
fix(auth): harden codex oauth bridge security
1 parent f6921fd commit a018257

19 files changed

Lines changed: 397 additions & 213 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
57d6ea3acdbaff64d59293bb42224be02ca975c5554afacebb78677642355b6f plugin-sdk-api-baseline.json
2-
faff507780c473574e22e73af63dcdc4f4043cea72fa3475606cd4eaf20c1b17 plugin-sdk-api-baseline.jsonl
1+
3447b4257e1eeaf3388b2c148e0086c534ba43147004100e9baa7cb662835c79 plugin-sdk-api-baseline.json
2+
78590addd53ec7db1ef5a56eecc93c39303344ccfad8349a14e8f97480fe64b2 plugin-sdk-api-baseline.jsonl

‎extensions/codex/src/app-server/auth-bridge.test.ts‎

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ import crypto from "node:crypto";
22
import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5-
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
6-
7-
const mocks = vi.hoisted(() => ({
8-
ensureAuthProfileStore: vi.fn(),
9-
}));
10-
11-
vi.mock("openclaw/plugin-sdk/provider-auth", () => ({
12-
ensureAuthProfileStore: mocks.ensureAuthProfileStore,
13-
}));
5+
import { saveAuthProfileStore } from "openclaw/plugin-sdk/agent-runtime";
6+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
147

158
let bridgeCodexAppServerStartOptions: typeof import("./auth-bridge.js").bridgeCodexAppServerStartOptions;
169

@@ -29,7 +22,6 @@ describe("bridgeCodexAppServerStartOptions", () => {
2922
});
3023

3124
afterEach(async () => {
32-
mocks.ensureAuthProfileStore.mockReset();
3325
await Promise.all(
3426
tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })),
3527
);
@@ -38,19 +30,22 @@ describe("bridgeCodexAppServerStartOptions", () => {
3830
it("bridges canonical OpenClaw oauth into an isolated CODEX_HOME", async () => {
3931
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
4032
tempDirs.push(agentDir);
41-
mocks.ensureAuthProfileStore.mockReturnValue({
42-
version: 1,
43-
profiles: {
44-
"openai-codex:default": {
45-
type: "oauth",
46-
provider: "openai-codex",
47-
access: "access-token",
48-
refresh: "refresh-token",
49-
expires: Date.now() + 60_000,
50-
accountId: "acct-123",
33+
saveAuthProfileStore(
34+
{
35+
version: 1,
36+
profiles: {
37+
"openai-codex:default": {
38+
type: "oauth",
39+
provider: "openai-codex",
40+
access: "access-token",
41+
refresh: "refresh-token",
42+
expires: Date.now() + 60_000,
43+
accountId: "acct-123",
44+
},
5145
},
5246
},
53-
});
47+
agentDir,
48+
);
5449

5550
const result = await bridgeCodexAppServerStartOptions({
5651
startOptions: {
@@ -98,10 +93,7 @@ describe("bridgeCodexAppServerStartOptions", () => {
9893
args: ["app-server"],
9994
headers: { authorization: "Bearer dev-token" },
10095
};
101-
mocks.ensureAuthProfileStore.mockReturnValue({
102-
version: 1,
103-
profiles: {},
104-
});
96+
saveAuthProfileStore({ version: 1, profiles: {} }, agentDir);
10597

10698
await expect(
10799
bridgeCodexAppServerStartOptions({
@@ -115,18 +107,21 @@ describe("bridgeCodexAppServerStartOptions", () => {
115107
it("refuses to overwrite a symlinked auth bridge file", async () => {
116108
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
117109
tempDirs.push(agentDir);
118-
mocks.ensureAuthProfileStore.mockReturnValue({
119-
version: 1,
120-
profiles: {
121-
"openai-codex:default": {
122-
type: "oauth",
123-
provider: "openai-codex",
124-
access: "access-token",
125-
refresh: "refresh-token",
126-
expires: Date.now() + 60_000,
110+
saveAuthProfileStore(
111+
{
112+
version: 1,
113+
profiles: {
114+
"openai-codex:default": {
115+
type: "oauth",
116+
provider: "openai-codex",
117+
access: "access-token",
118+
refresh: "refresh-token",
119+
expires: Date.now() + 60_000,
120+
},
127121
},
128122
},
129-
});
123+
agentDir,
124+
);
130125

131126
const codexHome = resolveHashedCodexHome(agentDir, "openai-codex:default");
132127
await fs.mkdir(codexHome, { recursive: true });
Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,29 @@
1-
import crypto from "node:crypto";
2-
import path from "node:path";
3-
import { ensureAuthProfileStore, type OAuthCredential } from "openclaw/plugin-sdk/provider-auth";
4-
import { writePrivateSecretFileAtomic } from "openclaw/plugin-sdk/secret-file-runtime";
1+
import { prepareCodexAuthBridgeFromProfile } from "openclaw/plugin-sdk/codex-auth-bridge-runtime";
52
import type { CodexAppServerStartOptions } from "./config.js";
63

74
const DEFAULT_CODEX_AUTH_PROFILE_ID = "openai-codex:default";
8-
const CODEX_AUTH_ENV_CLEAR_KEYS = ["OPENAI_API_KEY"] as const;
9-
10-
function isBridgeableCodexOAuthCredential(value: unknown): value is OAuthCredential {
11-
return Boolean(
12-
value &&
13-
typeof value === "object" &&
14-
value !== null &&
15-
"type" in value &&
16-
"provider" in value &&
17-
"access" in value &&
18-
"refresh" in value &&
19-
value.type === "oauth" &&
20-
value.provider === "openai-codex" &&
21-
typeof value.access === "string" &&
22-
value.access.trim().length > 0 &&
23-
typeof value.refresh === "string" &&
24-
value.refresh.trim().length > 0,
25-
);
26-
}
27-
28-
function resolveCodexBridgeHome(agentDir: string, profileId: string): string {
29-
const digest = crypto.createHash("sha256").update(profileId).digest("hex").slice(0, 16);
30-
return path.join(agentDir, "harness-auth", "codex", digest);
31-
}
32-
33-
function buildCodexAuthFile(credential: OAuthCredential): string {
34-
return `${JSON.stringify(
35-
{
36-
auth_mode: "chatgpt",
37-
tokens: {
38-
access_token: credential.access,
39-
refresh_token: credential.refresh,
40-
...(credential.accountId ? { account_id: credential.accountId } : {}),
41-
},
42-
},
43-
null,
44-
2,
45-
)}\n`;
46-
}
475

486
export async function bridgeCodexAppServerStartOptions(params: {
497
startOptions: CodexAppServerStartOptions;
508
agentDir: string;
519
authProfileId?: string;
5210
}): Promise<CodexAppServerStartOptions> {
5311
const profileId = params.authProfileId?.trim() || DEFAULT_CODEX_AUTH_PROFILE_ID;
54-
const store = ensureAuthProfileStore(params.agentDir, {
55-
allowKeychainPrompt: false,
12+
const bridge = await prepareCodexAuthBridgeFromProfile({
13+
agentDir: params.agentDir,
14+
authProfileId: profileId,
15+
bridgeRoot: "harness-auth",
5616
});
57-
const credential = store.profiles[profileId];
58-
if (!isBridgeableCodexOAuthCredential(credential)) {
17+
if (!bridge) {
5918
return params.startOptions;
6019
}
6120

62-
const codexHome = resolveCodexBridgeHome(params.agentDir, profileId);
63-
await writePrivateSecretFileAtomic({
64-
rootDir: params.agentDir,
65-
filePath: path.join(codexHome, "auth.json"),
66-
content: buildCodexAuthFile(credential),
67-
});
68-
6921
return {
7022
...params.startOptions,
7123
env: {
7224
...params.startOptions.env,
73-
CODEX_HOME: codexHome,
25+
CODEX_HOME: bridge.codexHome,
7426
},
75-
clearEnv: Array.from(
76-
new Set([...(params.startOptions.clearEnv ?? []), ...CODEX_AUTH_ENV_CLEAR_KEYS]),
77-
),
27+
clearEnv: Array.from(new Set([...(params.startOptions.clearEnv ?? []), ...bridge.clearEnv])),
7828
};
7929
}

‎extensions/openai/openai-codex-cli-bridge.test.ts‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
22
import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5+
import { saveAuthProfileStore } from "openclaw/plugin-sdk/agent-runtime";
56
import { afterEach, describe, expect, it } from "vitest";
67
import { prepareOpenAICodexCliExecution } from "./openai-codex-cli-bridge.js";
78

@@ -24,6 +25,22 @@ describe("prepareOpenAICodexCliExecution", () => {
2425
it("writes a private CODEX_HOME bridge from canonical OpenClaw oauth", async () => {
2526
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-cli-bridge-"));
2627
tempDirs.push(agentDir);
28+
saveAuthProfileStore(
29+
{
30+
version: 1,
31+
profiles: {
32+
"openai-codex:default": {
33+
type: "oauth",
34+
provider: "openai-codex",
35+
access: "access-token",
36+
refresh: "refresh-token",
37+
expires: Date.now() + 60_000,
38+
accountId: "acct-123",
39+
},
40+
},
41+
},
42+
agentDir,
43+
);
2744

2845
const result = await prepareOpenAICodexCliExecution({
2946
config: undefined,
@@ -32,14 +49,6 @@ describe("prepareOpenAICodexCliExecution", () => {
3249
provider: "codex-cli",
3350
modelId: "gpt-5.4",
3451
authProfileId: "openai-codex:default",
35-
authCredential: {
36-
type: "oauth",
37-
provider: "openai-codex",
38-
access: "access-token",
39-
refresh: "refresh-token",
40-
expires: Date.now() + 60_000,
41-
accountId: "acct-123",
42-
},
4352
});
4453

4554
expect(result).toMatchObject({
@@ -69,6 +78,19 @@ describe("prepareOpenAICodexCliExecution", () => {
6978
it("returns null when there is no bridgeable canonical oauth credential", async () => {
7079
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-cli-bridge-"));
7180
tempDirs.push(agentDir);
81+
saveAuthProfileStore(
82+
{
83+
version: 1,
84+
profiles: {
85+
"openai-codex:default": {
86+
type: "api_key",
87+
provider: "openai-codex",
88+
key: "sk-test",
89+
},
90+
},
91+
},
92+
agentDir,
93+
);
7294

7395
await expect(
7496
prepareOpenAICodexCliExecution({
@@ -78,11 +100,6 @@ describe("prepareOpenAICodexCliExecution", () => {
78100
provider: "codex-cli",
79101
modelId: "gpt-5.4",
80102
authProfileId: "openai-codex:default",
81-
authCredential: {
82-
type: "api_key",
83-
provider: "openai-codex",
84-
key: "sk-test",
85-
},
86103
}),
87104
).resolves.toBeNull();
88105
});
@@ -93,6 +110,21 @@ describe("prepareOpenAICodexCliExecution", () => {
93110
const codexHome = resolveHashedCodexHome(agentDir, "openai-codex:default");
94111
await fs.mkdir(codexHome, { recursive: true });
95112
await fs.symlink(path.join(agentDir, "outside.txt"), path.join(codexHome, "auth.json"));
113+
saveAuthProfileStore(
114+
{
115+
version: 1,
116+
profiles: {
117+
"openai-codex:default": {
118+
type: "oauth",
119+
provider: "openai-codex",
120+
access: "access-token",
121+
refresh: "refresh-token",
122+
expires: Date.now() + 60_000,
123+
},
124+
},
125+
},
126+
agentDir,
127+
);
96128

97129
await expect(
98130
prepareOpenAICodexCliExecution({
@@ -102,13 +134,6 @@ describe("prepareOpenAICodexCliExecution", () => {
102134
provider: "codex-cli",
103135
modelId: "gpt-5.4",
104136
authProfileId: "openai-codex:default",
105-
authCredential: {
106-
type: "oauth",
107-
provider: "openai-codex",
108-
access: "access-token",
109-
refresh: "refresh-token",
110-
expires: Date.now() + 60_000,
111-
},
112137
}),
113138
).rejects.toThrow("must not be a symlink");
114139
});
Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,29 @@
1-
import crypto from "node:crypto";
2-
import path from "node:path";
31
import type {
42
CliBackendPreparedExecution,
53
CliBackendPrepareExecutionContext,
64
} from "openclaw/plugin-sdk/cli-backend";
7-
import type { OAuthCredential } from "openclaw/plugin-sdk/provider-auth";
8-
import { writePrivateSecretFileAtomic } from "openclaw/plugin-sdk/secret-file-runtime";
9-
10-
const OPENAI_CODEX_PROVIDER_ID = "openai-codex";
11-
const CODEX_AUTH_ENV_CLEAR_KEYS = ["OPENAI_API_KEY"] as const;
12-
13-
function isCodexBridgeableOAuthCredential(value: unknown): value is OAuthCredential {
14-
return Boolean(
15-
value &&
16-
typeof value === "object" &&
17-
value !== null &&
18-
"type" in value &&
19-
"provider" in value &&
20-
"access" in value &&
21-
"refresh" in value &&
22-
value.type === "oauth" &&
23-
value.provider === OPENAI_CODEX_PROVIDER_ID &&
24-
typeof value.access === "string" &&
25-
value.access.trim().length > 0 &&
26-
typeof value.refresh === "string" &&
27-
value.refresh.trim().length > 0,
28-
);
29-
}
30-
31-
function resolveCodexBridgeHome(agentDir: string, profileId: string): string {
32-
const digest = crypto.createHash("sha256").update(profileId).digest("hex").slice(0, 16);
33-
return path.join(agentDir, "cli-auth", "codex", digest);
34-
}
35-
36-
function buildCodexAuthFile(credential: OAuthCredential): string {
37-
return `${JSON.stringify(
38-
{
39-
auth_mode: "chatgpt",
40-
tokens: {
41-
access_token: credential.access,
42-
refresh_token: credential.refresh,
43-
...(credential.accountId ? { account_id: credential.accountId } : {}),
44-
},
45-
},
46-
null,
47-
2,
48-
)}\n`;
49-
}
5+
import { prepareCodexAuthBridgeFromProfile } from "openclaw/plugin-sdk/codex-auth-bridge-runtime";
506

517
export async function prepareOpenAICodexCliExecution(
528
ctx: CliBackendPrepareExecutionContext,
539
): Promise<CliBackendPreparedExecution | null> {
54-
if (
55-
!ctx.agentDir ||
56-
!ctx.authProfileId ||
57-
!isCodexBridgeableOAuthCredential(ctx.authCredential)
58-
) {
10+
if (!ctx.agentDir || !ctx.authProfileId) {
5911
return null;
6012
}
6113

62-
const codexHome = resolveCodexBridgeHome(ctx.agentDir, ctx.authProfileId);
63-
await writePrivateSecretFileAtomic({
64-
rootDir: ctx.agentDir,
65-
filePath: path.join(codexHome, "auth.json"),
66-
content: buildCodexAuthFile(ctx.authCredential),
14+
const bridge = await prepareCodexAuthBridgeFromProfile({
15+
agentDir: ctx.agentDir,
16+
authProfileId: ctx.authProfileId,
17+
bridgeRoot: "cli-auth",
6718
});
19+
if (!bridge) {
20+
return null;
21+
}
6822

6923
return {
7024
env: {
71-
CODEX_HOME: codexHome,
25+
CODEX_HOME: bridge.codexHome,
7226
},
73-
clearEnv: [...CODEX_AUTH_ENV_CLEAR_KEYS],
27+
clearEnv: bridge.clearEnv,
7428
};
7529
}

0 commit comments

Comments
 (0)