mirror of
https://github.com/hanzoai/chat.git
synced 2026-09-30 20:51:17 +00:00
Two independent defects met in one feature, and either one alone made `BAN_VIOLATIONS=true` a trap. The store. `keyvMongo` backed `ViolationTypes.BAN` and `CacheKeys.ENCODED_DOMAINS` and threw `Mongoose connection not ready` the moment either was touched — `MONGO_URI` is unset and every collection is served from SQLite. Both namespaces hold state that must outlive the process: forgetting a ban un-bans a banned user, forgetting an encoded domain orphans every action tool that used it, so `standardCache`'s process memory is not a substitute. `durableCache` adds SQLite as the fallback store instead — a two-column `keyv` table on the one `sharedDatabase()` connection the document store already holds, so there is no second writer racing for the WAL lock. Redis still wins when it is configured; nothing about that path changes. The middleware. Upstream #12324 fixed `checkBan`'s `!!(ipBan||userBan)` coercion and the four defects it hid. The fork took that commit's tests and lost its implementation, so `checkBan.js` still read `Number(true.expiresAt)` → `NaN`: expired bans were never cleaned up, the memo was written with no expiry at all, and an IP-keyed entry blocked whoever next inherited that address — permanently. The test suite did not merely fail, it crashed the runner on an unhandled rejection from the un-awaited `banCache.set`. Restored, on the hanzo imports. Also: `redisClients.ts` imported `KV` twice, once as a value and once as a type. tsc tolerates it (separate declaration spaces); babel rejects it outright, which took every jest suite whose graph reached `~/utils` down with it — together with the untransformed ESM in `@mistralai`. Both fixed, which is what lets `durableCache` be tested at all: 37 suites in `packages/api` went from never loading to running. Proof: 25/25 checkBan, 6/6 keyv store (incl. durableCache resolving to the file and surviving a reopen). End-to-end against the built bundles with mongoose at readyState 0 — write via banLogs, reopen, read back, three rows in `keyv`, namespaces disjoint. Co-authored-by: Hanzo Dev <[email protected]>
153 lines
4.8 KiB
JavaScript
153 lines
4.8 KiB
JavaScript
const uap = require('ua-parser-js');
|
|
const { logger } = require('@hanzochat/data-schemas');
|
|
const { isEnabled, durableCache } = require('@hanzochat/api');
|
|
const { ViolationTypes } = require('@hanzochat/data-provider');
|
|
const { removePorts } = require('~/server/utils');
|
|
const denyRequest = require('./denyRequest');
|
|
const { getLogStores } = require('~/cache');
|
|
const { findUser } = require('~/models');
|
|
|
|
/** Read-through memo in front of the authoritative ban store; own namespace, per-entry TTL. */
|
|
const banCache = durableCache(ViolationTypes.BAN, 0);
|
|
const message = 'Your account has been temporarily banned due to violations of our service.';
|
|
|
|
/** @returns {string} Memo key for a ban lookup — prefixed under Redis, raw otherwise. */
|
|
const banCacheKey = (prefix, value, useRedis) => {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
return useRedis ? `ban_cache:${prefix}:${value}` : value;
|
|
};
|
|
|
|
/**
|
|
* Respond to the request if the user is banned.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} req - Express Request object.
|
|
* @param {Object} res - Express Response object.
|
|
*
|
|
* @returns {Promise<Object>} - Returns a Promise which when resolved sends a response status of 403 with a specific message if request is not of api/agents/chat. If it is, calls `denyRequest()` function.
|
|
*/
|
|
const banResponse = async (req, res) => {
|
|
const ua = uap(req.headers['user-agent']);
|
|
const { baseUrl, originalUrl } = req;
|
|
if (!ua.browser.name) {
|
|
return res.status(403).json({ message });
|
|
} else if (baseUrl === '/v1/chat/agents' && originalUrl.startsWith('/v1/chat/agents/chat')) {
|
|
return await denyRequest(req, res, { type: ViolationTypes.BAN });
|
|
}
|
|
|
|
return res.status(403).json({ message });
|
|
};
|
|
|
|
/**
|
|
* Checks if the source IP or user is banned or not.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} req - Express request object.
|
|
* @param {Object} res - Express response object.
|
|
* @param {import('express').NextFunction} next - Next middleware function.
|
|
*
|
|
* @returns {Promise<function|Object>} - Returns a Promise which when resolved calls next middleware if user or source IP is not banned. Otherwise calls `banResponse()` and sets ban details in `banCache`.
|
|
*/
|
|
const checkBan = async (req, res, next = () => {}) => {
|
|
try {
|
|
const { BAN_VIOLATIONS } = process.env ?? {};
|
|
|
|
if (!isEnabled(BAN_VIOLATIONS)) {
|
|
return next();
|
|
}
|
|
|
|
req.ip = removePorts(req);
|
|
let userId = req.user?.id ?? req.user?._id ?? null;
|
|
|
|
if (!userId && req?.body?.email) {
|
|
const user = await findUser({ email: req.body.email }, '_id');
|
|
userId = user?._id ? user._id.toString() : userId;
|
|
}
|
|
|
|
if (!userId && !req.ip) {
|
|
return next();
|
|
}
|
|
|
|
const useRedis = isEnabled(process.env.USE_REDIS);
|
|
const ipKey = banCacheKey('ip', req.ip, useRedis);
|
|
const userKey = banCacheKey('user', userId, useRedis);
|
|
|
|
const [cachedIPBan, cachedUserBan] = await Promise.all([
|
|
ipKey ? banCache.get(ipKey) : undefined,
|
|
userKey ? banCache.get(userKey) : undefined,
|
|
]);
|
|
|
|
if (cachedIPBan || cachedUserBan) {
|
|
req.banned = true;
|
|
return await banResponse(req, res);
|
|
}
|
|
|
|
const banLogs = getLogStores(ViolationTypes.BAN);
|
|
const duration = banLogs.opts.ttl;
|
|
|
|
if (duration <= 0) {
|
|
return next();
|
|
}
|
|
|
|
// `banLogs` is keyed by the raw ip / user id — `ipKey` and `userKey` are the
|
|
// memo's keys and carry a prefix under Redis, so they must not be used here.
|
|
const [ipBan, userBan] = await Promise.all([
|
|
req.ip ? banLogs.get(req.ip) : undefined,
|
|
userId ? banLogs.get(userId) : undefined,
|
|
]);
|
|
|
|
const ban = ipBan || userBan;
|
|
|
|
if (!ban) {
|
|
return next();
|
|
}
|
|
|
|
// A record with no readable expiry is a ban that cannot be timed out. Deny
|
|
// it, but never memo it — a NaN ttl is stored as no expiry at all, and the
|
|
// entry (IP-keyed, so shared) would outlive the ban forever.
|
|
const expiresAt = Number(ban.expiresAt);
|
|
if (!ban.expiresAt || Number.isNaN(expiresAt)) {
|
|
req.banned = true;
|
|
return await banResponse(req, res);
|
|
}
|
|
|
|
const timeLeft = expiresAt - Date.now();
|
|
|
|
if (timeLeft <= 0) {
|
|
const cleanups = [];
|
|
if (ipBan) {
|
|
cleanups.push(banLogs.delete(req.ip));
|
|
}
|
|
if (userBan) {
|
|
cleanups.push(banLogs.delete(userId));
|
|
}
|
|
await Promise.all(cleanups);
|
|
return next();
|
|
}
|
|
|
|
const writes = [];
|
|
if (ipKey) {
|
|
writes.push(banCache.set(ipKey, ban, timeLeft));
|
|
}
|
|
if (userKey) {
|
|
writes.push(banCache.set(userKey, ban, timeLeft));
|
|
}
|
|
// The memo is an optimization; a failed write must not un-ban the request.
|
|
await Promise.all(writes).catch((err) =>
|
|
logger.warn('[checkBan] Failed to write ban cache:', err),
|
|
);
|
|
|
|
req.banned = true;
|
|
return await banResponse(req, res);
|
|
} catch (error) {
|
|
logger.error('Error in checkBan middleware:', error);
|
|
return next(error);
|
|
}
|
|
};
|
|
|
|
module.exports = checkBan;
|