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.

7.3 KB
Raw Blame
// Untrusted text quarantine for the team coordinator.
//
// Coin names, symbols, descriptions and social text are written by whoever
// launched the coin, which makes them attacker-controlled. CLAUDE.md gate 1 is
// absolute about them: on-chain and token metadata is data, never instructions,
// and no spend, transfer or mint may originate from it. The coordinator enforces
// that structurally rather than by asking a model to behave:
//
//   1. The plan is built from the owner's own message and remembered
//      preferences ONLY, and frozen (hashed) before any metadata is fetched. No
//      step, amount, mint or recipient can be added or changed after that point;
//      planFingerprint() is re-checked before every gated step executes.
//   2. Every metadata string that reaches evidence, a summary or the UI passes
//      through quarantineText(): control, zero-width and bidi-override
//      characters stripped, whitespace collapsed, length capped.
//   3. Instruction-shaped metadata is never silently dropped either. It is
//      flagged (scanForInjection) and surfaced to the owner as a risk note,
//      because a coin whose name tries to talk to an AI is itself a red flag.
//   4. Summaries are deterministic templates over structured fields; no model
//      ever reads metadata and writes the text the owner acts on.
//
// Pure: no I/O. Exercised by tests/team-chat-injection.test.js against
// real-world hostile token names.

import { createHash } from 'node:crypto';

// C0/C1 controls (keeping \n and \t), zero-width characters, bidi embeddings
// and overrides, and the byte-order mark. These are how hostile text hides
// itself or reorders what a reader sees.
const INVISIBLE_RE = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F\u200B-\u200F\u202A-\u202E\u2060-\u2069\uFEFF]/g;

