Skip to content

Commit 87ea745

Browse files
committed
fix: reject stale session sharing callbacks
1 parent c0104d5 commit 87ea745

2 files changed

Lines changed: 98 additions & 24 deletions

File tree

‎ui/src/pages/chat/chat-pane-sharing.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
GatewaySessionRow,
88
SessionMembersListResult,
99
SessionVisibility,
10+
SessionsListResult,
1011
} from "../../api/types.ts";
1112
import type { SessionCapability } from "../../lib/sessions/index.ts";
1213
import {
@@ -58,6 +59,8 @@ function setSharingAuthorization(
5859
function createSharingTestChatPane(params: Parameters<typeof createTestChatPane>[0]) {
5960
const result = createTestChatPane(params);
6061
setSharingAuthorization(result.pane as SharingPane);
62+
result.state.sessionsResult = sharingSessionsResult(sessionRow());
63+
result.state.sessionsResultAgentId = "main";
6164
return result;
6265
}
6366

@@ -81,12 +84,27 @@ function sessionRow(): GatewaySessionRow {
8184
return {
8285
key: "agent:main:current",
8386
kind: "direct",
87+
sessionId: "session-current",
8488
updatedAt: 1,
8589
visibility: "draft",
8690
sharingRole: "owner",
8791
};
8892
}
8993

94+
function sharingSessionsResult(row: GatewaySessionRow): SessionsListResult {
95+
return {
96+
ts: 1,
97+
path: "",
98+
count: 1,
99+
defaults: {
100+
modelProvider: null,
101+
model: null,
102+
contextTokens: null,
103+
},
104+
sessions: [row],
105+
};
106+
}
107+
90108
function sharingResult(row: GatewaySessionRow): SessionMembersListResult {
91109
return {
92110
sessionKey: row.key,
@@ -264,6 +282,36 @@ describe("chat pane sharing authorization", () => {
264282
expect.objectContaining({ sessionKey: row.key }),
265283
);
266284
});
285+
286+
it.each([
287+
{
288+
name: "a replacement session",
289+
current: { ...sessionRow(), sessionId: "session-replacement" },
290+
},
291+
{
292+
name: "a current non-manager role",
293+
current: { ...sessionRow(), sharingRole: "member" as const },
294+
},
295+
])("refuses callbacks retained from $name", async ({ current }) => {
296+
const request = vi.fn();
297+
const sessions = {
298+
refreshReplacement: vi.fn(),
299+
} as unknown as SessionCapability;
300+
const { pane: testPane, state } = createSharingTestChatPane({
301+
client: { request } as unknown as GatewayBrowserClient,
302+
sessions,
303+
});
304+
const pane = testPane as SharingPane;
305+
const stale = sessionRow();
306+
state.sessionsResult = sharingSessionsResult(current);
307+
308+
await pane.loadSessionSharing(stale);
309+
await pane.setSessionVisibility(stale, "shared");
310+
await pane.setSessionMember(stale, "identity-alice", true);
311+
312+
expect(request).not.toHaveBeenCalled();
313+
expect(sessions.refreshReplacement).not.toHaveBeenCalled();
314+
});
267315
});
268316

269317
describe.each(mutations)("chat pane $name mutation connection ownership", (mutation) => {

‎ui/src/pages/chat/chat-pane-sharing.ts‎

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,38 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
5050
return `${this.sessionSharingAgentId(sessionKey) ?? ""}\0${sessionKey}`;
5151
}
5252

53+
protected currentSessionSharingRow(
54+
scope: ChatPaneConnectionScope,
55+
row: GatewaySessionRow,
56+
): GatewaySessionRow | null {
57+
const current = scope.state.sessionsResult?.sessions.find((candidate) =>
58+
areUiSessionKeysEquivalent(candidate.key, row.key),
59+
);
60+
if (!current || !canManageChatSessionSharing(row) || !canManageChatSessionSharing(current)) {
61+
return null;
62+
}
63+
if (current === row) {
64+
return current;
65+
}
66+
const rowSessionId = row.sessionId?.trim();
67+
const currentSessionId = current.sessionId?.trim();
68+
return rowSessionId && currentSessionId && rowSessionId === currentSessionId ? current : null;
69+
}
70+
5371
protected async loadSessionSharing(row: GatewaySessionRow, force = false): Promise<void> {
54-
const state = this.state;
72+
const scope = this.captureConnectionScope();
73+
const currentRow = scope ? this.currentSessionSharingRow(scope, row) : null;
5574
if (
56-
!state?.connected ||
57-
!state.client ||
58-
!canManageChatSessionSharing(row) ||
59-
!readSessionMethodAccess(this.context.gateway.snapshot, {
75+
!scope ||
76+
!currentRow ||
77+
!readSessionMethodAccess(scope.context.gateway.snapshot, {
6078
method: "session.members.list",
6179
requiredScope: "operator.read",
6280
}).allowed
6381
) {
6482
return;
6583
}
66-
const cacheKey = this.sessionSharingCacheKey(row.key);
84+
const cacheKey = this.sessionSharingCacheKey(currentRow.key);
6785
const current = this.sessionSharingStates.get(cacheKey);
6886
if (current?.loading && !force) {
6987
return;
@@ -72,21 +90,26 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
7290
// gateway/account change bumps the generation and clears this cache, so a
7391
// request that resolves after the switch must be dropped rather than
7492
// overwrite the new connection's menu with the previous account's data.
75-
const generation = this.connectionGeneration;
7693
this.setSessionSharingState(cacheKey, { ...current, loading: true, error: undefined });
7794
try {
78-
const result = await state.client.request<SessionMembersListResult>("session.members.list", {
79-
sessionKey: row.key,
80-
...(this.sessionSharingAgentId(row.key)
81-
? { agentId: this.sessionSharingAgentId(row.key) }
95+
const result = await scope.client.request<SessionMembersListResult>("session.members.list", {
96+
sessionKey: currentRow.key,
97+
...(this.sessionSharingAgentId(currentRow.key)
98+
? { agentId: this.sessionSharingAgentId(currentRow.key) }
8299
: {}),
83100
});
84-
if (this.connectionGeneration !== generation) {
101+
if (
102+
!this.isConnectionScopeCurrent(scope) ||
103+
!this.currentSessionSharingRow(scope, currentRow)
104+
) {
85105
return;
86106
}
87107
this.setSessionSharingState(cacheKey, { loading: false, result });
88108
} catch (error) {
89-
if (this.connectionGeneration !== generation) {
109+
if (
110+
!this.isConnectionScopeCurrent(scope) ||
111+
!this.currentSessionSharingRow(scope, currentRow)
112+
) {
90113
return;
91114
}
92115
this.setSessionSharingState(cacheKey, { loading: false, error: String(error) });
@@ -98,13 +121,14 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
98121
visibility: SessionVisibility,
99122
): Promise<void> {
100123
const scope = this.captureConnectionScope();
101-
if (!scope || visibility === row.visibility || !canManageChatSessionSharing(row)) {
124+
const currentRow = scope ? this.currentSessionSharingRow(scope, row) : null;
125+
if (!scope || !currentRow || visibility === currentRow.visibility) {
102126
return;
103127
}
104-
const agentId = this.sessionSharingAgentId(row.key);
105-
const cacheKey = this.sessionSharingCacheKey(row.key);
128+
const agentId = this.sessionSharingAgentId(currentRow.key);
129+
const cacheKey = this.sessionSharingCacheKey(currentRow.key);
106130
const params = {
107-
sessionKey: row.key,
131+
sessionKey: currentRow.key,
108132
visibility,
109133
...(agentId ? { agentId } : {}),
110134
};
@@ -122,10 +146,11 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
122146
return;
123147
}
124148
await scope.sessions.refreshReplacement(agentId);
125-
if (!this.isConnectionScopeCurrent(scope)) {
149+
const refreshedRow = this.currentSessionSharingRow(scope, currentRow);
150+
if (!this.isConnectionScopeCurrent(scope) || !refreshedRow) {
126151
return;
127152
}
128-
await this.loadSessionSharing(row, true);
153+
await this.loadSessionSharing(refreshedRow, true);
129154
} catch (error) {
130155
if (!this.isConnectionScopeCurrent(scope)) {
131156
return;
@@ -144,14 +169,15 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
144169
member: boolean,
145170
): Promise<void> {
146171
const scope = this.captureConnectionScope();
147-
if (!scope || !canManageChatSessionSharing(row)) {
172+
const currentRow = scope ? this.currentSessionSharingRow(scope, row) : null;
173+
if (!scope || !currentRow) {
148174
return;
149175
}
150-
const agentId = this.sessionSharingAgentId(row.key);
151-
const cacheKey = this.sessionSharingCacheKey(row.key);
176+
const agentId = this.sessionSharingAgentId(currentRow.key);
177+
const cacheKey = this.sessionSharingCacheKey(currentRow.key);
152178
const method = member ? "session.members.add" : "session.members.remove";
153179
const params = {
154-
sessionKey: row.key,
180+
sessionKey: currentRow.key,
155181
identityId,
156182
...(agentId ? { agentId } : {}),
157183
};
@@ -168,7 +194,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
168194
if (!this.isConnectionScopeCurrent(scope)) {
169195
return;
170196
}
171-
await this.loadSessionSharing(row, true);
197+
await this.loadSessionSharing(currentRow, true);
172198
if (!this.isConnectionScopeCurrent(scope)) {
173199
return;
174200
}

0 commit comments

Comments
 (0)