|
9 | 9 | getActivePluginHttpRouteRegistry, |
10 | 10 | getActivePluginSessionExtensionRegistry, |
11 | 11 | } from "../plugins/runtime.js"; |
12 | | -import { isIncognitoSessionKey } from "../routing/session-key.js"; |
13 | 12 | import { resolveReservedGatewayMethodScope } from "../shared/gateway-method-policy.js"; |
| 13 | +import { resolveDynamicSessionMutationRequiredScope } from "../shared/session-method-scopes.js"; |
14 | 14 | import { isAgentSessionResetCommand } from "./agent-command-policy.js"; |
15 | 15 | import { |
16 | 16 | isCoreGatewayMethodClassified, |
@@ -87,53 +87,6 @@ function resolveRequiredOperatorScopeForMethod(method: string): OperatorScope | |
87 | 87 | return resolveScopedMethod(method); |
88 | 88 | } |
89 | 89 |
|
90 | | -/** |
91 | | - * sessions.patch fields a write-scoped operator may mutate: user-level chat |
92 | | - * organization only. Any other field (model, sendPolicy, tool inheritance, |
93 | | - * exec routing, ...) keeps requiring operator.admin — fail closed on unknowns. |
94 | | - */ |
95 | | -const SESSIONS_PATCH_WRITE_SCOPE_FIELDS: ReadonlySet<string> = new Set([ |
96 | | - "key", |
97 | | - "agentId", |
98 | | - "label", |
99 | | - "category", |
100 | | - "boardFace", |
101 | | - "icon", |
102 | | - "pinned", |
103 | | - "archived", |
104 | | - "unread", |
105 | | -]); |
106 | | - |
107 | | -function resolveSessionsPatchRequiredScopes(params: unknown): OperatorScope[] { |
108 | | - if (!params || typeof params !== "object" || Array.isArray(params)) { |
109 | | - // Malformed params cannot mutate anything; let the handler return the |
110 | | - // precise validation error instead of a misleading missing-scope error. |
111 | | - return [WRITE_SCOPE]; |
112 | | - } |
113 | | - const safeOnly = Object.keys(params).every((key) => SESSIONS_PATCH_WRITE_SCOPE_FIELDS.has(key)); |
114 | | - return safeOnly ? [WRITE_SCOPE] : [ADMIN_SCOPE]; |
115 | | -} |
116 | | - |
117 | | -function resolveSessionsCreateRequiredScopes(params: unknown): OperatorScope[] { |
118 | | - if (!params || typeof params !== "object" || Array.isArray(params)) { |
119 | | - return [WRITE_SCOPE]; |
120 | | - } |
121 | | - const record = params as { incognito?: unknown; key?: unknown; parentSessionKey?: unknown }; |
122 | | - // Incognito creation and inheritance expose process-only session state; cwd and |
123 | | - // execNode target privileged host resources. All require operator.admin. |
124 | | - if ( |
125 | | - record.incognito === true || |
126 | | - (typeof record.key === "string" && isIncognitoSessionKey(record.key)) || |
127 | | - (typeof record.parentSessionKey === "string" && |
128 | | - isIncognitoSessionKey(record.parentSessionKey)) || |
129 | | - Object.hasOwn(params, "cwd") || |
130 | | - Object.hasOwn(params, "execNode") |
131 | | - ) { |
132 | | - return [ADMIN_SCOPE]; |
133 | | - } |
134 | | - return [WRITE_SCOPE]; |
135 | | -} |
136 | | - |
137 | 90 | function resolveSessionActionRegisteredScopes(params: unknown): OperatorScope[] | undefined { |
138 | 91 | if (!params || typeof params !== "object" || Array.isArray(params)) { |
139 | 92 | return undefined; |
@@ -221,46 +174,17 @@ function resolveDynamicLeastPrivilegeOperatorScopesForMethod( |
221 | 174 | return bootstrapCommandOwner === true ? [PAIRING_SCOPE, ADMIN_SCOPE] : [PAIRING_SCOPE]; |
222 | 175 | } |
223 | 176 | if (method === "sessions.patch") { |
224 | | - return resolveSessionsPatchRequiredScopes(params); |
| 177 | + return [resolveDynamicSessionMutationRequiredScope(method, params) ?? WRITE_SCOPE]; |
225 | 178 | } |
226 | 179 | if (method === "sessions.create") { |
227 | | - return resolveSessionsCreateRequiredScopes(params); |
| 180 | + return [resolveDynamicSessionMutationRequiredScope(method, params) ?? WRITE_SCOPE]; |
228 | 181 | } |
229 | 182 | if (method === "sessions.delete") { |
230 | | - return resolveSessionsDeleteRequiredScopes(params); |
| 183 | + return [resolveDynamicSessionMutationRequiredScope(method, params) ?? ADMIN_SCOPE]; |
231 | 184 | } |
232 | 185 | return [WRITE_SCOPE]; |
233 | 186 | } |
234 | 187 |
|
235 | | -/** |
236 | | - * sessions.delete params a write-scoped archive-then-delete request may carry. |
237 | | - * Internal controls (emitLifecycleHooks, expected* CAS guards) stay admin-only |
238 | | - * — fail closed on anything outside this set. |
239 | | - */ |
240 | | -const SESSIONS_DELETE_WRITE_SCOPE_FIELDS: ReadonlySet<string> = new Set([ |
241 | | - "key", |
242 | | - "agentId", |
243 | | - "deleteTranscript", |
244 | | - "archivedOnly", |
245 | | -]); |
246 | | - |
247 | | -function resolveSessionsDeleteRequiredScopes(params: unknown): OperatorScope[] { |
248 | | - // archivedOnly is the explicit archive-then-delete opt-in: write scope may |
249 | | - // delete only already-archived sessions (the handler enforces the state, |
250 | | - // both pre-lock and under the lifecycle lock). Everything else — including |
251 | | - // internal fallback/synthetic dispatch, which never sets the flag, and any |
252 | | - // request carrying internal-only params — keeps requiring admin. |
253 | | - if (!params || typeof params !== "object" || Array.isArray(params)) { |
254 | | - return [ADMIN_SCOPE]; |
255 | | - } |
256 | | - const record = params as { archivedOnly?: unknown }; |
257 | | - if (record.archivedOnly !== true) { |
258 | | - return [ADMIN_SCOPE]; |
259 | | - } |
260 | | - const safeOnly = Object.keys(params).every((key) => SESSIONS_DELETE_WRITE_SCOPE_FIELDS.has(key)); |
261 | | - return safeOnly ? [WRITE_SCOPE] : [ADMIN_SCOPE]; |
262 | | -} |
263 | | - |
264 | 188 | function findMissingOperatorScope( |
265 | 189 | requiredScopes: readonly OperatorScope[], |
266 | 190 | scopes: readonly string[], |
|
0 commit comments