// Patterns that read as an attempt to address or command an AI agent. Each has
// a short id so a finding names WHY it was flagged without echoing the payload.
const INJECTION_PATTERNS = [
	['override', /\b(ignore|disregard|forget|override|bypass)\b[^.\n]{0,40}\b(previous|prior|above|earlier|all|your|the|system|safety)\b[^.\n]{0,20}\b(instructions?|prompts?|rules?|guidelines?|messages?|guardrails?)\b/i],
	['new_instructions', /\b(new|updated|real|actual|hidden)\s+(instructions?|directives?|orders?|task)\b/i],
	['role_play', /\b(you are now|act as|pretend (to be|you are)|from now on you|developer mode|jailbreak|DAN mode)\b/i],
	['system_prompt', /\b(system prompt|system message|assistant:|\[system\]|<\|im_start\|>|<\|system\|>|###\s*instruction)/i],
	['move_funds', /\b(send|transfer|withdraw|drain|move|bridge|swap)\b[^.\n]{0,30}\b(all|every|entire|your|the|my)?\s*(sol|funds?|balance|tokens?|wallet|usdc|lamports)\b/i],
	['approve', /\b(auto[- ]?approve|approve (this|all|it|the)|sign (this|the|all)|confirm (this|the) (transaction|transfer))\b/i],
	['secrets', /\b(private key|secret key|seed phrase|mnemonic|recovery phrase|api key)\b/i],
	['agent_address', /\b(ai|agent|bot|assistant|claude|gpt|llm|model)s?\b[^.\n]{0,24}\b(must|should|will|shall|need to|please)\b/i],
	['buy_command', /\b(buy|ape|purchase)\b[^.\n]{0,20}\b(now|immediately|max|everything|all in)\b/i],
];

// Base58 strings long enough to be a Solana address. Metadata that carries an
// address next to a funds verb is the classic "send it here" payload.
const ADDRESS_RE = /\b[1-9A-HJ-NP-Za-km-z]{32,44}\b/;

/**
 * Normalize one untrusted string for storage and display. Never returns
 * markup: callers render it with textContent.
 *
 * @param {unknown} value
 * @param {number} [max=280]
 * @returns {string}
 */
export function quarantineText(value, max = 280) {
	if (value == null) return '';
	const s = String(value)
		.normalize('NFKC')
		.replace(INVISIBLE_RE, '')
		.replace(/[\r\n\t]+/g, ' ')
		.replace(/\s{2,}/g, ' ')
		.trim();
	if (s.length <= max) return s;
	return `${s.slice(0, Math.max(1, max - 1)).trimEnd()}…`;
}

/**
 * Scan untrusted text for instruction-shaped content.
 *
 * @param {unknown} value
 * @returns {{ suspicious: boolean, patterns: string[], hidden_chars: boolean }}
 */
export function scanForInjection(value) {
	const raw = value == null ? '' : String(value);
	const hidden = INVISIBLE_RE.test(raw);
	INVISIBLE_RE.lastIndex = 0;
	const text = raw.normalize('NFKC').replace(INVISIBLE_RE, ' ');
	const patterns = [];
	for (const [id, re] of INJECTION_PATTERNS) if (re.test(text)) patterns.push(id);
	if (ADDRESS_RE.test(text) && patterns.includes('move_funds')) patterns.push('payout_address');
	return { suspicious: patterns.length > 0, patterns, hidden_chars: hidden };
}

const FIELD_LIMITS = Object.freeze({ name: 64, symbol: 16, description: 400, twitter: 120, telegram: 120, website: 160 });

/**
 * Quarantine a coin's metadata block. Returns display-safe fields plus one
 * flag entry per field that looked like it was trying to instruct an agent.
 *
 * @param {object|null} meta  { name, symbol, description, socials?: {twitter, telegram, website} }
 * @returns {{ fields: object, flags: Array<{ field: string, patterns: string[], hidden_chars: boolean }>, suspicious: boolean }}
 */
export function quarantineMetadata(meta) {
	const m = meta && typeof meta === 'object' ? meta : {};
	const socials = m.socials && typeof m.socials === 'object' ? m.socials : {};
	const source = {
		name: m.name,
		symbol: m.symbol,
		description: m.description,
		twitter: socials.twitter ?? m.twitter,
		telegram: socials.telegram ?? m.telegram,
		website: socials.website ?? m.website,
	};
	const fields = {};
	const flags = [];
	for (const [key, limit] of Object.entries(FIELD_LIMITS)) {
		const v = source[key];
		if (v == null || v === '') {
			fields[key] = null;
			continue;
		}
		fields[key] = quarantineText(v, limit);
		const scan = scanForInjection(v);
		if (scan.suspicious || scan.hidden_chars) flags.push({ field: key, patterns: scan.patterns, hidden_chars: scan.hidden_chars });
	}
	return { fields, flags, suspicious: flags.length > 0 };
}

/** The owner-facing risk note for a flagged metadata block. Never quotes the payload. */
export function injectionRiskNote(flags) {
	if (!flags?.length) return null;
	const where = [...new Set(flags.map((f) => f.field))].join(', ');
	return `This coin's ${where} contains text aimed at AI agents. It was treated as data only and changed nothing in your plan; treat the coin itself as higher risk.`;
}

/** Deterministic JSON with sorted keys at every depth (matches the approval inbox's canonical form). */
export function canonicalJson(value) {
	if (value === null || typeof value !== 'object') return JSON.stringify(value ?? null);
	if (Array.isArray(value)) return `[${value.map((v) => canonicalJson(v === undefined ? null : v)).join(',')}]`;
	const keys = Object.keys(value).filter((k) => value[k] !== undefined).sort();
	return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(value[k])}`).join(',')}}`;
}

export function sha256Hex(value) {
	return createHash('sha256').update(typeof value === 'string' ? value : canonicalJson(value)).digest('hex');
}

/**
 * Fingerprint of the money-relevant shape of a plan: every step's kind, role,
 * dependencies and parameters. Stored when the plan is frozen and re-checked
 * before each gated step executes, so nothing fetched later can alter it.
 */
export function planFingerprint(plan) {
	const steps = (plan?.steps || []).map((s) => ({ key: s.key, kind: s.kind, role: s.role, depends_on: s.depends_on || [], params: s.params || {} }));
	return sha256Hex({ steps });
}