import { z } from "zod"; import { CONNECTION_INTENTS, REGION_BANDS } from "./taxonomy.js"; export const ABUSE_REPORT_REASONS = [ "harassment", "spam", "prompt_injection", "scam_or_phishing", "underage_or_unsafe", ] as const; export type AbuseReportReason = (typeof ABUSE_REPORT_REASONS)[number]; export const CONTACT_CHANNELS = ["signal", "matrix", "email", "discord", "telegram", "other_handle"] as const; export const ProfileRegisterContentSchema = z .object({ x25519_pubkey: z.string().regex(/^[0-9a-f]{64}$/i, "x25519_pubkey must be 64 hex chars"), age_attestation: z.literal("18+"), taxonomy_version: z.literal("1.0.0"), region_band: z.enum(REGION_BANDS), intents: z.array(z.enum(CONNECTION_INTENTS)).min(1).max(4), commitment: z.object({ version: z.literal("ristretto255-psi-v2"), top_points: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(8), core_points: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(16), any_points: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(48), geo_points: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(18), }), }) .strict(); export const ChallengeSubmitContentSchema = z .object({ target_pubkey: z.string().min(32).max(256), challenger_x25519_pubkey: z.string().regex(/^[0-9a-f]{64}$/i), taxonomy_version: z.literal("1.0.0"), challenge: z.object({ version: z.literal("ristretto255-psi-v2"), target_top_blinded: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(8), target_core_blinded: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(16), target_any_blinded: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(48), target_geo_blinded: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).length(18), challenger_top: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).max(5), challenger_core: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).max(12), challenger_any: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).min(1).max(48), challenger_geo: z.array(z.string().regex(/^[0-9a-f]{64}$/i)).max(18), }), }) .strict(); export const E2EEEnvelopeSchema = z .object({ version: z.literal("x25519-aes256gcm-v1"), ephemeral_pubkey: z.string().regex(/^[0-9a-f]{64}$/i), nonce: z.string().regex(/^[0-9a-f]{24}$/i), ciphertext: z.string().regex(/^[0-9a-f]{32,16384}$/i), }) .strict(); export const MatchOfferContentSchema = z .object({ challenge_id: z.string().min(16).max(128), recipient_pubkey: z.string().min(32).max(256), stage: z.enum(["offer_initial", "accept_with_contact", "final_contact_release"]), parent_offer_id: z.string().min(16).max(128).nullable().optional(), envelope: E2EEEnvelopeSchema, }) .strict(); export const BlockPeerContentSchema = z .object({ blocked_pubkey: z.string().min(32).max(256), }) .strict(); export const AbuseReportContentSchema = z .object({ reported_pubkey: z.string().min(32).max(256), offer_id: z.string().min(16).max(128), // Interaction-gated: must reference an actual received match offer! reason: z.enum(ABUSE_REPORT_REASONS), }) .strict(); /** * Decrypted post-match payload schema (validated strictly on BOTH sender and receiver local clients!). * Zero raw URLs, zero HTML, zero markdown links, zero prompt-injection phrases. */ export const DecryptedOfferPayloadSchema = z .object({ protocol: z.literal("socialpsi-offer-v1"), stage: z.enum(["offer_initial", "accept_with_contact", "final_contact_release"]), score_band: z.enum(["moderate", "strong", "exceptional", "grail"]).optional(), matched_categories: z.array(z.string().max(40)).max(18).optional(), matched_phrase_labels: z.array(z.string().max(80)).max(20).optional(), matched_quests: z.array(z.string().max(80)).max(5).optional(), locality_match: z.enum(["none", "metro", "neighborhood"]).optional(), intro_note: z.string().max(240).optional(), contact: z .object({ channel: z.enum(CONTACT_CHANNELS), handle: z.string().min(2).max(64), }) .strict() .optional(), }) .strict(); export type DecryptedOfferPayload = z.infer; const URL_OR_LINK_PATTERNS: RegExp[] = [ /https?:\/\//i, /\bwww\.[a-z0-9-]+\.[a-z]{2,}/i, /\[[^\]]+\]\([^)]+\)/, /<\/?[a-z][^>]*>/i, /!\[[^\]]*\]\([^)]*\)/, /\b(?:javascript|data|file):/i, ]; const PROMPT_INJECTION_PATTERNS: RegExp[] = [ /ignore\s+(?:all\s+)?(?:previous|prior|above)\s+(?:instructions|prompts|rules)/i, /disregard\s+(?:all\s+)?(?:previous|system)\s+(?:instructions|prompts)/i, /\bsystem\s*prompt\b/i, /\bexfiltrate\b/i, /\.ssh\/id_rsa|process\.env|ANTHROPIC_API_KEY|OPENAI_API_KEY|GEMINI_API_KEY/i, /you\s+are\s+now\s+(?:a|an|in)\s+(?:developer|jailbreak|unrestricted|dan)\s+mode/i, /call\s+(?:the\s+)?tool\s+[a-z0-9_]+\s+immediately/i, /auto[-\s]?approve\s+(?:and\s+)?send\s+(?:my|your|the)\s+contact/i, /send\s+(?:crypto|bitcoin|eth|usdt|seed\s+phrase|private\s+key)/i, ]; export function scanUntrustedPlainText(text: string): { safe: boolean; reason?: string } { if (/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text)) { return { safe: false, reason: "Prohibited control characters detected" }; } for (const re of URL_OR_LINK_PATTERNS) { if (re.test(text)) { return { safe: false, reason: "Raw URLs, markdown links, or HTML tags are strictly prohibited" }; } } for (const re of PROMPT_INJECTION_PATTERNS) { if (re.test(text)) { return { safe: false, reason: "Suspected prompt injection or social-engineering payload blocked" }; } } return { safe: true }; } /** * Validate and sanitize a decrypted E2EE offer/contact payload BEFORE showing to a human or agent. */ export function validateDecryptedOfferPayload(raw: unknown): { valid: boolean; data?: DecryptedOfferPayload; error?: string; } { const parsed = DecryptedOfferPayloadSchema.safeParse(raw); if (!parsed.success) { return { valid: false, error: `Offer schema violation: ${parsed.error.issues[0]?.message || "invalid"}` }; } const d = parsed.data; if (d.intro_note) { const check = scanUntrustedPlainText(d.intro_note); if (!check.safe) return { valid: false, error: `Blocked intro_note: ${check.reason}` }; } if (d.contact) { const handleCheck = scanUntrustedPlainText(d.contact.handle); if (!handleCheck.safe) return { valid: false, error: `Blocked contact.handle: ${handleCheck.reason}` }; if (/\s{2,}|\n|\r/.test(d.contact.handle)) { return { valid: false, error: "contact.handle must be single-line plain text" }; } } if (d.matched_phrase_labels) { for (const lbl of d.matched_phrase_labels) { const c = scanUntrustedPlainText(lbl); if (!c.safe) return { valid: false, error: `Blocked matched_phrase_label: ${c.reason}` }; } } if (d.matched_quests) { for (const q of d.matched_quests) { const c = scanUntrustedPlainText(q); if (!c.safe) return { valid: false, error: `Blocked matched_quest: ${c.reason}` }; } } return { valid: true, data: d }; }