Skip to content

Commit fa89d69

Browse files
fix(filesystem): reject existing move destinations
1 parent d31124c commit fa89d69

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/filesystem/__tests__/structured-content.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,81 @@ describe('structuredContent schema compliance', () => {
122122
// The content should contain success message
123123
expect(structuredContent.content).toContain('Successfully moved');
124124
});
125+
126+
it('should reject an existing destination without replacing it', async () => {
127+
const sourcePath = path.join(testDir, 'source.txt');
128+
const destPath = path.join(testDir, 'existing.txt');
129+
await fs.writeFile(sourcePath, 'source content');
130+
await fs.writeFile(destPath, 'existing content');
131+
132+
const result = await client.callTool({
133+
name: 'move_file',
134+
arguments: {
135+
source: sourcePath,
136+
destination: destPath
137+
}
138+
});
139+
140+
expect(result.isError).toBe(true);
141+
expect(result.content[0]).toMatchObject({ type: 'text' });
142+
expect((result.content[0] as { text: string }).text).toContain(
143+
'Destination already exists'
144+
);
145+
await expect(fs.readFile(sourcePath, 'utf8')).resolves.toBe('source content');
146+
await expect(fs.readFile(destPath, 'utf8')).resolves.toBe('existing content');
147+
});
148+
149+
it('should move directories recursively without replacing an existing tree', async () => {
150+
const sourcePath = path.join(testDir, 'source-dir');
151+
const destPath = path.join(testDir, 'moved-dir');
152+
await fs.mkdir(path.join(sourcePath, 'nested'), { recursive: true });
153+
await fs.writeFile(path.join(sourcePath, 'nested', 'file.txt'), 'source content');
154+
155+
const result = await client.callTool({
156+
name: 'move_file',
157+
arguments: {
158+
source: sourcePath,
159+
destination: destPath
160+
}
161+
});
162+
163+
expect(result.isError).not.toBe(true);
164+
await expect(fs.stat(sourcePath)).rejects.toMatchObject({ code: 'ENOENT' });
165+
await expect(
166+
fs.readFile(path.join(destPath, 'nested', 'file.txt'), 'utf8')
167+
).resolves.toBe('source content');
168+
});
169+
170+
it('should reject an existing directory destination without merging trees', async () => {
171+
const sourcePath = path.join(testDir, 'source-dir');
172+
const destPath = path.join(testDir, 'existing-dir');
173+
await fs.mkdir(sourcePath);
174+
await fs.mkdir(destPath);
175+
await fs.writeFile(path.join(sourcePath, 'source.txt'), 'source content');
176+
await fs.writeFile(path.join(destPath, 'existing.txt'), 'existing content');
177+
178+
const result = await client.callTool({
179+
name: 'move_file',
180+
arguments: {
181+
source: sourcePath,
182+
destination: destPath
183+
}
184+
});
185+
186+
expect(result.isError).toBe(true);
187+
expect((result.content[0] as { text: string }).text).toContain(
188+
'Destination already exists'
189+
);
190+
await expect(
191+
fs.readFile(path.join(sourcePath, 'source.txt'), 'utf8')
192+
).resolves.toBe('source content');
193+
await expect(
194+
fs.readFile(path.join(destPath, 'existing.txt'), 'utf8')
195+
).resolves.toBe('existing content');
196+
await expect(fs.stat(path.join(destPath, 'source.txt'))).rejects.toMatchObject({
197+
code: 'ENOENT'
198+
});
199+
});
125200
});
126201

127202
describe('list_directory (control - already working)', () => {

‎src/filesystem/index.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,27 @@ server.registerTool(
631631
async (args: z.infer<typeof MoveFileArgsSchema>) => {
632632
const validSourcePath = await validatePath(args.source);
633633
const validDestPath = await validatePath(args.destination);
634-
await fs.rename(validSourcePath, validDestPath);
634+
635+
try {
636+
// `rename()` can overwrite a destination created after a separate
637+
// existence check. `cp()` creates each destination entry exclusively
638+
// when these options are set, so a concurrent creator cannot be clobbered.
639+
await fs.cp(validSourcePath, validDestPath, {
640+
recursive: true,
641+
force: false,
642+
errorOnExist: true,
643+
preserveTimestamps: true,
644+
});
645+
} catch (error) {
646+
if (
647+
(error as NodeJS.ErrnoException).code === "EEXIST" ||
648+
(error as NodeJS.ErrnoException).code === "ERR_FS_CP_EEXIST"
649+
) {
650+
throw new Error(`Destination already exists: ${args.destination}`);
651+
}
652+
throw error;
653+
}
654+
await fs.rm(validSourcePath, { recursive: true });
635655
const text = `Successfully moved ${args.source} to ${args.destination}`;
636656
const contentBlock = { type: "text" as const, text };
637657
return {

0 commit comments

Comments
 (0)