mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
fix(config/scripts): Enhance User Creation and Ban Handling, Standardize Imports (#1144)
* chore: use relative imports for scripts * fix(create-user): newUser.save() now properly awaited, double-check user creation, use relative imports, catch exception * fix(ban-user): catch exception, handle case where IP is undefined, proper check of user ban on login
This commit is contained in:
parent
a2ee57568a
commit
5f3ecef575
7 changed files with 102 additions and 27 deletions
8
api/cache/banViolation.js
vendored
8
api/cache/banViolation.js
vendored
|
|
@ -54,11 +54,17 @@ const banViolation = async (req, res, errorMessage) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
req.ip = removePorts(req);
|
req.ip = removePorts(req);
|
||||||
console.log(`[BAN] Banning user ${user_id} @ ${req.ip} for ${duration / 1000 / 60} minutes`);
|
console.log(
|
||||||
|
`[BAN] Banning user ${user_id} ${req.ip ? `@ ${req.ip} ` : ''}for ${
|
||||||
|
duration / 1000 / 60
|
||||||
|
} minutes`,
|
||||||
|
);
|
||||||
|
|
||||||
const expiresAt = Date.now() + duration;
|
const expiresAt = Date.now() + duration;
|
||||||
await banLogs.set(user_id, { type, violation_count, duration, expiresAt });
|
await banLogs.set(user_id, { type, violation_count, duration, expiresAt });
|
||||||
|
if (req.ip) {
|
||||||
await banLogs.set(req.ip, { type, user_id, violation_count, duration, expiresAt });
|
await banLogs.set(req.ip, { type, user_id, violation_count, duration, expiresAt });
|
||||||
|
}
|
||||||
|
|
||||||
errorMessage.ban = true;
|
errorMessage.ban = true;
|
||||||
errorMessage.ban_duration = duration;
|
errorMessage.ban_duration = duration;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
const Keyv = require('keyv');
|
const Keyv = require('keyv');
|
||||||
const uap = require('ua-parser-js');
|
const uap = require('ua-parser-js');
|
||||||
const { getLogStores } = require('../../cache');
|
|
||||||
const denyRequest = require('./denyRequest');
|
const denyRequest = require('./denyRequest');
|
||||||
|
const { getLogStores } = require('../../cache');
|
||||||
const { isEnabled, removePorts } = require('../utils');
|
const { isEnabled, removePorts } = require('../utils');
|
||||||
const keyvRedis = require('../../cache/keyvRedis');
|
const keyvRedis = require('../../cache/keyvRedis');
|
||||||
|
const User = require('../../models/User');
|
||||||
|
|
||||||
const banCache = isEnabled(process.env.USE_REDIS)
|
const banCache = isEnabled(process.env.USE_REDIS)
|
||||||
? new Keyv({ store: keyvRedis })
|
? new Keyv({ store: keyvRedis })
|
||||||
|
|
@ -52,12 +53,33 @@ const checkBan = async (req, res, next = () => {}) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
req.ip = removePorts(req);
|
req.ip = removePorts(req);
|
||||||
const userId = req.user?.id ?? req.user?._id ?? null;
|
let userId = req.user?.id ?? req.user?._id ?? null;
|
||||||
const ipKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:ip:${req.ip}` : req.ip;
|
|
||||||
const userKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:user:${userId}` : userId;
|
if (!userId && req?.body?.email) {
|
||||||
|
const user = await User.findOne({ email: req.body.email }, '_id').lean();
|
||||||
|
userId = user?._id ? user._id.toString() : userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId && !req.ip) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
let cachedIPBan;
|
||||||
|
let cachedUserBan;
|
||||||
|
|
||||||
|
let ipKey = '';
|
||||||
|
let userKey = '';
|
||||||
|
|
||||||
|
if (req.ip) {
|
||||||
|
ipKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:ip:${req.ip}` : req.ip;
|
||||||
|
cachedIPBan = await banCache.get(ipKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
userKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:user:${userId}` : userId;
|
||||||
|
cachedUserBan = await banCache.get(userKey);
|
||||||
|
}
|
||||||
|
|
||||||
const cachedIPBan = await banCache.get(ipKey);
|
|
||||||
const cachedUserBan = await banCache.get(userKey);
|
|
||||||
const cachedBan = cachedIPBan || cachedUserBan;
|
const cachedBan = cachedIPBan || cachedUserBan;
|
||||||
|
|
||||||
if (cachedBan) {
|
if (cachedBan) {
|
||||||
|
|
@ -72,9 +94,18 @@ const checkBan = async (req, res, next = () => {}) => {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const ipBan = await banLogs.get(req.ip);
|
let ipBan;
|
||||||
const userBan = await banLogs.get(userId);
|
let userBan;
|
||||||
const isBanned = ipBan || userBan;
|
|
||||||
|
if (req.ip) {
|
||||||
|
ipBan = await banLogs.get(req.ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
userBan = await banLogs.get(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBanned = !!(ipBan || userBan);
|
||||||
|
|
||||||
if (!isBanned) {
|
if (!isBanned) {
|
||||||
return next();
|
return next();
|
||||||
|
|
@ -82,14 +113,23 @@ const checkBan = async (req, res, next = () => {}) => {
|
||||||
|
|
||||||
const timeLeft = Number(isBanned.expiresAt) - Date.now();
|
const timeLeft = Number(isBanned.expiresAt) - Date.now();
|
||||||
|
|
||||||
if (timeLeft <= 0) {
|
if (timeLeft <= 0 && ipKey) {
|
||||||
await banLogs.delete(ipKey);
|
await banLogs.delete(ipKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timeLeft <= 0 && userKey) {
|
||||||
await banLogs.delete(userKey);
|
await banLogs.delete(userKey);
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ipKey) {
|
||||||
banCache.set(ipKey, isBanned, timeLeft);
|
banCache.set(ipKey, isBanned, timeLeft);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userKey) {
|
||||||
banCache.set(userKey, isBanned, timeLeft);
|
banCache.set(userKey, isBanned, timeLeft);
|
||||||
|
}
|
||||||
|
|
||||||
req.banned = true;
|
req.banned = true;
|
||||||
return await banResponse(req, res);
|
return await banResponse(req, res);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ const registerUser = async (user) => {
|
||||||
const salt = bcrypt.genSaltSync(10);
|
const salt = bcrypt.genSaltSync(10);
|
||||||
const hash = bcrypt.hashSync(newUser.password, salt);
|
const hash = bcrypt.hashSync(newUser.password, salt);
|
||||||
newUser.password = hash;
|
newUser.password = hash;
|
||||||
newUser.save();
|
await newUser.save();
|
||||||
|
|
||||||
return { status: 200, user: newUser };
|
return { status: 200, user: newUser };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -117,7 +117,7 @@ const requestPasswordReset = async (email) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
let resetToken = crypto.randomBytes(32).toString('hex');
|
let resetToken = crypto.randomBytes(32).toString('hex');
|
||||||
const hash = await bcrypt.hashSync(resetToken, 10);
|
const hash = bcrypt.hashSync(resetToken, 10);
|
||||||
|
|
||||||
await new Token({
|
await new Token({
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ const updateUserPluginAuth = async (userId, authField, pluginKey, value) => {
|
||||||
value: encryptedValue,
|
value: encryptedValue,
|
||||||
pluginKey,
|
pluginKey,
|
||||||
});
|
});
|
||||||
newPluginAuth.save();
|
await newPluginAuth.save();
|
||||||
return newPluginAuth;
|
return newPluginAuth;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const connectDb = require('@librechat/backend/lib/db/connectDb');
|
const connectDb = require('../api/lib/db/connectDb');
|
||||||
const { askQuestion, silentExit } = require('./helpers');
|
const { askQuestion, silentExit } = require('./helpers');
|
||||||
const User = require('@librechat/backend/models/User');
|
const User = require('../api/models/User');
|
||||||
const Transaction = require('@librechat/backend/models/Transaction');
|
const Transaction = require('../api/models/Transaction');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const connectDb = require('@librechat/backend/lib/db/connectDb');
|
const connectDb = require('../api/lib/db/connectDb');
|
||||||
const { askQuestion, silentExit } = require('./helpers');
|
const { askQuestion, silentExit } = require('./helpers');
|
||||||
const banViolation = require('../api/cache/banViolation');
|
const banViolation = require('../api/cache/banViolation');
|
||||||
const User = require('@librechat/backend/models/User');
|
const User = require('../api/models/User');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
/**
|
/**
|
||||||
|
|
@ -97,3 +97,16 @@ const User = require('@librechat/backend/models/User');
|
||||||
|
|
||||||
silentExit(0);
|
silentExit(0);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
process.on('uncaughtException', (err) => {
|
||||||
|
if (!err.message.includes('fetch failed')) {
|
||||||
|
console.error('There was an uncaught error:');
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err.message.includes('fetch failed')) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const connectDb = require('@librechat/backend/lib/db/connectDb');
|
const connectDb = require('../api/lib/db/connectDb');
|
||||||
const { registerUser } = require('@librechat/backend/server/services/AuthService');
|
const { registerUser } = require('../api/server/services/AuthService');
|
||||||
const { askQuestion, silentExit } = require('./helpers');
|
const { askQuestion, silentExit } = require('./helpers');
|
||||||
const User = require('@librechat/backend/models/User');
|
const User = require('../api/models/User');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
/**
|
/**
|
||||||
|
|
@ -127,6 +127,22 @@ const User = require('@librechat/backend/models/User');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Done!
|
// Done!
|
||||||
|
const userCreated = await User.findOne({ $or: [{ email }, { username }] });
|
||||||
|
if (userCreated) {
|
||||||
console.green('User created successfully!');
|
console.green('User created successfully!');
|
||||||
silentExit(0);
|
silentExit(0);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
process.on('uncaughtException', (err) => {
|
||||||
|
if (!err.message.includes('fetch failed')) {
|
||||||
|
console.error('There was an uncaught error:');
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err.message.includes('fetch failed')) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue