From df52b9b77f30f4c878ea2ecd4581d394341710bf Mon Sep 17 00:00:00 2001 From: Cosimo Ricciardi Date: Thu, 14 May 2026 11:51:10 +0200 Subject: [PATCH] Add defensive config handling for multi-node settings --- backend/controllers/shared/RTLConf.js | 36 +++++++++++++-------------- backend/utils/common.js | 2 +- server/controllers/shared/RTLConf.ts | 32 ++++++++++++------------ server/utils/common.ts | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/backend/controllers/shared/RTLConf.js b/backend/controllers/shared/RTLConf.js index e15233bc..79dbb652 100644 --- a/backend/controllers/shared/RTLConf.js +++ b/backend/controllers/shared/RTLConf.js @@ -197,26 +197,26 @@ export const getConfig = (req, res, next) => { export const updateNodeSettings = (req, res, next) => { logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'RTLConf', msg: 'Updating Node Settings..' }); const RTLConfFile = common.appConfig.rtlConfFilePath + sep + 'RTL-Config.json'; - const config = JSON.parse(fs.readFileSync(RTLConfFile, 'utf-8')); - const node = config.nodes.find((node) => (node.index === req.session.selectedNode.index)); - if (node && node.settings) { - node.settings = { ...node.settings, ...req.body.settings }; - if (node.authentication && req.body.authentication) { - if (req.body.authentication.boltzMacaroonPath) { - node.authentication.boltzMacaroonPath = req.body.authentication.boltzMacaroonPath; - } - else { - delete node.authentication.boltzMacaroonPath; - } - if (req.body.authentication.swapMacaroonPath) { - node.authentication.swapMacaroonPath = req.body.authentication.swapMacaroonPath; - } - else { - delete node.authentication.swapMacaroonPath; + try { + const config = JSON.parse(fs.readFileSync(RTLConfFile, 'utf-8')); + const node = config.nodes.find((node) => (node.index === req.session.selectedNode.index)); + if (node && node.settings) { + node.settings = { ...node.settings, ...req.body.settings }; + if (node.authentication && req.body.authentication) { + if (req.body.authentication.boltzMacaroonPath) { + node.authentication.boltzMacaroonPath = req.body.authentication.boltzMacaroonPath; + } + else { + delete node.authentication.boltzMacaroonPath; + } + if (req.body.authentication.swapMacaroonPath) { + node.authentication.swapMacaroonPath = req.body.authentication.swapMacaroonPath; + } + else { + delete node.authentication.swapMacaroonPath; + } } } - } - try { fs.writeFileSync(RTLConfFile, JSON.stringify(config, null, 2), 'utf-8'); const selectedNode = common.findNode(req.session.selectedNode.index); if (selectedNode && selectedNode.settings) { diff --git a/backend/utils/common.js b/backend/utils/common.js index ff263243..455bc5ea 100644 --- a/backend/utils/common.js +++ b/backend/utils/common.js @@ -70,7 +70,7 @@ export class CommonService { config.secret2FA = this.appConfig.secret2FA; } const appConfigNodes = new Map(this.appConfig.nodes?.map((node) => [node.index, node])); - config.nodes.forEach((node) => { + config.nodes?.forEach((node) => { const appConfigNode = appConfigNodes.get(node.index); if (appConfigNode?.authentication) { if (appConfigNode.authentication.macaroonPath) { diff --git a/server/controllers/shared/RTLConf.ts b/server/controllers/shared/RTLConf.ts index 447ce6fc..dae540d3 100644 --- a/server/controllers/shared/RTLConf.ts +++ b/server/controllers/shared/RTLConf.ts @@ -201,24 +201,24 @@ export const getConfig = (req, res, next) => { export const updateNodeSettings = (req, res, next) => { logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'RTLConf', msg: 'Updating Node Settings..' }); const RTLConfFile = common.appConfig.rtlConfFilePath + sep + 'RTL-Config.json'; - const config = JSON.parse(fs.readFileSync(RTLConfFile, 'utf-8')); - const node = config.nodes.find((node) => (node.index === req.session.selectedNode.index)); - if (node && node.settings) { - node.settings = { ...node.settings, ...req.body.settings }; - if (node.authentication && req.body.authentication) { - if (req.body.authentication.boltzMacaroonPath) { - node.authentication.boltzMacaroonPath = req.body.authentication.boltzMacaroonPath; - } else { - delete node.authentication.boltzMacaroonPath; - } - if (req.body.authentication.swapMacaroonPath) { - node.authentication.swapMacaroonPath = req.body.authentication.swapMacaroonPath; - } else { - delete node.authentication.swapMacaroonPath; + try { + const config = JSON.parse(fs.readFileSync(RTLConfFile, 'utf-8')); + const node = config.nodes.find((node) => (node.index === req.session.selectedNode.index)); + if (node && node.settings) { + node.settings = { ...node.settings, ...req.body.settings }; + if (node.authentication && req.body.authentication) { + if (req.body.authentication.boltzMacaroonPath) { + node.authentication.boltzMacaroonPath = req.body.authentication.boltzMacaroonPath; + } else { + delete node.authentication.boltzMacaroonPath; + } + if (req.body.authentication.swapMacaroonPath) { + node.authentication.swapMacaroonPath = req.body.authentication.swapMacaroonPath; + } else { + delete node.authentication.swapMacaroonPath; + } } } - } - try { fs.writeFileSync(RTLConfFile, JSON.stringify(config, null, 2), 'utf-8'); const selectedNode = common.findNode(req.session.selectedNode.index); if (selectedNode && selectedNode.settings) { diff --git a/server/utils/common.ts b/server/utils/common.ts index 714ea1ce..53c27b57 100644 --- a/server/utils/common.ts +++ b/server/utils/common.ts @@ -79,7 +79,7 @@ export class CommonService { config.secret2FA = this.appConfig.secret2FA; } const appConfigNodes = new Map(this.appConfig.nodes?.map((node) => [node.index, node])); - config.nodes.forEach((node) => { + config.nodes?.forEach((node) => { const appConfigNode = appConfigNodes.get(node.index); if (appConfigNode?.authentication) { if (appConfigNode.authentication.macaroonPath) {