Sponsor survives a lost cookie: join page sends its own token with the code request, server remembers it per email for 30 days, framed visitors offered a full window

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-29 09:48:06 -05:00
parent b5baaf27e8
commit a7a15615ec
21 changed files with 1217 additions and 1188 deletions
+28 -4
View File
@@ -183,6 +183,23 @@ function chatLimited(ip) {
}
// magic-code sign-in: emailLower -> {code, exp, tries}
const emailCodes = new Map();
// Sponsor bound to the sign-up, not only to a cookie (Marty, 2026-09-29: nancyr #180 and others "supposed to land under
// Mad Dog" were created with no sponsor and fell to the company). The iap.sponsor cookie is SameSite=Lax, so it is
// rejected when the join page is shown inside another site (traffic-exchange frames), and it never crosses browsers
// (in-app browser -> mail app -> default browser). So: the join page sends the token from its own URL with the code
// request, the server remembers "this email asked for a code under <token>" for 30 days on disk (deploys restart the
// process), and account creation uses page token, then cookie, then that memory. A token-less request never
// overwrites a remembered token.
const pendingSponsor = {
FILE: () => path.join(DATA_DIR, 'pending-sponsors.json'), db: null,
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = {}; } },
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} },
set(email, f) { if (!email || !f || !f.ref) return; if (!this.db) this.load(); this.db[email] = { ref: f.ref, via: f.via || '', joinedRef: f.joinedRef || null, at: Date.now() }; this.prune(); this.save(); },
get(email) { if (!this.db) this.load(); const r = this.db[email]; return r && Date.now() - r.at < 30 * 86400e3 ? r : null; },
del(email) { if (!this.db) this.load(); if (this.db[email]) { delete this.db[email]; this.save(); } },
prune() { const cut = Date.now() - 30 * 86400e3; for (const k of Object.keys(this.db)) if (this.db[k].at < cut) delete this.db[k]; }
};
const pageTok = v => { const t = String(v || '').trim().toLowerCase(); return /^[a-z0-9_]{1,40}$/.test(t) ? (JOIN_ALIASES[t] || t) : ''; }; // a token the join page read from its own URL
// ── sign-up code guard (Marty, 2026-09-11): the email box is one field and one
// tap, so nothing visible stands in a human's way. Bots hit four invisible walls:
// a honeypot field, a minimum form age, per-IP + global send limits, and, only
@@ -1462,9 +1479,11 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/signup' && req.method === 'POST') {
const b = await readBody(req);
if (fraud.isBlocked(b.email)) return json(res, 403, { error: 'This address cannot open an account.' });
const ref = parseCookies(req)['iap.sponsor'] || ''; // last-touch attribution, locked at account creation
const pendS = pendingSponsor.get(String(b.email || '').trim().toLowerCase());
const ref = pageTok(b.ref) || parseCookies(req)['iap.sponsor'] || (pendS && pendS.ref) || ''; // last-touch attribution, locked at account creation
const r = await accounts.signup(b.email, b.password, ref);
if (r.error) return json(res, 400, r);
pendingSponsor.del(String(b.email || '').trim().toLowerCase());
// sponsor is notified once the new member picks a username (onboarding),
// so the email can name them — see /api/my/profile
if (b.newsletter) sendy.subscribe(r.account.email, r.account.username || '').catch(() => {}); // pre-checked opt-in, silent
@@ -1494,6 +1513,8 @@ const server = http.createServer(async (req, res) => {
if (guard) return json(res, guard.status, guard.body, devHdr);
const code = String(Math.floor(100000 + Math.random() * 900000));
emailCodes.set(e, { code, exp: Date.now() + 15 * 60 * 1000, tries: 0, nextAt: Date.now() + 60 * 1000 });
{ const ck = parseCookies(req); const bound = pageTok(b.ref) || ck['iap.sponsor'] || '';
if (bound && !(await accounts.byEmail(e))) pendingSponsor.set(e, { ref: bound, via: pageTok(b.ref) ? (String(b.angle || '').toLowerCase().slice(0, 20) || ck['iap.angle'] || 'page') : (ck['iap.angle'] || ''), joinedRef: decodeURIComponent(ck['iap.ref'] || '') || null }); }
if (mailer.hasKey()) {
try { await mailer.sendCode(e, code); } catch (err) {
console.error('sendCode failed', err.message);
@@ -1513,9 +1534,11 @@ const server = http.createServer(async (req, res) => {
if (rec.tries > 6) { emailCodes.delete(e); return json(res, 400, { error: 'Too many tries. Request a fresh code.' }); }
if (String(b.code || '').trim() !== rec.code) return json(res, 400, { error: 'That code does not match.' });
emailCodes.delete(e);
const ref = parseCookies(req)['iap.sponsor'] || '';
const via = parseCookies(req)['iap.angle'] || '';
const joinedRef = decodeURIComponent(parseCookies(req)['iap.ref'] || '') || null;
const pend = pendingSponsor.get(e);
const ref = pageTok(b.ref) || parseCookies(req)['iap.sponsor'] || (pend && pend.ref) || '';
const via = parseCookies(req)['iap.angle'] || (pend && pend.via) || '';
const joinedRef = decodeURIComponent(parseCookies(req)['iap.ref'] || '') || (pend && pend.joinedRef) || null;
if (ref && !parseCookies(req)['iap.sponsor']) console.log('sponsor recovered without cookie', e.replace(/^(.).*(@.*)$/, '$1***$2'), 'ref', ref, pageTok(b.ref) ? '(page token)' : '(remembered from the code request)');
if (fraud.isSuspended(e)) return json(res, 403, { error: 'This account is suspended. Contact support.' });
let fraudFlags = [];
const existing = await accounts.byEmail(e);
@@ -1530,6 +1553,7 @@ const server = http.createServer(async (req, res) => {
}
const r = await accounts.ensure(e, ref, via, joinedRef); // first touch wins; existing accounts unchanged
if (r.error) return json(res, 400, r);
if (r.created) pendingSponsor.del(e);
if (r.created) { fraud.recordSignup(e, req, fraudFlags).catch(() => {}); if (fraudFlags.length) fraudAlert(e, { flags: fraudFlags, ip: fraud.ipOf(req) }, null, false); }
else fraud.recordSeen(e, req).catch(() => {});
// the lead is in the door: queue the getting-started sequence (opt-in box is pre-checked on both forms)