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.

fix(crons): put every handler behind the shared method guard instead of a hand-rolled 405

nirholasfcd4e22df8ac
diff --git a/api/cron/forge-finalize.js b/api/cron/forge-finalize.js
index ceca14a63..7f68352fa 100644
--- a/api/cron/forge-finalize.js
+++ b/api/cron/forge-finalize.js
@@ -32,7 +32,7 @@
// other cron in /api/cron/.
import { sql } from '../_lib/db.js';
-import { cors, json, wrapCron } from '../_lib/http.js';
+import { cors, json, method, wrapCron } from '../_lib/http.js';
import { requireCron } from '../_lib/cron-auth.js';
import { materializeCreation, markFailed, forgeStoreEnabled, createCreation } from '../_lib/forge-store.js';
import { notifyForgeComplete, notifyForgeFailed } from '../_lib/forge-notify.js';
@@ -205,8 +205,8 @@ async function tryCronRedispatch(row, ageMinutes) {
}
export default wrapCron(async (req, res) => {
- cors(req, res, { methods: 'GET,POST,OPTIONS' });
- if (req.method?.toUpperCase() === 'OPTIONS') return;
+ if (cors(req, res, { methods: 'GET,POST,OPTIONS' })) return;
+ if (!method(req, res, ['GET', 'POST'])) return;
if (!requireCron(req, res)) return;
if (!forgeStoreEnabled()) {
return json(res, 200, { enabled: false, swept: 0 });
diff --git a/api/cron/forge-thumbnail-backfill.js b/api/cron/forge-thumbnail-backfill.js
index fdafb8022..79d86bd24 100644
--- a/api/cron/forge-thumbnail-backfill.js
+++ b/api/cron/forge-thumbnail-backfill.js
@@ -27,8 +27,7 @@
// which shares this claim ledger. `?dry_run=1` reports coverage without
// rendering. Skips cleanly when forge persistence is unconfigured.
-import { json, wrapCron } from '../_lib/http.js';
-import { env } from '../_lib/env.js';
+import { json, method, wrapCron } from '../_lib/http.js';
import { logger } from '../_lib/usage.js';
import { forgeStoreEnabled } from '../_lib/forge-store.js';
import { renderBatch, coverage } from '../_lib/forge-thumbs.js';
@@ -42,6 +41,7 @@ const RENDER_BATCH = Math.max(0, Number(process.env.FORGE_THUMBNAIL_RENDER_BATCH
const CONCURRENCY = Math.max(1, Number(process.env.FORGE_THUMBNAIL_CONCURRENCY || 2));
export default wrapCron(async (req, res) => {
+ if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
// Renders upload to R2 and read/write the DB — no store, nothing to do.
diff --git a/api/cron/three-buy-loop.js b/api/cron/three-buy-loop.js
index e91796253..a0d34fe20 100644
--- a/api/cron/three-buy-loop.js
+++ b/api/cron/three-buy-loop.js
@@ -34,7 +34,7 @@ import { randomUUID } from 'node:crypto';
import { PublicKey } from '@solana/web3.js';
import { getMint } from '@solana/spl-token';
-import { json, wrapCron } from '../_lib/http.js';
+import { json, method, wrapCron } from '../_lib/http.js';
import { env } from '../_lib/env.js';
import { sql } from '../_lib/db.js';
import { getRedis } from '../_lib/redis.js';
@@ -103,7 +103,7 @@ async function nextTickSeq() {
}
export default wrapCron(async (req, res) => {
- if (req.method !== 'GET') return json(res, 405, { error: 'method_not_allowed' });
+ if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
// ── Kill switches ───────────────────────────────────────────────────────
diff --git a/api/cron/x402-autonomous-loop.js b/api/cron/x402-autonomous-loop.js
index a9efd270b..263a4ee64 100644
--- a/api/cron/x402-autonomous-loop.js
+++ b/api/cron/x402-autonomous-loop.js
@@ -31,7 +31,7 @@ import {
TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getMint,
} from '@solana/spl-token';
-import { json, wrapCron } from '../_lib/http.js';
+import { json, method, wrapCron } from '../_lib/http.js';
import { env } from '../_lib/env.js';
import { getRedis } from '../_lib/redis.js';
import { sql } from '../_lib/db.js';
@@ -259,7 +259,7 @@ async function runStoreValue(entry, ctx) {
// ── Main handler ──────────────────────────────────────────────────────────────
export default wrapCron(async (req, res) => {
- if (req.method !== 'GET') return json(res, 405, { error: 'method_not_allowed' });
+ if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
if (process.env.X402_AUTONOMOUS_ENABLED === 'false') {
diff --git a/api/cron/x402-ring-tick.js b/api/cron/x402-ring-tick.js
index 5b2f75155..a5b7b5f65 100644
--- a/api/cron/x402-ring-tick.js
+++ b/api/cron/x402-ring-tick.js
@@ -42,7 +42,7 @@ import {
TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
-import { json, wrapCron } from '../_lib/http.js';
+import { json, method, wrapCron } from '../_lib/http.js';
import { env } from '../_lib/env.js';
import { getRedis } from '../_lib/redis.js';
import { sql } from '../_lib/db.js';
@@ -184,7 +184,7 @@ function isFloorSignal(result) {
}
export default wrapCron(async (req, res) => {
- if (req.method !== 'GET') return json(res, 405, { error: 'method_not_allowed' });
+ if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
const origin = ORIGIN();
@@ -474,7 +474,7 @@ export default wrapCron(async (req, res) => {
}),
isFloorSignal,
});
- const { results, calls, paid, errors, spent, lastTxSig, floorHit } = exec;
+ const { results, calls, paid, errors, spent, floorHit } = exec;
if (exec.capReached) {
log.info('ring_tick_cap_reached', { spent_atomic: spent, launched: calls, picks: picks.length });
}
diff --git a/api/cron/x402-seed-cron.js b/api/cron/x402-seed-cron.js
index 82ea22d47..40c2c8e65 100644
--- a/api/cron/x402-seed-cron.js
+++ b/api/cron/x402-seed-cron.js
@@ -22,7 +22,7 @@
import { readFileSync } from 'node:fs';
import bs58 from 'bs58';
import {
- Connection, PublicKey, Keypair, TransactionMessage, VersionedTransaction,
+ PublicKey, Keypair, TransactionMessage, VersionedTransaction,
ComputeBudgetProgram,
} from '@solana/web3.js';
import {
@@ -31,7 +31,7 @@ import {
TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getMint,
} from '@solana/spl-token';
-import { json, wrapCron } from '../_lib/http.js';
+import { json, method, wrapCron } from '../_lib/http.js';
import { env } from '../_lib/env.js';
import { getRedis, isRedisAuthError } from '../_lib/redis.js';
import { solanaConnection } from '../_lib/solana/connection.js';
@@ -220,7 +220,7 @@ async function pushFeedEntry(entry) {
// ── Main handler ──────────────────────────────────────────────────────────────
export default wrapCron(async (req, res) => {
- if (req.method !== 'GET') return json(res, 405, { error: 'method_not_allowed' });
+ if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
if (process.env.X402_SEED_ENABLED === 'false') {
diff --git a/tests/cron-method-guard.test.js b/tests/cron-method-guard.test.js
new file mode 100644
index 000000000..71c130385
--- /dev/null
+++ b/tests/cron-method-guard.test.js
@@ -0,0 +1,102 @@
+// Regression guard for the HTTP-method allowlist on /api/cron handlers.
+//
+// 2026-08-10 audit: forge-finalize and payment-session-sweep called cors() for
+// its headers but then only short-circuited OPTIONS, so they never checked the
+// verb. A `DELETE /api/cron/forge-finalize` with a valid CRON_SECRET ran the
+// full finalize batch and answered 200 (proven against a local server before
+// the fix). The same shape in payment-session-sweep meant any verb could drive
+// a money-moving sweep (session expiry + budget refunds to credit_ledger).
+//
+// Both are the exact pair that failed OPEN on the cron secret in the 2026-07-23
+// audit (see cron-auth-fail-closed.test.js), because both were written from the
+// same hand-rolled preamble instead of the shared helpers. So this suite guards
+// the class, not just the two files: every handler under api/cron must run its
+// verb through method() before doing any work.
+
+import { describe, it, expect } from 'vitest';
+import { readFileSync, readdirSync, statSync } from 'node:fs';
+import { join, dirname } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const API_CRON_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', 'api', 'cron');
+
+function cronFiles(dir, out = []) {
+ for (const entry of readdirSync(dir)) {
+ const full = join(dir, entry);
+ if (statSync(full).isDirectory()) cronFiles(full, out);
+ else if (full.endsWith('.js')) out.push(full);
+ }
+ return out;
+}
+
+function mockRes() {
+ return {
+ statusCode: 0,
+ headersSent: false,
+ writableEnded: false,
+ headers: {},
+ body: null,
+ setHeader(k, v) { this.headers[k.toLowerCase()] = v; },
+ end(payload) {
+ this.writableEnded = true;
+ this.body = payload ? JSON.parse(payload) : null;
+ },
+ };
+}
+
+const mockReq = (verb, url) => ({ method: verb, url, headers: {} });
+
+// The two handlers the audit caught. Driven through their real exported
+// handler, so this fails if the guard is ever removed again: a source grep
+// alone would not catch a guard that runs after the work.
+const AUDITED = [
+ { name: 'forge-finalize', path: '../api/cron/forge-finalize.js' },
+ { name: 'payment-session-sweep', path: '../api/cron/payment-session-sweep.js' },
+];
+
+describe('cron handlers enforce a method allowlist', () => {
+ for (const { name, path } of AUDITED) {
+ it(`${name} rejects a disallowed verb with 405 before doing any work`, async () => {
+ const handler = (await import(path)).default;
+ for (const verb of ['DELETE', 'PUT', 'PATCH']) {
+ const res = mockRes();
+ await handler(mockReq(verb, `/api/cron/${name}`), res);
+ expect(res.statusCode, `${verb} ${name}`).toBe(405);
+ expect(res.body?.error).toBe('method_not_allowed');
+ // The rejection must advertise what IS allowed (RFC 9110 section 15.5.6).
+ expect(res.headers.allow).toContain('GET');
+ }
+ });
+
+ it(`${name} still answers the CORS preflight with 204`, async () => {
+ const handler = (await import(path)).default;
+ const res = mockRes();
+ await handler(mockReq('OPTIONS', `/api/cron/${name}`), res);
+ expect(res.statusCode).toBe(204);
+ expect(res.headers['access-control-allow-methods']).toContain('GET');
+ });
+
+ it(`${name} lets an allowed verb through the guard to the auth gate`, async () => {
+ const handler = (await import(path)).default;
+ const res = mockRes();
+ // No CRON_SECRET header: the fail-closed cron gate must be what stops
+ // it (401 with a secret configured, 503 when the env has none, per
+ // api/_lib/cron-auth.js), which proves the method guard did not reject
+ // a legitimate verb before auth ever ran.
+ await handler(mockReq('GET', `/api/cron/${name}`), res);
+ expect([401, 503]).toContain(res.statusCode);
+ expect(res.body?.error).not.toBe('method_not_allowed');
+ });
+ }
+
+ // Class-level guard: no cron handler may set CORS headers and then act on a
+ // request without ever consulting method(). This is the shape that produced
+ // both bugs above.
+ it('no api/cron handler calls cors() without a method() allowlist', () => {
+ const offenders = cronFiles(API_CRON_DIR).filter((file) => {
+ const src = readFileSync(file, 'utf8');
+ return src.includes('cors(req, res') && !src.includes('method(req, res');
+ });
+ expect(offenders).toEqual([]);
+ });
+});