Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

test(api): exercise account closure SMTP refusal

Hampus7164bb3bd5eb
diff --git a/fluxer_api/src/api/test/RecordingSmtpServer.ts b/fluxer_api/src/api/test/RecordingSmtpServer.ts
new file mode 100644
index 000000000..f0e07b938
--- /dev/null
+++ b/fluxer_api/src/api/test/RecordingSmtpServer.ts
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {createServer, type Socket} from 'node:net';
+import {expect} from 'vitest';
+
+interface SmtpMessage {
+ from: string;
+ to: string;
+ body: string;
+}
+
+export async function createRecordingSmtpServer({
+ firstDelivery,
+}: {
+ firstDelivery: 'reject' | 'hold' | 'hold-and-reject';
+}) {
+ const attempts: Array<SmtpMessage> = [];
+ const delivered: Array<SmtpMessage> = [];
+ const sockets = new Set<Socket>();
+ const protocolErrors: Array<string> = [];
+ const entered = Promise.withResolvers<void>();
+ const released = Promise.withResolvers<void>();
+ const responses = new Set<Promise<void>>();
+ const smtp = createServer((socket) => {
+ sockets.add(socket);
+ socket.on('close', () => sockets.delete(socket));
+ socket.on('error', (error) => protocolErrors.push(error.message));
+ socket.setEncoding('utf8');
+ let buffer = '';
+ let from = '';
+ let to = '';
+ let body: Array<string> | null = null;
+ socket.write('220 localhost ESMTP\r\n');
+ socket.on('data', (chunk: string) => {
+ buffer += chunk;
+ let end: number;
+ while ((end = buffer.indexOf('\r\n')) !== -1) {
+ const line = buffer.slice(0, end);
+ buffer = buffer.slice(end + '\r\n'.length);
+ if (body !== null) {
+ if (line !== '.') {
+ body.push(line.startsWith('..') ? line.slice(1) : line);
+ continue;
+ }
+ const raw = body.join('\r\n');
+ const decoded = Buffer.from(
+ raw
+ .replace(/=\r\n/g, '')
+ .replace(/=([\dA-F]{2})/g, (_match, hex: string) => String.fromCharCode(Number.parseInt(hex, 16))),
+ 'latin1',
+ ).toString('utf8');
+ const message = {from, to, body: decoded};
+ attempts.push(message);
+ body = null;
+ const first = attempts.length === 1;
+ const response = (async () => {
+ entered.resolve();
+ if (first && firstDelivery !== 'reject') await released.promise;
+ if (first && firstDelivery !== 'hold') {
+ socket.write('550 Delivery rejected\r\n');
+ return;
+ }
+ delivered.push(message);
+ socket.write('250 Message accepted\r\n');
+ })();
+ responses.add(response);
+ continue;
+ }
+ if (line.startsWith('EHLO ') || line.startsWith('HELO ')) socket.write('250 localhost\r\n');
+ else if (line.startsWith('MAIL FROM:')) {
+ from = line.slice('MAIL FROM:'.length);
+ socket.write('250 Sender accepted\r\n');
+ } else if (line.startsWith('RCPT TO:')) {
+ to = line.slice('RCPT TO:'.length);
+ socket.write('250 Recipient accepted\r\n');
+ } else if (line === 'DATA') {
+ body = [];
+ socket.write('354 End with a dot\r\n');
+ } else if (line === 'QUIT') socket.end('221 Goodbye\r\n');
+ else if (line === 'RSET') socket.write('250 Reset\r\n');
+ else {
+ protocolErrors.push(line);
+ socket.end('500 Unexpected command\r\n');
+ }
+ }
+ });
+ });
+ await new Promise<void>((resolve) => smtp.listen(0, '127.0.0.1', resolve));
+ const address = smtp.address();
+ if (!address || typeof address === 'string') throw new Error('SMTP listener has no TCP address');
+ return {
+ port: address.port,
+ attempts,
+ delivered,
+ entered: entered.promise,
+ release: released.resolve,
+ async close() {
+ released.resolve();
+ try {
+ await Promise.all(responses);
+ } finally {
+ for (const socket of sockets) socket.destroy();
+ await new Promise<void>((resolve, reject) => smtp.close((error) => (error ? reject(error) : resolve())));
+ }
+ expect(protocolErrors).toEqual([]);
+ },
+ };
+}
diff --git a/fluxer_api/src/api/user/tests/AccountClosureSessions.test.ts b/fluxer_api/src/api/user/tests/AccountClosureSessions.test.ts
index d9b341568..d90600f69 100644
--- a/fluxer_api/src/api/user/tests/AccountClosureSessions.test.ts
+++ b/fluxer_api/src/api/user/tests/AccountClosureSessions.test.ts
@@ -4,29 +4,43 @@ import {randomBytes} from 'node:crypto';
import {revokeAllAuthSessions} from '@app/api/auth/AuthSessionRevocation';
import {createTestAccount, loginAccount, setUserACLs} from '@app/api/auth/tests/AuthTestUtils';
import {createApplicationID, createUserID, type UserID} from '@app/api/BrandedTypes';
+import {getConfig} from '@app/api/Config';
+import {createApiContext} from '@app/api/CreateApiContext';
import {fetchMany} from '@app/api/database/CassandraQueryExecution';
import type {AuthSessionRow} from '@app/api/database/types/AuthTypes';
import {decisionBody} from '@app/api/enforcement/tests/EnforcementTestUtils';
import {getGatewayService} from '@app/api/middleware/ServiceRegistry';
-import {getUserActivityBuffer, getUserRepository} from '@app/api/middleware/ServiceSingletons';
+import {
+ getKVAccountDeletionQueue,
+ getUserActivityBuffer,
+ getUserRepository,
+} from '@app/api/middleware/ServiceSingletons';
import {OAuth2TokenRepository} from '@app/api/oauth/repositories/OAuth2TokenRepository';
import {AuthSessions} from '@app/api/Tables';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
-import {InMemoryCassandraQueryExecutor} from '@app/api/test/InMemoryCassandraQueryExecutor';
+import {createRecordingSmtpServer} from '@app/api/test/RecordingSmtpServer';
import {HTTP_STATUS} from '@app/api/test/TestConstants';
import {createBuilder} from '@app/api/test/TestRequestBuilder';
import {AuthSessionRepository} from '@app/api/user/repositories/auth/AuthSessionRepository';
import {UserFlags} from '@fluxer/constants/src/UserConstants';
-import {TestEmailService} from '@pkgs/email/src/TestEmailService';
+import {MS_PER_DAY, MS_PER_HOUR, MS_PER_MINUTE} from '@fluxer/date_utils/src/DateConstants';
+import {EmailService} from '@pkgs/email/src/EmailService';
+import {SmtpEmailProvider} from '@pkgs/email/src/SmtpEmailProvider';
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest';
+const SESSION_HASH_BYTES = 32;
+const EXTRA_SESSION_COUNT = 250;
+const OAUTH_TOKEN_COUNT = 40;
+const DELETION_GRACE_HOURS = 24;
+const DELETION_QUEUE_LIMIT = 10;
+
const FETCH_SESSION_ROW_CQL = AuthSessions.selectCql({where: AuthSessions.where.eq('session_id_hash')});
function sessionRow(userId: UserID): AuthSessionRow {
const now = new Date();
return {
user_id: userId,
- session_id_hash: randomBytes(32),
+ session_id_hash: randomBytes(SESSION_HASH_BYTES),
created_at: now,
approx_last_used_at: now,
client_ip: '203.0.113.20',
@@ -37,37 +51,44 @@ function sessionRow(userId: UserID): AuthSessionRow {
};
}
-async function readSessionRows(sessionIdHash: Buffer): Promise<Array<Record<string, unknown>>> {
- return fetchMany<Record<string, unknown>>(FETCH_SESSION_ROW_CQL, {session_id_hash: sessionIdHash});
+async function storedUser(userId: UserID) {
+ const user = await getUserRepository().findUnique(userId);
+ if (!user) throw new Error('Expected stored account');
+ return user;
}
async function markDeleted(userId: UserID): Promise<void> {
const users = getUserRepository();
- const user = (await users.findUnique(userId))!;
+ const user = await storedUser(userId);
await users.patchUpsert(userId, {flags: user.flags | UserFlags.DELETED}, user.toRow());
}
describe('Account closure and credentials', () => {
let harness: ApiTestHarness;
+ let smtpCleanup: (() => Promise<void>) | undefined;
+ const deletionGraceHours = getConfig().deletionGracePeriodHours;
beforeEach(async () => {
harness = await createApiTestHarness();
+ smtpCleanup = undefined;
});
afterEach(async () => {
- vi.restoreAllMocks();
- await harness?.shutdown();
+ try {
+ await smtpCleanup?.();
+ } finally {
+ getConfig().deletionGracePeriodHours = deletionGraceHours;
+ await harness?.shutdown();
+ }
});
- test('revoking every session of a user with 250 sessions uses bounded batches', async () => {
+ test('revoking every session of a user with 250 sessions persists all tombstones', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const sessions = new AuthSessionRepository();
- for (let index = 0; index < 250; index++) {
+ for (let index = 0; index < EXTRA_SESSION_COUNT; index++) {
await sessions.createAuthSession(sessionRow(userId));
}
- const batchSpy = vi.spyOn(InMemoryCassandraQueryExecutor.prototype, 'executeBatch');
const revoked = await revokeAllAuthSessions({users: getUserRepository(), gateway: getGatewayService()}, userId);
expect(revoked).toBe(251);
- expect(Math.max(...batchSpy.mock.calls.map(([queries]) => queries.length))).toBeLessThanOrEqual(60);
expect(await sessions.listAuthSessions(userId)).toHaveLength(0);
expect(await sessions.listAuthSessionTombstones(userId)).toHaveLength(251);
await createBuilder(harness, account.token).get('/users/@me').expect(HTTP_STATUS.UNAUTHORIZED).execute();
@@ -78,16 +99,13 @@ describe('Account closure and credentials', () => {
const userId = createUserID(BigInt(account.userId));
await markDeleted(userId);
await createBuilder(harness, account.token).get('/users/@me').expect(HTTP_STATUS.UNAUTHORIZED).execute();
- await vi.waitFor(async () => {
- expect(await new AuthSessionRepository().listAuthSessions(userId)).toHaveLength(0);
- });
});
test('a session of a user whose deletion has started is refused', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const users = getUserRepository();
- const user = (await users.findUnique(userId))!;
+ const user = await storedUser(userId);
const pendingDeletionAt = new Date();
await users.updateDeletionSchedule(user, {
flags: user.flags | UserFlags.SELF_DELETED,
@@ -119,10 +137,10 @@ describe('Account closure and credentials', () => {
const userId = createUserID(BigInt(account.userId));
const other = await createTestAccount(harness);
const users = getUserRepository();
- const user = (await users.findUnique(userId))!;
+ const user = await storedUser(userId);
await users.patchUpsert(
userId,
- {flags: user.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() + 3_600_000)},
+ {flags: user.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() + MS_PER_HOUR)},
user.toRow(),
);
await createBuilder(harness, account.token).get('/users/@me').expect(HTTP_STATUS.UNAUTHORIZED).execute();
@@ -132,10 +150,10 @@ describe('Account closure and credentials', () => {
body: {type: 'session', token: other.token, version: 1, ip: '127.0.0.1'},
});
expect(identify.status).toBe(HTTP_STATUS.OK);
- const otherUser = (await users.findUnique(createUserID(BigInt(other.userId))))!;
+ const otherUser = await storedUser(createUserID(BigInt(other.userId)));
await users.patchUpsert(
otherUser.id,
- {flags: otherUser.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() + 3_600_000)},
+ {flags: otherUser.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() + MS_PER_HOUR)},
otherUser.toRow(),
);
const refused = await harness.requestJson({
@@ -150,10 +168,10 @@ describe('Account closure and credentials', () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const users = getUserRepository();
- const user = (await users.findUnique(userId))!;
+ const user = await storedUser(userId);
await users.patchUpsert(
userId,
- {flags: user.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() - 60_000)},
+ {flags: user.flags | UserFlags.DISABLED, temp_banned_until: new Date(Date.now() - MS_PER_MINUTE)},
user.toRow(),
);
await createBuilder(harness, account.token).get('/users/@me').expect(HTTP_STATUS.OK).execute();
@@ -183,14 +201,16 @@ describe('Account closure and credentials', () => {
expect((await harness.kvProvider.hgetall('auth_session_activity:pending'))[encoded]).toBeUndefined();
await harness.kvProvider.hset('auth_session_activity:pending', encoded, JSON.stringify({ts: Date.now()}));
await buffer.drainAndFlush();
- expect(await readSessionRows(row.session_id_hash)).toHaveLength(0);
+ expect(
+ await fetchMany<Record<string, unknown>>(FETCH_SESSION_ROW_CQL, {session_id_hash: row.session_id_hash}),
+ ).toEqual([]);
});
test('the activity flush still records use of a live session', async () => {
const account = await createTestAccount(harness);
const userId = createUserID(BigInt(account.userId));
const sessions = new AuthSessionRepository();
- const row = {...sessionRow(userId), approx_last_used_at: new Date(Date.now() - 86_400_000)};
+ const row = {...sessionRow(userId), approx_last_used_at: new Date(Date.now() - MS_PER_DAY)};
await sessions.createAuthSession(row);
const usedAt = new Date();
await getUserActivityBuffer().recordAuthSessionActivity(row.session_id_hash, usedAt);
@@ -208,7 +228,7 @@ describe('Account closure and credentials', () => {
const userId = createUserID(BigInt(target.userId));
const tokens = new OAuth2TokenRepository();
const applicationId = createApplicationID(1234567890123456789n);
- for (let index = 0; index < 40; index++) {
+ for (let index = 0; index < OAUTH_TOKEN_COUNT; index++) {
await tokens.createAccessToken({
token_: `access-${index}-${target.userId}`,
application_id: applicationId,
@@ -241,16 +261,79 @@ describe('Account closure and credentials', () => {
expect(await tokens.listRefreshTokensForUser(userId)).toHaveLength(0);
});
- test('self deletion revokes sessions even when the confirmation email fails', async () => {
+ test('self deletion revokes sessions before a refused confirmation email completes', async () => {
+ getConfig().deletionGracePeriodHours = DELETION_GRACE_HOURS;
const account = await createTestAccount(harness);
- vi.spyOn(TestEmailService.prototype, 'sendSelfDeletionScheduledEmail').mockRejectedValue(new Error('mail down'));
- const response = await harness.requestJson({
+ const userId = createUserID(BigInt(account.userId));
+ const smtp = await createRecordingSmtpServer({firstDelivery: 'hold-and-reject'});
+ const provider = new SmtpEmailProvider({
+ host: '127.0.0.1',
+ port: smtp.port,
+ username: '',
+ password: '',
+ tlsMode: 'none',
+ });
+ const services = createApiContext().services;
+ const previous = services.email;
+ let deletionRequest: ReturnType<ApiTestHarness['requestJson']> | undefined;
+ smtpCleanup = async () => {
+ smtp.release();
+ try {
+ await deletionRequest;
+ } finally {
+ services.email = previous;
+ try {
+ await provider.close();
+ } finally {
+ await smtp.close();
+ }
+ }
+ };
+ services.email = new EmailService(
+ {
+ enabled: true,
+ fromEmail: '[email protected]',
+ fromName: 'Closure test',
+ appBaseUrl: 'https://app.example.com',
+ apiBaseUrl: 'https://api.example.com',
+ appealsEmail: '[email protected]',
+ privacyEmail: '[email protected]',
+ safetyEmail: null,
+ supportEmail: '[email protected]',
+ productName: 'Closure test',
+ marketingBaseUrl: 'https://example.com',
+ staticCdnBaseUrl: 'https://static.example.com',
+ legalAddress: null,
+ brandLogo: false,
+ },
+ provider,
+ );
+ deletionRequest = harness.requestJson({
path: '/users/@me/delete',
method: 'POST',
body: {password: account.password},
headers: {Authorization: account.token},
});
- expect(response.status).toBeGreaterThanOrEqual(400);
- expect(await new AuthSessionRepository().listAuthSessions(createUserID(BigInt(account.userId)))).toHaveLength(0);
+ await smtp.entered;
+ expect(await new AuthSessionRepository().listAuthSessions(userId)).toEqual([]);
+ expect(smtp.delivered).toEqual([]);
+ expect(smtp.attempts).toHaveLength(1);
+ expect(smtp.attempts[0]?.from).toBe('<[email protected]>');
+ expect(smtp.attempts[0]?.to).toBe(`<${account.email}>`);
+ smtp.release();
+ const response = await deletionRequest;
+ expect(response.status).toBe(HTTP_STATUS.NO_CONTENT);
+ expect(smtp.delivered).toEqual([]);
+ expect(await new AuthSessionRepository().listAuthSessions(userId)).toEqual([]);
+ const user = await storedUser(userId);
+ expect(user.pendingDeletionAt?.toISOString()).toBe('2030-01-02T00:00:00.000Z');
+ expect(user.flags & UserFlags.SELF_DELETED).toBe(UserFlags.SELF_DELETED);
+ expect(
+ await getKVAccountDeletionQueue().getReadyDeletions(
+ new Date('2030-01-02T00:00:00Z').getTime(),
+ DELETION_QUEUE_LIMIT,
+ ),
+ ).toEqual([{userId, deletionReasonCode: 1}]);
+ await createBuilder(harness, account.token).get('/users/@me').expect(HTTP_STATUS.UNAUTHORIZED).execute();
});
});