From f9ee1171fb56f5af8a9bb91d660d55173eb9b0e9 Mon Sep 17 00:00:00 2001 From: wachtelhund Date: Wed, 18 Nov 2026 12:57:00 +0100 Subject: [PATCH] Let a new sign-up put an address back after it left that list. POST /subscribe already answered ok when a list-scoped unsubscribe suppression dropped the sign-up, so status stayed false. Any form now lifts that list's unsubscribe and signs the address up again. Bounce, complaint, and Mailchimp-cleaned are unchanged. --- docs/signup.md | 2 +- src/contacts.ts | 12 +++++++----- test/signup.test.ts | 7 ++++--- test/subscribe.test.ts | 28 +++++++++++++++++----------- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/docs/signup.md b/docs/signup.md index c370fa6..ac3fff5 100644 --- a/docs/signup.md +++ b/docs/signup.md @@ -14,7 +14,7 @@ Pick the first that fits. A browser form is the weakest: browsers send a true `O ## What a sign-up does - The address goes on the list with the consent id, the time and the form's name as its source (`subscriptions.source`). -- An address that is suppressed, for this list (someone who left) or for every list (a hard bounce, a spam complaint, or cleaned in Mailchimp), stays out; the sign-up is dropped. Without double opt-in a form cannot tell the person from someone typing their address, so someone who left and wants back writes to you until double opt-in exists. +- A new sign-up for a list lifts a list-scoped `unsubscribe` for that list, from any form, and puts the address back. An address suppressed for every list (a hard bounce, a spam complaint, or cleaned in Mailchimp) stays out; that sign-up is dropped. Those suppressions, and any suppression on every list (`*`), are never lifted. - The answer is the same in every case, so a form cannot be used to find out who is on a list. - A known reader keeps their first name and country, and an existing subscription keeps its consent id, time and source. Without double opt-in, anyone can type someone else's address into a form, and must not be able to change what that person agreed to. diff --git a/src/contacts.ts b/src/contacts.ts index 6a24969..3956e63 100644 --- a/src/contacts.ts +++ b/src/contacts.ts @@ -120,11 +120,11 @@ export function firstName(name: unknown): string | null { return n.length <= 40 && /^\p{L}[\p{L}\p{M} '’-]*$/u.test(n) ? n : null; } -// A sign-up someone made on a form. Until sign-ups are confirmed by mail (double opt-in), anyone can -// type anyone's address, so a sign-up only adds: it never lifts a suppression (someone who left and -// wants back writes to support), never changes a known contact, and never rewrites the consent of an -// existing subscription. A name that is not a name is dropped. The answer is the same in every case, -// so the form does not reveal who is on a list. +// A sign-up someone made on a form. A new sign-up for a list lifts a list-scoped unsubscribe for +// that list, so someone who left can join again. It never lifts a bounce, complaint, or +// Mailchimp-cleaned suppression, never changes a known contact, and never rewrites the consent of +// an existing subscription. A name that is not a name is dropped. The answer is the same in every +// case, so the form does not reveal who is on a list. export async function subscribe(env: Env, s: Signup, now = new Date()): Promise<{ ok: true }> { const { list } = s; if (!isList(env.CONFIG, list)) throw new Error('invalid_list'); @@ -138,6 +138,8 @@ export async function subscribe(env: Env, s: Signup, now = new Date()): Promise< // address takes as long as a new one, so the time of the answer tells nothing either. const free = `NOT EXISTS (SELECT 1 FROM suppressions WHERE email_hash = ?2 AND scope IN (?3, '*'))`; await env.DB.batch([ + // Only a list-scoped unsubscribe for this list; bounce, complaint, and Mailchimp-cleaned stay. + env.DB.prepare(`DELETE FROM suppressions WHERE email_hash = ?1 AND scope = ?2 AND reason = 'unsubscribe'`).bind(hash, list), joinedStatement(env.DB, list, email, hash, iso(now).slice(0, 10)), env.DB.prepare(`INSERT INTO contacts (email, email_hash, first_name, country) SELECT ?1, ?2, ?4, ?5 WHERE ${free} ON CONFLICT DO NOTHING`) .bind(email, hash, list, firstName(s.firstName), country), diff --git a/test/signup.test.ts b/test/signup.test.ts index e258249..5c83127 100644 --- a/test/signup.test.ts +++ b/test/signup.test.ts @@ -314,7 +314,7 @@ function unsubscribe(body: Record, o: Options = {}) { return new Request('https://news.test/unsubscribe', { method: o.method ?? 'POST', headers, body: o.method === 'GET' ? undefined : JSON.stringify(body) }); } -test('a server form takes an address off one of its lists with its key, and cannot sign it up again', async () => { +test('a server form takes an address off one of its lists with its key, and a new sign-up puts it back', async () => { const e = env(); const off = { email: 'Reader@Example.com', list: 'the-source' }; await handle(post(signup, { origin: null, key }), e); @@ -333,9 +333,10 @@ test('a server form takes an address off one of its lists with its key, and cann // Off The Source only; still on FTW news. assert.deepEqual((await stored(e)).map(r => r.list), ['ftw-news']); assert.deepEqual(await rows(e, 'SELECT scope, reason FROM suppressions'), [{ scope: 'the-source', reason: 'unsubscribe' }]); - // The same form cannot put the address back: someone who left writes in. + // A new sign-up for the same list puts the address back. assert.equal((await handle(post(signup, { origin: null, key }), e)).status, 200); - assert.deepEqual((await stored(e)).map(r => r.list), ['ftw-news']); + assert.deepEqual((await stored(e)).map(r => r.list), ['ftw-news', 'the-source']); + assert.deepEqual(await rows(e, 'SELECT * FROM suppressions'), []); // An address that was never on the list gets the same answer. assert.deepEqual(await (await handle(unsubscribe({ ...off, email: 'stranger@example.com' }, { key }), e)).json(), { ok: true }); }); diff --git a/test/subscribe.test.ts b/test/subscribe.test.ts index 14b2f16..5b221e8 100644 --- a/test/subscribe.test.ts +++ b/test/subscribe.test.ts @@ -19,32 +19,38 @@ test('a form sign-up lands on its list with its consent, time and source', async ]); }); -// Until sign-ups are confirmed by mail (double opt-in), a form cannot tell the person from someone -// who typed their address, so it may not undo what the person chose. Someone who left and wants back -// writes to support. This replaces the earlier rule that a new sign-up lifted "left this list". -test('signing up again after leaving does not lift the suppression', async () => { +// A new sign-up for a list puts back someone who left that list. It does not lift an unsubscribe +// on a different list, and the answer stays the same so the form reveals nothing. +test('signing up again after leaving puts the address back on that list', async () => { const env = testEnv(); await subscribe(env, signup); const { hash } = (await rows<{ hash: string }>(env, 'SELECT email_hash AS hash FROM contacts'))[0]; await env.DB.prepare(`INSERT INTO suppressions (email_hash, scope, reason) VALUES (?, 'ftw-news', 'unsubscribe')`).bind(hash).run(); await env.DB.prepare('DELETE FROM subscriptions').run(); assert.deepEqual(await subscribe(env, signup), { ok: true }, 'the same answer, so the form reveals nothing'); - assert.deepEqual(await rows(env, 'SELECT scope, reason FROM suppressions'), [{ scope: 'ftw-news', reason: 'unsubscribe' }]); - assert.equal((await rows(env, 'SELECT COUNT(*) AS n FROM subscriptions')).at(0)!.n, 0); - // Leaving one list does not stop a sign-up for another. + assert.deepEqual(await rows(env, 'SELECT list, source FROM subscriptions'), [{ list: 'ftw-news', source: 'ftw-web' }]); + assert.deepEqual(await rows(env, 'SELECT * FROM suppressions'), []); + // Leaving one list does not stop a sign-up for another, and does not lift the other list's unsubscribe. + await env.DB.prepare('DELETE FROM subscriptions').run(); + await env.DB.prepare(`INSERT INTO suppressions (email_hash, scope, reason) VALUES (?, 'ftw-news', 'unsubscribe')`).bind(hash).run(); await subscribe(env, { ...signup, list: 'ftw-box', consent: 'gateway-2026-10-30' }); assert.deepEqual(await rows(env, 'SELECT list FROM subscriptions'), [{ list: 'ftw-box' }]); + assert.deepEqual(await rows(env, 'SELECT scope, reason FROM suppressions'), [{ scope: 'ftw-news', reason: 'unsubscribe' }]); }); -test('a bounced or complaining address is not signed up again', async () => { +test('a bounced, complaining or Mailchimp-cleaned address is not signed up again', async () => { const env = testEnv(); await subscribe(env, signup); const { hash } = (await rows<{ hash: string }>(env, 'SELECT email_hash AS hash FROM contacts'))[0]; await env.DB.prepare('DELETE FROM contacts').run(); await env.DB.prepare('DELETE FROM subscriptions').run(); - await env.DB.prepare(`INSERT INTO suppressions (email_hash, scope, reason) VALUES (?, '*', 'complaint')`).bind(hash).run(); - assert.deepEqual(await subscribe(env, signup), { ok: true }, 'the same answer, so the form reveals nothing'); - assert.deepEqual(await rows(env, 'SELECT * FROM subscriptions'), []); + for (const reason of ['complaint', 'bounce', 'mailchimp-cleaned']) { + await env.DB.prepare('DELETE FROM suppressions').run(); + await env.DB.prepare(`INSERT INTO suppressions (email_hash, scope, reason) VALUES (?, '*', ?)`).bind(hash, reason).run(); + assert.deepEqual(await subscribe(env, signup), { ok: true }, 'the same answer, so the form reveals nothing'); + assert.deepEqual(await rows(env, 'SELECT * FROM subscriptions'), [], reason); + assert.deepEqual(await rows(env, 'SELECT scope, reason FROM suppressions'), [{ scope: '*', reason }]); + } }); test('an unknown list or a bad address is refused', async () => {