mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix: add a Default agent permission choice and stop forwarding cookies to OpenCode (OM-179)
Each agent permission select now offers Default, shown when the agent has no wildcard rule for the action. Choosing Allow, Ask or Deny writes an agent rule even when it matches the inherited effect; choosing Default removes the agent's wildcard rules so the global permission applies. The local OpenCode proxy drops the browser's Cookie header, as the linked-server proxy already does.
This commit is contained in:
1 parent
38d618be9b
commit
670c66f85b
4 files changed
+64
-24
No files matched your search
@@ -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<string> = new Set(['cookie'])
|
||||
|
||||
export interface OpenCodeClient {
|
||||
readonly api: OpenCodeApi
|
||||
forwardRaw(request: Request): Promise<Response>
|
||||
@@ -65,7 +67,7 @@ export class FetchOpenCodeClient implements OpenCodeClient {
|
||||
async forwardRaw(request: Request): Promise<Response> {
|
||||
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) {
|
||||
|
||||
@@ -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<string, string>)['host']).toBeUndefined()
|
||||
expect((capturedInit?.headers as Record<string, string>)['connection']).toBeUndefined()
|
||||
expect((capturedInit?.headers as Record<string, string>)['authorization']).toBeUndefined()
|
||||
expect((capturedInit?.headers as Record<string, string>)['cookie']).toBeUndefined()
|
||||
expect((capturedInit?.headers as Record<string, string>)['x-opencode-directory']).toBe(encodeURIComponent('/test/workspace'))
|
||||
})
|
||||
|
||||
|
||||
@@ -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(
|
||||
<AgentDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={onSubmit}
|
||||
editingAgent={{
|
||||
name: 'reviewer',
|
||||
agent: {
|
||||
system: 'Review code',
|
||||
permissions: [
|
||||
{ action: 'edit', resource: '*', effect: 'deny' },
|
||||
{ action: 'edit', resource: 'src/**', effect: 'allow' },
|
||||
],
|
||||
},
|
||||
}}
|
||||
/>,
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
@@ -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<ManagedPermissionAction, PermissionEffect>,
|
||||
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
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="inherit">Default</SelectItem>
|
||||
<SelectItem value="ask">Ask</SelectItem>
|
||||
<SelectItem value="allow">Allow</SelectItem>
|
||||
<SelectItem value="deny">Deny</SelectItem>
|
||||
@@ -363,6 +361,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="inherit">Default</SelectItem>
|
||||
<SelectItem value="ask">Ask</SelectItem>
|
||||
<SelectItem value="allow">Allow</SelectItem>
|
||||
<SelectItem value="deny">Deny</SelectItem>
|
||||
@@ -386,6 +385,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="inherit">Default</SelectItem>
|
||||
<SelectItem value="ask">Ask</SelectItem>
|
||||
<SelectItem value="allow">Allow</SelectItem>
|
||||
<SelectItem value="deny">Deny</SelectItem>
|
||||
|
||||
Reference in new issue
Block a user