From 6ef7a023dfabbaea300e4e0cc07ff71f8931dc75 Mon Sep 17 00:00:00 2001 From: martbost Date: Sun, 15 Nov 2026 12:43:14 -0600 Subject: [PATCH] Member erase: their username leaves the messages that stay (another member's chat to them, a notice to their sponsor about them), whole words, 5+ characters; names option for a second pass after the account is gone; residue sweep also finds a long username inside text (REGEXP whole word) Found on the LS-1021 erase: two kept messages still said "rajumiddha". qa/erase.mysql.test.cjs 3 pass (disposable mysql:8 on core), qa/erase.test.cjs 6 pass. NOT deployed (one InstantAdPay deploy today for this lane); ships with the next one. Co-Authored-By: Claude Opus 5.5 --- erase.js | 23 +++++++++++++++++++---- qa/erase.mysql.test.cjs | 13 ++++++++++--- server.js | 4 ++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/erase.js b/erase.js index d9b0165..39edd19 100644 --- a/erase.js +++ b/erase.js @@ -76,9 +76,10 @@ async function residue(E, exact) { const cols = await db.q("SELECT table_name t, column_name c FROM information_schema.columns WHERE table_schema = DATABASE() AND data_type IN ('char','varchar','text','mediumtext','longtext','json') AND table_name NOT IN ('docs','docs_writer')"); const out = []; for (const r of cols) { - const t = r.t || r.TABLE_NAME, c = r.c || r.COLUMN_NAME; const ex = exact.filter(Boolean); - const where = '`' + c + '` LIKE ?' + ex.map(() => ' OR `' + c + '` = ?').join(''); - try { const [x] = await db.q('SELECT COUNT(*) n FROM `' + t + '` WHERE ' + where, [like(E)].concat(ex)); if (Number(x.n)) out.push({ table: t, column: c, n: Number(x.n) }); } catch (e) {} + const t = r.t || r.TABLE_NAME, c = r.c || r.COLUMN_NAME; const ex = exact.filter(Boolean); const words = ex.filter(x => x.length >= 5); // a long username inside text too + const where = '`' + c + '` LIKE ?' + ex.map(() => ' OR `' + c + '` = ?').join('') + words.map(() => ' OR `' + c + '` REGEXP ?').join(''); + const wordRe = w => '(^|[^a-z0-9_])' + w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '([^a-z0-9_]|$)'; // the whole word, not a longer name + try { const [x] = await db.q('SELECT COUNT(*) n FROM `' + t + '` WHERE ' + where, [like(E)].concat(ex, words.map(wordRe))); if (Number(x.n)) out.push({ table: t, column: c, n: Number(x.n) }); } catch (e) {} } return out; } @@ -128,7 +129,9 @@ async function erase(email, opts) { const o = Object.assign({ dryRun: true, force: false, by: 'admin', sweep: true }, opts || {}); const E = norm(email); if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(E)) return { error: 'A valid email is required.' }; const h = hashOf(E); const acct = await R.accounts.byEmail(E); - const exact = acct ? [acct.username, acct.code].filter(Boolean).map(norm) : []; + // names: usernames of an account already erased (a second pass after the code learned something new), e.g. ['oldname'] + const names = (Array.isArray(o.names) ? o.names : []).map(norm).filter(x => /^[a-z0-9_.-]{5,30}$/.test(x)); + const exact = (acct ? [acct.username, acct.code].filter(Boolean).map(norm) : []).concat(names); const needles = { emails: [E], exact }; const blockers = acct ? await blockersOf(acct, E) : []; const res = { dryRun: !!o.dryRun, emailHash: h, found: !!acct, blockers, forced: !!o.force && blockers.length > 0, sql: {}, docs: {} }; @@ -153,6 +156,18 @@ async function erase(email, opts) { const mutes = await db.q('SELECT email, chat_mutes FROM accounts WHERE chat_mutes LIKE ?', [like(E)]); let mu = 0; for (const m of mutes) { let list = []; try { list = JSON.parse(m.chat_mutes || '[]'); } catch (e) {} const keep = list.filter(x => norm(x) !== E); if (keep.length !== list.length) { mu++; if (!o.dryRun) await db.q('UPDATE accounts SET chat_mutes=? WHERE email=?', [JSON.stringify(keep), m.email]); } } if (mu) res.sql['accounts.chat_mutes (others, scrubbed)'] = mu; + // their username inside messages that stay (another member's chat to them, a notice to their sponsor about them): the name + // goes, the message stays. Whole words only, usernames of 5+ characters (a short one could be an ordinary word). + let rd = 0; + for (const u of exact.filter(x => x.length >= 5 && ((acct && norm(acct.username) === x) || names.includes(x)))) { + const re = new RegExp('(^|[^a-z0-9_])@?' + u.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '(?![a-z0-9_])', 'gi'); + for (const m of await db.q('SELECT id, subject, body FROM sponsor_messages WHERE subject LIKE ? OR body LIKE ?', [like(u), like(u)])) { + const sub = String(m.subject || '').replace(re, '$1[erased member]'), body = m.body == null ? null : String(m.body).replace(re, '$1[erased member]'); + if (sub === m.subject && body === m.body) continue; + rd++; if (!o.dryRun) await db.q('UPDATE sponsor_messages SET subject=?, body=? WHERE id=?', [sub, body, m.id]); + } + } + if (rd) res.sql['sponsor_messages (others, name removed)'] = rd; } else if (acct && !o.dryRun) { const r = await R.accounts.removeAccount(E); res.sql.accounts = r && r.ok ? 1 : 0; } else if (acct) res.sql.accounts = 1; res.docs = docs(needles, o.dryRun); diff --git a/qa/erase.mysql.test.cjs b/qa/erase.mysql.test.cjs index fc6e1f3..578032d 100644 --- a/qa/erase.mysql.test.cjs +++ b/qa/erase.mysql.test.cjs @@ -39,7 +39,7 @@ const E = 'gone9@example.test', O = 'other9@example.test'; await q("INSERT INTO account_signals (email, signup_ip) VALUES (?, '1.2.3.4')", [E]); await q("INSERT INTO adoptions (adoptee, adopter, ts, expires) VALUES (?, ?, 1, 2)", [E, O]); await q("INSERT INTO event_marks (k, ts) VALUES (?, 1), ('keep:other', 1)", ['tankpay1:' + E + ':17']); - await q("INSERT INTO sponsor_messages (from_member, from_email, to_email, subject, body, sent, kind) VALUES (0, ?, ?, '', 'their words', 1, 'chat'), (0, ?, ?, '', 'my words', 2, 'chat'), (1, 'admin@example.test', ?, 'Your ad', 'n', 3, 'notice')", [E, O, O, E, E]); + await q("INSERT INTO sponsor_messages (from_member, from_email, to_email, subject, body, sent, kind) VALUES (0, ?, ?, '', 'their words', 1, 'chat'), (0, ?, ?, '', 'my words', 2, 'chat'), (1, 'admin@example.test', ?, 'Your ad', 'n', 3, 'notice'), (1, 'admin@example.test', ?, '@goneuser9 has not switched on payouts', '

