Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): r1-h1 reject drops when required permission checks fail

M3c1cc9a725ac
diff --git a/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts b/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
index 22f062dd1..62e9b8275 100644
--- a/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
+++ b/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
@@ -40,6 +40,7 @@ import {
type GfsAccessibleResource,
type GfsChildView,
GfsClient,
+ type GfsHeldAffordances,
type GfsTransport,
type ResolvedGfsResource,
} from '../../../../src/gfs/uriHandler.js'
@@ -275,3 +276,79 @@ export async function resolveResource(resource: ResolvedGfsResource): Promise<Re
const client = new GfsClient(stubTransport({ ok: true, data: resource }))
return structuredClone(await client.resolveUri(resource.gfsUri, SESSION_TOKEN))
}
+
+/** A `GfsHeldAffordances` — the direct (NOT enveloped) affordances route body. */
+export function heldAffordances(
+ held: string[],
+ overrides: Partial<GfsHeldAffordances> = {}
+): GfsHeldAffordances {
+ return { held, isOperator: false, ...overrides }
+}
+
+/**
+ * Run held bits through the real `GfsClient.affordances` (the direct,
+ * un-enveloped affordances route) and the IPC structured-clone boundary —
+ * the value `window.clerum.gfs.affordances` resolves with in production.
+ */
+export async function affordancesResult(held: GfsHeldAffordances): Promise<GfsHeldAffordances> {
+ const client = new GfsClient(stubTransport({ ...held }))
+ return structuredClone(await client.affordances('fixture-parent', SESSION_TOKEN, 'main'))
+}
+
+/**
+ * The Error message `window.clerum.gfs.affordances` rejects with when the
+ * route answers a non-2xx — derived, not hand-written (T1), at the same honest
+ * seam as `resolveDeniedMessage`: the real `httpClient.requestJson` mints the
+ * ApiError from the HTTP status, and `GfsClient.affordances` surfaces it with
+ * its vetted `httpStatus=` marker.
+ */
+export async function affordancesFailureMessage(
+ status = 500,
+ body: { code: string; message: string } = {
+ code: 'internal',
+ message: 'gfs controller failed',
+ },
+ resourceId = 'fixture-parent'
+): Promise<string> {
+ const server = createServer((_req, res) => {
+ res.writeHead(status, { 'content-type': 'application/json' })
+ res.end(JSON.stringify({ ok: false, error: body }))
+ })
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', () => resolve()))
+ try {
+ const address = server.address()
+ const port = typeof address === 'object' && address ? address.port : 0
+ const client = new GfsClient({
+ baseUrl: `http://127.0.0.1:${port}`,
+ requestJson,
+ fetchBytes: async () => new ArrayBuffer(0),
+ })
+ await client.affordances(resourceId, SESSION_TOKEN, 'main')
+ throw new Error(`expected affordances to reject with a ${status} failure`)
+ } catch (error) {
+ if (!(error instanceof Error)) return String(error)
+ return error.message
+ } finally {
+ await new Promise<void>(resolve => server.close(() => resolve()))
+ }
+}
+
+/**
+ * The Error message a transport-level affordances failure produces (dead
+ * endpoint, real `httpClient.requestJson`) — connection refused with no HTTP
+ * response, so `parseHttpStatus` must read no status from it.
+ */
+export async function affordancesTransportFailureMessage(): Promise<string> {
+ const client = new GfsClient({
+ baseUrl: 'http://127.0.0.1:9',
+ requestJson,
+ fetchBytes: async () => new ArrayBuffer(0),
+ })
+ try {
+ await client.affordances('fixture-parent', SESSION_TOKEN, 'main')
+ throw new Error('expected affordances to fail at the transport layer')
+ } catch (error) {
+ if (!(error instanceof Error)) return String(error)
+ return error.message
+ }
+}
diff --git a/desktop-app/ui/src/gfs/__tests__/dragAdjudication.test.tsx b/desktop-app/ui/src/gfs/__tests__/dragAdjudication.test.tsx
new file mode 100644
index 000000000..d2d25ecd3
--- /dev/null
+++ b/desktop-app/ui/src/gfs/__tests__/dragAdjudication.test.tsx
@@ -0,0 +1,272 @@
+// @vitest-environment jsdom
+import { type ReactNode } from 'react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { cleanup, renderHook } from '@testing-library/react'
+import { describeGfsReadError, parseHttpStatus } from '@lib/gfsGrantErrors'
+import { desktopQueryDefaults } from '@lib/queryClient'
+import {
+ affordancesFailureMessage,
+ affordancesResult,
+ affordancesTransportFailureMessage,
+ heldAffordances,
+} from '../__fixtures__/gfsProducerFixtures'
+import { useGfsDragAdjudication } from '../dragAdjudication'
+import type { GfsDragSource, GfsDropTargetFact } from '../dragSession.types'
+
+const SCOPE = 'test:user-1:team-1'
+
+function dragSource(overrides: Partial<GfsDragSource> = {}): GfsDragSource {
+ return {
+ resourceId: 'src-1',
+ gfsUri: 'gfs://main/src1',
+ drive: 'main',
+ kind: 'file',
+ name: 'a.txt',
+ version: 2,
+ parentKnown: true,
+ parentResourceId: 'parent-1',
+ knownAncestorIds: ['parent-1'],
+ origin: 'files',
+ scope: SCOPE,
+ generation: 1,
+ ...overrides,
+ }
+}
+
+function dropTarget(): GfsDropTargetFact {
+ return {
+ resourceId: 'dest-1',
+ name: 'Reports',
+ kind: 'directory',
+ ancestorIds: ['root-1', null],
+ }
+}
+
+type ProbeOutcome = { held: string[] } | { error: Error }
+
+let probes: Record<string, ProbeOutcome>
+let resolve: ReturnType<typeof vi.fn>
+
+function installClerumGfsBridge() {
+ Object.defineProperty(window, 'clerum', {
+ configurable: true,
+ value: {
+ gfs: {
+ affordances: (resourceId: string, _drive?: string) => {
+ const outcome = probes[resourceId]
+ if (!outcome) {
+ return Promise.reject(new Error(`no probe fixture for ${resourceId}`))
+ }
+ if ('error' in outcome) return Promise.reject(outcome.error)
+ return Promise.resolve({
+ ...outcome,
+ canDelegate: false,
+ grantableBits: [],
+ canCreateShare: false,
+ })
+ },
+ },
+ },
+ })
+}
+
+function renderAdjudication() {
+ const queryClient = new QueryClient({ defaultOptions: desktopQueryDefaults })
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ )
+ return renderHook(
+ () =>
+ useGfsDragAdjudication({
+ scope: SCOPE,
+ drive: 'main',
+ resolve: resolve as unknown as (gfsUri: string) => Promise<{
+ resourceId: string
+ parentResourceId: string | null
+ name: string
+ version: number
+ }>,
+ }),
+ { wrapper }
+ )
+}
+
+async function grantedProbes(
+ overrides: Record<string, ProbeOutcome> = {}
+): Promise<Record<string, ProbeOutcome>> {
+ return {
+ 'dest-1': { held: (await affordancesResult(heldAffordances(['write']))).held },
+ 'parent-1': { held: (await affordancesResult(heldAffordances(['write', 'delete']))).held },
+ 'src-1': { held: (await affordancesResult(heldAffordances(['write']))).held },
+ ...overrides,
+ }
+}
+
+beforeEach(() => {
+ probes = {}
+ resolve = vi.fn(async (gfsUri: string) => {
+ throw new Error(`unexpected resolve of ${gfsUri}`)
+ })
+ installClerumGfsBridge()
+})
+
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+})
+
+describe('useGfsDragAdjudication — required-probe drop admission (R1-H1)', () => {
+ it('refuses the drop when the destination probe fails with 500', async () => {
+ const message = await affordancesFailureMessage(500)
+ expect(parseHttpStatus(message)).toBe(500)
+ probes = await grantedProbes({ 'dest-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(new Error(message)).message,
+ })
+ })
+
+ it('refuses the drop when the destination probe fails at the transport layer', async () => {
+ const message = await affordancesTransportFailureMessage()
+ expect(parseHttpStatus(message)).toBeNull()
+ probes = await grantedProbes({ 'dest-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(new Error(message)).message,
+ })
+ })
+
+ it('refuses the drop when the source-parent probe fails with 403', async () => {
+ const message = await affordancesFailureMessage(403, {
+ code: 'forbidden',
+ message: 'not authorized to read affordances',
+ })
+ expect(parseHttpStatus(message)).toBe(403)
+ probes = await grantedProbes({ 'parent-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(new Error(message)).message,
+ })
+ })
+
+ it('refuses the drop when the source-parent probe fails with 500', async () => {
+ const message = await affordancesFailureMessage(500)
+ probes = await grantedProbes({ 'parent-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(new Error(message)).message,
+ })
+ })
+
+ it('maps a destination 401 to session-lost (existing semantics preserved)', async () => {
+ const message = await affordancesFailureMessage(401, {
+ code: 'unauthorized',
+ message: 'session expired',
+ })
+ probes = await grantedProbes({ 'dest-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({ allowed: false, reason: 'session-lost' })
+ })
+
+ it('maps a destination 429 to rate-limited unavailable (existing semantics preserved)', async () => {
+ const message = await affordancesFailureMessage(429, {
+ code: 'rate_limited',
+ message: 'too many requests',
+ })
+ probes = await grantedProbes({ 'dest-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(new Error(message)).message,
+ })
+ })
+
+ it('maps a destination 404 to target-unavailable (existing semantics preserved)', async () => {
+ const message = await affordancesFailureMessage(404, {
+ code: 'not_found',
+ message: 'resource gone',
+ })
+ probes = await grantedProbes({ 'dest-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision).toEqual({
+ allowed: false,
+ reason: 'target-unavailable',
+ message: 'This folder is no longer available.',
+ })
+ })
+
+ it('admits a fully granted drop and never resolves the read plane', async () => {
+ probes = await grantedProbes()
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision.allowed).toBe(true)
+ if (decision.allowed) {
+ expect(decision.allowNumberedRename).toBe(true)
+ expect(decision.authority.kind).toBe('observed-allowed')
+ }
+ expect(resolve).not.toHaveBeenCalled()
+ })
+
+ it('denies a destination without write and a source parent without delete', async () => {
+ probes = await grantedProbes({
+ 'dest-1': { held: (await affordancesResult(heldAffordances(['read']))).held },
+ })
+ const first = renderAdjudication()
+ const deniedDestination = await first.result.current.adjudicateDrop(dragSource(), dropTarget())
+ expect(deniedDestination).toEqual({ allowed: false, reason: 'destination-write' })
+
+ cleanup()
+ probes = await grantedProbes({
+ 'parent-1': { held: (await affordancesResult(heldAffordances(['write']))).held },
+ })
+ const second = renderAdjudication()
+ const deniedDelete = await second.result.current.adjudicateDrop(dragSource(), dropTarget())
+ expect(deniedDelete).toEqual({ allowed: false, reason: 'source-delete' })
+ })
+
+ it('still admits an authorized move when the optional rename probe fails', async () => {
+ const message = await affordancesFailureMessage(500)
+ probes = await grantedProbes({ 'src-1': { error: new Error(message) } })
+ const { result } = renderAdjudication()
+
+ const decision = await result.current.adjudicateDrop(dragSource(), dropTarget())
+
+ expect(decision.allowed).toBe(true)
+ if (decision.allowed) {
+ expect(decision.allowNumberedRename).toBe(false)
+ }
+ })
+})
diff --git a/desktop-app/ui/src/gfs/dragAdjudication.ts b/desktop-app/ui/src/gfs/dragAdjudication.ts
index 39a97ccdc..d73f966e8 100644
--- a/desktop-app/ui/src/gfs/dragAdjudication.ts
+++ b/desktop-app/ui/src/gfs/dragAdjudication.ts
@@ -59,6 +59,22 @@ function unavailableDecision(error: unknown): GfsDropRejection | null {
return null
}
+/**
+ * Required (admission-gating) probe failure: keep the mapped 401/429/404
+ * semantics and fail CLOSED with a generic unavailable for every other
+ * failure class (403, 5xx, transport). A required probe must positively
+ * establish its bits before any confirmation or mutation is offered.
+ */
+function requiredProbeRejection(error: unknown): GfsDropRejection {
+ return (
+ unavailableDecision(error) ?? {
+ allowed: false,
+ reason: 'unavailable',
+ message: describeGfsReadError(error).message,
+ }
+ )
+}
+
/**
* Hover/drop adjudication shared by the Files page and the sidebar explorer
* (rev-2 §3, §4.1). Structure comes from loaded observations first (common
@@ -150,11 +166,7 @@ export function useGfsDragAdjudication(deps: GfsDragAdjudicationDeps) {
* priority check). No network resolves happen on hover.
*/
const scheduleColdCheck = useCallback(
- (
- target: GfsDropTargetFact,
- sourceParentId: string | null,
- onComplete: () => void
- ) => {
+ (target: GfsDropTargetFact, sourceParentId: string | null, onComplete: () => void) => {
if (inFlightRef.current >= 2) return
if (coldChecksRef.current >= DRAG_COLD_CHECK_BUDGET) return
coldChecksRef.current += 1
@@ -167,10 +179,7 @@ export function useGfsDragAdjudication(deps: GfsDragAdjudicationDeps) {
if (readCachedAffordances(target.resourceId) === null) {
await fetchAffordances(target.resourceId, AFFORDANCES_HOVER_STALE_MS)
}
- if (
- sourceParentId !== null &&
- readCachedAffordances(sourceParentId) === null
- ) {
+ if (sourceParentId !== null && readCachedAffordances(sourceParentId) === null) {
await fetchAffordances(sourceParentId, AFFORDANCES_HOVER_STALE_MS)
}
onComplete()
@@ -282,9 +291,7 @@ export function useGfsDragAdjudication(deps: GfsDragAdjudicationDeps) {
}
// Exact authority: destination write + source-parent write & delete.
- let sourceParentForProbe: string | null = source.parentKnown
- ? source.parentResourceId
- : null
+ let sourceParentForProbe: string | null = source.parentKnown ? source.parentResourceId : null
if (sourceParentForProbe === null) {
try {
const resolved = await deps.resolve(source.gfsUri)
@@ -296,7 +303,8 @@ export function useGfsDragAdjudication(deps: GfsDragAdjudicationDeps) {
return {
allowed: false,
reason: 'source-location-unknown',
- message: 'EvenDrive cannot verify this item’s current location. Refresh and try again.',
+ message:
+ 'EvenDrive cannot verify this item’s current location. Refresh and try again.',
}
}
return {
@@ -311,24 +319,34 @@ export function useGfsDragAdjudication(deps: GfsDragAdjudicationDeps) {
return { allowed: false, reason: 'root-source' }
}
- const destinationProbe = await fetchAffordances(target.resourceId, AFFORDANCES_MUTATION_STALE_MS)
+ const destinationProbe = await fetchAffordances(
+ target.resourceId,
+ AFFORDANCES_MUTATION_STALE_MS
+ )
if (!destinationProbe.ok) {
- const decision = unavailableDecision(destinationProbe.error)
- if (decision) return decision
+ return requiredProbeRejection(destinationProbe.error)
}
const sourceParentProbe = await fetchAffordances(
sourceParentForProbe,
AFFORDANCES_MUTATION_STALE_MS
)
if (!sourceParentProbe.ok) {
- const decision = unavailableDecision(sourceParentProbe.error)
- if (decision) return decision
+ return requiredProbeRejection(sourceParentProbe.error)
}
const authority = decideDragAuthority({
- destinationHeld: destinationProbe.ok ? destinationProbe.held : null,
- sourceParentHeld: sourceParentProbe.ok ? sourceParentProbe.held : null,
+ destinationHeld: destinationProbe.held,
+ sourceParentHeld: sourceParentProbe.held,
})
if (authority.kind === 'deny') return { allowed: false, reason: authority.reason }
+ if (authority.kind !== 'observed-allowed') {
+ // Both required probes succeeded, so their bits are non-null; any
+ // non-positive verdict here must never be admitted as allowed.
+ return {
+ allowed: false,
+ reason: 'unavailable',
+ message: 'EvenDrive could not confirm permission for this move. Try again.',
+ }
+ }
// Rename capability (not user consent): source-resource write proves a
// combined rename+move; the folder confirmation records the opt-in.