From a8388259ab5929ddfad531c570152b2c4a73af82 Mon Sep 17 00:00:00 2001 From: Lumi Date: Sat, 30 Mar 2024 19:57:23 +0100 Subject: [PATCH 1/2] Update server.js Print warning if basicAuth username or password fails to parse. In a normal case the user has no way to be informed if the username or password fails to parse. While this might end up being a skill issue on the users side it could help them to troubleshoot the issue. --- server.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server.js b/server.js index 538679dbc..8aa2397a6 100644 --- a/server.js +++ b/server.js @@ -655,6 +655,18 @@ const setupTasks = async function () { if (listen) { console.log('\n0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). If you want to limit it only to internal localhost (127.0.0.1), change the setting in config.yaml to "listen: false". Check "access.log" file in the SillyTavern directory if you want to inspect incoming connections.\n'); } + + if (getConfigValue("basicAuthMode", false)) { + const basicAuthUser = getConfigValue("basicAuthUser", null); + if (!basicAuthUser.username || !basicAuthUser.password) { + console.warn( + color.yellow( + "Basic Authentication is set, but username or password is not set or empty!" + ) + ); + } + } + }; /** From af6deda64d1e687e711a0b181f231c21e24736a2 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 30 Mar 2024 22:46:18 +0200 Subject: [PATCH 2/2] Null safety + reuse variable --- server.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index a57b082cc..af8e692b7 100644 --- a/server.js +++ b/server.js @@ -109,7 +109,8 @@ app.use(responseTime()); const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT); const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl; const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN); -const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY) +const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY); +const basicAuthMode = getConfigValue('basicAuthMode', false); const { DIRECTORIES, UPLOADS_PATH } = require('./src/constants'); @@ -121,7 +122,7 @@ const CORS = cors({ app.use(CORS); -if (listen && getConfigValue('basicAuthMode', false)) app.use(basicAuthMiddleware); +if (listen && basicAuthMode) app.use(basicAuthMiddleware); app.use(whitelistMiddleware(listen)); @@ -516,14 +517,10 @@ const setupTasks = async function () { console.log('\n0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). If you want to limit it only to internal localhost (127.0.0.1), change the setting in config.yaml to "listen: false". Check "access.log" file in the SillyTavern directory if you want to inspect incoming connections.\n'); } - if (getConfigValue("basicAuthMode", false)) { - const basicAuthUser = getConfigValue("basicAuthUser", null); - if (!basicAuthUser.username || !basicAuthUser.password) { - console.warn( - color.yellow( - "Basic Authentication is set, but username or password is not set or empty!" - ) - ); + if (basicAuthMode) { + const basicAuthUser = getConfigValue('basicAuthUser', {}); + if (!basicAuthUser?.username || !basicAuthUser?.password) { + console.warn(color.yellow('Basic Authentication is enabled, but username or password is not set or empty!')); } } @@ -541,7 +538,7 @@ async function loadPlugins() { return cleanupPlugins; } catch { console.log('Plugin loading failed.'); - return () => {}; + return () => { }; } }