halkyon/packages/halkyon_base_server/test/integration/support/mailbox.dart
Tizian 「ティツィアーン」 1d4320370b
fix(auth): a code by mail serves only what it was asked for
Codes by mail were tied to nothing but their address (the re-review's
seventh finding): a hostile instance could answer that a setup never
finished, have the person type a code into it, relay it to start the real
instance's account reset, and 72 hours later relay a second to finish it
with its own password and keys; the mail said "to sign in" whatever the
code was for. A code is now asked for a purpose (signUp, finishSetup,
authenticatorReset, accountReset, deletion), only that purpose takes it (any
other is refused as no longer valid, leaving the code as it was), and its
mail says what it would do ("to reset your account on learn.halkyon.eu: a
new password and new keys, and what was encrypted with the old ones is
lost") and to type it only in the app on that instance, nowhere else. The
app asks for each code with the purpose of its step.

Migration (module, Learn's and Todo's servers, Learn's device schema):
ALTER TABLE "email_code_request" ADD COLUMN "purpose" text; nullable,
so the requests of the last 15 minutes before it stay, taken by nothing
and forgotten within the hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-23 14:12:48 +02:00

73 lines
1.8 KiB
Dart

import 'package:halkyon_base_server/base.dart';
import 'package:serverpod/serverpod.dart';
/// Keeps every code and notice mailed, in place of a mail service.
final class Mailbox implements CodeMailer {
final codes =
<({String to, String code, EmailCodePurpose purpose, String language})>[];
final notices =
<
({
String to,
AccountResetKind kind,
DateTime takesEffectAt,
String language,
})
>[];
/// The notices that codes wait (ADR 0017).
final codesWait = <({String to, DateTime until, String language})>[];
/// Whether it takes no mail, as a mail service down.
bool refusing = false;
@override
Future<void> send(
Session session, {
required String to,
required String code,
required EmailCodePurpose purpose,
required String language,
}) async {
if (refusing) {
throw Exception('Connection refused');
}
codes.add((to: to, code: code, purpose: purpose, language: language));
}
@override
Future<void> sendResetNotice(
Session session, {
required String to,
required AccountResetKind kind,
required DateTime takesEffectAt,
required String language,
}) async {
if (refusing) {
throw Exception('Connection refused');
}
notices.add((
to: to,
kind: kind,
takesEffectAt: takesEffectAt,
language: language,
));
}
@override
Future<void> sendCodesWaitNotice(
Session session, {
required String to,
required DateTime until,
required String language,
}) async {
if (refusing) {
throw Exception('Connection refused');
}
codesWait.add((to: to, until: until, language: language));
}
/// The last code mailed to [address].
String codeFor(String address) =>
codes.lastWhere((mail) => mail.to == address).code;
}