From c7e87af2555ee8d9ab6ae835a165bd454ee69fbd Mon Sep 17 00:00:00 2001 From: Nilsson Date: Wed, 18 Nov 2026 10:44:29 +0100 Subject: [PATCH] Let the Sourceful app sign a user up again after they left the list. POST /subscribe always answered ok, even when a list-scoped unsubscribe suppression dropped the sign-up, so GET /subscribe/status stayed false right after an app opt-in. Website forms still cannot lift that suppression. Bounce, complaint, and Mailchimp-cleaned are unchanged. --- docs/signup.md | 1 + src/contacts.ts | 7 +++++++ test/signup.test.ts | 3 +++ test/subscribe.test.ts | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/signup.md b/docs/signup.md index c370fa6..7b8b849 100644 --- a/docs/signup.md +++ b/docs/signup.md @@ -15,6 +15,7 @@ Pick the first that fits. A browser form is the weakest: browsers send a true `O - 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. +- Exception: the Sourceful app form (`sourceful-app`) signs the owner of the address in first. For that form only, a list-scoped `unsubscribe` is lifted and the address is signed up again. Bounce, complaint, Mailchimp-cleaned, and suppressions 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..eaefe6b 100644 --- a/src/contacts.ts +++ b/src/contacts.ts @@ -137,7 +137,14 @@ export async function subscribe(env: Env, s: Signup, now = new Date()): Promise< // One batch whatever the address, with the suppression checked in each insert: a suppressed // 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, '*'))`; + // The Sourceful app has already signed the owner in, so they may turn this list back on + // after leaving. Website forms must not: they cannot tell the person from someone typing + // their address. Bounce, complaint, and Mailchimp-cleaned stays in force. + const statements = source === 'sourceful-app' + ? [env.DB.prepare(`DELETE FROM suppressions WHERE email_hash = ?1 AND scope = ?2 AND reason = 'unsubscribe'`).bind(hash, list)] + : []; await env.DB.batch([ + ...statements, 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..c587940 100644 --- a/test/signup.test.ts +++ b/test/signup.test.ts @@ -287,6 +287,9 @@ test('Sourceful\'s config: the app\'s accounts go to a list of their own, apart assert.deepEqual((await stored(e)).map(r => [r.list, r.consent, r.source]), [['app-users', 'app-account-2026-11', 'sourceful-app']]); assert.equal((await handle(unsubscribe({ email: body.email, list: 'app-users' }, { key: app }), e)).status, 200); assert.deepEqual(await stored(e), []); + // The app has signed the user in, so they may opt in again after leaving. + assert.equal((await handle(post(body, { origin: null, key: app }), e)).status, 200); + assert.deepEqual((await stored(e)).map(r => [r.list, r.source]), [['app-users', 'sourceful-app']]); }); test('the rate limit counts an IPv6 client per /64 and an IPv4 client per address', async () => { diff --git a/test/subscribe.test.ts b/test/subscribe.test.ts index 14b2f16..22a3e44 100644 --- a/test/subscribe.test.ts +++ b/test/subscribe.test.ts @@ -1,7 +1,7 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { handle } from '../src/app.ts'; -import { firstName, subscribe } from '../src/contacts.ts'; +import { firstName, forget, subscribe } from '../src/contacts.ts'; import { normalizeEmail } from '../src/util.ts'; import { admin, rows, testEnv } from './helpers.ts'; @@ -36,6 +36,23 @@ test('signing up again after leaving does not lift the suppression', async () => assert.deepEqual(await rows(env, 'SELECT list FROM subscriptions'), [{ list: 'ftw-box' }]); }); +test('the Sourceful app can sign the same user up again after they left that list', async () => { + const env = testEnv(); + const appSignup = { ...signup, source: 'sourceful-app' }; + await subscribe(env, appSignup); + await forget(env, appSignup.email, 'ftw-news', 'unsubscribe'); + assert.equal((await rows(env, 'SELECT COUNT(*) AS n FROM subscriptions')).at(0)!.n, 0); + assert.deepEqual(await subscribe(env, appSignup), { ok: true }); + assert.deepEqual(await rows(env, 'SELECT list, source FROM subscriptions'), [{ list: 'ftw-news', source: 'sourceful-app' }]); + assert.deepEqual(await rows(env, 'SELECT * FROM suppressions'), []); + // Bounce and complaint still block the app form. + const { hash } = (await rows<{ hash: string }>(env, 'SELECT email_hash AS hash FROM contacts'))[0]; + await env.DB.prepare('DELETE FROM subscriptions').run(); + await env.DB.prepare(`INSERT INTO suppressions (email_hash, scope, reason) VALUES (?, '*', 'bounce')`).bind(hash).run(); + assert.deepEqual(await subscribe(env, appSignup), { ok: true }); + assert.deepEqual(await rows(env, 'SELECT * FROM subscriptions'), []); +}); + test('a bounced or complaining address is not signed up again', async () => { const env = testEnv(); await subscribe(env, signup);