Merge pull request #4654 from node-red/fix-subflow-recursion-check

Prevent subflow being added to itself
This commit is contained in:
Nick O'Leary 2024-04-11 19:12:43 +01:00 committed by GitHub
commit 7ac7f9b4c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -646,6 +646,7 @@ RED.view = (function() {
} }
d3.event = event; d3.event = event;
var selected_tool = $(ui.draggable[0]).attr("data-palette-type"); var selected_tool = $(ui.draggable[0]).attr("data-palette-type");
try {
var result = createNode(selected_tool); var result = createNode(selected_tool);
if (!result) { if (!result) {
return; return;
@ -761,6 +762,13 @@ RED.view = (function() {
if (nn._def.autoedit) { if (nn._def.autoedit) {
RED.editor.edit(nn); RED.editor.edit(nn);
} }
} catch (error) {
if (error.code != "NODE_RED") {
RED.notify(RED._("notification.error",{message:error.toString()}),"error");
} else {
RED.notify(RED._("notification.error",{message:error.message}),"error");
}
}
} }
}); });
chart.on("focus", function() { chart.on("focus", function() {
@ -6063,14 +6071,19 @@ RED.view = (function() {
function createNode(type, x, y, z) { function createNode(type, x, y, z) {
const wasDirty = RED.nodes.dirty() const wasDirty = RED.nodes.dirty()
var m = /^subflow:(.+)$/.exec(type); var m = /^subflow:(.+)$/.exec(type);
var activeSubflow = z ? RED.nodes.subflow(z) : null; var activeSubflow = (z || RED.workspaces.active()) ? RED.nodes.subflow(z || RED.workspaces.active()) : null;
if (activeSubflow && m) { if (activeSubflow && m) {
var subflowId = m[1]; var subflowId = m[1];
let err
if (subflowId === activeSubflow.id) { if (subflowId === activeSubflow.id) {
throw new Error(RED._("notification.error", { message: RED._("notification.errors.cannotAddSubflowToItself") })) err = new Error(RED._("notification.errors.cannotAddSubflowToItself"))
} else if (RED.nodes.subflowContains(m[1], activeSubflow.id)) {
err = new Error(RED._("notification.errors.cannotAddCircularReference"))
} }
if (RED.nodes.subflowContains(m[1], activeSubflow.id)) { if (err) {
throw new Error(RED._("notification.error", { message: RED._("notification.errors.cannotAddCircularReference") })) err.code = 'NODE_RED'
throw err
} }
} }