🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(device): 修复设备文件操作误报不在批准的工作区内的多个场景 (#16962)
Hardye966c4f94891
diff --git a/apps/desktop/src/main/core/infrastructure/LocalFileProtocolManager.ts b/apps/desktop/src/main/core/infrastructure/LocalFileProtocolManager.tsindex 201f1cc382c..e1ae2ef8670 100644--- a/apps/desktop/src/main/core/infrastructure/LocalFileProtocolManager.ts+++ b/apps/desktop/src/main/core/infrastructure/LocalFileProtocolManager.ts@@ -656,11 +656,16 @@ export class LocalFileProtocolManager {const workspaceRootApproved =this.approvedWorkspaceRoots.has(normalizedRealWorkspaceRoot) ||this.indexedProjectRoots.has(normalizedRealWorkspaceRoot);- if (- workspaceRootApproved &&- isPathWithinRoot(normalizedRealFilePath, normalizedRealWorkspaceRoot)- ) {- return normalizedRealFilePath;+ if (workspaceRootApproved) {+ // Realpath containment: file resolved inside the real workspace root.+ if (isPathWithinRoot(normalizedRealFilePath, normalizedRealWorkspaceRoot)) {+ return normalizedRealFilePath;+ }+ // Non-resolved containment: file appears inside the workspace root even+ // when the resolved path points elsewhere (symlinked directories).+ if (isPathWithinRoot(normalizedFilePath, normalizedWorkspaceRoot)) {+ return normalizedRealFilePath;+ }}if (this.hasExternalPreviewApproval(normalizedRealFilePath)) return normalizedRealFilePath;diff --git a/apps/server/src/routers/lambda/__tests__/deviceWorkspaceGuard.test.ts b/apps/server/src/routers/lambda/__tests__/deviceWorkspaceGuard.test.tsindex 269a0cf446a..a263bfad452 100644--- a/apps/server/src/routers/lambda/__tests__/deviceWorkspaceGuard.test.ts+++ b/apps/server/src/routers/lambda/__tests__/deviceWorkspaceGuard.test.ts@@ -5,7 +5,12 @@ import type { DeviceModel } from '@/database/models/device';import { assertWorkspaceDeviceVisible, assertWorkspaceRootApproved } from '../deviceWorkspaceGuard';-const mockModel = (row: { defaultCwd?: string | null; workingDirs?: { path: string }[] } | null) =>+const mockModel = (+ row: {+ defaultCwd?: string | null;+ workingDirs?: Array<{ path: string; git?: { activeWorktree?: string } }>;+ } | null,+) =>({findByDeviceId: vi.fn().mockResolvedValue(row),}) as unknown as DeviceModel;@@ -46,6 +51,15 @@ describe('assertWorkspaceRootApproved', () => {).rejects.toMatchObject({ code: 'FORBIDDEN' });});+ it('allows a root that matches a worktree activeWorktree', async () => {+ const model = mockModel({+ workingDirs: [{ path: '/Users/me/proj', git: { activeWorktree: '/Users/me/proj-feat-x' } }],+ });+ await expect(+ assertWorkspaceRootApproved(model, 'dev-1', '/Users/me/proj-feat-x'),+ ).resolves.toBeUndefined();+ });+it('rejects when the device has no approved roots at all', async () => {const model = mockModel({ workingDirs: [] });await expect(diff --git a/apps/server/src/routers/lambda/device.ts b/apps/server/src/routers/lambda/device.tsindex eaf4506c23e..2c4c89c4bd4 100644--- a/apps/server/src/routers/lambda/device.ts+++ b/apps/server/src/routers/lambda/device.ts@@ -28,7 +28,11 @@ import { type DeviceAttachment, deviceGateway } from '@/server/services/deviceGaimport { filterAuthorizedDevicePresence } from '@/server/services/deviceGateway/scopedDevicePresence';import { preserveWorkspaceCache } from './deviceWorkingDirs';-import { assertWorkspaceDeviceVisible, assertWorkspaceRootApproved } from './deviceWorkspaceGuard';+import {+ assertWorkspaceDeviceVisible,+ assertWorkspaceRootApproved,+ registerDeviceSkillRoots,+} from './deviceWorkspaceGuard';// Derive the zod enum from the canonical config so new platforms are// automatically covered without touching this file.@@ -755,6 +759,17 @@ export const deviceRouter = router({userId: ctx.userId,workspaceId: ctx.workspaceId,});++ // Register device-scoped skill roots (~/.agents/skills / ~/.claude/skills)+ // in an in-memory cache so subsequent getLocalFilePreview calls pass the+ // workspace root guard without polluting the UI's working-directory list.+ if (result?.skills) {+ const skillRoots = [+ ...new Set(result.skills.filter((s) => s.scope === 'device').map((s) => s.previewRoot)),+ ].filter(Boolean);+ registerDeviceSkillRoots(input.deviceId, skillRoots);+ }+return result ?? null;}),diff --git a/apps/server/src/routers/lambda/deviceWorkspaceGuard.ts b/apps/server/src/routers/lambda/deviceWorkspaceGuard.tsindex e0eaa464772..ded910d8171 100644--- a/apps/server/src/routers/lambda/deviceWorkspaceGuard.ts+++ b/apps/server/src/routers/lambda/deviceWorkspaceGuard.ts@@ -3,6 +3,25 @@ import { TRPCError } from '@trpc/server';import type { DeviceModel } from '@/database/models/device';import { isPathWithinRoot } from '@/server/services/deviceGateway';+/**+ * In-memory cache of device-scoped skill preview roots, populated by+ * `listProjectSkills` and consumed by `assertWorkspaceRootApproved` to+ * authorize read-only file previews without persisting skill paths into+ * the DB (which would leak into the UI's working-directory list).+ */+const deviceSkillRoots = new Map<string, Set<string>>();++export const registerDeviceSkillRoots = (deviceId: string, roots: string[]) => {+ if (roots.length === 0) return;+ deviceSkillRoots.set(deviceId, new Set(roots));+};++const isWithinSkillRoots = (deviceId: string, workingDirectory: string): boolean => {+ const roots = deviceSkillRoots.get(deviceId);+ if (!roots) return false;+ return [...roots].some((root) => isPathWithinRoot(root, workingDirectory));+};+/*** Validate that a client-supplied workspace root is actually one the user has* bound to this device.@@ -36,12 +55,14 @@ export const assertWorkspaceRootApproved = async (const device = await deviceModel.findByDeviceId(deviceId);- const approvedRoots = [- ...(device?.workingDirs ?? []).map((dir) => dir.path),- ...(device?.defaultCwd ? [device.defaultCwd] : []),- ].filter((root): root is string => Boolean(root));+ const approvedRoots = (device?.workingDirs ?? [])+ .flatMap((dir) => [dir.path, dir.git?.activeWorktree])+ .concat(device?.defaultCwd ? [device.defaultCwd] : [])+ .filter((root): root is string => Boolean(root));- const approved = approvedRoots.some((root) => isPathWithinRoot(root, workingDirectory));+ const approved =+ approvedRoots.some((root) => isPathWithinRoot(root, workingDirectory)) ||+ isWithinSkillRoots(deviceId, workingDirectory);if (!approved) {throw new TRPCError({diff --git a/apps/server/src/services/deviceGateway/index.ts b/apps/server/src/services/deviceGateway/index.tsindex aede3e31501..5404d145846 100644--- a/apps/server/src/services/deviceGateway/index.ts+++ b/apps/server/src/services/deviceGateway/index.ts@@ -1,5 +1,3 @@-import path from 'node:path';-import { type DeviceAttachment } from '@lobechat/builtin-tool-remote-device';import {describeGatewayRequestFailure,@@ -48,6 +46,7 @@ import type {WorkspaceInitResult,} from '@lobechat/types';import debug from 'debug';+import { isAbsolute, relative, resolve } from 'pathe';import { gatewayEnv } from '@/envs/gateway';@@ -57,15 +56,15 @@ const log = debug('lobe-server:device-gateway');* Is `target` the same as, or nested inside, `root`?** The device's working directory may be a POSIX path (`/Users/…`) or a Windows- * path (`C:\…`) while this check runs on the cloud server (POSIX). We pick the- * path flavour from the root's shape so a Windows device path is still resolved- * with Windows semantics rather than being mangled by `path.posix`.+ * path (`C:\…` / `\\server\share`) while this check runs on the cloud server+ * (POSIX). `pathe` auto-detects the path flavour per argument, so Windows+ * device paths are resolved with Windows semantics without us branching on+ * `path.win32` / `path.posix`.*/export const isPathWithinRoot = (root: string, target: string): boolean => {- const p = /^[A-Z]:[/\\]/i.test(root) ? path.win32 : path.posix;- if (!p.isAbsolute(root) || !p.isAbsolute(target)) return false;- const relative = p.relative(p.resolve(root), p.resolve(target));- return relative === '' || (!relative.startsWith('..') && !p.isAbsolute(relative));+ if (!isAbsolute(root) || !isAbsolute(target)) return false;+ const rel = relative(resolve(root), resolve(target));+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));};/**