Skip to content

Commit b0bfe0a

Browse files
committed
remove copy/paste
1 parent 86888d9 commit b0bfe0a

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

js/node_helper.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require("express");
22
const Log = require("logger");
33
const Class = require("./class");
4+
const { replaceSecretPlaceholder } = require("#server_functions");
45

56
const NodeHelper = Class.extend({
67
init () {
@@ -90,9 +91,7 @@ const NodeHelper = Class.extend({
9091
socket.onAny((notification, payload) => {
9192
if (config.hideConfigSecrets && payload && typeof payload === "object") {
9293
try {
93-
const payloadStr = JSON.stringify(payload).replaceAll(/\*\*(SECRET_[^*]+)\*\*/g, (match, group) => {
94-
return process.env[group];
95-
});
94+
const payloadStr = replaceSecretPlaceholder(JSON.stringify(payload));
9695
this.socketNotificationReceived(notification, JSON.parse(payloadStr));
9796
} catch (e) {
9897
Log.error("Error substituting variables in payload: ", e);

js/server_functions.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ function getStartup (req, res) {
1313
res.send(startUp);
1414
}
1515

16+
/**
17+
* A method that replaces the secret placeholders `**SECRET_ABC**` with the environment variable SECRET_ABC
18+
* @param {string} input - the input string
19+
* @returns {string} the input with real variable content
20+
*/
21+
function replaceSecretPlaceholder (input) {
22+
return input.replaceAll(/\*\*(SECRET_[^*]+)\*\*/g, (match, group) => {
23+
return process.env[group];
24+
});
25+
}
26+
1627
/**
1728
* A method that forwards HTTP Get-methods to the internet to avoid CORS-errors.
1829
*
@@ -35,10 +46,10 @@ async function cors (req, res) {
3546
return res.status(400).send(url);
3647
} else {
3748
url = match[1];
38-
if (config.hideConfigSecrets) {
39-
url = url.replaceAll(/\*\*(SECRET_[^*]+)\*\*/g, (match, group) => {
40-
return process.env[group];
41-
});
49+
if (typeof config !== "undefined") {
50+
if (config.hideConfigSecrets) {
51+
url = replaceSecretPlaceholder(url);
52+
}
4253
}
4354

4455
const headersToSend = getHeadersToSend(req.url);
@@ -191,4 +202,4 @@ function getConfigFilePath () {
191202
return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`);
192203
}
193204

194-
module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath };
205+
module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath, replaceSecretPlaceholder };

0 commit comments

Comments
 (0)