[bug] Don't allow per user auth with accounts disabled

fe1f9fafbd3f93726a5bd4b676463dd162411abb

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +7 -3Ignore whitespace
server.js+3 -1
@@ -758,7 +758,9 @@ const postSetupTasks = async function (v6Failed, v4Failed) {
758 }758 }
759759
760 if (basicAuthMode) {760 if (basicAuthMode) {
761 if (!perUserBasicAuth) {761 if (perUserBasicAuth && !enableAccounts) {
762 console.error(color.red('Per-user basic authentication is enabled, but user accounts are disabled. This configuration may be insecure.'));
763 } else if (!perUserBasicAuth) {
762 const basicAuthUser = getConfigValue('basicAuthUser', {});764 const basicAuthUser = getConfigValue('basicAuthUser', {});
763 if (!basicAuthUser?.username || !basicAuthUser?.password) {765 if (!basicAuthUser?.username || !basicAuthUser?.password) {
764 console.warn(color.yellow('Basic Authentication is enabled, but username or password is not set or empty!'));766 console.warn(color.yellow('Basic Authentication is enabled, but username or password is not set or empty!'));
src/middleware/basicAuth.js+4 -2
@@ -7,6 +7,7 @@ const { getConfig, getConfigValue } = require('../util.js');
7const storage = require('node-persist');7const storage = require('node-persist');
88
9const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);9const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
10const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
1011
11const unauthorizedResponse = (res) => {12const unauthorizedResponse = (res) => {
12 res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');13 res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
@@ -27,13 +28,14 @@ const basicAuthMiddleware = async function (request, response, callback) {
27 return unauthorizedResponse(response);28 return unauthorizedResponse(response);
28 }29 }
2930
31 const usePerUserAuth = PER_USER_BASIC_AUTH && ENABLE_ACCOUNTS;
30 const [username, password] = Buffer.from(credentials, 'base64')32 const [username, password] = Buffer.from(credentials, 'base64')
31 .toString('utf8')33 .toString('utf8')
32 .split(':');34 .split(':');
3335
34 if (!PER_USER_BASIC_AUTH && username === config.basicAuthUser.username && password === config.basicAuthUser.password) {36 if (!usePerUserAuth && username === config.basicAuthUser.username && password === config.basicAuthUser.password) {
35 return callback();37 return callback();
36 } else if (PER_USER_BASIC_AUTH) {38 } else if (usePerUserAuth) {
37 const userHandles = await getAllUserHandles();39 const userHandles = await getAllUserHandles();
38 for (const userHandle of userHandles) {40 for (const userHandle of userHandles) {
39 if (username === userHandle) {41 if (username === userHandle) {