Email typo check on new addresses: "Did you mean you@gmail.com?" for gmai.com / gmial.com / hotmial.com / outlook.comz / gmail.con etc., and refuse domains that cannot receive mail (no MX/A). Applies only to addresses with no account yet (existing members always sign in), on sign-in/sign-up, password signup and the support form; skipped when the server has no mail key (local QA). DNS errors fail open. Same emailcheck.js in IAP and LinkSpin. Prompted by SendGrid bounces to gmai.com and outlook.comz (2026-10-22).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-22 06:09:38 -05:00
parent 8b1395a90c
commit 3ee605a8bf
2 changed files with 112 additions and 0 deletions
+104
View File
@@ -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 };
+8
View File
@@ -18,6 +18,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');
@@ -2201,6 +2205,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 tickets.create({ source: 'public', email, name: b.name || (acct && acct.username) || '', username: acct && acct.username, subject: b.subject, category: b.category, body: b.body });
@@ -2241,6 +2246,7 @@ 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 iss = await newEmailIssue(String(b.email || '').trim().toLowerCase()); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); }
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);
@@ -2269,6 +2275,7 @@ const server = http.createServer(async (req, res) => {
const devHdr = fraud.deviceOf(req) ? undefined : { 'Set-Cookie': fraud.deviceCookie(fraud.newDeviceId(), IS_PROD) }; // browser id for one-account-per-person checks
if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(e)) return json(res, 400, { error: 'That email address does not look right.' });
if (fraud.isBlocked(e)) return json(res, 403, { error: 'This address cannot open an account.' });
{ const iss = await newEmailIssue(e); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }); }
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.' }, devHdr); }
const guard = codeGuard(req, b); // honeypot, form age, per-IP + global limits, icon check once limited
@@ -2374,6 +2381,7 @@ const server = http.createServer(async (req, res) => {
const e = String(b.email || '').trim().toLowerCase();
if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(e)) return json(res, 400, { error: 'That email address does not look right.' }, cors);
if (fraud.isBlocked(e)) return json(res, 403, { error: 'This address cannot open an account.' }, cors);
{ const iss = await newEmailIssue(e); if (iss) return json(res, 400, { error: iss.error, suggestion: iss.suggestion }, cors); }
const prev = emailCodes.get(e);
if (prev && Date.now() < prev.nextAt) return json(res, 429, { error: 'Code already sent. Give it a minute, then try again.' }, cors);
const guard = codeGuard(req, b); // honeypot, form age, per-IP + global limits, icon check once limited