Skip to content

Commit 040e0f5

Browse files
committed
fix: disable unauthorized session controls
1 parent efaf46c commit 040e0f5

11 files changed

Lines changed: 221 additions & 25 deletions

‎ui/src/app/app-host-native-shell.test.ts‎

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,33 @@ type TestWebKitWindow = Window & {
3131
type MacosTitlebarControlsState = HTMLElement & {
3232
navCollapsed: boolean;
3333
historyOnly: boolean;
34+
newSessionDisabledReason?: string;
3435
onOpenPalette?: () => void;
3536
onOpenNewSession?: () => void;
3637
updateComplete: Promise<boolean>;
3738
};
3839

40+
function nativeSessionContext(
41+
navigate: ReturnType<typeof vi.fn>,
42+
selectedId: string,
43+
options: { methods?: string[]; scopes?: string[] } = {},
44+
): ApplicationContext {
45+
return {
46+
navigate,
47+
agentSelection: { state: { selectedId } },
48+
gateway: {
49+
snapshot: {
50+
client: {},
51+
phase: "connected",
52+
hello: {
53+
auth: { role: "operator", scopes: options.scopes ?? ["operator.write"] },
54+
features: { methods: options.methods ?? ["sessions.create"] },
55+
},
56+
},
57+
},
58+
} as unknown as ApplicationContext;
59+
}
60+
3961
afterEach(() => {
4062
resetAppHostTestGlobals();
4163
});
@@ -114,10 +136,7 @@ describe("OpenClaw native shell", () => {
114136
value: { openPalette, togglePalette },
115137
});
116138
shell.runtime = {
117-
context: {
118-
navigate,
119-
agentSelection: { state: { selectedId: "agent/a" } },
120-
} as unknown as ApplicationContext,
139+
context: nativeSessionContext(navigate, "agent/a"),
121140
};
122141
shell.handleNativeOpenSearch();
123142
const toggleEvent = new CustomEvent("openclaw:native-toggle-search", { cancelable: true });
@@ -156,23 +175,57 @@ describe("OpenClaw native shell", () => {
156175
controls.remove();
157176
});
158177

178+
it("disables the native titlebar new-session control with its access reason", async () => {
179+
const onOpenNewSession = vi.fn();
180+
const controls = document.createElement(
181+
"openclaw-macos-titlebar-controls",
182+
) as unknown as MacosTitlebarControlsState;
183+
controls.navCollapsed = true;
184+
controls.newSessionDisabledReason = "Operator write access is required.";
185+
controls.onOpenNewSession = onOpenNewSession;
186+
document.body.append(controls);
187+
await controls.updateComplete;
188+
189+
const button = controls.querySelector<HTMLButtonElement>(
190+
".macos-titlebar-controls__new-session",
191+
);
192+
expect(button?.disabled).toBe(true);
193+
button?.click();
194+
expect(onOpenNewSession).not.toHaveBeenCalled();
195+
controls.remove();
196+
});
197+
159198
it("retains a native new-session request until a context exists", () => {
160199
const navigate = vi.fn();
161200
const shell = document.createElement("openclaw-app-shell") as unknown as ShellNavigationState;
162201

163202
shell.handleNativeNewSession();
164203

165204
shell.runtime = {
166-
context: {
167-
navigate,
168-
agentSelection: { state: { selectedId: "main" } },
169-
} as unknown as ApplicationContext,
205+
context: nativeSessionContext(navigate, "main"),
170206
};
171207
shell.handleNativeNewSession();
172208

173209
expect(navigate).toHaveBeenCalledExactlyOnceWith("new-session", { search: "?agent=main" });
174210
});
175211

212+
it("does not start a native session without exact sessions.create access", () => {
213+
for (const options of [
214+
{ methods: ["sessions.list"], scopes: ["operator.write"] },
215+
{ methods: ["sessions.create"], scopes: ["operator.read"] },
216+
]) {
217+
const navigate = vi.fn();
218+
const shell = document.createElement("openclaw-app-shell") as unknown as ShellNavigationState;
219+
shell.runtime = {
220+
context: nativeSessionContext(navigate, "main", options),
221+
};
222+
223+
shell.handleNativeNewSession();
224+
225+
expect(navigate).not.toHaveBeenCalled();
226+
}
227+
});
228+
176229
it("navigates valid native Dashboard paths and acknowledges them", () => {
177230
const navigate = vi.fn();
178231
const shell = document.createElement("openclaw-app-shell") as unknown as ShellNavigationState;

‎ui/src/app/app-shell-chrome.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import type { BoardFace } from "../lib/board/settings.ts";
2121
import { isGatewayMethodAdvertised } from "../lib/gateway-methods.ts";
2222
import { resolveAsciiShortcutKey } from "../lib/keyboard-shortcuts.ts";
23+
import { readSessionMethodAccess } from "../lib/session-method-access.ts";
2324
import { isTerminalAvailable } from "../lib/terminal-availability.ts";
2425
import type { ShellRouteState } from "./app-host-route-state.ts";
2526
import type { ApplicationContext, ApplicationNavigationOptions } from "./context.ts";
@@ -235,6 +236,14 @@ export class ShellChromeOwner {
235236
host.pendingNativeNewSession = true;
236237
return;
237238
}
239+
if (
240+
!readSessionMethodAccess(context.gateway.snapshot, {
241+
method: "sessions.create",
242+
params: {},
243+
}).allowed
244+
) {
245+
return;
246+
}
238247
host.openNewSession(context.agentSelection.state.selectedId ?? "");
239248
};
240249

‎ui/src/app/app-shell-view.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ export function renderApplicationShell(host: ShellViewHost) {
297297
.historyOnly=${settingsTakeover}
298298
.canGoBack=${host.nativeHistoryState.canGoBack}
299299
.canGoForward=${host.nativeHistoryState.canGoForward}
300+
.newSessionDisabledReason=${newSessionAccess.allowed
301+
? undefined
302+
: newSessionAccess.reason}
300303
.onToggleSidebar=${() => host.toggleNavigationSurface()}
301304
.onOpenPalette=${() => host.openPalette()}
302305
.onOpenNewSession=${() => host.handleNativeNewSession()}

‎ui/src/components/app-sidebar-session-catalog-render.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type SessionCatalogGroupsParams = {
5656
onLoadMore: (catalogId: string) => void;
5757
onOpenNewSession?: (agentId: string, target?: NewSessionTarget) => void;
5858
newSessionDisabledReason?: string;
59+
sectionDragDisabledReason?: string;
5960
onNavigate?: (routeId: NavigationRouteId, options?: ApplicationNavigationOptions) => void;
6061
catalogOpenTarget: "viewer" | "terminal";
6162
terminalAvailable: boolean;
@@ -159,12 +160,19 @@ export function renderSessionCatalogGroups(params: SessionCatalogGroupsParams) {
159160
<div
160161
class=${sectionClass}
161162
data-session-section=${sectionId}
162-
@dragover=${(event: DragEvent) => params.onSectionDragOver(event, sectionId)}
163-
@dragleave=${(event: DragEvent) => params.onSectionDragLeave(event, sectionId)}
164-
@drop=${(event: DragEvent) => params.onSectionDrop(event, sectionId)}
163+
@dragover=${params.sectionDragDisabledReason
164+
? nothing
165+
: (event: DragEvent) => params.onSectionDragOver(event, sectionId)}
166+
@dragleave=${params.sectionDragDisabledReason
167+
? nothing
168+
: (event: DragEvent) => params.onSectionDragLeave(event, sectionId)}
169+
@drop=${params.sectionDragDisabledReason
170+
? nothing
171+
: (event: DragEvent) => params.onSectionDrop(event, sectionId)}
165172
>
166173
${renderSidebarSessionSectionHeader({
167174
sectionId,
175+
disabledReason: params.sectionDragDisabledReason,
168176
onStartDrag: params.onStartSectionDrag,
169177
onFinishDrag: params.onFinishSectionDrag,
170178
content: html`

‎ui/src/components/app-sidebar-session-list-render.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ function renderSessionSection(params: {
8181
collapsed &&
8282
section.rows.some((row) => rowDemandsVisibility(row, RowVisibilityReason.Attention));
8383
const newSessionAccess = host.readNewSessionAccess();
84+
const groupWriteAccess = host.readSessionMutationAccess({
85+
method: "sessions.groups.put",
86+
requiredScope: "operator.write",
87+
});
8488
const sectionClass = [
8589
"sidebar-recent-sessions__group",
8690
`sidebar-recent-sessions__group--zone-${zone}`,
@@ -101,12 +105,19 @@ function renderSessionSection(params: {
101105
<div
102106
class=${sectionClass}
103107
data-session-section=${section.id}
104-
@dragover=${(event: DragEvent) => host.sectionDragOver(event, section.id, group)}
105-
@dragleave=${(event: DragEvent) => host.sectionDragLeave(event, section.id, group)}
106-
@drop=${(event: DragEvent) => host.sectionDrop(event, section.id, group)}
108+
@dragover=${groupWriteAccess.allowed
109+
? (event: DragEvent) => host.sectionDragOver(event, section.id, group)
110+
: nothing}
111+
@dragleave=${groupWriteAccess.allowed
112+
? (event: DragEvent) => host.sectionDragLeave(event, section.id, group)
113+
: nothing}
114+
@drop=${groupWriteAccess.allowed
115+
? (event: DragEvent) => host.sectionDrop(event, section.id, group)
116+
: nothing}
107117
>
108118
${renderSidebarSessionSectionHeader({
109119
sectionId: section.id,
120+
disabledReason: groupWriteAccess.allowed ? undefined : groupWriteAccess.reason,
110121
onStartDrag: (sectionId) => host.startSidebarSectionDrag(sectionId),
111122
onFinishDrag: () => host.finishSidebarSectionDrag(),
112123
onContextMenu: group
@@ -305,6 +316,10 @@ function renderSessionCatalog(params: {
305316
}) {
306317
const { host, snapshot, catalog, renderer } = params;
307318
const newSessionAccess = host.readNewSessionAccess();
319+
const groupWriteAccess = host.readSessionMutationAccess({
320+
method: "sessions.groups.put",
321+
requiredScope: "operator.write",
322+
});
308323
return html`
309324
${renderer({
310325
catalogs: [catalog],
@@ -344,6 +359,7 @@ function renderSessionCatalog(params: {
344359
onLoadMore: (catalogId) => void host.sessionData.loadMoreSessionCatalog(catalogId),
345360
onOpenNewSession: (agentId, target) => host.requestOpenNewSession(agentId, target),
346361
newSessionDisabledReason: newSessionAccess.allowed ? undefined : newSessionAccess.reason,
362+
sectionDragDisabledReason: groupWriteAccess.allowed ? undefined : groupWriteAccess.reason,
347363
onNavigate: host.onNavigate,
348364
catalogOpenTarget: snapshot.catalogOpenTarget,
349365
terminalAvailable: snapshot.terminalAvailable,

‎ui/src/components/app-sidebar-session-row-render.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,27 @@ export function renderRecentSession(params: {
217217
.filter(Boolean)
218218
.join(" ");
219219
const childrenExpanded = host.isSessionChildrenExpanded(session);
220+
const groupWriteAccess = host.readSessionMutationAccess({
221+
method: "sessions.groups.put",
222+
requiredScope: "operator.write",
223+
});
224+
const rowDraggable = !session.isChild && groupWriteAccess.allowed;
220225
const row = html`
221226
<div
222227
class=${rowClass}
223228
data-session-key=${session.key}
224229
role="listitem"
225-
draggable=${session.isChild ? "false" : "true"}
226-
@dragstart=${session.isChild
230+
draggable=${rowDraggable ? "true" : "false"}
231+
title=${!session.isChild && !groupWriteAccess.allowed ? groupWriteAccess.reason : nothing}
232+
@dragstart=${!rowDraggable
227233
? nothing
228234
: (event: DragEvent) => {
229235
if (event.dataTransfer) {
230236
writeSessionDragData(event.dataTransfer, session.key);
231237
host.startSessionDrag(session);
232238
}
233239
}}
234-
@dragend=${session.isChild
240+
@dragend=${!rowDraggable
235241
? nothing
236242
: () => {
237243
host.finishSessionDrag();

‎ui/src/components/app-sidebar-session-section-header.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import { writeSidebarSectionDragData } from "../lib/sessions/drag.ts";
44
export function renderSidebarSessionSectionHeader(params: {
55
sectionId: string;
66
content: TemplateResult;
7+
disabledReason?: string;
78
onStartDrag: (sectionId: string) => void;
89
onFinishDrag: () => void;
910
onContextMenu?: (event: MouseEvent) => void;
1011
}) {
1112
return html`
1213
<div
13-
class="sidebar-recent-sessions__head sidebar-recent-sessions__head--draggable"
14-
draggable="true"
14+
class="sidebar-recent-sessions__head ${params.disabledReason
15+
? ""
16+
: "sidebar-recent-sessions__head--draggable"}"
17+
draggable=${params.disabledReason ? "false" : "true"}
18+
title=${params.disabledReason ?? nothing}
1519
@mousedown=${(event: MouseEvent) => {
1620
const header = event.currentTarget as HTMLElement;
1721
header.toggleAttribute(
@@ -23,6 +27,10 @@ export function renderSidebarSessionSectionHeader(params: {
2327
(event.currentTarget as HTMLElement).removeAttribute("data-section-drag-blocked");
2428
}}
2529
@dragstart=${(event: DragEvent) => {
30+
if (params.disabledReason) {
31+
event.preventDefault();
32+
return;
33+
}
2634
const header = event.currentTarget as HTMLElement;
2735
const startedFromButton =
2836
Boolean((event.target as HTMLElement).closest("button")) ||

‎ui/src/components/macos-titlebar-controls.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class MacosTitlebarControls extends OpenClawLightDomContentsElement {
1111
@property({ attribute: false }) historyOnly = false;
1212
@property({ attribute: false }) canGoBack = false;
1313
@property({ attribute: false }) canGoForward = false;
14+
@property({ attribute: false }) newSessionDisabledReason?: string;
1415
@property({ attribute: false }) onToggleSidebar?: () => void;
1516
@property({ attribute: false }) onOpenPalette?: () => void;
1617
@property({ attribute: false }) onOpenNewSession?: () => void;
@@ -54,9 +55,11 @@ class MacosTitlebarControls extends OpenClawLightDomContentsElement {
5455
${this.navCollapsed
5556
? this.renderButton({
5657
// While the sidebar rail is collapsed, this mirrors the native
57-
// ⌘N item and stays deliberately free of connection state.
58+
// new-session item and its current Gateway authorization.
5859
label: t("chat.runControls.newSession"),
60+
tooltip: this.newSessionDisabledReason,
5961
icon: icons.plus,
62+
disabled: Boolean(this.newSessionDisabledReason),
6063
onClick: this.onOpenNewSession,
6164
className: "macos-titlebar-controls__new-session",
6265
})

‎ui/src/pages/sessions/view.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,49 @@ describe("sessions view", () => {
549549
expect(onAssignCategory).toHaveBeenCalledWith("agent:main:main", "Research");
550550
});
551551

552+
it("disables category assignment controls without group write access", async () => {
553+
const container = document.createElement("div");
554+
const onAssignCategory = vi.fn();
555+
const reason = "Operator write access is required.";
556+
render(
557+
renderSessions({
558+
...buildProps(
559+
buildMultiResult([
560+
{ key: "agent:main:main", kind: "direct", updatedAt: 1, category: "Research" },
561+
]),
562+
),
563+
groupBy: "category",
564+
knownCategories: ["Research"],
565+
groupWriteDisabledReason: reason,
566+
onAssignCategory,
567+
}),
568+
container,
569+
);
570+
await Promise.resolve();
571+
572+
const select = container.querySelector<HTMLSelectElement>(
573+
'select[aria-label="Move thread to a group"]',
574+
);
575+
expect(select?.disabled).toBe(true);
576+
expect(select?.title).toBe(reason);
577+
if (select) {
578+
select.value = "";
579+
select.dispatchEvent(new Event("change", { bubbles: true }));
580+
}
581+
582+
const headerRow = container.querySelector(".session-group-row");
583+
const drop = new Event("drop", { bubbles: true, cancelable: true });
584+
Object.defineProperty(drop, "dataTransfer", {
585+
value: {
586+
types: ["application/x-openclaw-session-key"],
587+
getData: () => "agent:main:main",
588+
},
589+
});
590+
headerRow?.dispatchEvent(drop);
591+
592+
expect(onAssignCategory).not.toHaveBeenCalled();
593+
});
594+
552595
it("opens the session menu from the kebab and row context menu", async () => {
553596
const container = document.createElement("div");
554597
const onOpenSessionMenu = vi.fn();
@@ -1236,6 +1279,7 @@ describe("sessions view", () => {
12361279
}),
12371280
),
12381281
expandedSessionKey: "agent:main:main",
1282+
patchAdminDisabledReason: "Operator admin access is required.",
12391283
checkpointItemsByKey: {
12401284
"agent:main:main": [
12411285
{
@@ -1294,6 +1338,12 @@ describe("sessions view", () => {
12941338
(label) => label.textContent?.trim(),
12951339
),
12961340
).toEqual(["Label", "Thinking", "Fast", "Verbose", "Reasoning"]);
1341+
for (const select of overridesSection?.querySelectorAll<HTMLSelectElement>(
1342+
".session-override-field select",
1343+
) ?? []) {
1344+
expect(select.disabled).toBe(true);
1345+
expect(select.title).toBe("Operator admin access is required.");
1346+
}
12971347

12981348
expect(
12991349
compactionSection?.querySelector(".session-details-panel__eyebrow")?.textContent?.trim(),

0 commit comments

Comments
 (0)