fix(editor): Prevent node renaming to restricted JS method names (#16270)

This commit is contained in:
oleg
2025-06-12 16:11:36 +02:00
committed by GitHub
parent 739ad853cd
commit ecfb6674ef
4 changed files with 188 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ import type {
INodeConnections,
WorkflowExecuteMode,
} from 'n8n-workflow';
import { NodeConnectionTypes, NodeHelpers } from 'n8n-workflow';
import { NodeConnectionTypes, NodeHelpers, UserError } from 'n8n-workflow';
import { useCanvasOperations } from '@/composables/useCanvasOperations';
import type { CanvasConnection, CanvasNode } from '@/types';
import { CanvasConnectionMode } from '@/types';
@@ -1041,6 +1041,35 @@ describe('useCanvasOperations', () => {
expect(ndvStore.activeNodeName).toBe(oldName);
});
it('should show error toast when renameNode throws an error', async () => {
const workflowsStore = mockedStore(useWorkflowsStore);
const ndvStore = mockedStore(useNDVStore);
const toast = useToast();
const oldName = 'Old Node';
const newName = 'New Node';
const errorMessage = 'Node name already exists';
const errorDescription = 'Please choose a different name';
const workflowObject = createTestWorkflowObject();
workflowObject.renameNode = vi.fn().mockImplementation(() => {
const error = new UserError(errorMessage, { description: errorDescription });
throw error;
});
workflowsStore.getCurrentWorkflow.mockReturnValue(workflowObject);
workflowsStore.getNodeByName = vi.fn().mockReturnValue({ name: oldName });
ndvStore.activeNodeName = oldName;
const { renameNode } = useCanvasOperations();
await renameNode(oldName, newName);
expect(workflowObject.renameNode).toHaveBeenCalledWith(oldName, newName);
expect(toast.showMessage).toHaveBeenCalledWith({
type: 'error',
title: errorMessage,
message: errorDescription,
});
});
});
describe('revertRenameNode', () => {

View File

@@ -301,7 +301,16 @@ export function useCanvasOperations() {
// Rename the node and update the connections
const workflow = workflowsStore.getCurrentWorkflow(true);
workflow.renameNode(currentName, newName);
try {
workflow.renameNode(currentName, newName);
} catch (error) {
toast.showMessage({
type: 'error',
title: error.message,
message: error.description,
});
return;
}
if (trackHistory) {
historyStore.pushCommandToUndo(new RenameNodeCommand(currentName, newName, Date.now()));