Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(filesystem): reject writes that escape scopes through a symlink

SeashoreShi757cdd8453c1
diff --git a/src/capabilities/permissions.test.ts b/src/capabilities/permissions.test.ts
index d037cd44..d7085b51 100644
--- a/src/capabilities/permissions.test.ts
+++ b/src/capabilities/permissions.test.ts
@@ -1,4 +1,7 @@
-import { describe, expect, it, vi } from 'vitest';
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, mkdirSync, writeFileSync, rmSync, symlinkSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
import { PermissionManager, splitShellSegments } from './permissions.js';
describe('splitShellSegments', () => {
@@ -146,3 +149,49 @@ describe('PermissionManager remote safety', () => {
expect(ask).toHaveBeenCalledTimes(4);
});
});
+
+describe('PermissionManager symlink write confinement', () => {
+ let root: string;
+ let ws: string;
+ let outside: string;
+
+ function makePermissions(): PermissionManager {
+ const permissions = new PermissionManager();
+ const manifest = permissions.getManifest();
+ manifest.capabilities.filesystem.enabled = true;
+ manifest.capabilities.filesystem.scopes.push({ path: ws, read: true, write: true });
+ permissions.setAutoApproveAll(true);
+ return permissions;
+ }
+
+ beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), 'mercury-fs-'));
+ ws = join(root, 'ws');
+ outside = join(root, 'outside');
+ mkdirSync(ws);
+ mkdirSync(outside);
+ writeFileSync(join(outside, 'secret.txt'), 'x');
+ symlinkSync(join(outside, 'secret.txt'), join(ws, 'alias.txt'));
+ });
+
+ afterEach(() => {
+ rmSync(root, { recursive: true, force: true });
+ });
+
+ it('denies a write that escapes the scope through an in-scope symlink', async () => {
+ const permissions = makePermissions();
+ await expect(permissions.checkFsAccess(join(ws, 'alias.txt'), 'write')).resolves.toMatchObject({ allowed: false });
+ });
+
+ it('still allows a plain write inside the scope', async () => {
+ const permissions = makePermissions();
+ await expect(permissions.checkFsAccess(join(ws, 'new.txt'), 'write')).resolves.toMatchObject({ allowed: true });
+ });
+
+ it('allows a symlink whose target stays inside the scope', async () => {
+ const permissions = makePermissions();
+ writeFileSync(join(ws, 'inner.txt'), 'x');
+ symlinkSync(join(ws, 'inner.txt'), join(ws, 'inner-alias.txt'));
+ await expect(permissions.checkFsAccess(join(ws, 'inner-alias.txt'), 'write')).resolves.toMatchObject({ allowed: true });
+ });
+});
diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.ts
index 3ebdc281..15626504 100644
--- a/src/capabilities/permissions.ts
+++ b/src/capabilities/permissions.ts
@@ -1,5 +1,5 @@
-import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
-import { join, resolve, sep } from 'node:path';
+import { existsSync, readFileSync, writeFileSync, mkdirSync, realpathSync } from 'node:fs';
+import { join, resolve, sep, dirname, basename } from 'node:path';
import { homedir } from 'node:os';
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
import { getMercuryHome } from '../utils/config.js';
@@ -416,6 +416,19 @@ export class PermissionManager {
const scope = this.findScope(resolved);
const tempScope = this.findTempScope(resolved);
+ // A path that is lexically inside a scope can still leave it through a
+ // symlink, because Node follows symlinks at the write sink. Reject writes
+ // whose canonicalised target falls outside every writable scope.
+ if (mode === 'write') {
+ const canonical = this.canonicalizePath(resolved);
+ if (canonical !== resolved && !this.isWithinWritableScope(canonical)) {
+ return {
+ allowed: false,
+ reason: `Permission denied: write to ${path} resolves outside the approved scopes (${canonical})`,
+ };
+ }
+ }
+
// Read access: allow if any scope covers it (reads are safe in any mode)
if (mode === 'read') {
if (scope && scope.read) return { allowed: true };
@@ -649,6 +662,35 @@ export class PermissionManager {
return undefined;
}
+ /**
+ * Canonicalise a path by resolving symlinks, tolerating paths that do not
+ * exist yet (e.g. create_file): the deepest existing ancestor is resolved
+ * and the remaining tail re-appended.
+ */
+ private canonicalizePath(resolved: string): string {
+ try {
+ return realpathSync(resolved);
+ } catch {
+ const parent = dirname(resolved);
+ if (parent === resolved) return resolved;
+ return join(this.canonicalizePath(parent), basename(resolved));
+ }
+ }
+
+ /** True when a canonical path falls inside one of the writable scopes. */
+ private isWithinWritableScope(canonicalPath: string): boolean {
+ const writable = [...this.manifest.capabilities.filesystem.scopes, ...this.tempScopes].filter(
+ (scope) => scope.write,
+ );
+ for (const scope of writable) {
+ const base = this.canonicalizePath(resolve(scope.path.replace(/^~/, homedir())));
+ if (canonicalPath === base || canonicalPath.startsWith(base + sep)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private matchPattern(command: string, pattern: string): boolean {
const regexStr = '^' + pattern.replace(/\*/g, '.*').replace(/\?/g, '.') + '$';
try {