Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/signup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 7 additions & 5 deletions src/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions test/signup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function unsubscribe(body: Record<string, string>, 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: '[email protected]', list: 'the-source' };
await handle(post(signup, { origin: null, key }), e);
Expand All @@ -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: '[email protected]' }, { key }), e)).json(), { ok: true });
});
Expand Down
28 changes: 17 additions & 11 deletions test/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading