Skip to content

Commit 1dc3032

Browse files
authored
allow environment variables in cors urls (#4033)
and centralize and optimize replace regex. Another follow up to #4029 With this PR you can use secrets in urls in browser modules if you use the cors proxy.
1 parent 172ca18 commit 1dc3032

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

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: 17 additions & 1 deletion
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,6 +46,11 @@ async function cors (req, res) {
3546
return res.status(400).send(url);
3647
} else {
3748
url = match[1];
49+
if (typeof config !== "undefined") {
50+
if (config.hideConfigSecrets) {
51+
url = replaceSecretPlaceholder(url);
52+
}
53+
}
3854

3955
const headersToSend = getHeadersToSend(req.url);
4056
const expectedReceivedHeaders = geExpectedReceivedHeaders(req.url);
@@ -186,4 +202,4 @@ function getConfigFilePath () {
186202
return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`);
187203
}
188204

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

tests/unit/functions/server_functions_spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
const { cors, getUserAgent } = require("#server_functions");
1+
const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions");
22

33
describe("server_functions tests", () => {
4+
describe("The replaceSecretPlaceholder method", () => {
5+
it("Calls string without secret placeholder", () => {
6+
const teststring = "test string without secret placeholder";
7+
const result = replaceSecretPlaceholder(teststring);
8+
expect(result).toBe(teststring);
9+
});
10+
11+
it("Calls string with 2 secret placeholders", () => {
12+
const teststring = "test string with secret1=**SECRET_ONE** and secret2=**SECRET_TWO**";
13+
process.env.SECRET_ONE = "secret1";
14+
process.env.SECRET_TWO = "secret2";
15+
const resultstring = `test string with secret1=${process.env.SECRET_ONE} and secret2=${process.env.SECRET_TWO}`;
16+
const result = replaceSecretPlaceholder(teststring);
17+
expect(result).toBe(resultstring);
18+
});
19+
});
20+
421
describe("The cors method", () => {
522
let fetchResponse;
623
let fetchResponseHeadersGet;

0 commit comments

Comments
 (0)