Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

harden(admin/ops-alerts): fail closed, admin-session or dedicated OPS_SECRET

fudjia7f96b3aae322
diff --git a/api/admin/ops-alerts.js b/api/admin/ops-alerts.js
index d2711cad0..11aaf4d5a 100644
--- a/api/admin/ops-alerts.js
+++ b/api/admin/ops-alerts.js
@@ -16,13 +16,42 @@ import { env } from '../_lib/env.js';
import { constantTimeEquals } from '../_lib/crypto.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { sql } from '../_lib/db.js';
+import { getSessionUser } from '../_lib/auth.js';
+import { isAdminUser } from '../_lib/admin.js';
-function authorized(req) {
- const secret = env.OPS_SECRET || env.CRON_SECRET;
- if (!secret) return true; // no secret configured (dev) → open, same as /api/ops/health
- const provided =
- req.headers['x-ops-secret'] || req.headers['authorization']?.replace(/^Bearer\s+/i, '');
- return !!provided && constantTimeEquals(provided, secret);
+const IS_PROD = env.NODE_ENV === 'production' || env.VERCEL_ENV === 'production';
+
+// Authorize a request and return the actor identity for attribution. Two ways
+// in, both fail CLOSED in production:
+// 1. A signed-in platform admin (session + admin wallet, the requireAdmin
+// model) — the strongest path, and the only one that yields a real per-user
+// identity for the acknowledge audit trail.
+// 2. A dedicated OPS_SECRET presented as `x-ops-secret` (bookmarkable page /
+// headless). This is deliberately NOT CRON_SECRET: the ops dashboard must
+// never share a credential with the crons that move real funds, so viewing
+// health can't be escalated into triggering a payment job. Set OPS_SECRET
+// and CRON_SECRET is no longer accepted here.
+// No secret configured + no admin session → allowed ONLY off-production (local
+// dev); in production this denies. The endpoint exposes wallet addresses, tx
+// signatures, key-rotation hints, and stack traces, so an open default is unsafe.
+async function authorize(req) {
+ try {
+ const user = await getSessionUser(req);
+ if (user && (await isAdminUser(user))) {
+ return { ok: true, actor: user.wallet_address || `user:${user.id}` };
+ }
+ } catch {
+ /* no/invalid session — fall through to the secret path */
+ }
+ const secret = env.OPS_SECRET;
+ if (secret) {
+ const provided =
+ req.headers['x-ops-secret'] || req.headers['authorization']?.replace(/^Bearer\s+/i, '');
+ if (provided && constantTimeEquals(provided, secret)) return { ok: true, actor: 'ops-secret' };
+ return { ok: false };
+ }
+ // No OPS_SECRET set: open in dev for local work, closed in production.
+ return { ok: !IS_PROD, actor: 'dev' };
}
const ACTIVE_LIMIT = 200;
@@ -64,7 +93,8 @@ export default wrap(async (req, res) => {
const rl = await limits.authIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
- if (!authorized(req)) return error(res, 401, 'unauthorized', 'x-ops-secret required');
+ const auth = await authorize(req);
+ if (!auth.ok) return error(res, 401, 'unauthorized', 'admin session or x-ops-secret required');
const m = req.method?.toUpperCase();
@@ -91,7 +121,9 @@ export default wrap(async (req, res) => {
if (action !== 'ack' && action !== 'unack') {
return error(res, 400, 'bad_request', "action must be 'ack' or 'unack'");
}
- const by = String(body?.by || 'admin').slice(0, 120);
+ // Attribution is server-derived from the authorized identity, never the
+ // client body — a caller can't forge who acknowledged an alert.
+ const by = String(auth.actor || 'admin').slice(0, 120);
try {
const rows =
action === 'ack'