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(premium): tighten the key rotation and subscribe paths

nirholas5f873ebb354e
diff --git a/api/premium/keys.js b/api/premium/keys.js
index 4c84cbdce..e5617b45b 100644
--- a/api/premium/keys.js
+++ b/api/premium/keys.js
@@ -1,10 +1,10 @@
-// POST /api/premium/keys — rotate or revoke the signed-in user's premium key.
+// POST /api/premium/keys: rotate or revoke the signed-in user's premium key.
//
// body: { action: 'rotate' | 'revoke', id: '<x402 subscription id>' }
//
// Session + CSRF authed. Only keys minted by a premium-pass purchase that is
// linked to this user (meta.source = 'premium-pass', meta.user_id = session
-// user) can be managed here — partner/AWS keys live at
+// user) can be managed here. Partner/AWS keys live at
// /api/user/x402-subscriptions, and wallet-only purchases (no session at buy
// time) manage nothing here by design: the key follows the account that
// bought it.
@@ -54,10 +54,17 @@ export default wrap(async (req, res) => {
return json(res, 200, { revoked: true, id }, { 'cache-control': 'no-store' });
}
- // rotate — revoke the old credential, mint a replacement with the same
- // expiry/limits/meta, and re-point the pass rows at the new key.
- if (key.revoked_at) return error(res, 409, 'key_revoked', 'key is revoked — buy or renew a pass to get a new one');
- await revokeSubscription(id);
+ // rotate: mint a replacement with the same expiry/limits/meta, re-point the
+ // pass rows at it, and only then revoke the old credential.
+ //
+ // The order is load-bearing. Revoking first means a failed mint leaves a
+ // paying customer with no key at all and no way back (a second rotate hits
+ // the key_revoked branch below), so a transient DB blip would cost them the
+ // pass they paid for. Minting first makes the worst case a stale key that
+ // still expires with the pass, and the buyer is never locked out. A failed
+ // revoke still propagates as a 5xx so a rotation is never reported as done
+ // while the old credential is live.
+ if (key.revoked_at) return error(res, 409, 'key_revoked', 'key is revoked: buy or renew a pass to get a new one');
const fresh = await createSubscription({
name: key.name,
rateLimitPerMinute: key.rate_limit_per_minute,
@@ -69,6 +76,7 @@ export default wrap(async (req, res) => {
update premium_passes set api_subscription_id = ${fresh.id}
where api_subscription_id = ${id}
`;
+ await revokeSubscription(id);
return json(
res, 200,
{ rotated: true, id: fresh.id, key_prefix: fresh.key_prefix, api_key: fresh.token },
diff --git a/api/premium/mine.js b/api/premium/mine.js
index b62ccd007..b186864c3 100644
--- a/api/premium/mine.js
+++ b/api/premium/mine.js
@@ -1,4 +1,4 @@
-// GET /api/premium/mine — the signed-in user's premium passes and keys.
+// GET /api/premium/mine: the signed-in user's premium passes and keys.
//
// Session-cookie authed (the billing dashboard's data source). Returns passes
// purchased while signed in (user_id-linked) plus, when the account has a
diff --git a/api/premium/subscribe.js b/api/premium/subscribe.js
index ab44a1945..09f23c41d 100644
--- a/api/premium/subscribe.js
+++ b/api/premium/subscribe.js
@@ -1,13 +1,13 @@
-// POST /api/premium/subscribe — redeem a paid quote for a 30-day premium pass.
+// POST /api/premium/subscribe: redeem a paid quote for a 30-day premium pass.
//
// body: { quote_id, tx_signature }
-// → 200 { pass, api_key?, renewed } — api_key (x402_live_…) is returned
-// exactly once, on first key mint
-// 202 { pending: true } — tx not confirmed yet; poll again
-// 4xx { error } — mismatch / expired / already used
+// → 200 { pass, api_key?, renewed } api_key (x402_live_…) is returned
+// exactly once, on first key mint
+// 202 { pending: true } tx not confirmed yet; poll again
+// 4xx { error } mismatch / expired / already used
//
// Verification is against the LANDED transaction (balance delta to the
-// treasury, quoted wallet among the signers) and the persisted quote — never
+// treasury, quoted wallet among the signers) and the persisted quote, never
// client-supplied numbers. tx_signature is UNIQUE, so replays and double
// submits idempotently return the already-issued pass.
@@ -18,11 +18,15 @@ import { getSessionUser } from '../_lib/auth.js';
import { verifyPassPayment, activatePass } from '../_lib/premium.js';
// A quote is redeemable for 30 minutes after creation (10-min price lock plus
-// grace for slow confirmation) — the price was locked at signing time, and the
+// grace for slow confirmation). The price was locked at signing time, and the
// blockhash in the built tx expires long before this window anyway.
const REDEEM_WINDOW_MS = 30 * 60_000;
const SIG_RE = /^[1-9A-HJ-NP-Za-km-z]{64,96}$/;
+// premium_quotes.id is a uuid column, so anything else is not a lookup miss but
+// a driver-level type error (SQLSTATE 22P02) that would 500 the request. Shape
+// the id at the boundary and answer with the same 4xx the caller can act on.
+const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS', origins: '*', credentials: true })) return;
@@ -35,6 +39,9 @@ export default wrap(async (req, res) => {
const quoteId = String(body?.quote_id || '').trim();
const txSignature = String(body?.tx_signature || '').trim();
if (!quoteId) return error(res, 400, 'bad_quote', 'quote_id is required');
+ if (!UUID_RE.test(quoteId)) {
+ return error(res, 400, 'bad_quote', 'quote_id must be the uuid returned by /api/premium/quote');
+ }
if (!SIG_RE.test(txSignature)) {
return error(res, 400, 'bad_signature', 'tx_signature must be a base58 Solana signature');
}
@@ -48,12 +55,12 @@ export default wrap(async (req, res) => {
}
const [quote] = await sql`select * from premium_quotes where id = ${quoteId} limit 1`;
- if (!quote) return error(res, 404, 'quote_not_found', 'unknown quote_id — request a fresh quote');
+ if (!quote) return error(res, 404, 'quote_not_found', 'unknown quote_id, request a fresh quote');
if (quote.status === 'used' && quote.tx_signature !== txSignature) {
return error(res, 409, 'quote_used', 'this quote was already redeemed with a different transaction');
}
if (Date.now() - new Date(quote.created_at).getTime() > REDEEM_WINDOW_MS) {
- return error(res, 410, 'quote_expired', 'quote expired — request a fresh quote and pay again');
+ return error(res, 410, 'quote_expired', 'quote expired, request a fresh quote and pay again');
}
const verdict = await verifyPassPayment(quote, txSignature);