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(api): harden the rider pass, seed, signals, RSS and studio-asset handlers

nirholasc48b2ec7798b
diff --git a/api/_lib/env.js b/api/_lib/env.js
index faa97bac6..7c8038b88 100644
--- a/api/_lib/env.js
+++ b/api/_lib/env.js
@@ -1569,6 +1569,31 @@ export const env = {
return opt('RIDER_HELIUS_WEBHOOK_SECRET');
},
+ // Rider Firebase web-app config, served to the browser by /api/rider/firebase
+ // so the client bundle carries no project literals. These are the public
+ // client-side identifiers Firebase itself publishes in a web app config, NOT
+ // service-account credentials — access is governed by Firebase security rules,
+ // so the endpoint is deliberately unauthenticated. api/rider/firebase.js
+ // answers 503 not_configured until at least the apiKey and projectId are set.
+ get RIDER_FIREBASE_API_KEY() {
+ return opt('RIDER_FIREBASE_API_KEY');
+ },
+ get RIDER_FIREBASE_AUTH_DOMAIN() {
+ return opt('RIDER_FIREBASE_AUTH_DOMAIN');
+ },
+ get RIDER_FIREBASE_DATABASE_URL() {
+ return opt('RIDER_FIREBASE_DATABASE_URL');
+ },
+ get RIDER_FIREBASE_PROJECT_ID() {
+ return opt('RIDER_FIREBASE_PROJECT_ID');
+ },
+ get RIDER_FIREBASE_STORAGE_BUCKET() {
+ return opt('RIDER_FIREBASE_STORAGE_BUCKET');
+ },
+ get RIDER_FIREBASE_MESSAGING_SENDER_ID() {
+ return opt('RIDER_FIREBASE_MESSAGING_SENDER_ID');
+ },
+
// Neynar API key — the preferred (indexed) rung of the Farcaster read chain
// in api/_lib/farcaster-client.js. Optional: without it the chain falls back
// to the keyless public hub below, so Farcaster memory seeding still works.
diff --git a/api/_lib/rss-feed.js b/api/_lib/rss-feed.js
index 60cff706a..3bdca715b 100644
--- a/api/_lib/rss-feed.js
+++ b/api/_lib/rss-feed.js
@@ -230,18 +230,28 @@ function deriveTitle(text) {
// Linkify URLs and @handles in a single pass that swaps URLs out for
// placeholders so the handle pass can't accidentally match @-segments inside
// the URLs we just wrapped (e.g. https://x.com/Handle).
-function linkify(html) {
+//
+// The placeholder must be a sequence the post itself cannot contain, or a post
+// that merely writes the placeholder's own text restores a URL that was never
+// there (and, past the end of the array, throws and takes the whole feed down
+// with it). Callers hand us XML-escaped text, so a raw `<` is impossible by
+// construction: `<0>` is therefore collision-proof, and the restore pass can
+// trust every index it finds.
+const URL_SLOT = (i) => `<${i}>`;
+const URL_SLOT_RE = /<(\d+)>/g;
+
+function linkify(escapedHtml) {
const urls = [];
- let buf = html.replace(/\bhttps?:\/\/[^\s<]+/g, (url) => {
+ let buf = escapedHtml.replace(/\bhttps?:\/\/[^\s<]+/g, (url) => {
const trimmed = url.replace(/[.,!?)]+$/, '');
const tail = url.slice(trimmed.length);
urls.push({ trimmed, tail });
- return ` URL${urls.length - 1} `;
+ return URL_SLOT(urls.length - 1);
});
buf = buf.replace(/(^|[^A-Za-z0-9_])@([A-Za-z0-9_]{1,15})/g, (_m, pre, handle) =>
`${pre}<a href="https://x.com/${handle}" rel="noopener">@${handle}</a>`,
);
- return buf.replace(/ URL(\d+) /g, (_m, i) => {
+ return buf.replace(URL_SLOT_RE, (_m, i) => {
const { trimmed, tail } = urls[Number(i)];
return `<a href="${trimmed}" rel="noopener">${trimmed}</a>${tail}`;
});
@@ -329,7 +339,7 @@ function renderItemXml(item) {
\t\t\t<pubDate>${rfc822(item.timestamp)}</pubDate>
\t\t\t<dc:creator>${escapeXml(item.author)}</dc:creator>${tagsXml}${sourceXml}${enclosureXml}${mediaXml}
\t\t\t<description>${escapeXml(item.summary)}</description>
-\t\t\t<content:encoded><![CDATA[${bodyWithAttribution}]]></content:encoded>
+\t\t\t<content:encoded><![CDATA[${cdata(bodyWithAttribution)}]]></content:encoded>
\t\t</item>`;
}
const account = ACCOUNTS[item.account] || { handle: '@' + item.account };
@@ -341,6 +351,6 @@ function renderItemXml(item) {
\t\t\t<pubDate>${rfc822(item.timestamp)}</pubDate>
\t\t\t<dc:creator>${escapeXml(account.handle)}</dc:creator>
\t\t\t<description>${escapeXml(renderDescription(item))}</description>
-\t\t\t<content:encoded><![CDATA[${renderBodyHtml(item)}]]></content:encoded>
+\t\t\t<content:encoded><![CDATA[${cdata(renderBodyHtml(item))}]]></content:encoded>
\t\t</item>`;
}
diff --git a/api/rider/check.js b/api/rider/check.js
index 02526b456..444175cf5 100644
--- a/api/rider/check.js
+++ b/api/rider/check.js
@@ -1,9 +1,44 @@
+// GET /api/rider/check?address=<solana wallet>
+//
+// Answers "does this wallet hold a rider pass?" from BOTH sources that can grant
+// one. Either source alone gives the wrong answer: reading only the on-chain
+// balance denies every wallet that bought a pass by sending its $THREE to the
+// vault (api/rider/webhook.js records those in rider_passes), and reading only
+// rider_passes denies every holder who never had to pay.
+
import { PublicKey } from '@solana/web3.js';
import { solanaConnection } from '../_lib/solana/connection.js';
+import { sql } from '../_lib/db.js';
+import { env } from '../_lib/env.js';
import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
+import { REQUIRED_AMOUNT } from '../_lib/rider.js';
import { TOKEN_MINT as THREE_MINT } from '../_lib/token/config.js';
-const RPC = process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com';
+
+async function heldBalance(owner) {
+ const connection = solanaConnection({ url: env.SOLANA_RPC_URL, commitment: 'confirmed' });
+ // Filter by MINT, not by token program: $THREE is a Token-2022 mint, so a
+ // classic-program-only query never sees it (every holder read as balance 0).
+ // The mint filter matches the holder's account under whichever program owns
+ // the mint, and returns only that account instead of the whole wallet.
+ const accounts = await connection.getParsedTokenAccountsByOwner(owner, {
+ mint: new PublicKey(THREE_MINT),
+ });
+ return accounts.value.reduce(
+ (sum, a) => sum + Number(a.account.data.parsed.info.tokenAmount.uiAmount ?? 0),
+ 0,
+ );
+}
+
+async function recordedPayment(address) {
+ const rows = await sql`
+ select amount_paid, tx_signature, created_at
+ from rider_passes
+ where wallet_address = ${address}
+ limit 1
+ `;
+ return rows[0] ?? null;
+}
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
@@ -12,7 +47,10 @@ export default wrap(async (req, res) => {
const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
- const address = req.query?.address?.trim();
+ // A repeated query param (?address=a&address=b) arrives as an array, which
+ // has no .trim() — reading it directly turned a malformed request into a 500.
+ const raw = req.query?.address;
+ const address = (Array.isArray(raw) ? raw[0] : raw ?? '').toString().trim();
if (!address) return error(res, 400, 'validation_error', 'address required');
let owner;
@@ -22,23 +60,41 @@ export default wrap(async (req, res) => {
return error(res, 400, 'validation_error', 'invalid Solana address');
}
- const connection = solanaConnection({ url: RPC, commitment: 'confirmed' });
- // Filter by MINT, not by token program: $THREE is a Token-2022 mint, so a
- // classic-program-only query never sees it (every holder read as balance 0).
- // The mint filter matches the holder's account under whichever program owns
- // the mint, and returns only that account instead of the whole wallet.
- const accounts = await connection.getParsedTokenAccountsByOwner(owner, {
- mint: new PublicKey(THREE_MINT),
- });
+ // Both lookups run together, and a positive result from either one is already
+ // authoritative — a failed lookup only blocks the answer when it was the one
+ // that had to prove the wallet has NO pass.
+ const [balanceRead, paymentRead] = await Promise.allSettled([
+ heldBalance(owner),
+ recordedPayment(address),
+ ]);
- const balance = accounts.value.reduce(
- (sum, a) => sum + Number(a.account.data.parsed.info.tokenAmount.uiAmount ?? 0),
- 0,
- );
+ const balance = balanceRead.status === 'fulfilled' ? balanceRead.value : null;
+ const payment = paymentRead.status === 'fulfilled' ? paymentRead.value : null;
+ const holderPass = balance != null && balance > 0;
+ const paidPass = payment != null;
+
+ if (!holderPass && balanceRead.status === 'rejected') {
+ if (!paidPass) {
+ // Never echo the rejection message: a web3.js network error embeds the
+ // keyed RPC URL, which would leak HELIUS_API_KEY to the caller.
+ console.error('[rider/check] $THREE balance read failed', balanceRead.reason);
+ return error(res, 502, 'rpc_unavailable', 'could not read the $THREE balance for this wallet');
+ }
+ }
+ if (!paidPass && paymentRead.status === 'rejected') {
+ // Hand a DB outage to wrap(), which already classifies it as a throttled 503
+ // instead of a per-request 5xx alert storm.
+ if (!holderPass) throw paymentRead.reason;
+ }
return json(res, 200, {
- has_pass: balance > 0,
+ has_pass: holderPass || paidPass,
+ holder_pass: holderPass,
+ paid_pass: paidPass,
balance,
+ amount_paid: payment ? Number(payment.amount_paid) : null,
+ tx_signature: payment?.tx_signature ?? null,
+ required_amount: REQUIRED_AMOUNT,
mint: THREE_MINT,
});
});
diff --git a/api/rider/firebase.js b/api/rider/firebase.js
index 9322f382c..091620c6b 100644
--- a/api/rider/firebase.js
+++ b/api/rider/firebase.js
@@ -1,16 +1,40 @@
-import { cors, json, method, wrap } from '../_lib/http.js';
+// GET /api/rider/firebase
+//
+// Serves the rider client's Firebase web-app config so the browser bundle ships
+// no project literals. These are Firebase's public client identifiers (access is
+// enforced by security rules, not by hiding them), which is why the endpoint is
+// unauthenticated; no service-account material is ever returned here.
+//
+// Fails closed with 503 rather than handing back an all-null object: a config
+// whose apiKey is null crashes initializeApp() inside the client with an opaque
+// Firebase error, so the caller can't tell a misconfigured deployment from a bug
+// in its own code.
+
+import { cors, json, method, wrap, error } from '../_lib/http.js';
import { env } from '../_lib/env.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
if (!method(req, res, ['GET'])) return;
- return json(res, 200, {
+ const config = {
apiKey: env.RIDER_FIREBASE_API_KEY ?? null,
authDomain: env.RIDER_FIREBASE_AUTH_DOMAIN ?? null,
databaseURL: env.RIDER_FIREBASE_DATABASE_URL ?? null,
projectId: env.RIDER_FIREBASE_PROJECT_ID ?? null,
storageBucket: env.RIDER_FIREBASE_STORAGE_BUCKET ?? null,
messagingSenderId: env.RIDER_FIREBASE_MESSAGING_SENDER_ID ?? null,
- });
+ };
+
+ // apiKey + projectId are the two Firebase refuses to initialize without.
+ if (!config.apiKey || !config.projectId) {
+ return error(
+ res,
+ 503,
+ 'not_configured',
+ 'rider Firebase is not configured — set RIDER_FIREBASE_API_KEY and RIDER_FIREBASE_PROJECT_ID',
+ );
+ }
+
+ return json(res, 200, config);
});
diff --git a/api/rider/info.js b/api/rider/info.js
index d207651cd..e2803d979 100644
--- a/api/rider/info.js
+++ b/api/rider/info.js
@@ -1,15 +1,24 @@
+// GET /api/rider/info
+//
+// Public terms of the rider pass: where to send $THREE, which mint, and how much.
+// `accepting_payments` is false when RIDER_VAULT_ADDRESS is unset, so a client
+// never renders a "send 8,000 $THREE" call to action with no destination.
+
import { cors, json, method, wrap } from '../_lib/http.js';
import { env } from '../_lib/env.js';
+import { REQUIRED_AMOUNT } from '../_lib/rider.js';
import { TOKEN_MINT as THREE_MINT, TOKEN_SYMBOL } from '../_lib/token/config.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
if (!method(req, res, ['GET'])) return;
+ const vaultAddress = env.RIDER_VAULT_ADDRESS ?? null;
return json(res, 200, {
- vault_address: env.RIDER_VAULT_ADDRESS ?? null,
+ vault_address: vaultAddress,
token_mint: THREE_MINT,
token_symbol: TOKEN_SYMBOL,
- required_amount: 8000,
+ required_amount: REQUIRED_AMOUNT,
+ accepting_payments: Boolean(vaultAddress),
});
});
diff --git a/api/rss/announcements.js b/api/rss/announcements.js
index b13068ba2..474abd22b 100644
--- a/api/rss/announcements.js
+++ b/api/rss/announcements.js
@@ -2,10 +2,11 @@
// Default source: data/rss/items.json (curated, hand-edited).
// Mirror modes: ?source=trythreews | ?source=nichxbt | ?source=archive (X scrape).
-import { cors, method, reportServerError, redactUrl } from '../_lib/http.js';
+import { cors, method, error, reportServerError, redactUrl } from '../_lib/http.js';
import { loadCuratedItems, loadAnnouncementItems, buildRssXml } from '../_lib/rss-feed.js';
const ARCHIVE_SOURCES = new Set(['archive', 'trythreews', 'nichxbt']);
+const VALID_SOURCES = ['curated', ...ARCHIVE_SOURCES];
export default async function handler(req, res) {
if (cors(req, res, { origins: '*', methods: 'GET,OPTIONS', credentials: false })) return;
@@ -14,6 +15,14 @@ export default async function handler(req, res) {
const url = new URL(req.url, 'https://three.ws');
const sourceParam = (url.searchParams.get('source') || 'curated').toLowerCase();
+ // A misspelled source used to fall through to the curated feed, so a reader
+ // subscribed to ?source=trythreewz silently received a different feed and had
+ // no way to notice. Say so instead.
+ if (!VALID_SOURCES.includes(sourceParam)) {
+ error(res, 400, 'unknown_source', `unknown source "${sourceParam}"; expected one of: ${VALID_SOURCES.join(', ')}`);
+ return;
+ }
+
try {
let items;
let source;
@@ -37,6 +46,6 @@ export default async function handler(req, res) {
res.statusCode = 500;
res.setHeader('content-type', 'text/plain; charset=utf-8');
res.setHeader('cache-control', 'no-store');
- res.end(`feed unavailable — quote ref ${ref} to support`);
+ res.end(`feed unavailable. Quote ref ${ref} to support.`);
}
}
diff --git a/api/seed/farcaster.js b/api/seed/farcaster.js
index 1c1e257d2..6a8925064 100644
--- a/api/seed/farcaster.js
+++ b/api/seed/farcaster.js
@@ -1,24 +1,49 @@
// GET /api/seed/farcaster?handle=<fname-or-fid>
//
-// Public Farcaster footprint connector for the three.ws memory-seeding
-// demo. Uses the @neynar/nodejs-sdk to look up the user (by username or
-// fid) and pull the last 20 casts.
+// Public Farcaster footprint connector for the three.ws memory-seeding demo.
+// Resolves a user by fname or fid and returns their profile plus the casts
+// worth remembering, which is what /api/seed/synthesize turns into a memory
+// seed.
//
-// When NEYNAR_API_KEY is not configured we return { ok: false, reason }
-// with a 200 status so the demo can render a "connector not configured"
-// state cleanly.
+// Reads through api/_lib/farcaster-client.js so this endpoint inherits the
+// platform's two-rung failover: Neynar when NEYNAR_API_KEY is configured
+// (indexed data, so engagement is known), and otherwise a public Farcaster hub
+// over HTTP with no key at all. The connector therefore returns real data on a
+// deployment with zero social credentials instead of a "not configured" card.
+//
+// `lane` in the response names the rung that answered, and every field the hub
+// lane cannot know (follower counts, per-cast engagement) is reported as null
+// rather than a zero the UI would render as fact.
-import { NeynarAPIClient, Configuration } from '@neynar/nodejs-sdk';
import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
+import {
+ FarcasterError,
+ fetchRecentCasts,
+ resolveFarcasterUser,
+} from '../_lib/farcaster-client.js';
+import { selectSeedCasts } from '../_lib/farcaster-seed.js';
+
+// Farcaster fnames: lowercase alphanumerics, hyphens, and (for ENS-backed
+// names) dots. Anything else cannot resolve, so reject it before the network.
+const FNAME_RE = /^[a-z0-9][a-z0-9.-]{0,47}$/;
-let _client = null;
-function client() {
- if (_client) return _client;
- const apiKey = process.env.NEYNAR_API_KEY;
- if (!apiKey) return null;
- _client = new NeynarAPIClient(new Configuration({ apiKey }));
- return _client;
+// Pulled wide, then ranked down: selectSeedCasts drops link-only and duplicate
+// posts, so a raw 20 would often leave far fewer than 20 usable casts.
+const CAST_FETCH_LIMIT = 100;
+const CAST_RETURN_LIMIT = 20;
+
+/**
+ * Shape the client's normalized casts into the connector's wire format.
+ * Exported for tests: this is the only place the endpoint decides what a
+ * caller sees, and the null-vs-number engagement contract matters to the UI.
+ */
+export function shapeCasts(casts, limit = CAST_RETURN_LIMIT) {
+ return selectSeedCasts(casts, { limit, includeReplies: false }).map((cast) => ({
+ text: cast.text,
+ timestamp: Number.isFinite(cast.timestamp) ? new Date(cast.timestamp).toISOString() : null,
+ engagement: typeof cast.engagement === 'number' ? cast.engagement : null,
+ }));
}
export default wrap(async (req, res) => {
@@ -29,70 +54,55 @@ export default wrap(async (req, res) => {
if (!rl.success) return rateLimited(res, rl);
const url = new URL(req.url, 'http://x');
- const raw = (url.searchParams.get('handle') || '').trim().replace(/^@/, '');
+ const raw = (url.searchParams.get('handle') || '').trim().replace(/^@/, '').toLowerCase();
if (!raw) return error(res, 400, 'invalid_request', 'handle query param required');
- const c = client();
- if (!c) {
- res.setHeader('cache-control', 'no-store');
- return json(res, 200, {
- ok: false,
- reason: 'Farcaster connector not configured',
- detail: 'Set NEYNAR_API_KEY in the environment to enable this connector.',
- });
- }
-
- // Resolve to a Farcaster user. Numeric → fid, otherwise → username lookup.
- let user = null;
const numeric = /^\d+$/.test(raw) ? Number(raw) : null;
+ if (numeric != null && (!Number.isSafeInteger(numeric) || numeric <= 0))
+ return error(res, 400, 'invalid_handle', 'fid must be a positive integer');
+ if (numeric == null && !FNAME_RE.test(raw))
+ return error(res, 400, 'invalid_handle', 'Farcaster name has invalid characters');
- try {
- if (numeric != null) {
- const resp = await c.fetchBulkUsers({ fids: [numeric] });
- user = resp?.users?.[0] || null;
- } else {
- const resp = await c.lookupUserByUsername({ username: raw });
- user = resp?.user || null;
- }
- } catch (err) {
- const status = err?.response?.status || 502;
- if (status === 404) return error(res, 404, 'not_found', `no Farcaster user "${raw}"`);
- res.setHeader('cache-control', 'no-store');
- return json(res, 200, {
- ok: false,
- reason: 'Farcaster upstream error',
- detail: err?.response?.data?.message || err.message || 'lookup failed',
- });
- }
-
- if (!user) return error(res, 404, 'not_found', `no Farcaster user "${raw}"`);
-
- const fid = user.fid;
+ let user;
let casts = [];
+ let castLane = null;
try {
- const resp = await c.fetchCastsForUser({ fid, limit: 20, includeReplies: false });
- casts = (resp?.casts || []).map((cast) => ({
- text: cast.text || '',
- timestamp: cast.timestamp || null,
- likes: cast.reactions?.likes_count ?? 0,
- recasts: cast.reactions?.recasts_count ?? 0,
- replies: cast.replies?.count ?? 0,
- }));
+ user = await resolveFarcasterUser(numeric != null ? { fid: numeric } : { fname: raw });
+ const recent = await fetchRecentCasts(user.fid, CAST_FETCH_LIMIT);
+ casts = shapeCasts(recent.casts);
+ castLane = recent.lane;
} catch (err) {
- console.warn('[seed/farcaster] casts fetch failed', err?.message || err);
+ if (err instanceof FarcasterError) {
+ if (err.status === 404)
+ return error(res, 404, 'not_found', `no Farcaster user "${raw}"`);
+ // The profile is the primary signal. A cast-page failure after the
+ // user resolved still has a payload worth returning.
+ if (!user) {
+ console.warn('[seed/farcaster] lookup failed', err.message);
+ res.setHeader('cache-control', 'no-store');
+ return json(res, 200, {
+ ok: false,
+ reason: 'Farcaster upstream error',
+ detail: err.message,
+ });
+ }
+ console.warn('[seed/farcaster] casts fetch failed', err.message);
+ } else {
+ throw err;
+ }
}
res.setHeader('cache-control', 'public, s-maxage=300, stale-while-revalidate=600');
return json(res, 200, {
ok: true,
- handle: user.username || String(fid),
- fid,
- display_name: user.display_name || null,
- avatar_url: user.pfp_url || null,
- bio: user.profile?.bio?.text || '',
- follower_count: user.follower_count ?? 0,
- following_count: user.following_count ?? 0,
- power_badge: !!user.power_badge,
+ lane: castLane || user.lane,
+ handle: user.fname || String(user.fid),
+ fid: user.fid,
+ display_name: user.displayName || null,
+ avatar_url: user.pfpUrl || null,
+ bio: user.bio || '',
+ follower_count: Number.isFinite(user.followerCount) ? user.followerCount : null,
+ following_count: Number.isFinite(user.followingCount) ? user.followingCount : null,
recent_casts: casts,
});
});
diff --git a/api/seed/x.js b/api/seed/x.js
index 7e818bd40..d0d543de2 100644
--- a/api/seed/x.js
+++ b/api/seed/x.js
@@ -4,9 +4,15 @@
// memory-seeding demo. Uses twitter-api-v2 in app-only (bearer) mode so
// we can look up any public profile without OAuth on the visitor side.
//
-// When TWITTER_BEARER_TOKEN is not configured (typical in local dev) we
-// return { ok: false, reason } with a 200 status so the demo UI can
-// render a graceful "connector not configured" state instead of erroring.
+// Two credential rungs, because X has no keyless public read lane and a
+// deployment that holds one of these should not show a dead connector:
+// 1. TWITTER_BEARER_TOKEN, an app-only bearer minted in the developer
+// portal. Used directly.
+// 2. X_API_KEY + X_API_SECRET, the OAuth 1.0a consumer pair the changelog
+// poster already uses. appLogin() exchanges them for the same app-only
+// bearer at runtime, so no extra credential has to be provisioned.
+// With neither configured we return { ok: false, reason } at 200 so the demo
+// UI renders a "connector not configured" state instead of erroring.
import { TwitterApi } from 'twitter-api-v2';
import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
@@ -14,7 +20,7 @@ import { limits, clientIp } from '../_lib/rate-limit.js';
const HANDLE_RE = /^[A-Za-z0-9_]{1,15}$/;
-// Compact English stopword list — pure JS so we don't pull in an NLP dep.
+// Compact English stopword list: pure JS so we don't pull in an NLP dep.
const STOPWORDS = new Set([
'the',
'a',
@@ -175,14 +181,18 @@ const STOPWORDS = new Set([
'okay',
]);
-function extractTopTopics(tweets, limit = 8) {
+/**
+ * Rank the words a timeline keeps coming back to. Hashtags and handles survive
+ * tokenisation intact because they are the strongest topic signal X carries.
+ * Exported for tests.
+ */
+export function extractTopTopics(tweets, limit = 8) {
const counts = new Map();
for (const text of tweets) {
if (!text) continue;
const tokens = text
.toLowerCase()
- .replace(/https?:\/\/\S+/g, '')
- .replace(/[@#]?[\w-]+/g, (m) => (m.startsWith('#') || m.startsWith('@') ? m : m))
+ .replace(/https?:\/\/\S+/g, ' ')
.replace(/[^a-z0-9#@_\s-]/g, ' ')
.split(/\s+/)
.filter(Boolean);
@@ -199,6 +209,34 @@ function extractTopTopics(tweets, limit = 8) {
.map(([tok, n]) => ({ topic: tok, count: n }));
}
+// The app-only bearer is stable for the life of the app credentials, so the
+// appLogin() exchange happens once per process rather than once per request.
+let _clientPromise = null;
+function appOnlyClient() {
+ if (_clientPromise) return _clientPromise;
+
+ const bearer = process.env.TWITTER_BEARER_TOKEN;
+ if (bearer) {
+ _clientPromise = Promise.resolve(new TwitterApi(bearer).readOnly);
+ return _clientPromise;
+ }
+
+ const appKey = process.env.X_API_KEY;
+ const appSecret = process.env.X_API_SECRET;
+ if (!appKey || !appSecret) return Promise.resolve(null);
+
+ _clientPromise = new TwitterApi({ appKey, appSecret })
+ .appLogin()
+ .then((c) => c.readOnly)
+ .catch((err) => {
+ // Never cache a failed exchange: a transient X outage would otherwise
+ // keep the connector dark for the rest of the process lifetime.
+ _clientPromise = null;
+ throw err;
+ });
+ return _clientPromise;
+}
+
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
if (!method(req, res, ['GET'])) return;
@@ -212,18 +250,28 @@ export default wrap(async (req, res) => {
if (!HANDLE_RE.test(handle))
return error(res, 400, 'invalid_handle', 'X handle has invalid characters');
- const bearer = process.env.TWITTER_BEARER_TOKEN;
- if (!bearer) {
+ let client;
+ try {
+ client = await appOnlyClient();
+ } catch (err) {
+ console.warn('[seed/x] app-only login failed', err?.message || err);
+ res.setHeader('cache-control', 'no-store');
+ return json(res, 200, {
+ ok: false,
+ reason: 'X upstream error',
+ detail: 'X rejected the configured app credentials.',
+ });
+ }
+ if (!client) {
res.setHeader('cache-control', 'no-store');
return json(res, 200, {
ok: false,
reason: 'X connector not configured',
- detail: 'Set TWITTER_BEARER_TOKEN in the environment to enable this connector.',
+ detail:
+ 'Set TWITTER_BEARER_TOKEN, or X_API_KEY and X_API_SECRET, in the environment to enable this connector.',
});
}
- const client = new TwitterApi(bearer).readOnly;
-
let userData;
try {
userData = await client.v2.userByUsername(handle, {
diff --git a/api/signals/marketplace.js b/api/signals/marketplace.js
index e9d74ed4b..f08831489 100644
--- a/api/signals/marketplace.js
+++ b/api/signals/marketplace.js
@@ -2,10 +2,10 @@
* GET /api/signals/marketplace?network=mainnet&sort=edge&limit=60
*
* The public signal-feed directory. Every active, public feed is scored by its
- * PROVEN realized edge — hit-rate × realized ROI, confidence-regressed toward the
- * publisher's own verified track-record score until enough signals have closed —
- * and ranked. A thin feed riding one lucky call can never top a deep, consistent
- * one. Open read: no auth, cacheable.
+ * PROVEN realized edge (hit-rate times realized ROI, confidence-regressed toward
+ * the publisher's own verified track-record score until enough signals have
+ * closed) and ranked. A thin feed riding one lucky call can never top a deep,
+ * consistent one. Open read: no auth, cacheable.
*/
import { cors, json, method, wrap, rateLimited } from '../_lib/http.js';
diff --git a/api/signals/stream.js b/api/signals/stream.js
index 742ad945f..671fc7490 100644
--- a/api/signals/stream.js
+++ b/api/signals/stream.js
@@ -4,7 +4,7 @@
* The PAID live feed of a publisher's emissions, as Server-Sent Events. Reads are
* gated by entitlement: the caller must either own the publishing agent (the
* publisher previews their own feed) or own an agent with an ACTIVE, non-killed
- * subscription to it — and that subscription is what settles the x402 USDC, so a
+ * subscription to it, and that subscription is what settles the x402 USDC, so a
* non-subscriber can never read the live alpha. Unentitled callers get a 402.
*
* Like the sniper stream, this DB-polls signal_emissions (Neon serverless HTTP
@@ -18,7 +18,7 @@ import { cors, method, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { sql } from '../_lib/db.js';
import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';
-import { normNetwork, NETWORKS } from './_common.js';
+import { normNetwork } from './_common.js';
const MAX_DURATION_MS = 90_000;
const PING_INTERVAL_MS = 15_000;
@@ -58,7 +58,6 @@ export default async function handleSignalStream(req, res) {
const url = new URL(req.url, `http://${req.headers.host || 'x'}`);
const slug = url.searchParams.get('slug');
if (!slug) return error(res, 400, 'invalid_slug', 'slug required');
- const network = NETWORKS.has(url.searchParams.get('network')) ? url.searchParams.get('network') : normNetwork(url.searchParams.get('network'));
// Authenticate.
const session = await getSessionUser(req);
@@ -68,6 +67,10 @@ export default async function handleSignalStream(req, res) {
const [feed] = await sql`select id, network, owner_user_id from signal_feeds where slug = ${slug} limit 1`;
if (!feed) return error(res, 404, 'not_found', 'feed not found');
+ // The slug is globally unique, so the ROW decides which cluster the emitted
+ // explorer links point at. Trusting a caller-supplied `?network=` would hand a
+ // mainnet feed's subscribers devnet solscan links that resolve to nothing.
+ const network = normNetwork(feed.network);
// Entitlement: publisher-owner OR an active, non-killed subscriber.
let entitled = feed.owner_user_id === userId;
diff --git a/api/signals/subscribe.js b/api/signals/subscribe.js
index 304f0cb0d..9959c3f29 100644
--- a/api/signals/subscribe.js
+++ b/api/signals/subscribe.js
@@ -20,7 +20,7 @@ import { limits, clientIp } from '../_lib/rate-limit.js';
import { requireCsrf } from '../_lib/csrf.js';
import { sql } from '../_lib/db.js';
import { deliverSubscription } from '../_lib/signal-engine.js';
-import { requireUser, loadOwnedAgent, normNetwork } from './_common.js';
+import { requireUser, loadOwnedAgent, normNetwork, parseRowId } from './_common.js';
function num(v, fallback) { const n = Number(v); return Number.isFinite(n) ? n : fallback; }
@@ -90,8 +90,8 @@ export default wrap(async (req, res) => {
if (auth.viaSession && !(await requireCsrf(req, res, userId))) return;
if (req.method === 'DELETE') {
- const id = new URL(req.url, 'http://x').searchParams.get('id');
- if (!id) return error(res, 400, 'invalid_id', 'id required');
+ const id = parseRowId(new URL(req.url, 'http://x').searchParams.get('id'));
+ if (!id) return error(res, 400, 'invalid_id', 'numeric subscription id required');
const [row] = await sql`
update signal_subscriptions set status = 'stopped', updated_at = now()
where id = ${id} and owner_user_id = ${userId} returning id
@@ -105,18 +105,20 @@ export default wrap(async (req, res) => {
// ── Mutations on an existing subscription (status / kill / sync) ────────────
if (body.id && !body.feed_id) {
+ const subId = parseRowId(body.id);
+ if (!subId) return error(res, 400, 'invalid_id', 'numeric subscription id required');
// Instant kill: the halt path, takes precedence.
if (body.killed != null) {
const killed = body.killed === true || body.killed === 'true';
const [row] = await sql`
update signal_subscriptions set killed = ${killed}, status = ${killed ? 'paused' : 'active'}, updated_at = now()
- where id = ${body.id} and owner_user_id = ${userId} returning *
+ where id = ${subId} and owner_user_id = ${userId} returning *
`;
if (!row) return error(res, 404, 'not_found', 'subscription not found');
return json(res, 200, { subscription: shapeSub(row) });
}
if (body.action === 'sync') {
- const [row] = await sql`select * from signal_subscriptions where id = ${body.id} and owner_user_id = ${userId} limit 1`;
+ const [row] = await sql`select * from signal_subscriptions where id = ${subId} and owner_user_id = ${userId} limit 1`;
if (!row) return error(res, 404, 'not_found', 'subscription not found');
const result = await deliverSubscription(row, { maxEvents: 10 });
return json(res, 200, { ok: true, ...result });
@@ -127,10 +129,10 @@ export default wrap(async (req, res) => {
const [row] = status === 'active'
? await sql`
update signal_subscriptions set status = 'active', killed = false, updated_at = now()
- where id = ${body.id} and owner_user_id = ${userId} returning *`
+ where id = ${subId} and owner_user_id = ${userId} returning *`
: await sql`
update signal_subscriptions set status = ${status}, updated_at = now()
- where id = ${body.id} and owner_user_id = ${userId} returning *`;
+ where id = ${subId} and owner_user_id = ${userId} returning *`;
if (!row) return error(res, 404, 'not_found', 'subscription not found');
return json(res, 200, { subscription: shapeSub(row) });
}
@@ -139,10 +141,12 @@ export default wrap(async (req, res) => {
// ── Create / update a subscription ──────────────────────────────────────────
if (!body.feed_id) return error(res, 400, 'invalid_feed', 'feed_id required');
+ const feedId = parseRowId(body.feed_id);
+ if (!feedId) return error(res, 400, 'invalid_feed', 'numeric feed_id required');
const owned = await loadOwnedAgent(req, res, userId, body.agent_id);
if (owned.error) return;
- const [feed] = await sql`select * from signal_feeds where id = ${body.feed_id} limit 1`;
+ const [feed] = await sql`select * from signal_feeds where id = ${feedId} limit 1`;
if (!feed) return error(res, 404, 'feed_not_found', 'feed not found');
if (feed.status !== 'active') return error(res, 409, 'feed_inactive', 'this feed is not active');
if (feed.publisher_agent_id === body.agent_id) return error(res, 400, 'self_subscribe', 'an agent cannot subscribe to its own feed');
@@ -157,7 +161,7 @@ export default wrap(async (req, res) => {
const firewallLevel = body.firewall_level === 'warn' ? 'warn' : 'block';
const copyExits = body.copy_exits !== false;
- // New subscriptions start at the current emission head — never charged for or
+ // New subscriptions start at the current emission head: never charged for or
// made to mirror a backlog of signals emitted before they subscribed.
const [head] = await sql`select coalesce(max(id),0) as head from signal_emissions where feed_id = ${feed.id}`;
const startCursor = Number(head?.head || 0);
diff --git a/api/studio-assets/[...path].js b/api/studio-assets/[...path].js
index fbb4a3986..abe4392b4 100644
--- a/api/studio-assets/[...path].js
+++ b/api/studio-assets/[...path].js
@@ -7,7 +7,9 @@
// (VRM, FBX, PNG, GLB, KTX2, etc.) stream through unchanged.
//
// Cache for 1 day at the edge / 7 days in the browser. The upstream content
-// is immutable per path so long TTLs are safe.
+// is immutable per path so long TTLs are safe. Because these responses ARE
+// edge-cached, nothing in the body may depend on a request header: see
+// rewriteJson.
import { cors, error, wrap } from '../_lib/http.js';
@@ -21,6 +23,18 @@ const TEXT_TYPES = new Set([
'text/plain',
]);
+// Content types that execute in a browsing context. The upstream trait library
+// serves none of them, so anything claiming one is either an upstream error
+// page or a mirror we do not want to hand back under the three.ws origin,
+// where it would run as same-origin script.
+const ACTIVE_TYPES = new Set([
+ 'text/html',
+ 'application/xhtml+xml',
+ 'image/svg+xml',
+ 'application/xml',
+ 'text/xml',
+]);
+
// Whitelist of upstream prefixes we're willing to mirror. Anything else gets
// a 404 — prevents this endpoint from being abused as an open proxy.
const ALLOWED_PREFIXES = ['anata/', '0N1/', 'tubbycats/', 'animations/', 'loot/'];
@@ -34,16 +48,38 @@ function joinPath(parts) {
return String(parts || '');
}
-// Rewrite any absolute upstream URL inside a JSON manifest to a path under
-// this proxy. Also rewrites the `assetsLocation` field so that downstream
-// asset resolution stays on three.ws origin.
-function rewriteJson(text, origin) {
+// Rewrite any absolute upstream URL inside a JSON manifest to a ROOT-RELATIVE
+// path under this proxy, so downstream asset resolution stays on whatever
+// origin served the studio.
+//
+// This deliberately does not read the request's Host / X-Forwarded-Host to
+// build an absolute URL. Cloud Run does not set X-Forwarded-Host, so a client
+// could send its own; the rewritten manifest would then point `assetsLocation`
+// at an attacker origin, and this response is `public, s-maxage=86400`, so the
+// CDN would serve that poisoned manifest to every studio user for a day. A
+// root-relative prefix is byte-identical for every caller, which makes the
+// response safe to cache and removes the injection entirely. The studio
+// concatenates `assetsLocation` with a leading-slash `traitsDirectory`
+// (CharacterManifestData.getTraitsDirectory), and the resulting doubled slash
+// resolves back through this same handler.
+function rewriteJson(text) {
const upstreamPattern = /https?:\/\/m3-org\.github\.io\/loot-assets\//g;
- return text.replace(upstreamPattern, `${origin}${PROXY_PREFIX}`);
+ return text.replace(upstreamPattern, PROXY_PREFIX);
+}
+
+// Upstream statuses are not ours to echo verbatim: a 3xx that `redirect:follow`
+// left unresolved would carry a body it must not have, and a 5xx would blame
+// three.ws for the mirror being down. Missing stays missing; everything else is
+// a bad gateway.
+function mapUpstreamStatus(status) {
+ if (status === 404 || status === 403 || status === 410) {
+ return { status: 404, code: 'not_found', message: 'asset not found in studio mirror' };
+ }
+ return { status: 502, code: 'upstream_error', message: `mirror returned ${status}` };
}
export default wrap(async (req, res) => {
- if (cors(req, res, { methods: 'GET,OPTIONS', credentials: false })) return;
+ if (cors(req, res, { origins: '*', methods: 'GET,HEAD,OPTIONS', credentials: false })) return;
if (req.method !== 'GET' && req.method !== 'HEAD') {
return error(res, 405, 'method_not_allowed', `method ${req.method} not allowed`);
}
@@ -65,7 +101,17 @@ export default wrap(async (req, res) => {
path = path.replace(/^\/+/, '');
// Anything still percent-encoded after 3 decode passes is hostile noise.
- if (!path || path.includes('%') || path.includes('..') || !isAllowed(path)) {
+ // `?` and `#` would truncate the upstream URL we build by concatenation,
+ // and a backslash is a path separator to some upstream servers.
+ if (
+ !path ||
+ path.includes('%') ||
+ path.includes('..') ||
+ path.includes('?') ||
+ path.includes('#') ||
+ path.includes('\\') ||
+ !isAllowed(path)
+ ) {
return error(res, 404, 'not_found', 'asset not in studio mirror whitelist');
}
@@ -77,12 +123,23 @@ export default wrap(async (req, res) => {
return error(res, 502, 'upstream_unreachable', `mirror fetch failed: ${err?.message}`);
}
+ // `redirect: follow` means the bytes we are about to serve under our own
+ // origin may have come from wherever the mirror pointed us. Only the mirror
+ // itself is trusted to fill this whitelist.
+ if (upstream.url && !upstream.url.startsWith(UPSTREAM_BASE)) {
+ return error(res, 502, 'upstream_error', 'mirror redirected off the asset library');
+ }
+
if (!upstream.ok) {
- return error(res, upstream.status, 'upstream_error', `mirror returned ${upstream.status}`);
+ const mapped = mapUpstreamStatus(upstream.status);
+ return error(res, mapped.status, mapped.code, mapped.message);
}
const contentType = upstream.headers.get('content-type') || 'application/octet-stream';
const baseType = contentType.split(';')[0].trim().toLowerCase();
+ if (ACTIVE_TYPES.has(baseType)) {
+ return error(res, 502, 'upstream_error', `mirror returned an unexpected ${baseType} document`);
+ }
const isText = TEXT_TYPES.has(baseType);
res.setHeader('content-type', contentType);
@@ -93,20 +150,23 @@ export default wrap(async (req, res) => {
res.setHeader('access-control-allow-origin', '*');
if (req.method === 'HEAD') {
+ // Only advertise a length we can stand behind. Upstream answers this
+ // fetch gzipped (undici asks for it), so its content-length is the
+ // COMPRESSED size while a GET here returns the decoded bytes — for a
+ // trait manifest that is 2.4 KB claimed against 20 KB delivered. Text
+ // bodies are rewritten on top of that, changing the length again.
const len = upstream.headers.get('content-length');
- if (len) res.setHeader('content-length', len);
+ if (len && !isText && !upstream.headers.get('content-encoding')) {
+ res.setHeader('content-length', len);
+ }
res.statusCode = 200;
return res.end();
}
if (isText) {
const text = await upstream.text();
- const proto = (req.headers['x-forwarded-proto'] || 'https').toString().split(',')[0].trim();
- const host = (req.headers['x-forwarded-host'] || req.headers.host || '').toString();
- const origin = host ? `${proto}://${host}` : '';
- const rewritten = origin ? rewriteJson(text, origin) : text;
res.statusCode = 200;
- return res.end(rewritten);
+ return res.end(rewriteJson(text));
}
const buf = Buffer.from(await upstream.arrayBuffer());