ReviewOS

also looking at this

mail-os/mail

feat: mailbox recovery addresses and password reset by email

#13
Merged chris wants to merge feat/mailbox-recovery into main
21 files +1503 -10
packages/zig/src/storage/database.zigmodified+55-2
Changes to packages/zig/src/storage/database.zig
@@ -352,6 +352,37 @@ pub const Database = struct {
352352 // not be bundled with the quota migration above).
353353 self.exec("ALTER TABLE users ADD COLUMN digest_ha1 TEXT;") catch {};
354354
355 // Migration: a mailbox's recovery address, where a password reset link
356 // is sent. `recovery_source` records who set it ('operator' for the CLI
357 // and deploy provisioning, 'user' for the mailbox owner in webmail), so
358 // provisioning can leave an owner's own choice alone. One statement
359 // each: sqlite3_exec stops at the first error, and an existing column
360 // is that error.
361 self.exec("ALTER TABLE users ADD COLUMN recovery_email TEXT;") catch {};
362 self.exec("ALTER TABLE users ADD COLUMN recovery_source TEXT;") catch {};
363 self.exec("ALTER TABLE users ADD COLUMN recovery_updated_at INTEGER;") catch {};
364
365 // Single-use password reset tokens (see src/api/webmail_recovery.zig).
366 // Only the SHA-256 of a token is stored. Rows outlive their use for an
367 // hour so the per-mailbox send limit can count them.
368 //
369 // No foreign key to users(username), deliberately: a username is
370 // rewritten by `domain migrate` and by deploy-time username migrations,
371 // and a referencing row would make that UPDATE fail. deleteUser and
372 // renameDomain remove a mailbox's tokens explicitly instead.
373 try self.exec(
374 \\CREATE TABLE IF NOT EXISTS mailbox_reset_tokens (
375 \\ token_hash TEXT PRIMARY KEY,
376 \\ username TEXT NOT NULL,
377 \\ recovery_email TEXT NOT NULL,
378 \\ created_at INTEGER NOT NULL,
379 \\ expires_at INTEGER NOT NULL,
380 \\ used_at INTEGER,
381 \\ ip_address TEXT
382 \\);
383 \\CREATE INDEX IF NOT EXISTS idx_mailbox_reset_tokens_user ON mailbox_reset_tokens(username, created_at);
384 );
385
355386 // IMAP UID persistence tables
356387 try self.exec(
357388 \\CREATE TABLE IF NOT EXISTS imap_mailboxes (
@@ -621,6 +652,7 @@ pub const Database = struct {
621652 defer self.mutex.unlock();
622653
623654 try self.deleteUserSessionsLocked(username);
655 try self.deleteUserResetTokensLocked(username);
624656
625657 const sql = "DELETE FROM users WHERE username = ?1";
626658
@@ -841,6 +873,23 @@ pub const Database = struct {
841873 return self.deleteUserSessionsLocked(username);
842874 }
843875
876 /// Void every password reset link for a mailbox. Called when its password
877 /// or recovery address changes, so an older link cannot outlive either.
878 pub fn deleteUserResetTokens(self: *Database, username: []const u8) !void {
879 self.mutex.lock();
880 defer self.mutex.unlock();
881 return self.deleteUserResetTokensLocked(username);
882 }
883
884 fn deleteUserResetTokensLocked(self: *Database, username: []const u8) !void {
885 var raw: ?*sqlite.sqlite3_stmt = null;
886 if (sqlite.sqlite3_prepare_v2(self.db, "DELETE FROM mailbox_reset_tokens WHERE username = ?1", -1, &raw, null) != sqlite.SQLITE_OK) return DatabaseError.PrepareFailed;
887 const stmt = Statement{ .stmt = raw.?, .allocator = self.allocator };
888 defer stmt.finalize();
889 try stmt.bind(1, username);
890 _ = try stmt.step();
891 }
892
844893 fn deleteUserSessionsLocked(self: *Database, username: []const u8) !void {
845894 var raw: ?*sqlite.sqlite3_stmt = null;
846895 if (sqlite.sqlite3_prepare_v2(self.db, "DELETE FROM webmail_sessions WHERE username = ?1", -1, &raw, null) != sqlite.SQLITE_OK) return DatabaseError.PrepareFailed;
@@ -1639,8 +1688,12 @@ pub const Database = struct {
16391688 if (i == 0) moved = @intCast(sqlite.sqlite3_changes(self.db));
16401689 }
16411690
1642 {
1643 const sql: [:0]const u8 = "DELETE FROM webmail_sessions WHERE username LIKE ?1 ESCAPE '\\'";
1691 // Sessions and reset links carry the old address, so they are voided
1692 // rather than rewritten. Both are cheap to obtain again.
1693 for ([_][:0]const u8{
1694 "DELETE FROM webmail_sessions WHERE username LIKE ?1 ESCAPE '\\'",
1695 "DELETE FROM mailbox_reset_tokens WHERE username LIKE ?1 ESCAPE '\\'",
1696 }) |sql| {
16441697 var stmt: ?*sqlite.sqlite3_stmt = null;
16451698 if (sqlite.sqlite3_prepare_v2(self.db, sql.ptr, -1, &stmt, null) != sqlite.SQLITE_OK)
16461699 return DatabaseError.PrepareFailed;