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.

security: fail closed when CRON_SECRET is unset (auth, medium)

nirholas8a95092bcc1b
diff --git a/api/cron/forge-finalize.js b/api/cron/forge-finalize.js
index 35a953551..8d56b7b83 100644
--- a/api/cron/forge-finalize.js
+++ b/api/cron/forge-finalize.js
@@ -47,9 +47,16 @@ const GRACE_MINUTES = 2;
// 30 minutes; beyond it the worker task has been evicted or lost.
const HARD_TTL_MINUTES = 45;
-function requireCron(req, res) {
+export function requireCron(req, res) {
const secret = process.env.CRON_SECRET;
- if (!secret) return false; // allow in dev if no secret set
+ if (!secret) {
+ // Fail closed, same as the rest of /api/cron: an unset CRON_SECRET must
+ // never silently open the endpoint — a misconfigured deploy would
+ // otherwise let anyone trigger the finalize sweep on demand.
+ res.writeHead(503, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ error: 'not_configured', error_description: 'CRON_SECRET unset' }));
+ return true; // handled
+ }
const provided = req.headers['x-cron-secret'] || req.headers['authorization']?.replace(/^Bearer\s+/i, '');
if (!provided || !constantTimeEquals(provided, secret)) {
res.writeHead(401, { 'content-type': 'application/json' });
diff --git a/api/cron/payment-session-sweep.js b/api/cron/payment-session-sweep.js
index 277bba151..6199e843c 100644
--- a/api/cron/payment-session-sweep.js
+++ b/api/cron/payment-session-sweep.js
@@ -24,9 +24,17 @@ export const maxDuration = 30;
const BATCH_LIMIT = Number(process.env.PAYMENT_SESSION_SWEEP_BATCH) || 100;
-function requireCron(req, res) {
+export function requireCron(req, res) {
const secret = process.env.CRON_SECRET;
- if (!secret) return false; // allow in dev if no secret set
+ if (!secret) {
+ // Fail closed, same as the rest of /api/cron: an unset CRON_SECRET must
+ // never silently open the endpoint. This sweep moves money (it refunds
+ // session budgets into credit balances), so a misconfigured deploy
+ // would otherwise expose unauthenticated money-moving on demand.
+ res.writeHead(503, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ error: 'not_configured', error_description: 'CRON_SECRET unset' }));
+ return true; // handled
+ }
const provided = req.headers['x-cron-secret'] || req.headers['authorization']?.replace(/^Bearer\s+/i, '');
if (!provided || !constantTimeEquals(provided, secret)) {
res.writeHead(401, { 'content-type': 'application/json' });
diff --git a/tests/cron-auth-fail-closed.test.js b/tests/cron-auth-fail-closed.test.js
new file mode 100644
index 000000000..b13249f62
--- /dev/null
+++ b/tests/cron-auth-fail-closed.test.js
@@ -0,0 +1,89 @@
+// Regression guard for the 2026-07-23 audit finding: payment-session-sweep
+// and forge-finalize failed OPEN when CRON_SECRET was unset ("allow in dev"),
+// the only two handlers in /api/cron with an inverted posture. An unset
+// CRON_SECRET on a misconfigured deploy would have exposed unauthenticated
+// triggering of a money-moving sweep (session-budget refunds) and the forge
+// finalize batch. Both must now fail closed with 503, like the other 66.
+
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { requireCron as requireCronSweep } from '../api/cron/payment-session-sweep.js';
+import { requireCron as requireCronFinalize } from '../api/cron/forge-finalize.js';
+
+function mockRes() {
+ return {
+ writeHead: vi.fn().mockReturnThis(),
+ end: vi.fn(),
+ };
+}
+
+const handlers = [
+ ['payment-session-sweep', requireCronSweep],
+ ['forge-finalize', requireCronFinalize],
+];
+
+describe('cron auth fails closed', () => {
+ beforeEach(() => {
+ delete process.env.CRON_SECRET;
+ });
+ afterEach(() => {
+ delete process.env.CRON_SECRET;
+ });
+
+ for (const [name, requireCron] of handlers) {
+ describe(name, () => {
+ it('503s and runs nothing when CRON_SECRET is unset', () => {
+ const res = mockRes();
+ const handled = requireCron({ headers: {} }, res);
+ expect(handled).toBe(true);
+ expect(res.writeHead).toHaveBeenCalledWith(503, expect.anything());
+ });
+
+ it('503s even when a header is presented but CRON_SECRET is unset', () => {
+ const res = mockRes();
+ const handled = requireCron(
+ { headers: { 'x-cron-secret': 'anything' } },
+ res,
+ );
+ expect(handled).toBe(true);
+ expect(res.writeHead).toHaveBeenCalledWith(503, expect.anything());
+ });
+
+ it('401s when the secret is set but no header is presented', () => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+ const res = mockRes();
+ expect(requireCron({ headers: {} }, res)).toBe(true);
+ expect(res.writeHead).toHaveBeenCalledWith(401, expect.anything());
+ });
+
+ it('401s on a wrong secret', () => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+ const res = mockRes();
+ expect(
+ requireCron({ headers: { 'x-cron-secret': 'wrong' } }, res),
+ ).toBe(true);
+ expect(res.writeHead).toHaveBeenCalledWith(401, expect.anything());
+ });
+
+ it('admits the correct secret via x-cron-secret', () => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+ const res = mockRes();
+ expect(
+ requireCron({ headers: { 'x-cron-secret': 'test-cron-secret' } }, res),
+ ).toBe(false);
+ expect(res.writeHead).not.toHaveBeenCalled();
+ });
+
+ it('admits the correct secret via Bearer', () => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+ const res = mockRes();
+ expect(
+ requireCron(
+ { headers: { authorization: 'Bearer test-cron-secret' } },
+ res,
+ ),
+ ).toBe(false);
+ expect(res.writeHead).not.toHaveBeenCalled();
+ });
+ });
+ }
+});