Auto-Commit: 2026-10-28 08:51:46
This commit is contained in:
1 parent
ff34e0865f
commit
d32eabf485
1 file changed
+40
-2
+40
-2
@@ -1,5 +1,8 @@
|
||||
import { Hono } from 'hono';
|
||||
import { cors } from 'hono/cors';
|
||||
import { bodyLimit } from 'hono/body-limit';
|
||||
import { adminGuard } from './auth.js';
|
||||
import { rateLimit } from './rateLimit.js';
|
||||
import { pool, initDb, uid, getChars, safeJson, getState, setState, updateUserActivity, getUserActivityStatus, isUserActive, banCharacter, getAllSettings, saveSettings, updateCharacter, getBudgetStatus } from './db.js';
|
||||
import { SEED_CHARACTERS, personaSystemPrompt, buildDefaultSystemPrompt, pickDailyChar, seedCharacters } from './personas.js';
|
||||
import { chatText, genImage, enhanceCharacterPrompt, getEffectiveAIConfig } from './ai.js';
|
||||
@@ -8,8 +11,27 @@ import { getImageFile, saveImageBuffer } from './images.js';
|
||||
|
||||
export const app = new Hono();
|
||||
|
||||
// Enable CORS for web clients and future mobile APKs
|
||||
app.use('*', cors());
|
||||
// CORS: allow an explicit origin whitelist (comma-separated) for the web client
|
||||
// and future mobile APKs. Falls back to same-origin + localhost when unset.
|
||||
const corsOriginsCsv = process.env.CORS_ORIGINS
|
||||
? process.env.CORS_ORIGINS.split(',').map((s) => s.trim()).filter(Boolean)
|
||||
: ['http://localhost:8787', 'http://127.0.0.1:8787'];
|
||||
|
||||
app.use('*', cors({
|
||||
origin: (origin) => {
|
||||
if (!origin) return true;
|
||||
return corsOriginsCsv.includes(origin);
|
||||
},
|
||||
allowHeaders: ['Content-Type', 'X-Admin-Token', 'Authorization'],
|
||||
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
maxAge: 86400,
|
||||
}));
|
||||
|
||||
// Global request body size limit (16 MB) to mitigate memory-exhaustion DoS.
|
||||
app.use('*', bodyLimit({
|
||||
maxSize: 16 * 1024 * 1024,
|
||||
onError: (c) => c.json({ error: 'Anfrage zu groß (max 16 MB)' }, 413),
|
||||
}));
|
||||
|
||||
// Global error handler
|
||||
app.onError((err, c) => {
|
||||
@@ -32,6 +54,15 @@ app.use('*', async (c, next) => {
|
||||
|
||||
const USER = { id: 'user', name: 'Du', handle: 'du' };
|
||||
|
||||
// Global rate-limit for mutating (state-changing) requests, keyed by client IP.
|
||||
const MUTATE_RATE_MAX = parseInt(process.env.RATE_LIMIT_MAX, 10) || 300;
|
||||
app.use('*', (c, next) => {
|
||||
if (c.req.method === 'GET' || c.req.method === 'HEAD' || c.req.method === 'OPTIONS') {
|
||||
return next();
|
||||
}
|
||||
return rateLimit(MUTATE_RATE_MAX, 60 * 1000)(c, next);
|
||||
});
|
||||
|
||||
function charBrief(c) {
|
||||
return { id: c.id, name: c.name, handle: c.handle, gender: c.gender || 'unspecified', avatarKey: c.avatar_key, isBanned: c.is_banned === 1, systemPrompt: c.system_prompt || '' };
|
||||
}
|
||||
@@ -779,6 +810,13 @@ app.post('/api/user-activity', async (c) => {
|
||||
});
|
||||
|
||||
// ---------- ADMIN API ----------
|
||||
// All admin routes require a valid ADMIN_TOKEN (via X-Admin-Token or Authorization: Bearer).
|
||||
app.use('/api/admin/*', adminGuard);
|
||||
|
||||
// Rate-limit mutating admin operations (per IP, sliding window, in-memory).
|
||||
const ADMIN_RATE_MAX = parseInt(process.env.ADMIN_RATE_MAX, 10) || 60;
|
||||
app.use('/api/admin/*', rateLimit(ADMIN_RATE_MAX, 60 * 1000));
|
||||
|
||||
app.get('/api/admin/status', async (c) => {
|
||||
const act = await getUserActivityStatus();
|
||||
const totalCharsRes = await pool.query('SELECT COUNT(*)::int AS n FROM characters;');
|
||||
|
||||
Reference in new issue
Block a user