From 51d8ccfe42a2a9073410ab6e07b1d22ceffe81d5 Mon Sep 17 00:00:00 2001 From: Adam Stachowicz Date: Fri, 5 Jan 2024 09:48:25 +0100 Subject: [PATCH 1/4] Fix encodeBase64 for empty password or user --- server/model/monitor.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index e407e5a2d8..f52de66280 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -233,7 +233,15 @@ class Monitor extends BeanModel { * @returns {string} */ encodeBase64(user, pass) { - return Buffer.from(user + ":" + pass).toString("base64"); + if (!user) { + return Buffer.from(`:${pass}`).toString("base64"); + } + + if (!pass) { + return Buffer.from(`${user}:`).toString("base64"); + } + + return Buffer.from(`${user}:${pass}`).toString("base64"); } /** From dbf492947ec7cc9ff427f560141febe62931e7dd Mon Sep 17 00:00:00 2001 From: Adam Stachowicz Date: Fri, 5 Jan 2024 22:10:31 +0100 Subject: [PATCH 2/4] Simplify `encodeBase64` --- server/model/monitor.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index f52de66280..451924ccae 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -233,15 +233,7 @@ class Monitor extends BeanModel { * @returns {string} */ encodeBase64(user, pass) { - if (!user) { - return Buffer.from(`:${pass}`).toString("base64"); - } - - if (!pass) { - return Buffer.from(`${user}:`).toString("base64"); - } - - return Buffer.from(`${user}:${pass}`).toString("base64"); + return Buffer.from(`${user || ''}:${pass || ''}`).toString("base64"); } /** From 9dff412fe47fa6321e2ca5792a92c9c7dd6645a0 Mon Sep 17 00:00:00 2001 From: Adam Stachowicz Date: Sat, 6 Jan 2024 00:12:11 +0100 Subject: [PATCH 3/4] Fix ESLint --- server/model/monitor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 451924ccae..717abafd7d 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -233,7 +233,7 @@ class Monitor extends BeanModel { * @returns {string} */ encodeBase64(user, pass) { - return Buffer.from(`${user || ''}:${pass || ''}`).toString("base64"); + return Buffer.from(`${user || ""}:${pass || ""}`).toString("base64"); } /** From ce2c61f3b80ab1bb19f191197b7c6a07f434fc2e Mon Sep 17 00:00:00 2001 From: Adam Stachowicz Date: Sat, 6 Jan 2024 16:02:54 +0100 Subject: [PATCH 4/4] Update JSDoc --- server/model/monitor.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/model/monitor.js b/server/model/monitor.js index 717abafd7d..2dc77fbd3d 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -230,6 +230,8 @@ class Monitor extends BeanModel { /** * Encode user and password to Base64 encoding * for HTTP "basic" auth, as per RFC-7617 + * @param {string|null} user - The username (nullable if not changed by a user) + * @param {string|null} pass - The password (nullable if not changed by a user) * @returns {string} */ encodeBase64(user, pass) {