@goneuser9 needs a nudge; goneuser99 is someone else

', 4, 'notice')", [E, O, O, E, E, O]); const tk = await q("INSERT INTO tickets (ref, platform, email, name, username, subject, category, created, updated) VALUES ('LS-9001', 'linkspin', ?, 'Gone Person', 'goneuser9', 'delete me', 'account', 1, 1)", [E]); await q("INSERT INTO ticket_messages (ticket_id, author, body, created) VALUES (?, 'member', 'please delete gone9@example.test', 1), (?, 'admin', 'Done.', 2)", [tk.insertId, tk.insertId]); let fails = 0; @@ -60,11 +60,18 @@ const E = 'gone9@example.test', O = 'other9@example.test'; assert.strictEqual(await one('SELECT COUNT(*) n FROM accounts WHERE email=?', [E]), 0); assert.strictEqual(await one('SELECT COUNT(*) n FROM accounts WHERE email=?', [O]), 1); assert.strictEqual(await one('SELECT COUNT(*) n FROM campaigns WHERE id=?', [cid]), 0); assert.strictEqual(await one('SELECT COUNT(*) n FROM visit_seen WHERE campaign_id=?', [cid]), 0); assert.strictEqual(await one('SELECT COUNT(*) n FROM earned_credits WHERE email=?', [O]), 1); assert.strictEqual(await one("SELECT COUNT(*) n FROM event_marks WHERE k='keep:other'"), 1); - const msgs = await q('SELECT from_email, to_email, body FROM sponsor_messages ORDER BY sent'); - assert.deepStrictEqual(msgs.map(m => [m.from_email, m.body]), [[O, 'my words']]); assert.ok(/^erased-[0-9a-f]{12}@erased\.invalid$/.test(msgs[0].to_email)); + const msgs = await q('SELECT from_email, to_email, subject, body FROM sponsor_messages ORDER BY sent'); + assert.deepStrictEqual(msgs.map(m => [m.from_email, m.body]), [[O, 'my words'], ['admin@example.test', '

