Skip to content
Merged
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
1 change: 1 addition & 0 deletions docs/signup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 7 additions & 0 deletions src/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions test/signup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
19 changes: 18 additions & 1 deletion test/subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
Expand Down
Loading