Skip to content

Commit 502bdb2

Browse files
authored
fix(sessions): fail closed without catalog profile (#123438)
1 parent 1e6de9a commit 502bdb2

2 files changed

Lines changed: 81 additions & 16 deletions

File tree

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type TestPluginRegistry = Omit<PluginRegistry, "sessionCatalogs"> & {
1010
type TestClient = {
1111
connect: { scopes: string[] };
1212
connId?: string;
13-
authenticatedUserProfile: { profileId: string };
13+
authenticatedUserProfile?: { profileId: string };
1414
};
1515

1616
const hoisted = vi.hoisted(() => ({
@@ -42,6 +42,10 @@ function client(profileId: string, scopes = ["operator.read", "operator.write"])
4242
return { connect: { scopes }, authenticatedUserProfile: { profileId } };
4343
}
4444

45+
function unprofiledClient(scopes = ["operator.read", "operator.write"]): TestClient {
46+
return { connect: { scopes } };
47+
}
48+
4549
function session(threadId: string, sessionKey?: string) {
4650
return {
4751
threadId,
@@ -197,6 +201,57 @@ describe("session catalog caller visibility", () => {
197201
expect(archive).not.toHaveBeenCalled();
198202
});
199203

204+
it("hides every row from an unprofiled multi-identity caller", async () => {
205+
hoisted.hasMultipleSessionSharingIdentities.mockReturnValue(true);
206+
const listedHost = host([session("unadopted-thread")]);
207+
hoisted.activeRegistry.sessionCatalogs = [
208+
{ provider: provider({ list: vi.fn(async () => [listedHost]) }) },
209+
];
210+
211+
const listed = await call("sessions.catalog.list", {}, unprofiledClient());
212+
213+
expect(listed).toHaveBeenCalledWith(true, {
214+
catalogs: [
215+
expect.objectContaining({
216+
hosts: [expect.objectContaining({ sessions: [] })],
217+
}),
218+
],
219+
});
220+
});
221+
222+
it("rejects reads for an unprofiled multi-identity caller", async () => {
223+
hoisted.hasMultipleSessionSharingIdentities.mockReturnValue(true);
224+
const read = vi.fn(async () => ({
225+
hostId: "gateway:local",
226+
threadId: "unadopted-thread",
227+
items: [{ type: "userMessage" as const, text: "private host history" }],
228+
}));
229+
hoisted.activeRegistry.sessionCatalogs = [
230+
{
231+
provider: provider({
232+
list: vi.fn(async () => [host([session("unadopted-thread")])]),
233+
read,
234+
}),
235+
},
236+
];
237+
238+
const transcript = await call(
239+
"sessions.catalog.read",
240+
{ catalogId: "codex", hostId: "gateway:local", threadId: "unadopted-thread" },
241+
unprofiledClient(),
242+
);
243+
244+
expect(transcript).toHaveBeenCalledWith(
245+
false,
246+
undefined,
247+
expect.objectContaining({
248+
code: ErrorCodes.FORBIDDEN,
249+
message: "session catalog thread is not visible to this caller",
250+
}),
251+
);
252+
expect(read).not.toHaveBeenCalled();
253+
});
254+
200255
it.each([
201256
{ label: "admin", multiple: true, scopes: ["operator.admin"] },
202257
{ label: "solo Gateway", multiple: false, scopes: ["operator.read"] },
@@ -314,20 +369,23 @@ describe("session catalog caller visibility", () => {
314369
host([
315370
session("alpha-thread", "agent:main:alpha"),
316371
session("beta-thread", "agent:main:beta"),
372+
session("unadopted-thread"),
317373
]),
318374
]);
319375
hoisted.activeRegistry.sessionCatalogs = [{ provider: provider({ list }) }];
320376
const config = {};
321377

322378
const alpha = await call("sessions.catalog.list", {}, client("profile-alpha"), config);
379+
const unprofiled = await call("sessions.catalog.list", {}, unprofiledClient(), config);
323380
const beta = await call("sessions.catalog.list", {}, client("profile-beta"), config);
324381
const rows = (respond: ReturnType<typeof vi.fn>) =>
325382
respond.mock.calls[0]?.[1]?.catalogs[0]?.hosts[0]?.sessions.map(
326383
(item: { threadId: string }) => item.threadId,
327384
);
328385

329386
expect(rows(alpha)).toEqual(["alpha-thread"]);
387+
expect(rows(unprofiled)).toEqual([]);
330388
expect(rows(beta)).toEqual(["beta-thread"]);
331-
expect(list).toHaveBeenCalledTimes(2);
389+
expect(list).toHaveBeenCalledTimes(3);
332390
});
333391
});

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import { ADMIN_SCOPE, authorizeOperatorScopesForRequiredScope } from "../method-
99
import { createSessionCatalogRequestEntrySnapshot } from "./session-catalog-entry-snapshot.js";
1010
import type { GatewayClient } from "./types.js";
1111

12-
type SessionCatalogVisibility = {
13-
cacheKey: string;
14-
profileId?: string;
15-
restricted: boolean;
16-
};
12+
type SessionCatalogVisibility =
13+
| { cacheKey: string; kind: "unrestricted" }
14+
| { cacheKey: string; kind: "restricted-unprofiled" }
15+
| { cacheKey: string; kind: "restricted-owner"; ownerProfileId: string };
1716

1817
export function resolveSessionCatalogVisibility(
1918
client: GatewayClient | null,
@@ -22,26 +21,31 @@ export function resolveSessionCatalogVisibility(
2221
const admin = authorizeOperatorScopesForRequiredScope(ADMIN_SCOPE, scopes).allowed;
2322
const multipleIdentities = hasMultipleSessionSharingIdentities();
2423
const profileId = client?.authenticatedUserProfile?.profileId;
25-
return {
26-
cacheKey: JSON.stringify({ admin, multipleIdentities, profileId: profileId ?? null }),
27-
...(profileId ? { profileId } : {}),
28-
restricted: multipleIdentities && !admin,
29-
};
24+
const cacheKey = JSON.stringify({ admin, multipleIdentities, profileId: profileId ?? null });
25+
if (!multipleIdentities || admin) {
26+
return { cacheKey, kind: "unrestricted" };
27+
}
28+
return profileId
29+
? { cacheKey, kind: "restricted-owner", ownerProfileId: profileId }
30+
: { cacheKey, kind: "restricted-unprofiled" };
3031
}
3132

3233
export function filterSessionCatalogHost(
3334
host: SessionCatalogHost,
3435
visibility: SessionCatalogVisibility,
3536
): SessionCatalogHost {
36-
if (!visibility.restricted) {
37+
if (visibility.kind === "unrestricted") {
3738
return host;
3839
}
40+
if (visibility.kind === "restricted-unprofiled") {
41+
return { ...host, sessions: [] };
42+
}
3943
return {
4044
...host,
4145
sessions: host.sessions.filter((session) => {
4246
// No sessionKey means the provider cannot link this host-owned CLI row to an adopted
4347
// OpenClaw session. Keep it private from non-admin callers on multi-identity Gateways.
44-
return session.createdActor?.id === visibility.profileId;
48+
return session.createdActor?.id === visibility.ownerProfileId;
4549
}),
4650
};
4751
}
@@ -56,9 +60,12 @@ export async function isSessionCatalogThreadVisible(params: {
5660
threadId: string;
5761
visibility: SessionCatalogVisibility;
5862
}): Promise<boolean> {
59-
if (!params.visibility.restricted) {
63+
if (params.visibility.kind === "unrestricted") {
6064
return true;
6165
}
66+
if (params.visibility.kind === "restricted-unprofiled") {
67+
return false;
68+
}
6269
const requestEntries = createSessionCatalogRequestEntrySnapshot({
6370
cfg: params.config,
6471
fallbackAgentId: params.fallbackAgentId,
@@ -80,7 +87,7 @@ export async function isSessionCatalogThreadVisible(params: {
8087
const projected = requestEntries.projectHostCreatedActors(host);
8188
const session = projected.sessions.find((candidate) => candidate.threadId === params.threadId);
8289
if (session) {
83-
return session.createdActor?.id === params.visibility.profileId;
90+
return session.createdActor?.id === params.visibility.ownerProfileId;
8491
}
8592
const nextCursor = host.nextCursor;
8693
if (!nextCursor || seenCursors.has(nextCursor)) {

0 commit comments

Comments
 (0)