Free check partly gated behind joining the network (Marty 2026-11-16)

Not signed in: grade, verdict, first finding and the opinion line; the rest (scores, findings,
chain facts, screenshot, evidence) stays on the server. Join or sign in by one-time email link
(hub /v1/join with the wwi.ref sponsor when HUB_JOIN=1; staging reads only). 1 free check a day
without an account, 3 for members. Red flags deduped (deterministic VERIFIED one kept), grade
recomputed on read. Privacy: joining and emails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-16 11:36:29 -06:00
parent 5f1a8239f5
commit 47de249a8b
20 changed files with 317 additions and 56 deletions
+94
View File
@@ -0,0 +1,94 @@
// Join free / sign in by email (Marty 2026-11-16: the free check is partly gated behind joining the network).
// One form for both: a visitor enters their email and we send a one-time link (30 minutes). Opening it:
// - an existing network member (the hub knows the email): signs in here as that member
// - a new person: joins the network through the hub (POST /v1/join {email, ref, username}, sponsor = the wwi.ref referral cookie,
// so the member who shared the link gets the new member), then signs in. HUB_JOIN=1 writes the join; without it (staging) the
// hub is only read and nobody is created there: the session is local to this site.
// - a member the hub blocked: refused
// Then the browser lands back on the page they came from (to = a relative path only), so a report opens unlocked.
// Mail: MAIL_URL (a relay taking JSON {from, to, subject, text}, MAIL_KEY as bearer). Unset = no mail is sent (staging): the admin
// page lists pending links, and on staging only, addresses at the reserved test domains example.com/.org/.net get the link back in
// the answer (those domains cannot receive mail, so nobody real is involved).
// Emails: the sign-in link only, plus network news if the person ticked the opt-in box; every news email carries an unsubscribe link.
'use strict';
const crypto = require('crypto');
const EMAIL_RE = /^[a-z0-9._%+-]{1,64}@[a-z0-9.-]{1,190}\.[a-z]{2,24}$/;
const NAME_RE = /^[A-Za-z0-9_.-]{3,30}$/;
const TEST_RE = /@example\.(com|org|net)$/;
const TTL = 30 * 60000;
let D = null;
const sha = s => crypto.createHash('sha256').update(String(s)).digest('hex');
const safeTo = t => { t = String(t || '/my'); return /^\/(?!\/)[A-Za-z0-9/_.?=&%-]{0,190}$/.test(t) ? t : '/my'; };
async function mail(to, subject, text) {
const url = String(process.env.MAIL_URL || '').trim(); if (!url) return false;
const r = await fetch(url, { method: 'POST', signal: AbortSignal.timeout(10000), headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + String(process.env.MAIL_KEY || '') },
body: JSON.stringify({ from: process.env.MAIL_FROM || 'WebWorthIt <no-reply@webworthit.com>', to, subject, text }) });
return r.ok;
}
async function hubJoin(email, ref, username) {
const raw = JSON.stringify({ email, ref: ref || undefined, username: username || undefined }); const ts = String(Date.now());
const sig = crypto.createHmac('sha256', String(process.env.HUB_SECRET).trim()).update(ts + '\n' + raw).digest('hex');
const r = await fetch(String(process.env.HUB_URL).trim().replace(/\/+$/, '') + '/v1/join', { method: 'POST', body: raw, signal: AbortSignal.timeout(8000),
headers: { 'Content-Type': 'application/json', 'X-Hub-Site': String(process.env.HUB_SITE || 'webworthit').trim(), 'X-Hub-Ts': ts, 'X-Hub-Sig': sig } });
const j = await r.json().catch(() => ({}));
if (r.status !== 200) return { ok: false, status: r.status, error: j && j.error };
return { ok: true, created: !!j.created, person: j.person || null };
}
async function start(req, res) {
const { db, json, readBody, cookies, unsign, ipHash, limited } = D;
if (limited(req, 'join', 5, 15 * 60000)) return json(res, 429, { error: 'Too many tries from this connection. Wait 15 minutes.' });
const b = await readBody(req);
const email = String(b.email || '').trim().toLowerCase();
if (!EMAIL_RE.test(email)) return json(res, 400, { error: 'That email address does not look right.' });
const username = b.username && NAME_RE.test(String(b.username)) ? String(b.username) : null;
const recent = await db.one('SELECT COUNT(*) n FROM joins WHERE email = ? AND created > ?', [email, Date.now() - 3600000]);
if (Number(recent.n) >= 3) return json(res, 429, { error: 'We already sent links to that address in the last hour. Check your inbox and spam folder.' });
const token = crypto.randomBytes(24).toString('base64url');
const ref = unsign(cookies(req)['wwi.ref']);
const to = safeTo(b.to);
await db.q('INSERT INTO joins (token_hash, email, username, ref, optin, to_path, created, expires, ip_hash) VALUES (?,?,?,?,?,?,?,?,?)',
[sha(token), email, username, ref || null, b.optin ? 1 : 0, to, Date.now(), Date.now() + TTL, ipHash(req)]);
const link = 'https://' + D.SITE_HOST + '/join/confirm?t=' + token;
let mailed = false;
try { mailed = await mail(email, 'Your WebWorthIt sign-in link', 'Open this link within 30 minutes to sign in to WebWorthIt (one account across the network). New here? The same link creates your free account.\n\n' + link + '\n\nIf you did not ask for this, ignore this email: nothing happens without the link.'); } catch (e) { mailed = false; }
if (mailed) await db.q('UPDATE joins SET mailed = 1 WHERE token_hash = ?', [sha(token)]);
const out = { ok: true, mailed };
if (!mailed && D.STAGING && TEST_RE.test(email)) out.testLink = link; // staging, reserved test domain only
if (!mailed && !out.testLink) out.note = D.STAGING ? 'Staging: email is not switched on yet, so no link was sent.' : 'We could not send the email. Try again in a few minutes.';
return json(res, 200, out);
}
async function confirm(req, res, u) {
const { db, hubpeople } = D;
const t = String(u.searchParams.get('t') || '');
const bad = msg => { res.writeHead(302, { Location: '/join?err=' + encodeURIComponent(msg), 'Cache-Control': 'no-store' }); res.end(); };
if (!/^[A-Za-z0-9_-]{20,60}$/.test(t)) return bad('That link is not valid.');
const row = await db.one('SELECT * FROM joins WHERE token_hash = ?', [sha(t)]);
if (!row || row.status !== 'pending') return bad('That link was already used. Ask for a new one.');
if (row.expires < Date.now()) return bad('That link has expired. Ask for a new one.');
const r0 = await db.q("UPDATE joins SET status = 'used', confirmed_at = ? WHERE token_hash = ? AND status = 'pending'", [Date.now(), sha(t)]);
if (!r0.affectedRows) return bad('That link was already used. Ask for a new one.');
const email = row.email;
let person = null, result = '';
const look = await hubpeople.resolve({ email }).catch(() => ({ ok: false }));
if (look.ok && look.person && look.person.blocked) { await db.q('UPDATE joins SET hub_result = ? WHERE token_hash = ?', ['blocked on the hub', sha(t)]); return bad('This account cannot sign in. Contact network support.'); }
if (look.ok && look.person) { person = look.person; result = 'existing network member, signed in'; }
else if (String(process.env.HUB_JOIN || '') === '1' && hubpeople.on()) {
const j = await hubJoin(email, row.ref, row.username).catch(e => ({ ok: false, error: e.message }));
if (!j.ok) { await db.q('UPDATE joins SET hub_result = ? WHERE token_hash = ?', [('hub join failed ' + (j.status || '') + ' ' + (j.error || '')).slice(0, 300), sha(t)]); return bad('Joining the network did not work just now. Try again in a few minutes.'); }
person = j.person; result = (j.created ? 'joined the network' : 'already on the network') + (row.ref ? ', sponsor ref ' + row.ref : '');
} else {
result = look.ok ? 'staging: HUB_JOIN is off, nobody was created on the hub; local session only' + (row.ref ? ' (ref ' + row.ref + ' would be the sponsor)' : '') : 'hub unreachable; local session only';
}
await db.q('UPDATE joins SET hub_result = ? WHERE token_hash = ?', [result.slice(0, 300), sha(t)]);
const ck = await D.makeSession(email, { hubId: person && person.id, username: person && person.username, wallet: person && hubpeople.wallet(person), sponsor: person && person.sponsor ? person.sponsor.username : null,
optin: !!row.optin, via: person ? 'email-hub' : 'email-local' });
res.writeHead(302, { Location: safeTo(row.to_path), 'Set-Cookie': ck, 'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer' }); res.end();
}
function init(deps) { D = deps; return { start, confirm }; }
module.exports = { init, safeTo, TEST_RE };