Skip to content

Commit 3630fd7

Browse files
committed
fix(codex): gate node continue on operator.admin and resume by CLI session id
Second review round: (1) bound node turns pass canMutateCodexHost only for owners/admins, while sessions.catalog.continue is operator.write — a write-scoped Control UI client could adopt a node session whose every turn would then be rejected. The gateway now forwards the caller's scopes to the provider (SessionCatalogContinueProviderParams.clientScopes) and codex fails node continues closed without operator.admin, before the dedupe join so a non-admin cannot ride an admin's in-flight operation. (2) The node binding stored the app-server threadId, but codex exec resume takes the CLI session id and forked threads share a session tree where the two differ; the binding now prefers record.sessionId.
1 parent be5b8b2 commit 3630fd7

6 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎extensions/codex/src/session-catalog-node-continue.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ async function continueNodeCodexSessionInner(params: {
237237
config: OpenClawConfig;
238238
hostId: string;
239239
threadId: string;
240+
clientScopes?: readonly string[];
240241
}): Promise<{
241242
sessionKey: string;
242243
disposition: CodexSessionDisposition;
@@ -307,7 +308,9 @@ async function continueNodeCodexSessionInner(params: {
307308
detachHint: "Start a new chat to leave the paired-node Codex session.",
308309
data: createCodexCliNodeConversationBindingData({
309310
nodeId,
310-
sessionId: params.threadId,
311+
// codex exec resume takes the CLI session id; forked threads share a
312+
// session tree where the thread id and session id differ.
313+
sessionId: record.sessionId?.trim() || params.threadId,
311314
agentId: adopted.agentId,
312315
cwd: record.cwd,
313316
}),
@@ -322,7 +325,14 @@ export async function continueNodeCodexSession(params: {
322325
config: OpenClawConfig;
323326
hostId: string;
324327
threadId: string;
328+
clientScopes?: readonly string[];
325329
}) {
330+
// Bound turns run native Codex on the node and pass canMutateCodexHost only
331+
// for owners/admins; gate before the dedupe join so a non-admin caller can
332+
// neither mint an always-rejected session nor ride an admin's operation.
333+
if (params.clientScopes?.includes("operator.admin") !== true) {
334+
throw new CatalogParamsError("continuing a paired-node Codex session requires operator.admin");
335+
}
326336
const nodeId = params.hostId.slice("node:".length).trim();
327337
if (!nodeId || params.hostId !== `node:${nodeId}`) {
328338
throw new CatalogParamsError("Codex session catalog hostId is invalid");

‎extensions/codex/src/session-catalog.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,7 @@ describe("Codex supervision actions", () => {
26302630
provider?.continueSession?.({
26312631
hostId: "node:devbox",
26322632
threadId: "thread-remote",
2633+
clientScopes: ["operator.admin"],
26332634
}),
26342635
).rejects.toThrow("paired node does not permit Codex session continuation");
26352636
expect(control.readThread).toHaveBeenCalledOnce();
@@ -2714,6 +2715,7 @@ describe("Codex supervision actions", () => {
27142715
sessions: [
27152716
{
27162717
threadId: "thread-remote",
2718+
sessionId: "cli-session-remote",
27172719
name: "Remote task",
27182720
cwd: "/remote/repo",
27192721
status: "idle",
@@ -2765,6 +2767,7 @@ describe("Codex supervision actions", () => {
27652767
const first = await provider?.continueSession?.({
27662768
hostId: "node:devbox",
27672769
threadId: "thread-remote",
2770+
clientScopes: ["operator.admin"],
27682771
});
27692772
const pendingList = await provider?.list({ hostIds: ["node:devbox"] });
27702773
expect(pendingList?.[0]?.sessions[0]?.openClawSessionKey).toBeUndefined();
@@ -2775,6 +2778,7 @@ describe("Codex supervision actions", () => {
27752778
const second = await provider?.continueSession?.({
27762779
hostId: "node:devbox",
27772780
threadId: "thread-remote",
2781+
clientScopes: ["operator.admin"],
27782782
});
27792783
await second?.afterConversationBound?.();
27802784

@@ -2785,7 +2789,8 @@ describe("Codex supervision actions", () => {
27852789
kind: "codex-cli-node-session",
27862790
version: 1,
27872791
nodeId: "devbox",
2788-
sessionId: "thread-remote",
2792+
// codex exec resume needs the CLI session id, not the thread id.
2793+
sessionId: "cli-session-remote",
27892794
agentId: "alpha",
27902795
cwd: "/remote/repo",
27912796
},
@@ -2864,6 +2869,7 @@ describe("Codex supervision actions", () => {
28642869
getProvider()?.continueSession?.({
28652870
hostId: "node:devbox",
28662871
threadId: "thread-remote",
2872+
clientScopes: ["operator.admin"],
28672873
}),
28682874
).rejects.toThrow("paired node does not permit Codex session continuation");
28692875
expect(createSessionEntry).not.toHaveBeenCalled();
@@ -2892,11 +2898,41 @@ describe("Codex supervision actions", () => {
28922898
getProvider()?.continueSession?.({
28932899
hostId: "node:devbox ",
28942900
threadId: "thread-remote",
2901+
clientScopes: ["operator.admin"],
28952902
}),
28962903
).rejects.toThrow("hostId is invalid");
28972904
expect(createSessionEntry).not.toHaveBeenCalled();
28982905
});
28992906

2907+
it("requires operator.admin before continuing a paired-node session", async () => {
2908+
const { runtime, createSessionEntry } = createRuntime({
2909+
nodes: [
2910+
{
2911+
nodeId: "devbox",
2912+
connected: true,
2913+
commands: [...CODEX_NODE_CONTINUE_COMMANDS],
2914+
invocableCommands: [...CODEX_NODE_CONTINUE_COMMANDS],
2915+
},
2916+
],
2917+
});
2918+
const { api, getProvider } = createGatewayApi(runtime);
2919+
registerCodexSessionCatalog({
2920+
api,
2921+
bindingStore: createCodexTestBindingStore(),
2922+
control: createControl(),
2923+
getRuntimeConfig: () => config,
2924+
});
2925+
2926+
await expect(
2927+
getProvider()?.continueSession?.({
2928+
hostId: "node:devbox",
2929+
threadId: "thread-remote",
2930+
clientScopes: ["operator.write"],
2931+
}),
2932+
).rejects.toThrow("requires operator.admin");
2933+
expect(createSessionEntry).not.toHaveBeenCalled();
2934+
});
2935+
29002936
it("rejects a non-continuable paired-node session status", async () => {
29012937
const invoke = vi.fn<PluginRuntime["nodes"]["invoke"]>(async () => ({
29022938
payloadJSON: JSON.stringify({
@@ -2933,6 +2969,7 @@ describe("Codex supervision actions", () => {
29332969
getProvider()?.continueSession?.({
29342970
hostId: "node:devbox",
29352971
threadId: "thread-remote",
2972+
clientScopes: ["operator.admin"],
29362973
}),
29372974
).rejects.toThrow("active on the paired node");
29382975
expect(createSessionEntry).not.toHaveBeenCalled();

‎extensions/codex/src/session-catalog.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@ function registerCodexSessionCatalog(params: {
12991299
config,
13001300
hostId: request.hostId,
13011301
threadId: request.threadId,
1302+
clientScopes: request.clientScopes,
13021303
});
13031304
}
13041305
if (request.hostId !== CODEX_LOCAL_SESSION_HOST_ID) {

‎src/gateway/server-methods/session-catalog.test.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ async function call(
3737
method: keyof typeof sessionCatalogHandlers,
3838
params: unknown,
3939
config: Record<string, unknown> = {},
40+
client?: { connect?: { scopes?: string[] } },
4041
) {
4142
const respond = vi.fn();
4243
await sessionCatalogHandlers[method]?.({
4344
params,
4445
respond,
46+
client,
4547
context: { getRuntimeConfig: () => config },
4648
} as never);
4749
return respond;
@@ -216,19 +218,40 @@ describe("session catalog Gateway methods", () => {
216218
});
217219
});
218220

219-
it("dispatches continue by catalog id", async () => {
221+
it("dispatches continue by catalog id with the caller's scopes", async () => {
220222
const continueSession = vi.fn(async () => ({ sessionKey: "agent:main:adopted" }));
221223
activeRegistry.sessionCatalogs = [{ provider: provider("codex", { continueSession }) }];
222-
const respond = await call("sessions.catalog.continue", {
224+
const respond = await call(
225+
"sessions.catalog.continue",
226+
{
227+
catalogId: "codex",
228+
hostId: "gateway:local",
229+
threadId: "thread-1",
230+
},
231+
{},
232+
{ connect: { scopes: ["operator.write", "operator.admin"] } },
233+
);
234+
expect(continueSession).toHaveBeenCalledWith({
235+
hostId: "gateway:local",
236+
threadId: "thread-1",
237+
clientScopes: ["operator.write", "operator.admin"],
238+
});
239+
expect(respond).toHaveBeenCalledWith(true, { sessionKey: "agent:main:adopted" });
240+
});
241+
242+
it("forwards empty scopes for unscoped callers", async () => {
243+
const continueSession = vi.fn(async () => ({ sessionKey: "agent:main:adopted" }));
244+
activeRegistry.sessionCatalogs = [{ provider: provider("codex", { continueSession }) }];
245+
await call("sessions.catalog.continue", {
223246
catalogId: "codex",
224247
hostId: "gateway:local",
225248
threadId: "thread-1",
226249
});
227250
expect(continueSession).toHaveBeenCalledWith({
228251
hostId: "gateway:local",
229252
threadId: "thread-1",
253+
clientScopes: [],
230254
});
231-
expect(respond).toHaveBeenCalledWith(true, { sessionKey: "agent:main:adopted" });
232255
});
233256

234257
it("installs a provider-requested binding on the adopted Control UI session", async () => {

‎src/gateway/server-methods/session-catalog.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
217217
}
218218
},
219219

220-
"sessions.catalog.continue": async ({ params, respond }) => {
220+
"sessions.catalog.continue": async ({ params, respond, client }) => {
221221
if (
222222
!assertValidParams(
223223
params,
@@ -240,7 +240,10 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
240240
}
241241
try {
242242
const { catalogId: _catalogId, ...providerRequest } = request;
243-
const result = await provider.continueSession(providerRequest);
243+
// Fail closed for unscoped callers: providers gate high-authority
244+
// continues (e.g. node-executing bindings) on these scopes.
245+
const clientScopes = Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [];
246+
const result = await provider.continueSession({ ...providerRequest, clientScopes });
244247
if (result.conversationBinding) {
245248
// operator.write on Continue is the approval boundary. Per-turn plugin and
246249
// node command authorization still applies after this binding is installed.

‎src/plugins/session-catalog.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export type SessionCatalogListProviderParams = {
1313
cursors?: Record<string, string>;
1414
};
1515
export type SessionCatalogReadProviderParams = Omit<SessionsCatalogReadParams, "catalogId">;
16-
export type SessionCatalogContinueProviderParams = Omit<SessionsCatalogContinueParams, "catalogId">;
16+
export type SessionCatalogContinueProviderParams = Omit<
17+
SessionsCatalogContinueParams,
18+
"catalogId"
19+
> & {
20+
/** Caller's gateway scopes so providers can gate high-authority continues up front. */
21+
clientScopes?: readonly string[];
22+
};
1723
export type SessionCatalogArchiveProviderParams = Omit<SessionsCatalogArchiveParams, "catalogId">;
1824

1925
export type SessionCatalogCreateTarget = {

0 commit comments

Comments
 (0)