diff --git a/packages/nodes-base/nodes/Files/ReadWriteFile/actions/read.operation.ts b/packages/nodes-base/nodes/Files/ReadWriteFile/actions/read.operation.ts index 101ca70ab767c..9d34b46c57572 100644 --- a/packages/nodes-base/nodes/Files/ReadWriteFile/actions/read.operation.ts +++ b/packages/nodes-base/nodes/Files/ReadWriteFile/actions/read.operation.ts @@ -7,7 +7,7 @@ import type { } from 'n8n-workflow'; import glob from 'fast-glob'; -import { errorMapper } from '../helpers/utils'; +import { errorMapper, escapeSpecialCharacters } from '../helpers/utils'; import { updateDisplayOptions } from '@utils/utilities'; export const properties: INodeProperties[] = [ @@ -82,6 +82,8 @@ export async function execute(this: IExecuteFunctions, items: INodeExecutionData try { fileSelector = String(this.getNodeParameter('fileSelector', itemIndex)); + fileSelector = escapeSpecialCharacters(fileSelector); + if (/^[a-zA-Z]:/.test(fileSelector)) { fileSelector = fileSelector.replace(/\\\\/g, '/'); } diff --git a/packages/nodes-base/nodes/Files/ReadWriteFile/helpers/utils.ts b/packages/nodes-base/nodes/Files/ReadWriteFile/helpers/utils.ts index 8ecbb66706f29..7e487227d0c46 100644 --- a/packages/nodes-base/nodes/Files/ReadWriteFile/helpers/utils.ts +++ b/packages/nodes-base/nodes/Files/ReadWriteFile/helpers/utils.ts @@ -30,3 +30,10 @@ export function errorMapper( return new NodeOperationError(this.getNode(), error, { itemIndex, message, description }); } + +export function escapeSpecialCharacters(str: string) { + // Escape parentheses + str = str.replace(/[()]/g, '\\$&'); + + return str; +} diff --git a/packages/nodes-base/nodes/Files/ReadWriteFile/test/utils.test.ts b/packages/nodes-base/nodes/Files/ReadWriteFile/test/utils.test.ts new file mode 100644 index 0000000000000..fb9dece3b2052 --- /dev/null +++ b/packages/nodes-base/nodes/Files/ReadWriteFile/test/utils.test.ts @@ -0,0 +1,15 @@ +import { escapeSpecialCharacters } from '../helpers/utils'; + +describe('Read/Write Files from Disk, escapeSpecialCharacters', () => { + it('should escape parentheses in a string', () => { + const input = '/home/michael/Desktop/test(1).txt'; + const expectedOutput = '/home/michael/Desktop/test\\(1\\).txt'; + expect(escapeSpecialCharacters(input)).toBe(expectedOutput); + }); + + it('should not modify strings that do not contain parentheses', () => { + const input = '/home/michael/Desktop/test.txt'; + const expectedOutput = '/home/michael/Desktop/test.txt'; + expect(escapeSpecialCharacters(input)).toBe(expectedOutput); + }); +});