[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) {
758758 }
759759
760760 if (basicAuthMode) {
761761 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) {
762764 const basicAuthUser = getConfigValue('basicAuthUser', {});
763765 if (!basicAuthUser?.username || !basicAuthUser?.password) {
764766 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');
77const storage = require('node-persist');
88
99const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
10+const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
1011
1112const unauthorizedResponse = (res) => {
1213 res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
@@ -27,13 +28,14 @@ const basicAuthMiddleware = async function (request, response, callback) {
2728 return unauthorizedResponse(response);
2829 }
2930
31+ const usePerUserAuth = PER_USER_BASIC_AUTH && ENABLE_ACCOUNTS;
3032 const [username, password] = Buffer.from(credentials, 'base64')
3133 .toString('utf8')
3234 .split(':');
3335
3436 if (!PER_USER_BASIC_AUTHusePerUserAuth && username === config.basicAuthUser.username && password === config.basicAuthUser.password) {
3537 return callback();
3638 } else if (PER_USER_BASIC_AUTHusePerUserAuth) {
3739 const userHandles = await getAllUserHandles();
3840 for (const userHandle of userHandles) {
3941 if (username === userHandle) {