diff --git a/backend/src/services/opencode/client.ts b/backend/src/services/opencode/client.ts index 4d89cf07..3ff04c47 100644 --- a/backend/src/services/opencode/client.ts +++ b/backend/src/services/opencode/client.ts @@ -6,6 +6,8 @@ import { forwardProxyRequest } from '../../utils/proxy-forward' import { getOpenCodeBasicAuthHeader, type OpenCodePasswordResolver } from './auth' import { getOpenCodeUpstreamBaseUrl, withDefaultOpenCodeDirectory } from './upstream' +const FORWARD_EXCLUDED_HEADERS: ReadonlySet = new Set(['cookie']) + export interface OpenCodeClient { readonly api: OpenCodeApi forwardRaw(request: Request): Promise @@ -65,7 +67,7 @@ export class FetchOpenCodeClient implements OpenCodeClient { async forwardRaw(request: Request): Promise { const url = new URL(request.url) const path = url.pathname.replace(/^\/api\/opencode/, '') + url.search - const headers = this.withDirectoryHeader(filterProxyHeaders(request.headers)) + const headers = this.withDirectoryHeader(filterProxyHeaders(request.headers, FORWARD_EXCLUDED_HEADERS)) const basicAuth = await this.getBasicAuth() if (basicAuth) { diff --git a/backend/test/services/opencode/client.test.ts b/backend/test/services/opencode/client.test.ts index abeca5c5..3de72b52 100644 --- a/backend/test/services/opencode/client.test.ts +++ b/backend/test/services/opencode/client.test.ts @@ -46,7 +46,7 @@ describe('OpenCodeClient', () => { const basicAuth = 'Basic dXNlcjpwYXNz' describe('forwardRaw', () => { - it('should strip /api/opencode prefix from path, preserve search string, and strip host/connection/authorization headers', async () => { + it('should strip /api/opencode prefix from path, preserve search string, and strip host/connection/authorization/cookie headers', async () => { const mockResponse = new Response(JSON.stringify({}), { status: 200 }) let capturedUrl: URL | undefined let capturedInit: RequestInit | undefined @@ -63,6 +63,7 @@ describe('OpenCodeClient', () => { 'Host': 'localhost:5003', 'Connection': 'keep-alive', 'Authorization': 'Bearer token', + 'Cookie': 'session=manager', 'X-Custom-Header': 'value', }, }) @@ -78,6 +79,7 @@ describe('OpenCodeClient', () => { expect((capturedInit?.headers as Record)['host']).toBeUndefined() expect((capturedInit?.headers as Record)['connection']).toBeUndefined() expect((capturedInit?.headers as Record)['authorization']).toBeUndefined() + expect((capturedInit?.headers as Record)['cookie']).toBeUndefined() expect((capturedInit?.headers as Record)['x-opencode-directory']).toBe(encodeURIComponent('/test/workspace')) }) diff --git a/frontend/src/components/settings/AgentDialog.test.tsx b/frontend/src/components/settings/AgentDialog.test.tsx index dfcd7046..a1bbd1f1 100644 --- a/frontend/src/components/settings/AgentDialog.test.tsx +++ b/frontend/src/components/settings/AgentDialog.test.tsx @@ -276,7 +276,7 @@ describe('AgentDialog', () => { ]) }) - it('prepends a wildcard rule only for a changed action that has none', async () => { + it('prepends a wildcard rule only for an action set away from Default', async () => { const onSubmit = vi.fn() const user = userEvent.setup() render( @@ -305,6 +305,42 @@ describe('AgentDialog', () => { ]) }) + it('writes an explicit Allow over an inherited permission and removes the wildcard rule for Default', async () => { + const onSubmit = vi.fn() + const user = userEvent.setup() + render( + , + ) + + expect(screen.getByRole('combobox', { name: 'Shell' })).toHaveTextContent('Default') + + await user.click(screen.getByRole('combobox', { name: 'Shell' })) + await user.click(screen.getByRole('option', { name: 'Allow' })) + await user.click(screen.getByRole('combobox', { name: 'Edit' })) + await user.click(screen.getByRole('option', { name: 'Default' })) + await user.click(screen.getByRole('button', { name: 'Update' })) + + const agent = onSubmit.mock.calls[0][1] + expect(agent.permissions).toEqual([ + { action: 'shell', resource: '*', effect: 'allow' }, + { action: 'edit', resource: 'src/**', effect: 'allow' }, + ]) + }) + it('writes no permissions for a prompt-only save of an agent without permission rules', async () => { const onSubmit = vi.fn() const user = userEvent.setup() diff --git a/frontend/src/components/settings/AgentDialog.tsx b/frontend/src/components/settings/AgentDialog.tsx index 781458e5..b92b1865 100644 --- a/frontend/src/components/settings/AgentDialog.tsx +++ b/frontend/src/components/settings/AgentDialog.tsx @@ -18,7 +18,7 @@ const MANAGED_PERMISSION_ACTIONS = ['edit', 'shell', 'webfetch'] as const type ManagedPermissionAction = typeof MANAGED_PERMISSION_ACTIONS[number] -type PermissionEffect = OpenCodePermissionRule['effect'] +type PermissionEffect = OpenCodePermissionRule['effect'] | 'inherit' const isManagedWildcardRule = (rule: OpenCodePermissionRule): boolean => rule.resource === '*' && MANAGED_PERMISSION_ACTIONS.includes(rule.action as ManagedPermissionAction) @@ -41,29 +41,29 @@ const permissionEffect = ( action: ManagedPermissionAction, ): PermissionEffect => { const rule = lastMatchingRule(permissions, (entry) => entry.action === action && entry.resource === '*') - return rule?.effect ?? 'allow' + return rule?.effect ?? 'inherit' } /** * Applies the form effects to the managed wildcard rules in place, so the rule - * order OpenCode resolves with last-match-wins stays unchanged, and prepends a - * wildcard fallback only for changed actions that have no wildcard rule yet, so - * an untouched action keeps inheriting the global permission. + * order OpenCode resolves with last-match-wins stays unchanged. An `inherit` + * effect removes the action's wildcard rules so the global permission applies; + * any other effect prepends a wildcard rule when the action has none yet. */ const withManagedPermissionEffects = ( permissions: OpenCodePermissionRule[] | undefined, effects: Record, - changedActions: readonly ManagedPermissionAction[], ): OpenCodePermissionRule[] => { - const updated = (permissions ?? []).map((rule) => - isManagedWildcardRule(rule) - ? { ...rule, effect: effects[rule.action as ManagedPermissionAction] } - : rule, - ) + const updated = (permissions ?? []).flatMap((rule) => { + if (!isManagedWildcardRule(rule)) return [rule] + const effect = effects[rule.action as ManagedPermissionAction] + return effect === 'inherit' ? [] : [{ ...rule, effect }] + }) const actionsWithWildcard = new Set(updated.filter(isManagedWildcardRule).map((rule) => rule.action)) - const fallbacks = changedActions - .filter((action) => !actionsWithWildcard.has(action)) - .map((action) => ({ action, resource: '*', effect: effects[action] })) + const fallbacks = MANAGED_PERMISSION_ACTIONS.flatMap((action) => { + const effect = effects[action] + return effect === 'inherit' || actionsWithWildcard.has(action) ? [] : [{ action, resource: '*', effect }] + }) return [...fallbacks, ...updated] } @@ -80,9 +80,9 @@ const agentFormSchema = z.object({ temperature: z.number().min(0).max(2), topP: z.number().min(0).max(1), model: z.string().optional(), - editPermission: z.enum(['ask', 'allow', 'deny']), - shellPermission: z.enum(['ask', 'allow', 'deny']), - webfetchPermission: z.enum(['ask', 'allow', 'deny']), + editPermission: z.enum(['inherit', 'ask', 'allow', 'deny']), + shellPermission: z.enum(['inherit', 'ask', 'allow', 'deny']), + webfetchPermission: z.enum(['inherit', 'ask', 'allow', 'deny']), disable: z.boolean(), }) @@ -132,10 +132,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen const handleSubmit = (values: AgentFormValues) => { const previous = editingAgent?.agent - const effects = formPermissionEffects(values) - const loadedEffects = formPermissionEffects(getDefaultValues(editingAgent)) - const changedActions = MANAGED_PERMISSION_ACTIONS.filter((action) => effects[action] !== loadedEffects[action]) - const permissions = withManagedPermissionEffects(previous?.permissions, effects, changedActions) + const permissions = withManagedPermissionEffects(previous?.permissions, formPermissionEffects(values)) const agent: OpenCodeAgentPatch = { ...previous, system: values.prompt, @@ -340,6 +337,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen + Default Ask Allow Deny @@ -363,6 +361,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen + Default Ask Allow Deny @@ -386,6 +385,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen + Default Ask Allow Deny