[erased member] needs a nudge; goneuser99 is someone else

']]); assert.ok(/^erased-[0-9a-f]{12}@erased\.invalid$/.test(msgs[0].to_email)); + assert.strictEqual(msgs[1].subject, '[erased member] has not switched on payouts', 'a notice to someone else about them keeps its text, not their name'); assert.strictEqual(r.sql['sponsor_messages (others, name removed)'], 1); const [tkt] = await q("SELECT email, name, username FROM tickets WHERE ref='LS-9001'"); assert.ok(/@erased\.invalid$/.test(tkt.email)); assert.strictEqual(tkt.name, null); const tm = await q('SELECT author, body FROM ticket_messages ORDER BY id'); assert.ok(/removed/.test(tm[0].body)); assert.strictEqual(tm[1].body, 'Done.'); assert.deepStrictEqual(JSON.parse((await q('SELECT chat_mutes FROM accounts WHERE email=?', [O]))[0].chat_mutes), ['x@example.test']); }); + await t('a second pass with names cleans a username left in a kept message after the account is gone', async () => { + await q("INSERT INTO sponsor_messages (from_member, from_email, to_email, subject, body, sent, kind) VALUES (0, ?, ?, '', 'Hey goneuser9, welcome', 9, 'chat')", [O, 'x@example.test']); + const r = await erase.erase(E, { dryRun: false, names: ['goneuser9'] }); + assert.strictEqual(r.found, false); assert.strictEqual(r.sql['sponsor_messages (others, name removed)'], 1); assert.deepStrictEqual(r.residue, []); + assert.strictEqual((await q('SELECT body FROM sponsor_messages WHERE sent=9'))[0].body, 'Hey [erased member], welcome'); + }); console.log(fails ? fails + ' FAILED' : 'erase mysql: all passed'); process.exit(fails ? 1 : 0); })().catch(e => { console.error(e); process.exit(1); }); diff --git a/server.js b/server.js index 05d629f..4773239 100644 --- a/server.js +++ b/server.js @@ -3357,10 +3357,10 @@ const server = http.createServer(async (req, res) => { spamscan.setAllow(e, !b.remove); console.log('spamscan allowlist', b.remove ? 'removed' : 'added', e); return json(res, 200, spamscan.view()); } - // member erase (2026-11-15, ticket LS-1021): {email, dryRun (default true; only dryRun: false erases), force}. erase.js + // member erase (2026-11-15, ticket LS-1021): {email, dryRun (default true; only dryRun: false erases), force, names}. erase.js if (p === '/api/admin/erase' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); - const b = await readBody(req); const r = await erase.erase(String(b.email || ''), { dryRun: b.dryRun !== false, force: b.force === true, by: 'admin' }); + const b = await readBody(req); const r = await erase.erase(String(b.email || ''), { dryRun: b.dryRun !== false, force: b.force === true, by: 'admin', names: b.names }); return json(res, r.error ? 400 : 200, r); } if (p === '/api/admin/erase' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, erase.view()); }