mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
fix(core): Handle non-existing files when checking if it is a symlink (#18010)
This commit is contained in:
@@ -160,6 +160,26 @@ describe('isFilePathBlocked', () => {
|
||||
);
|
||||
expect(await isFilePathBlocked(allowedPath)).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle non-existent file when it is allowed', async () => {
|
||||
const filePath = '/non/existent/file';
|
||||
const error = new Error('ENOENT');
|
||||
// @ts-expect-error undefined property
|
||||
error.code = 'ENOENT';
|
||||
(fsRealpath as jest.Mock).mockRejectedValueOnce(error);
|
||||
expect(await isFilePathBlocked(filePath)).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle non-existent file when it is not allowed', async () => {
|
||||
const filePath = '/non/existent/file';
|
||||
const allowedPath = '/some/allowed/path';
|
||||
process.env[RESTRICT_FILE_ACCESS_TO] = allowedPath;
|
||||
const error = new Error('ENOENT');
|
||||
// @ts-expect-error undefined property
|
||||
error.code = 'ENOENT';
|
||||
(fsRealpath as jest.Mock).mockRejectedValueOnce(error);
|
||||
expect(await isFilePathBlocked(filePath)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileSystemHelperFunctions', () => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
writeFile as fsWriteFile,
|
||||
realpath as fsRealpath,
|
||||
} from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
import {
|
||||
BINARY_DATA_STORAGE_PATH,
|
||||
@@ -34,7 +35,17 @@ const getAllowedPaths = () => {
|
||||
|
||||
export async function isFilePathBlocked(filePath: string): Promise<boolean> {
|
||||
const allowedPaths = getAllowedPaths();
|
||||
const resolvedFilePath = await fsRealpath(filePath);
|
||||
let resolvedFilePath = '';
|
||||
try {
|
||||
resolvedFilePath = await fsRealpath(filePath);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (error.code === 'ENOENT') {
|
||||
resolvedFilePath = resolve(filePath);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const blockFileAccessToN8nFiles = process.env[BLOCK_FILE_ACCESS_TO_N8N_FILES] !== 'false';
|
||||
|
||||
const restrictedPaths = blockFileAccessToN8nFiles ? getN8nRestrictedPaths() : [];
|
||||
|
||||
Reference in New Issue
Block a user