diff --git a/emailcheck.js b/emailcheck.js new file mode 100644 index 0000000..03c30cc --- /dev/null +++ b/emailcheck.js @@ -0,0 +1,104 @@ +// Email typo check for NEW addresses (2026-10-22, after SendGrid bounces to gmai.com / outlook.comz). +// Same file lives in InstantAdPay and LinkSpin. Catches: +// 1. near-misses of the big mailbox providers (gmai.com, gmial.com, hotmial.com, yaho.com) -> "Did you mean ...?" +// 2. mistyped endings (.con, .cmo, .comz, .nett) -> "Did you mean ...?" +// 3. domains that cannot receive mail at all (no MX and no A/AAAA record) +// A DNS timeout or server error never blocks anyone (fails open). Only call it where an address enters for the +// first time - existing accounts must always be able to sign in with the address they already have. +const dns = require('dns').promises; + +const POPULAR = ['gmail.com', 'googlemail.com', 'yahoo.com', 'ymail.com', 'hotmail.com', 'outlook.com', 'live.com', + 'msn.com', 'icloud.com', 'me.com', 'mac.com', 'aol.com', 'protonmail.com', 'proton.me', 'mail.com', 'gmx.com', + 'gmx.de', 'yandex.com', 'yandex.ru', 'zoho.com', 'comcast.net', 'att.net', 'verizon.net', 'sbcglobal.net', + 'bellsouth.net', 'cox.net', 'charter.net', 'yahoo.co.uk', 'hotmail.co.uk', 'btinternet.com', 'yahoo.fr', + 'hotmail.fr', 'orange.fr', 'web.de', 't-online.de', 'libero.it', 'yahoo.com.br', 'outlook.com.br', 'rediffmail.com', + 'qq.com', '163.com', 'naver.com', 'seznam.cz', 'wp.pl', 'mail.ru']; +const POP = new Set(POPULAR); +// endings that are typos of .com/.net/.org (real TLDs like .co, .cm, .om are deliberately NOT here) +const BAD_TLD = { con: 'com', cmo: 'com', comm: 'com', comz: 'com', coom: 'com', cpm: 'com', vom: 'com', xom: 'com', + ocm: 'com', cmon: 'com', 'com.com': 'com', nett: 'net', nte: 'net', ent: 'net', ogr: 'org', orgg: 'org', rog: 'org' }; + +// optimal string alignment distance (Damerau-Levenshtein with adjacent swaps), capped +function dist(a, b) { + if (Math.abs(a.length - b.length) > 2) return 9; + const d = []; + for (let i = 0; i <= a.length; i++) { d[i] = [i]; } + for (let j = 0; j <= b.length; j++) d[0][j] = j; + for (let i = 1; i <= a.length; i++) { + for (let j = 1; j <= b.length; j++) { + const c = a[i - 1] === b[j - 1] ? 0 : 1; + d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + c); + if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1); + } + } + return d[a.length][b.length]; +} + +// providers that only ever use ONE domain, so gmail.co / gmail.cm / icloud.net are always typos. +// (outlook, hotmail, yahoo, live have real national domains like hotmail.it, so they are not listed.) +const SINGLE = { gmail: 'gmail.com', googlemail: 'googlemail.com', icloud: 'icloud.com', aol: 'aol.com', + protonmail: 'protonmail.com', ymail: 'ymail.com' }; +const split = d => { const i = d.indexOf('.'); return i < 0 ? [d, ''] : [d.slice(0, i), d.slice(i + 1)]; }; + +function suggestDomain(domain) { + if (POP.has(domain)) return null; + const labels = domain.split('.'); + const two = labels.slice(-2).join('.'), last = labels[labels.length - 1]; + let d = domain; + if (BAD_TLD[two]) d = labels.slice(0, -2).concat(BAD_TLD[two]).join('.'); + else if (BAD_TLD[last]) d = labels.slice(0, -1).concat(BAD_TLD[last]).join('.'); + if (POP.has(d)) return d; + const near = nearestPopular(d); + if (near) return near; + return d !== domain ? d : null; // ending fixed but not a known provider: still suggest the fixed ending +} + +function nearestPopular(domain) { + const [name, suffix] = split(domain); + if (SINGLE[name] && domain !== SINGLE[name]) return SINGLE[name]; + let best = null, bestD = 9; + for (const p of POPULAR) { + const [pn, ps] = split(p); + if (ps !== suffix) continue; // only compare names that share the same ending + const dd = dist(name, pn); + if (dd < bestD) { best = p; bestD = dd; } + } + // 1 edit always counts; 2 edits only for longer names, so short real domains are left alone + return (bestD === 1 || (bestD === 2 && name.length >= 6)) ? best : null; +} + +function withTimeout(promise, ms) { + return Promise.race([promise, new Promise((_, rej) => setTimeout(() => rej(Object.assign(new Error('timeout'), { code: 'ETIMEOUT' })), ms))]); +} + +// true = has somewhere to deliver, false = definitely cannot receive mail, null = could not tell (fail open) +async function domainReceives(domain) { + const gone = c => c === 'ENOTFOUND' || c === 'ENODATA' || c === 'ESERVFAIL_NXDOMAIN'; + try { + const mx = await withTimeout(dns.resolveMx(domain), 3000); + if (mx && mx.length && !(mx.length === 1 && mx[0].exchange === '')) return true; // RFC 7505 null MX = no mail + if (mx && mx.length) return false; + } catch (e) { if (!gone(e.code)) return null; } + try { await withTimeout(dns.resolve4(domain), 3000); return true; } catch (e) { if (!gone(e.code)) return null; } + try { await withTimeout(dns.resolve6(domain), 3000); return true; } catch (e) { if (!gone(e.code)) return null; } + return false; +} + +// -> null when the address is fine, or { error, suggestion } to show the person +async function emailIssue(email) { + const e = String(email || '').trim().toLowerCase(); + const at = e.lastIndexOf('@'); + if (at < 1) return null; // the caller's own format check handles this + const local = e.slice(0, at), domain = e.slice(at + 1); + const fix = suggestDomain(domain); + if (fix && fix !== domain) { + const suggestion = local + '@' + fix; + return { error: 'Did you mean ' + suggestion + '? Please check the email address.', suggestion }; + } + if (POP.has(domain)) return null; + const ok = await domainReceives(domain); + if (ok === false) return { error: 'The email domain "' + domain + '" cannot receive mail. Please check the address.', suggestion: null }; + return null; +} + +module.exports = { emailIssue, suggestDomain, domainReceives }; diff --git a/server.js b/server.js index ae45cb6..3f9b061 100644 --- a/server.js +++ b/server.js @@ -17,6 +17,10 @@ const auth = require('./auth'); const accounts = require('./accounts'); const ads = require('./ads'); const mailer = require('./mailer'); +const { emailIssue } = require('./emailcheck'); // typo / no-mail-domain check for NEW addresses (2026-10-22) +// only addresses we have never seen get the typo check; existing members always sign in with what they have. +// Skipped when this server cannot send mail (local QA copies sign in as @example.com via devCode). +const newEmailIssue = async e => (!mailer.hasKey() || (await accounts.byEmail(e)) ? null : emailIssue(e)); const messages = require('./messages'); const reports = require('./reports'); const drip = require('./drip'); @@ -1457,6 +1461,7 @@ const server = http.createServer(async (req, res) => { const acct = s && s.email ? await accounts.byEmail(s.email) : null; // signed in: the ticket is theirs, whatever was typed const email = acct ? acct.email : String(b.email || '').trim().toLowerCase(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) return json(res, 400, { error: 'That email address does not look right.' }); + if (!acct) { const iss = await newEmailIssue(email); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); } // replies must reach them const guard = supportGuard(req, b, email); if (guard) return json(res, guard.status, guard.body); const r = await helpdesk.call('create', { source: 'public', email, name: b.name || (acct && acct.username) || '', username: acct && acct.username, subject: b.subject, category: b.category, body: b.body }); @@ -1528,6 +1533,7 @@ const server = http.createServer(async (req, res) => { if (!SIGNUPS_OPEN) return json(res, 403, SHUT); const b = await readBody(req); const ref = parseCookies(req)['iap.sponsor'] || ''; // last-touch attribution, locked at account creation + { const iss = await newEmailIssue(String(b.email || '').trim().toLowerCase()); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); } const r = await accounts.signup(b.email, b.password, ref); if (r.error) return json(res, 400, r); { const lc = parseCookies(req)['ls.click']; if (lc) { postbacks.bindClick(String(b.email || '').trim().toLowerCase(), lc); postbacks.convertFor(String(b.email || '').trim().toLowerCase(), 'join', {}).catch(() => {}); } } @@ -1555,6 +1561,7 @@ const server = http.createServer(async (req, res) => { // closed sign-ups shut the door to NEW accounts only; members already here (incl. imported shells) still get their code const acct0 = await accounts.byEmail(e); if (!SIGNUPS_OPEN && !acct0) return json(res, 403, SHUT); + if (!acct0) { const iss = await newEmailIssue(e); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); } if (!isTester(e, acct0)) return json(res, 403, TESTING); const prev = emailCodes.get(e); if (prev && Date.now() < prev.nextAt) { console.log('signup-guard cooldown', clientIp(req), e.replace(/^(.).*(@.*)$/, '$1***$2')); return json(res, 429, { error: 'Code already sent. Give it a minute, then try again.' }); }