// Submission body validation. Runs BEFORE x402: an invalid body gets 400 and never a 402, so nobody pays for it.
export const LIMITS = { name: 80, url: 500, email: 254, category: 48, description: 600, notes: 1000 };
const CTRL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
const EMAIL = /^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/;
const CATEGORY = /^[\p{L}\p{N}][\p{L}\p{N} &/,+.'-]*$/u;
const PRIVATE_HOST = /^(localhost|.*\.localhost|.*\.local|.*\.internal|.*\.lan|.*\.home\.arpa)$/i;
const IPV4 = /^\d{1,3}(\.\d{1,3}){3}$/;
/** True when the body is "empty" (nothing, whitespace or {}): the route answers with the 402 terms, like a bare GET. */
export function isEmptyBody(text) {
const t = String(text || '').trim();
if (!t) return true;
if (t === '{}') return true;
try { const o = JSON.parse(t); return o && typeof o === 'object' && !Array.isArray(o) && Object.keys(o).length === 0; } catch { return false; }
}
function str(o, k, { min = 1, max, required = true } = {}) {
const v = o[k];
if (v === undefined || v === null || v === '') return required ? { err: `${k} is required` } : { v: undefined };
if (typeof v !== 'string') return { err: `${k} must be a string` };
const s = v.trim().normalize('NFC');
if (CTRL.test(s)) return { err: `${k} must not contain control characters` };
if (s.length < min) return { err: `${k} must be at least ${min} characters` };
if (s.length > max) return { err: `${k} must be at most ${max} characters` };
return { v: s };
}
/** @returns {{ok:true, value:object} | {ok:false, errors:string[]}} */
export function validateSubmission(body) {
if (!body || typeof body !== 'object' || Array.isArray(body)) return { ok: false, errors: ['body must be a JSON object: {"tool_url","name","contact_email","category"}'] };
const errors = [];
const name = str(body, 'name', { min: 2, max: LIMITS.name });
const url = str(body, 'tool_url', { min: 8, max: LIMITS.url });
const email = str(body, 'contact_email', { min: 6, max: LIMITS.email });
const category = str(body, 'category', { min: 2, max: LIMITS.category });
const description = str(body, 'description', { max: LIMITS.description, required: false });
const notes = str(body, 'notes', { max: LIMITS.notes, required: false });
for (const r of [url, name, email, category, description, notes]) if (r.err) errors.push(r.err);
let tool_url;
if (url.v) {
let u = null; try { u = new URL(url.v); } catch { errors.push('tool_url must be a full URL, e.g. https://example.com'); }
if (u) {
const host = u.hostname.toLowerCase().replace(/\.$/, '');
if (u.protocol !== 'https:') errors.push('tool_url must use https://');
else if (u.username || u.password) errors.push('tool_url must not contain a username or password');
else if (!host.includes('.') || PRIVATE_HOST.test(host) || IPV4.test(host) || host.startsWith('[')) errors.push('tool_url must be a public domain name (no IP addresses or local hosts)');
else if (u.port && u.port !== '443') errors.push('tool_url must use the default https port');
else { u.hash = ''; tool_url = u.toString(); }
}
}
let contact_email;
if (email.v) { if (!EMAIL.test(email.v)) errors.push('contact_email must be a valid email address'); else { const [l, d] = email.v.split('@'); contact_email = `${l}@${d.toLowerCase()}`; } }
if (category.v && !CATEGORY.test(category.v)) errors.push("category may only contain letters, numbers, spaces and & / , + . ' -");
if (errors.length) return { ok: false, errors };
return { ok: true, value: { tool_url, name: name.v, contact_email, category: category.v, ...(description.v ? { description: description.v } : {}), ...(notes.v ? { notes: notes.v } : {}) } };
}
agentgate-source: code mirror on Cloudflare Pages. All repos