const std = @import("std"); const clock = @import("../core/clock.zig"); const app_config = @import("../core/config.zig"); const log = @import("../core/log.zig"); const auth = @import("../auth/tokens.zig"); const blobstore = @import("blobstore.zig"); const eventlog = @import("eventlog.zig"); const request_context = @import("../internal/request_context.zig"); const sharded_locks = @import("../internal/sharded_locks.zig"); const space_policy = @import("../internal/space_policy.zig"); const zat = @import("zat"); const zqlite = @import("zqlite"); const Io = std.Io; pub const Error = error{ InvalidCollection, InvalidCursor, InvalidRecordKey, InvalidRecordType, RecordTooLarge, TooManyWrites, CommitTooLarge, BlobNotFound, InvalidSwap, RecordAlreadyExists, ValidationRequired, InvalidReservedSigningKey, InvalidKemKey, MissingRecord, RecordNotFound, CommitTreeMismatch, MissingReservedSigningKey, MissingRecordBlock, RepoNotFound, InvalidRepoPath, InvalidDagCbor, InvalidRefreshSession, InvalidAccountStatus, AccountNotFound, HandleNotAvailable, RateLimitExceeded, StoreNotInitialized, }; pub const AccountStatus = enum { active, takendown, suspended, deactivated, deleted, pub fn asString(self: AccountStatus) []const u8 { return switch (self) { .active => "active", .takendown => "takendown", .suspended => "suspended", .deactivated => "deactivated", .deleted => "deleted", }; } pub fn parse(raw: []const u8) AccountStatus { if (std.mem.eql(u8, raw, "takendown")) return .takendown; if (std.mem.eql(u8, raw, "suspended")) return .suspended; if (std.mem.eql(u8, raw, "deactivated")) return .deactivated; if (std.mem.eql(u8, raw, "deleted")) return .deleted; return .active; } pub fn isActive(self: AccountStatus) bool { return self == .active; } }; pub const Record = struct { did: []const u8, collection: []const u8, rkey: []const u8, cid: []const u8, value_json: []const u8, validation_status: []const u8, rev: []const u8, seq: u64, pub fn uri(self: Record, allocator: std.mem.Allocator) ![]const u8 { return std.fmt.allocPrint(allocator, "at://{s}/{s}/{s}", .{ self.did, self.collection, self.rkey }); } }; pub const BlobRecord = struct { mime_type: []const u8, data: []const u8, }; pub const SeqEvent = struct { seq: u64, frame: []const u8, }; pub const EmailInfo = struct { email: []const u8, email_confirmed: bool, pending_email: ?[]const u8, }; pub const CodeStatus = enum { valid, invalid, expired, }; pub const OAuthRequest = struct { request_id: []const u8, client_id: []const u8, redirect_uri: []const u8, scope: []const u8, state: []const u8, code_challenge: []const u8, code_challenge_method: []const u8, response_mode: []const u8, login_hint: ?[]const u8, dpop_jkt: ?[]const u8, expires_at: i64, sub: ?[]const u8, code: ?[]const u8, auth_method: ?[]const u8, prompt: ?[]const u8 = null, /// Digest of the browser sign-in cookie that authenticated this request, /// with the account and method, while the person reviews consent. signin_hash: ?[]const u8 = null, signin_did: ?[]const u8 = null, signin_method: ?[]const u8 = null, }; pub const OAuthToken = struct { pub const Kind = enum { access, refresh, previous_refresh, used_refresh }; family_id: []const u8, did: []const u8, client_id: []const u8, scope: []const u8, access_token: []const u8, refresh_token: []const u8, access_expires_at: i64, refresh_expires_at: i64, previous_refresh_token: ?[]const u8, previous_refresh_expires_at: ?i64, revoked: bool, dpop_jkt: ?[]const u8, auth_method: ?[]const u8, /// when the family's absolute session lifetime started counting; families /// issued before this was tracked start from the migration session_started_at: i64, kind: Kind, }; pub const SessionInfo = struct { id: []const u8, did: []const u8, handle: []const u8, auth_method: []const u8, app_password_name: ?[]const u8, controller_did: ?[]const u8, created_at: i64, updated_at: i64, last_used_at: ?i64, access_expires_at: i64, refresh_expires_at: i64, revoked_at: ?i64, active: bool, }; pub const OAuthGrantInfo = struct { did: []const u8, handle: []const u8, client_id: []const u8, scope: []const u8, created_at: i64, expires_at: i64, revoked_at: ?i64, active: bool, auth_method: ?[]const u8, }; pub const AppPassword = struct { name: []const u8, password_hash: []const u8, created_at: i64, privileged: bool, scopes: ?[]const u8, created_by_controller_did: ?[]const u8, }; pub const Passkey = struct { id: []const u8, did: []const u8, credential_id: []const u8, public_key: []const u8, sign_count: u32, friendly_name: ?[]const u8, created_at: i64, last_used: ?i64, }; pub const WebAuthnChallenge = struct { id: []const u8, did: []const u8, challenge: []const u8, kind: []const u8, state_json: []const u8, expires_at: i64, }; pub const DiscoverableWebAuthnChallenge = struct { request_id: []const u8, challenge: []const u8, expires_at: i64, }; pub const InviteCode = struct { code: []const u8, available: i64, disabled: bool, for_account: []const u8, created_by: []const u8, created_at: i64, uses: []InviteCodeUse, }; pub const ReservedSigningKey = struct { did: ?[]const u8, signing_key: []const u8, secret_key: [32]u8, expires_at: i64, }; pub const InviteCodeUse = struct { used_by: []const u8, used_at: i64, }; pub const AppPreference = struct { name: []const u8, value_json: []const u8, }; pub const Resident = struct { handle: []const u8, did: []const u8, active: bool, rev: []const u8, record_count: u64, }; pub const CollectionSummary = struct { collection: []const u8, count: u64, }; pub const ActivityBucket = struct { start_us: u64, end_us: u64, count: u64, }; pub const RecentRecord = struct { handle: []const u8, did: []const u8, collection: []const u8, rkey: []const u8, cid: []const u8, seq: u64, }; pub const ImportedRecord = struct { collection: []const u8, rkey: []const u8, cid: []const u8, blob_cids: []const []const u8, }; pub const ImportedBlock = struct { cid: []const u8, data: []const u8, }; pub const CommitInfo = struct { cid: []const u8, rev: []const u8, }; pub const WriteResult = struct { commit: CommitInfo, records: []Record, }; pub const SpaceConfig = struct { uri: []const u8, authority_did: []const u8, space_type: []const u8, skey: []const u8, /// Normalized policy JSON; null where this host only keeps a writer's repo. policy_json: ?[]const u8, is_authority: bool, deleted_at: ?i64, pub fn policy(self: SpaceConfig, allocator: std.mem.Allocator) !?space_policy.Policy { return space_policy.parseStored(allocator, self.policy_json orelse return null) catch |err| switch (err) { error.OutOfMemory => error.OutOfMemory, else => Error.InvalidRecordType, }; } }; pub const OplogCursor = struct { rev: []const u8, idx: i64, }; pub const SpaceMember = struct { did: []const u8, created_at: i64, read: bool, write: bool, }; pub const SpaceRecord = struct { space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, cid: []const u8, value_json: []const u8, validation_status: []const u8, repo_rev: []const u8, updated_at: i64, pub fn uri(self: SpaceRecord, allocator: std.mem.Allocator) ![]const u8 { return zat.space.SpaceUri.formatRecord(allocator, self.space, self.repo_did, self.collection, self.rkey); } }; pub const SpaceRecordRef = struct { collection: []const u8, rkey: []const u8, cid: []const u8, value_json: ?[]const u8 = null, }; pub const SpaceRecordOplogEntry = struct { rev: []const u8, idx: i64, action: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, cid: ?[]const u8, prev: ?[]const u8, value_json: ?[]const u8, }; pub const SpaceState = struct { set_hash: ?[]const u8, rev: ?[]const u8, }; pub const SpaceWriterState = struct { space_rev: []const u8, repo_did: []const u8, hash: []const u8, rev: []const u8, /// Left the writer set; listed as removed until it writes again. removed: bool = false, }; /// A syncer's notification registration, identified by its endpoint and token /// together. The token is the syncer's secret: never log it. pub const NotifyRegistration = struct { endpoint: []const u8, token: []const u8, }; pub const PreparedRecord = struct { cid: []const u8, value_json: []const u8, validation_status: []const u8, blob_cids: []const []const u8, }; pub const SpaceWriteOp = union(enum) { create: struct { collection: []const u8, rkey: []const u8, prepared: PreparedRecord }, put: struct { collection: []const u8, rkey: []const u8, prepared: PreparedRecord }, update: struct { collection: []const u8, rkey: []const u8, prepared: PreparedRecord }, delete: struct { collection: []const u8, rkey: []const u8 }, }; pub const SpaceWriteResult = union(enum) { create: SpaceRecord, update: SpaceRecord, delete: void, }; pub const WriteProfile = struct { validation_ns: u64 = 0, lock_wait_ns: u64 = 0, load_repo_ns: u64 = 0, stage_records_ns: u64 = 0, build_commit_ns: u64 = 0, sql_ns: u64 = 0, event_publish_ns: u64 = 0, total_ns: u64 = 0, }; pub const WriteOp = union(enum) { create: struct { collection: []const u8, rkey: ?[]const u8, value: std.json.Value, }, update: struct { collection: []const u8, rkey: []const u8, value: std.json.Value, swap: RecordSwap = .none, }, delete: struct { collection: []const u8, rkey: []const u8, swap: RecordSwap = .none, }, }; pub const RecordSwap = union(enum) { none, missing, cid: []const u8, }; pub const ValidationMode = enum { known, require, skip, }; pub const WriteOptions = struct { swap_commit: ?[]const u8 = null, validate: ValidationMode = .known, /// Permit a commit with no record operations. Only the account-creation /// genesis commit uses this; XRPC applyWrites keeps rejecting empty writes. allow_empty_commit: bool = false, initialize_only: bool = false, /// receives the referenced blob that was missing when a write fails /// with `BlobNotFound` missing_blob: ?*?[]const u8 = null, }; const BlobRef = struct { cid: []const u8, uri: []const u8, }; const RepoBlockReader = struct { allocator: std.mem.Allocator, did: []const u8, connection: ?zqlite.Conn = null, locked: bool = false, fn reader(self: *RepoBlockReader) zat.mst.BlockReader { return .{ .ctx = self, .getFn = getBlock, }; } fn getBlock(ctx: *anyopaque, cid_raw: []const u8) anyerror!?[]const u8 { const self: *RepoBlockReader = @ptrCast(@alignCast(ctx)); const cid = try zat.cbor.Cid.toString(.{ .raw = cid_raw }, self.allocator); if (self.connection) |read_conn| { return repoBlockDataFrom(read_conn, self.allocator, self.did, cid); } if (self.locked) { return repoBlockDataLocked(self.allocator, self.did, cid); } db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return repoBlockDataLocked(self.allocator, self.did, cid); } }; fn repoBlockDataLocked(allocator: std.mem.Allocator, did: []const u8, cid: []const u8) !?[]const u8 { return repoBlockDataFrom(conn, allocator, did, cid); } fn repoBlockDataFrom( read_conn: zqlite.Conn, allocator: std.mem.Allocator, did: []const u8, cid: []const u8, ) !?[]const u8 { const row = try read_conn.row( \\SELECT data \\FROM repo_blocks \\WHERE did = ? AND cid = ? \\LIMIT 1 , .{ did, cid }); if (row == null) return null; defer row.?.deinit(); const data = row.?.nullableBlob(0) orelse return null; return try allocator.dupe(u8, data); } var conn: zqlite.Conn = undefined; var database_path: ?[:0]u8 = null; var initialized = false; var store_io: Io = undefined; const DatabaseMutex = struct { inner: Io.Mutex = .init, owner_ptr: std.atomic.Value(usize) = .init(0), owner_len: std.atomic.Value(usize) = .init(0), acquired_at_ms: std.atomic.Value(u32) = .init(0), fn lockUncancelable(self: *DatabaseMutex, io: Io) void { const owner_ptr = self.owner_ptr.load(.acquire); if (owner_ptr != 0) { const held_ms = nowMsWrapping() -% self.acquired_at_ms.load(.acquire); if (held_ms >= 100) { const owner_len = self.owner_len.load(.acquire); const owner: [*]const u8 = @ptrFromInt(owner_ptr); log.err( "database lock contention owner={s} held_ms={d} waiter={s}\n", .{ owner[0..owner_len], held_ms, request_context.currentRoute() }, ); } } self.inner.lockUncancelable(io); const owner = request_context.currentRoute(); self.owner_len.store(owner.len, .monotonic); self.acquired_at_ms.store(nowMsWrapping(), .monotonic); self.owner_ptr.store(@intFromPtr(owner.ptr), .release); } fn unlock(self: *DatabaseMutex, io: Io) void { self.owner_ptr.store(0, .release); self.inner.unlock(io); } }; var db_mutex: DatabaseMutex = .{}; /// Read-only WAL connections for request-path reads, so a record or account /// lookup does not queue behind a write, a blob GC pass or another read on /// `db_mutex`. SQLite gives every statement a consistent snapshot of /// committed data, and a write commits before it returns, so a read after a /// write sees it. `:memory:` stores and `ZDS_DB_READERS=0` have no pool; /// their reads take `db_mutex` on the writer connection, as every read did /// before. const max_db_readers = 16; var readers: ?*zqlite.Pool = null; /// A connection for one read. Release it before calling anything else that /// takes a reader or `db_mutex`: without a pool the lease is `db_mutex` /// itself, which does not nest. const ReadLease = struct { db: zqlite.Conn, pooled: bool, fn release(self: ReadLease) void { if (self.pooled) { self.db.release(store_io); } else { db_mutex.unlock(store_io); } } }; fn acquireReader() !ReadLease { if (readers) |pool| return .{ .db = try pool.acquire(store_io), .pooled = true }; db_mutex.lockUncancelable(store_io); errdefer db_mutex.unlock(store_io); try requireInitialized(); return .{ .db = conn, .pooled = false }; } fn openReaders(path: [:0]const u8, wanted: usize) !void { if (wanted == 0 or std.mem.eql(u8, path, ":memory:")) return; readers = try zqlite.Pool.init(std.heap.page_allocator, .{ .size = @min(wanted, max_db_readers), .flags = zqlite.OpenFlags.ReadOnly, .path = path.ptr, .on_connection = &configureReader, }); } fn configureReader(read_conn: zqlite.Conn, _: ?*anyopaque) anyerror!void { try read_conn.busyTimeout(5000); try read_conn.execNoArgs("PRAGMA query_only=ON"); } fn closeReaders() void { const pool = readers orelse return; readers = null; pool.deinit(); } var write_lanes: sharded_locks.ShardedLocks(32) = .{}; var blob_lanes: sharded_locks.ShardedLocks(64) = .{}; var next_seq: u64 = 1; var blob_gc_worker_started: std.atomic.Value(bool) = .init(false); const blob_gc_grace_seconds: i64 = 24 * 60 * 60; const blob_gc_batch: usize = 256; const blob_gc_interval_seconds: i64 = 60; pub const BlobGcResult = struct { deleted: usize = 0, bytes: u64 = 0, failed: usize = 0, }; pub fn init(io: Io, path: []const u8) !void { if (initialized) return; store_io = io; if (!std.mem.eql(u8, path, ":memory:") and !std.fs.path.isAbsolute(path)) { if (std.fs.path.dirname(path)) |dir| try Io.Dir.createDirPath(.cwd(), io, dir); } const path_z = try std.heap.page_allocator.dupeSentinel(u8, path, 0); var path_owned = true; defer if (path_owned) std.heap.page_allocator.free(path_z); conn = try zqlite.open(path_z.ptr, zqlite.OpenFlags.Create | zqlite.OpenFlags.ReadWrite); database_path = path_z; path_owned = false; initialized = true; errdefer close(); try conn.busyTimeout(5000); try conn.execNoArgs("PRAGMA journal_mode=WAL"); try conn.execNoArgs("PRAGMA foreign_keys=ON"); const migrations_started = clock.monotonicNs(); try migrate(); const migrations_ready = clock.monotonicNs(); next_seq = try loadNextSeqLocked(); const floor = app_config.seqFloor(); if (next_seq <= floor) { log.info("startup seq raised to the configured floor from={d} to={d}\n", .{ next_seq, floor + 1 }); next_seq = floor + 1; } try openReaders(path_z, app_config.dbReaders()); log.info("startup db_readers={d}\n", .{if (readers) |pool| pool.conns.len else 0}); log.info("startup migrations_ms={d}\n", .{(migrations_ready - migrations_started) / std.time.ns_per_ms}); log.info("startup sequence_ms={d}\n", .{(clock.monotonicNs() - migrations_ready) / std.time.ns_per_ms}); } pub fn close() void { if (!initialized) return; closeReaders(); conn.close(); if (database_path) |path| std.heap.page_allocator.free(path); database_path = null; initialized = false; } pub fn nowMs() i64 { return clock.nowMs(); } /// low 32 bits of nowMs(); differences via -% stay exact for spans under ~49 days fn nowMsWrapping() u32 { return @truncate(@as(u64, @bitCast(nowMs()))); } pub fn randomBytes(buffer: []u8) void { store_io.random(buffer); } pub fn randomToken(allocator: std.mem.Allocator, prefix: []const u8, comptime byte_len: usize) ![]const u8 { var bytes: [byte_len]u8 = undefined; randomBytes(&bytes); const hex = std.fmt.bytesToHex(bytes, .lower); return std.fmt.allocPrint(allocator, "{s}{s}", .{ prefix, &hex }); } pub fn currentIo() Io { return store_io; } pub fn latestSequence() u64 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); return next_seq -| 1; } pub fn resolveRepo(allocator: std.mem.Allocator, repo: []const u8) ?auth.Account { return findAccount(allocator, repo) catch null; } pub fn findAccount(allocator: std.mem.Allocator, identifier: []const u8) !?auth.Account { const reader = try acquireReader(); defer reader.release(); return try findAccountFrom(reader.db, allocator, identifier); } /// Whether `did` is the account an OAuth request's `login_hint` names. A /// request without a hint accepts any account. pub fn accountMatchesLoginHint(allocator: std.mem.Allocator, login_hint: ?[]const u8, did: []const u8) !bool { const identifier = login_hint orelse return true; const hinted = (try findAccount(allocator, identifier)) orelse return false; return std.mem.eql(u8, hinted.did, did); } pub fn findActiveAccount(allocator: std.mem.Allocator, identifier: []const u8) !?auth.Account { const reader = try acquireReader(); defer reader.release(); const row = try reader.db.row( \\SELECT handle, did, email, password_hash \\FROM accounts \\WHERE account_status = 'active' \\ AND (lower(handle) = lower(?) OR did = ? OR lower(email) = lower(?)) \\LIMIT 1 , .{ identifier, identifier, identifier }); if (row == null) return null; defer row.?.deinit(); return try accountFromRow(allocator, row.?); } pub fn consumeRateLimit(subject: []const u8, action: []const u8, now_ms: i64, window_ms: i64, limit: usize) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.execNoArgs("BEGIN IMMEDIATE"); errdefer conn.execNoArgs("ROLLBACK") catch {}; try conn.exec( \\DELETE FROM rate_limit_events \\WHERE action = ? AND occurred_at < ? , .{ action, now_ms - window_ms }); const row = try conn.row( \\SELECT COUNT(*) \\FROM rate_limit_events \\WHERE subject = ? AND action = ? AND occurred_at >= ? , .{ subject, action, now_ms - window_ms }); defer if (row) |value| value.deinit(); if (row != null and row.?.int(0) >= @as(i64, @intCast(limit))) { try conn.execNoArgs("ROLLBACK"); return Error.RateLimitExceeded; } try conn.exec( \\INSERT INTO rate_limit_events (subject, action, occurred_at) \\VALUES (?, ?, ?) , .{ subject, action, now_ms }); try conn.execNoArgs("COMMIT"); } pub fn searchAccounts(allocator: std.mem.Allocator, query: []const u8, limit: usize) ![]auth.Account { const reader = try acquireReader(); defer reader.release(); const actual_limit = if (limit == 0) 25 else limit; const pattern = try std.fmt.allocPrint(allocator, "%{s}%", .{query}); var rows = if (query.len == 0) try reader.db.rows( \\SELECT handle, did, email, password_hash \\FROM accounts \\ORDER BY handle \\LIMIT ? , .{@as(i64, @intCast(actual_limit))}) else try reader.db.rows( \\SELECT handle, did, email, password_hash \\FROM accounts \\WHERE lower(handle) LIKE lower(?) OR did LIKE ? \\ORDER BY handle \\LIMIT ? , .{ pattern, pattern, @as(i64, @intCast(actual_limit)) }); defer rows.deinit(); var accounts: std.ArrayList(auth.Account) = .empty; while (rows.next()) |row| { try accounts.append(allocator, try accountFromRow(allocator, row)); } if (rows.err) |err| return err; return accounts.toOwnedSlice(allocator); } pub fn listResidents(allocator: std.mem.Allocator, limit: usize) ![]Resident { const reader = try acquireReader(); defer reader.release(); const actual_limit = if (limit == 0) 24 else @min(limit, 100); var rows = try reader.db.rows( \\SELECT a.handle, a.did, a.account_status, COALESCE(c.rev, ''), COUNT(r.uri) \\FROM accounts a \\LEFT JOIN ( \\ SELECT c1.did, c1.rev \\ FROM commits c1 \\ JOIN ( \\ SELECT did, MAX(seq) AS seq \\ FROM commits \\ GROUP BY did \\ ) latest ON latest.did = c1.did AND latest.seq = c1.seq \\) c ON c.did = a.did \\LEFT JOIN records r ON r.did = a.did \\WHERE a.handle NOT LIKE '%.test' \\GROUP BY a.did \\ORDER BY a.activated_at IS NULL, a.handle COLLATE NOCASE \\LIMIT ? , .{@as(i64, @intCast(actual_limit))}); defer rows.deinit(); var residents: std.ArrayList(Resident) = .empty; while (rows.next()) |row| { try residents.append(allocator, .{ .handle = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .active = AccountStatus.parse(row.text(2)).isActive(), .rev = try allocator.dupe(u8, row.text(3)), .record_count = @intCast(row.int(4)), }); } if (rows.err) |err| return err; return residents.toOwnedSlice(allocator); } pub const ProfileView = struct { display_name: ?[]const u8 = null, avatar_cid: ?[]const u8 = null, }; pub fn profileAvatarCid(allocator: std.mem.Allocator, did: []const u8) !?[]const u8 { return (try profileView(allocator, did)).avatar_cid; } /// The display name and stored avatar blob of an account's /// `app.bsky.actor.profile/self` record, where present. pub fn profileView(allocator: std.mem.Allocator, did: []const u8) !ProfileView { const reader = try acquireReader(); defer reader.release(); const row = try reader.db.row( \\SELECT rb.data \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = 'app.bsky.actor.profile' AND r.rkey = 'self' \\LIMIT 1 , .{did}); if (row == null) return .{}; defer row.?.deinit(); const profile_json = recordJsonFromBlock(allocator, row.?.nullableBlob(0) orelse "") catch return .{}; var parsed = std.json.parseFromSlice(std.json.Value, allocator, profile_json, .{}) catch return .{}; defer parsed.deinit(); if (parsed.value != .object) return .{}; var view: ProfileView = .{}; if (parsed.value.object.get("displayName")) |name| { if (name == .string and std.mem.trim(u8, name.string, " \t\r\n").len > 0) view.display_name = try allocator.dupe(u8, name.string); } const avatar = parsed.value.object.get("avatar") orelse return view; if (avatar != .object) return view; const ref = avatar.object.get("ref") orelse return view; if (ref != .object) return view; const link = ref.object.get("$link") orelse return view; if (link != .string) return view; const blob_row = try reader.db.row( \\SELECT 1 \\FROM blobs \\WHERE did = ? AND cid = ? \\LIMIT 1 , .{ did, link.string }); if (blob_row == null) return view; defer blob_row.?.deinit(); view.avatar_cid = try allocator.dupe(u8, link.string); return view; } /// Collections by record count across residents, or within one resident's /// repo when `did` is set. Test accounts are left out. pub fn listCollectionSummaries(allocator: std.mem.Allocator, did: ?[]const u8, limit: usize) ![]CollectionSummary { const reader = try acquireReader(); defer reader.release(); const actual_limit: i64 = @intCast(if (limit == 0) 12 else @min(limit, 1000)); var rows = try reader.db.rows( \\SELECT r.collection, COUNT(*) AS count \\FROM records r \\JOIN accounts a ON a.did = r.did \\WHERE a.handle NOT LIKE '%.test' AND (?1 IS NULL OR r.did = ?1) \\GROUP BY r.collection \\ORDER BY count DESC, r.collection COLLATE NOCASE \\LIMIT ?2 , .{ did, actual_limit }); defer rows.deinit(); var summaries: std.ArrayList(CollectionSummary) = .empty; while (rows.next()) |row| { try summaries.append(allocator, .{ .collection = try allocator.dupe(u8, row.text(0)), .count = @intCast(row.int(1)), }); } if (rows.err) |err| return err; return summaries.toOwnedSlice(allocator); } pub const telemetry_buckets = 19; /// One kind's requests in one minute (unix minute). pub const TelemetryMinute = struct { minute: i64, kind: u8, count: u32, failures: u32, hist: [telemetry_buckets]u32, }; /// One route and method's requests in one hour (unix hour). pub const TelemetryHour = struct { hour: i64, route: []const u8, method: []const u8, count: u32, failures: u32, hist: [telemetry_buckets]u32, }; /// One account's authenticated requests through one app in one hour. pub const TelemetryApp = struct { hour: i64, did: []const u8, app: []const u8, count: u32, }; pub const TelemetryRows = struct { minutes: []const TelemetryMinute, hours: []const TelemetryHour, apps: []const TelemetryApp = &.{}, }; /// Minute rows are kept two days, hour rows ninety. const telemetry_minute_keep_s = 2 * 24 * 3600; const telemetry_hour_keep_s = 90 * 24 * 3600; fn histBlob(hist: [telemetry_buckets]u32) [telemetry_buckets * 4]u8 { var out: [telemetry_buckets * 4]u8 = undefined; for (hist, 0..) |n, i| std.mem.writeInt(u32, out[i * 4 ..][0..4], n, .little); return out; } fn histFromBlob(bytes: []const u8) [telemetry_buckets]u32 { var hist: [telemetry_buckets]u32 = @splat(0); if (bytes.len != telemetry_buckets * 4) return hist; for (&hist, 0..) |*n, i| n.* = std.mem.readInt(u32, bytes[i * 4 ..][0..4], .little); return hist; } fn addHist(a: [telemetry_buckets]u32, b: [telemetry_buckets]u32) [telemetry_buckets]u32 { var out = a; for (&out, b) |*x, y| x.* +|= y; return out; } /// Adds drained counters to their rows, creating rows as needed. pub fn addTelemetry(rows: TelemetryRows) !void { if (rows.minutes.len == 0 and rows.hours.len == 0 and rows.apps.len == 0) return; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); for (rows.minutes) |row| { var hist = row.hist; var total: u64 = row.count; var failures: u64 = row.failures; if (try conn.row("SELECT count, failures, hist FROM telemetry_minutes WHERE minute = ? AND kind = ?", .{ row.minute, row.kind })) |existing| { defer existing.deinit(); total += @intCast(existing.int(0)); failures += @intCast(existing.int(1)); hist = addHist(hist, histFromBlob(existing.blob(2))); } const blob = histBlob(hist); try conn.exec( \\INSERT INTO telemetry_minutes (minute, kind, count, failures, hist) VALUES (?, ?, ?, ?, ?) \\ON CONFLICT(minute, kind) DO UPDATE SET count = excluded.count, failures = excluded.failures, hist = excluded.hist , .{ row.minute, row.kind, @as(i64, @intCast(total)), @as(i64, @intCast(failures)), zqlite.blob(&blob) }); } for (rows.hours) |row| { var hist = row.hist; var total: u64 = row.count; var failures: u64 = row.failures; if (try conn.row("SELECT count, failures, hist FROM telemetry_hours WHERE hour = ? AND route = ? AND method = ?", .{ row.hour, row.route, row.method })) |existing| { defer existing.deinit(); total += @intCast(existing.int(0)); failures += @intCast(existing.int(1)); hist = addHist(hist, histFromBlob(existing.blob(2))); } const blob = histBlob(hist); try conn.exec( \\INSERT INTO telemetry_hours (hour, route, method, count, failures, hist) VALUES (?, ?, ?, ?, ?, ?) \\ON CONFLICT(hour, route, method) DO UPDATE SET count = excluded.count, failures = excluded.failures, hist = excluded.hist , .{ row.hour, row.route, row.method, @as(i64, @intCast(total)), @as(i64, @intCast(failures)), zqlite.blob(&blob) }); } for (rows.apps) |row| { try conn.exec( \\INSERT INTO telemetry_apps (hour, did, app, count) VALUES (?, ?, ?, ?) \\ON CONFLICT(hour, did, app) DO UPDATE SET count = count + excluded.count , .{ row.hour, row.did, row.app, @as(i64, row.count) }); } try conn.commit(); } pub fn pruneTelemetry(now_s: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM telemetry_minutes WHERE minute < ?", .{@divFloor(now_s - telemetry_minute_keep_s, 60)}); try conn.exec("DELETE FROM telemetry_hours WHERE hour < ?", .{@divFloor(now_s - telemetry_hour_keep_s, 3600)}); try conn.exec("DELETE FROM telemetry_apps WHERE hour < ?", .{@divFloor(now_s - telemetry_hour_keep_s, 3600)}); try conn.exec("DELETE FROM telemetry_storage WHERE hour < ?", .{@divFloor(now_s - telemetry_hour_keep_s, 3600)}); } /// Minute rows with `from <= minute < to`, oldest first. pub fn listTelemetryMinutes(allocator: std.mem.Allocator, from: i64, to: i64) ![]TelemetryMinute { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows("SELECT minute, kind, count, failures, hist FROM telemetry_minutes WHERE minute >= ? AND minute < ? ORDER BY minute", .{ from, to }); defer rows.deinit(); var out: std.ArrayList(TelemetryMinute) = .empty; while (rows.next()) |row| try out.append(allocator, .{ .minute = row.int(0), .kind = @intCast(row.int(1)), .count = @intCast(row.int(2)), .failures = @intCast(row.int(3)), .hist = histFromBlob(row.blob(4)), }); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// Hour rows with `from <= hour < to`, oldest first. pub fn listTelemetryHours(allocator: std.mem.Allocator, from: i64, to: i64) ![]TelemetryHour { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows("SELECT hour, route, method, count, failures, hist FROM telemetry_hours WHERE hour >= ? AND hour < ? ORDER BY hour", .{ from, to }); defer rows.deinit(); var out: std.ArrayList(TelemetryHour) = .empty; while (rows.next()) |row| try out.append(allocator, .{ .hour = row.int(0), .route = try allocator.dupe(u8, row.text(1)), .method = try allocator.dupe(u8, row.text(2)), .count = @intCast(row.int(3)), .failures = @intCast(row.int(4)), .hist = histFromBlob(row.blob(5)), }); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// What this server holds: database file size, blob bytes, records and /// accounts (test accounts included), taken hourly for the stats page. pub const StorageSnapshot = struct { hour: i64 = 0, db_bytes: u64, blob_bytes: u64, records: u64, accounts: u64, }; pub fn storageSnapshot() !StorageSnapshot { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const pages = (try conn.row("SELECT page_count * page_size FROM pragma_page_count(), pragma_page_size()", .{})).?; defer pages.deinit(); const blobs = (try conn.row("SELECT COALESCE(SUM(size), 0) FROM blobs", .{})).?; defer blobs.deinit(); const records = (try conn.row("SELECT COUNT(*) FROM records", .{})).?; defer records.deinit(); const accounts = (try conn.row("SELECT COUNT(*) FROM accounts", .{})).?; defer accounts.deinit(); return .{ .db_bytes = @intCast(pages.int(0)), .blob_bytes = @intCast(blobs.int(0)), .records = @intCast(records.int(0)), .accounts = @intCast(accounts.int(0)), }; } pub fn recordStorageSnapshot(hour: i64, snapshot: StorageSnapshot) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\INSERT INTO telemetry_storage (hour, db_bytes, blob_bytes, records, accounts) VALUES (?, ?, ?, ?, ?) \\ON CONFLICT(hour) DO UPDATE SET db_bytes = excluded.db_bytes, blob_bytes = excluded.blob_bytes, records = excluded.records, accounts = excluded.accounts , .{ hour, @as(i64, @intCast(snapshot.db_bytes)), @as(i64, @intCast(snapshot.blob_bytes)), @as(i64, @intCast(snapshot.records)), @as(i64, @intCast(snapshot.accounts)) }); } pub fn listStorageSnapshots(allocator: std.mem.Allocator, from: i64, to: i64) ![]StorageSnapshot { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows("SELECT hour, db_bytes, blob_bytes, records, accounts FROM telemetry_storage WHERE hour >= ? AND hour < ? ORDER BY hour", .{ from, to }); defer rows.deinit(); var out: std.ArrayList(StorageSnapshot) = .empty; while (rows.next()) |row| try out.append(allocator, .{ .hour = row.int(0), .db_bytes = @intCast(row.int(1)), .blob_bytes = @intCast(row.int(2)), .records = @intCast(row.int(3)), .accounts = @intCast(row.int(4)), }); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// App rows with `from <= hour < to`, for one account or all of them. pub fn listTelemetryApps(allocator: std.mem.Allocator, from: i64, to: i64, did: ?[]const u8) ![]TelemetryApp { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows("SELECT hour, did, app, count FROM telemetry_apps WHERE hour >= ?1 AND hour < ?2 AND (?3 IS NULL OR did = ?3) ORDER BY hour", .{ from, to, did }); defer rows.deinit(); var out: std.ArrayList(TelemetryApp) = .empty; while (rows.next()) |row| try out.append(allocator, .{ .hour = row.int(0), .did = try allocator.dupe(u8, row.text(1)), .app = try allocator.dupe(u8, row.text(2)), .count = @intCast(row.int(3)), }); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// The spans the landing page charts record writes over. pub const ActivityRange = enum { day, week, month, year, all, pub fn label(self: ActivityRange) []const u8 { return switch (self) { .day => "24h", .week => "7d", .month => "30d", .year => "1y", .all => "all", }; } pub fn parse(text: []const u8) ?ActivityRange { for (std.enums.values(ActivityRange)) |range| { if (std.mem.eql(u8, text, range.label())) return range; } return null; } }; pub const Activity = struct { buckets: []ActivityBucket, /// Whether each bucket is a calendar month rather than a fixed width. monthly: bool = false, }; /// Record writes per bucket, timed by each record's TID key: when the record /// was written, which survives an account moving here, unlike the commit /// that imported it. Records keyed some other way (`self`, a name) are not /// counted. Fixed ranges end at the bucket holding `now_us`; `all` runs by /// month from the first record, widening months so it stays at most 60 bars. pub fn listRecordActivity(allocator: std.mem.Allocator, range: ActivityRange, did: ?[]const u8, now_us: u64) !Activity { const hour: u64 = std.time.us_per_hour; const day: u64 = 24 * hour; const fixed: ?struct { width: u64, count: usize } = switch (range) { .day => .{ .width = hour, .count = 24 }, .week => .{ .width = 4 * hour, .count = 42 }, .month => .{ .width = day, .count = 30 }, .year => .{ .width = 7 * day, .count = 53 }, .all => null, }; // Keys before atproto existed, or ahead of the clock, are not real // write times. const earliest: u64 = 1_640_995_200 * std.time.us_per_s; // 2022-01-01 const end = if (fixed) |f| (now_us / f.width + 1) * f.width else now_us + 1; const start = if (fixed) |f| end - f.width * f.count else earliest; var times: std.ArrayList(u64) = .empty; defer times.deinit(allocator); { const reader = try acquireReader(); defer reader.release(); const low = zat.Tid.fromTimestamp(@max(start, earliest), 0).str(); const high = zat.Tid.fromTimestamp(@min(end, now_us + 1), 0).str(); var rows = try reader.db.rows( \\SELECT r.rkey \\FROM records r \\JOIN accounts a ON a.did = r.did \\WHERE r.rkey >= ?1 AND r.rkey < ?2 AND length(r.rkey) = 13 \\ AND a.handle NOT LIKE '%.test' AND (?3 IS NULL OR r.did = ?3) , .{ @as([]const u8, &low), @as([]const u8, &high), did }); defer rows.deinit(); while (rows.next()) |row| { const tid = zat.Tid.parse(row.text(0)) orelse continue; try times.append(allocator, tid.timestamp()); } if (rows.err) |err| return err; } if (fixed) |f| { const buckets = try allocator.alloc(ActivityBucket, f.count); for (buckets, 0..) |*bucket, i| bucket.* = .{ .start_us = start + f.width * i, .end_us = start + f.width * (i + 1), .count = 0 }; for (times.items) |time| { if (time < start or time >= end) continue; buckets[@intCast((time - start) / f.width)].count += 1; } return .{ .buckets = buckets }; } if (times.items.len == 0) return .{ .buckets = &.{}, .monthly = true }; var first = times.items[0]; for (times.items) |time| first = @min(first, time); const first_month = monthIndex(first); const last_month = monthIndex(now_us); const months = last_month - first_month + 1; const step = (months + 59) / 60; const bar_count = (months + step - 1) / step; const buckets = try allocator.alloc(ActivityBucket, @intCast(bar_count)); for (buckets, 0..) |*bucket, i| { const month = first_month + @as(u64, i) * step; bucket.* = .{ .start_us = monthStart(month), .end_us = @min(monthStart(month + step), now_us), .count = 0 }; } for (times.items) |time| buckets[@intCast((monthIndex(time) - first_month) / step)].count += 1; return .{ .buckets = buckets, .monthly = true }; } /// Months since January 1970, UTC. fn monthIndex(timestamp_us: u64) u64 { const days: i64 = @intCast(timestamp_us / std.time.us_per_day); const date = std.time.epoch.EpochDay{ .day = @intCast(days) }; const year_day = date.calculateYearDay(); const month_day = year_day.calculateMonthDay(); return (@as(u64, year_day.year) - 1970) * 12 + (@backingInt(month_day.month) - 1); } fn monthStart(index: u64) u64 { const year: std.time.epoch.Year = @intCast(1970 + index / 12); const month = index % 12; var days: u64 = 0; var y: std.time.epoch.Year = 1970; while (y < year) : (y += 1) days += std.time.epoch.getDaysInYear(y); var m: u64 = 0; while (m < month) : (m += 1) days += std.time.epoch.getDaysInMonth(year, @fromBackingInt(@intCast(m + 1))); return days * std.time.us_per_day; } pub fn listLandingRecentRecords(allocator: std.mem.Allocator, limit: usize) ![]RecentRecord { const reader = try acquireReader(); defer reader.release(); const actual_limit = if (limit == 0) 8 else @min(limit, 24); var rows = try reader.db.rows( \\SELECT a.handle, r.did, r.collection, r.rkey, r.cid, r.seq \\FROM records r \\JOIN accounts a ON a.did = r.did \\WHERE a.handle NOT LIKE '%.test' \\ORDER BY r.seq DESC, r.collection COLLATE NOCASE \\LIMIT ? , .{@as(i64, @intCast(actual_limit))}); defer rows.deinit(); var records: std.ArrayList(RecentRecord) = .empty; while (rows.next()) |row| { try records.append(allocator, .{ .handle = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .collection = try allocator.dupe(u8, row.text(2)), .rkey = try allocator.dupe(u8, row.text(3)), .cid = try allocator.dupe(u8, row.text(4)), .seq = @intCast(row.int(5)), }); } if (rows.err) |err| return err; return records.toOwnedSlice(allocator); } fn findAccountLocked(allocator: std.mem.Allocator, identifier: []const u8) !?auth.Account { return findAccountFrom(conn, allocator, identifier); } fn findAccountFrom(db: zqlite.Conn, allocator: std.mem.Allocator, identifier: []const u8) !?auth.Account { const row = try db.row( \\SELECT handle, did, email, password_hash \\FROM accounts \\WHERE lower(handle) = lower(?) OR did = ? OR lower(email) = lower(?) \\LIMIT 1 , .{ identifier, identifier, identifier }); if (row == null) return null; defer row.?.deinit(); return try accountFromRow(allocator, row.?); } fn accountFromRow(allocator: std.mem.Allocator, row: zqlite.Row) !auth.Account { return .{ .handle = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .email = try allocator.dupe(u8, row.text(2)), .password = try allocator.dupe(u8, row.text(3)), }; } pub fn createAccount( allocator: std.mem.Allocator, handle: []const u8, email: []const u8, password: []const u8, did: []const u8, activated: bool, ) !auth.Account { const signing_key = try generateAccountSigningKey(); return createAccountWithSigningKey(allocator, handle, email, password, did, activated, signing_key); } /// The account's `#atproto_kem` P-256 secret, which space MACs are keyed from. /// Null for an account created before KEM keys existed, until the backfill /// reaches it. pub fn kemSecretKey(did: []const u8) !?[32]u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row("SELECT kem_key FROM accounts WHERE did = ?", .{did}); if (row == null) return null; defer row.?.deinit(); const bytes = row.?.nullableBlob(0) orelse return null; if (bytes.len != 32) return Error.InvalidKemKey; return bytes[0..32].*; } /// The account's `#atproto_kem` key as a did:key, if it has one. pub fn kemDidKey(allocator: std.mem.Allocator, did: []const u8) !?[]const u8 { const secret = (try kemSecretKey(did)) orelse return null; const keypair = try zat.Keypair.fromSecretKey(.p256, secret); return try keypair.did(allocator); } /// Store a KEM key for an account that lacks one. False if it already had one. pub fn setMissingKemSecretKey(did: []const u8, kem_key: [32]u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE accounts SET kem_key = ? WHERE did = ? AND kem_key IS NULL", .{ zqlite.blob(&kem_key), did }); return conn.changes() > 0; } pub fn generateKemSecretKey() ![32]u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return generateKemKey(); } /// Accounts without a KEM key, in DID order after `after`. pub fn listDidsWithoutKemKey(allocator: std.mem.Allocator, after: ?[]const u8, limit: usize) ![]const []const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT did FROM accounts \\WHERE kem_key IS NULL AND did > ? \\ORDER BY did \\LIMIT ? , .{ after orelse "", @as(i64, @intCast(limit)) }); defer rows.deinit(); var dids: std.ArrayList([]const u8) = .empty; while (rows.next()) |row| try dids.append(allocator, try allocator.dupe(u8, row.text(0))); if (rows.err) |err| return err; return dids.toOwnedSlice(allocator); } pub fn createAccountWithSigningKey( allocator: std.mem.Allocator, handle: []const u8, email: []const u8, password: []const u8, did: []const u8, activated: bool, signing_key: [32]u8, ) !auth.Account { return createAccountWithSigningKeyAndInvite(allocator, handle, email, password, did, activated, signing_key, try generateKemSecretKey(), null); } pub fn createAccountWithSigningKeyAndInvite( allocator: std.mem.Allocator, handle: []const u8, email: []const u8, password: []const u8, did: []const u8, activated: bool, signing_key: [32]u8, kem_key: ?[32]u8, invite_code: ?[]const u8, ) !auth.Account { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (invite_code) |code| { try ensureInviteAvailableLocked(code); } var salt: [16]u8 = undefined; store_io.random(&salt); const password_hash = try auth.hashPassword(allocator, password, salt); try conn.execNoArgs("BEGIN IMMEDIATE"); errdefer conn.execNoArgs("ROLLBACK") catch {}; try conn.exec( \\INSERT INTO accounts (did, handle, email, password_hash, activated_at, account_status, signing_key_type, signing_key, kem_key) \\VALUES (?, ?, ?, ?, CASE WHEN ? THEN unixepoch() ELSE NULL END, CASE WHEN ? THEN 'active' ELSE 'deactivated' END, 'secp256k1', ?, ?) , .{ did, handle, email, password_hash, activated, activated, zqlite.blob(&signing_key), if (kem_key) |*key| zqlite.blob(key) else null }); if (invite_code) |code| { try recordInviteUseLocked(code, did); } try conn.execNoArgs("COMMIT"); return .{ .handle = try allocator.dupe(u8, handle), .did = try allocator.dupe(u8, did), .email = try allocator.dupe(u8, email), .password = try allocator.dupe(u8, password_hash), }; } pub fn generateAccountSigningKey() ![32]u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return generateSigningKey(); } pub fn reserveSigningKey(allocator: std.mem.Allocator, did: ?[]const u8) !ReservedSigningKey { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const secret_key = try generateSigningKey(); var keypair = try zat.Keypair.fromSecretKey(.secp256k1, secret_key); const signing_key = try keypair.did(allocator); const expires_at = nowMs() + (24 * 60 * 60 * 1000); try conn.exec( \\INSERT INTO reserved_signing_keys (did, signing_key, signing_key_type, secret_key, expires_at) \\VALUES (?, ?, 'secp256k1', ?, ?) , .{ did, signing_key, zqlite.blob(&secret_key), expires_at }); return .{ .did = if (did) |value| try allocator.dupe(u8, value) else null, .signing_key = signing_key, .secret_key = secret_key, .expires_at = expires_at, }; } pub fn consumeReservedSigningKey(signing_key_or_did: []const u8, did: []const u8) ![32]u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return consumeReservedSigningKeyLocked(signing_key_or_did, did); } pub fn createInviteCode(allocator: std.mem.Allocator, public_url: []const u8, use_count: i64, for_account: []const u8, created_by: []const u8) ![]const u8 { if (use_count < 1) return error.InvalidUseCount; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var tries: usize = 0; while (tries < 16) : (tries += 1) { const code = try generateInviteCode(allocator, public_url); const existing = try conn.row("SELECT 1 FROM invite_codes WHERE code = ? LIMIT 1", .{code}); if (existing) |row| { row.deinit(); continue; } try insertInviteCodeLocked(code, use_count, for_account, created_by); return code; } return error.InviteCodeCollision; } pub fn createInviteCodes(allocator: std.mem.Allocator, public_url: []const u8, code_count: i64, use_count: i64, for_account: []const u8, created_by: []const u8) ![][]const u8 { if (code_count < 1 or use_count < 1) return error.InvalidUseCount; const max_count: i64 = 100; if (code_count > max_count) return error.InvalidUseCount; var codes: std.ArrayList([]const u8) = .empty; errdefer { for (codes.items) |code| allocator.free(code); codes.deinit(allocator); } var i: i64 = 0; while (i < code_count) : (i += 1) { try codes.append(allocator, try createInviteCode(allocator, public_url, use_count, for_account, created_by)); } return codes.toOwnedSlice(allocator); } pub fn ensureBootstrapInviteCode(allocator: std.mem.Allocator, public_url: []const u8) !?[]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const accounts_row = try conn.row("SELECT COUNT(*) FROM accounts", .{}); if (accounts_row == null) return null; defer accounts_row.?.deinit(); const invites_row = try conn.row("SELECT COUNT(*) FROM invite_codes", .{}); if (invites_row == null) return null; defer invites_row.?.deinit(); const accounts = accounts_row.?.int(0); const invites = invites_row.?.int(0); if (accounts != 0 or invites != 0) return null; const code = try generateInviteCode(allocator, public_url); try insertInviteCodeLocked(code, 1, "admin", "admin"); return code; } pub fn getAccountInviteCodes(allocator: std.mem.Allocator, did: []const u8, include_used: bool) ![]InviteCode { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT code, available_uses, disabled, for_account, created_by, created_at \\FROM invite_codes \\WHERE for_account = ? \\ORDER BY created_at DESC, code , .{did}); defer rows.deinit(); var codes: std.ArrayList(InviteCode) = .empty; while (rows.next()) |row| { const code_text = row.text(0); const uses = try inviteCodeUsesLocked(allocator, code_text); const available = row.int(1); const disabled = row.int(2) != 0; if (!include_used and (disabled or @as(i64, @intCast(uses.len)) >= available)) continue; try codes.append(allocator, .{ .code = try allocator.dupe(u8, code_text), .available = available, .disabled = disabled, .for_account = try allocator.dupe(u8, row.text(3)), .created_by = try allocator.dupe(u8, row.text(4)), .created_at = row.int(5), .uses = uses, }); } if (rows.err) |err| return err; return codes.toOwnedSlice(allocator); } pub fn inviteCodeIsAvailable(code: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); ensureInviteAvailableLocked(code) catch return false; return true; } pub fn signingKeypair(did: []const u8) !zat.Keypair { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return signingKeypairLocked(did); } pub fn updateAccountHandle(did: []const u8, handle: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const existing = try conn.row( \\SELECT did \\FROM accounts \\WHERE lower(handle) = lower(?) \\LIMIT 1 , .{handle}); if (existing) |row| { defer row.deinit(); if (!std.mem.eql(u8, row.text(0), did)) return Error.HandleNotAvailable; } const account = try conn.row( \\SELECT 1 \\FROM accounts \\WHERE did = ? , .{did}); if (account == null) return Error.AccountNotFound; account.?.deinit(); try conn.exec( \\UPDATE accounts \\SET handle = ? \\WHERE did = ? , .{ handle, did }); } pub fn setAccountActive(did: []const u8, active: bool) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (active) { const status = try accountStatusLocked(did); if (status != .active and status != .deactivated) return Error.InvalidAccountStatus; try conn.exec( \\UPDATE accounts \\SET account_status = 'active', \\ activated_at = COALESCE(activated_at, unixepoch()) \\WHERE did = ? , .{did}); } else { // As the reference keeps takedown and deactivation apart: a taken down // account that deactivates stays taken down, and is deactivated, not // active, once the takedown is lifted. switch (try accountStatusLocked(did)) { .active, .deactivated => try conn.exec( \\UPDATE accounts \\SET account_status = 'deactivated', \\ activated_at = COALESCE(activated_at, unixepoch()) \\WHERE did = ? , .{did}), .takendown => try conn.exec("UPDATE accounts SET deactivated_beneath_takedown = 1 WHERE did = ?", .{did}), .suspended, .deleted => return Error.InvalidAccountStatus, } } } pub fn setAccountTakendown(did: []const u8, applied: bool, status_ref: ?[]const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (applied) { try conn.execNoArgs("BEGIN IMMEDIATE"); errdefer conn.execNoArgs("ROLLBACK") catch {}; try conn.exec( \\UPDATE accounts \\SET account_status = 'takendown', \\ account_status_ref = ? \\WHERE did = ? , .{ status_ref, did }); // As in the reference PDS, access tokens stay valid until they expire, // so clients hear AccountTakedown instead of a dead session. try conn.exec( \\UPDATE session_tokens \\SET refresh_expires_at = MIN(refresh_expires_at, unixepoch()), \\ previous_refresh_expires_at = MIN(previous_refresh_expires_at, unixepoch()), \\ updated_at = unixepoch() \\WHERE did = ? AND revoked_at IS NULL , .{did}); try conn.exec( \\UPDATE oauth_tokens \\SET revoked_at = unixepoch() \\WHERE did = ? AND revoked_at IS NULL , .{did}); try conn.execNoArgs("COMMIT"); } else { try conn.exec( \\UPDATE accounts \\SET account_status = CASE deactivated_beneath_takedown WHEN 1 THEN 'deactivated' ELSE 'active' END, \\ deactivated_beneath_takedown = 0, \\ account_status_ref = NULL, \\ activated_at = COALESCE(activated_at, unixepoch()) \\WHERE did = ? AND account_status = 'takendown' , .{did}); } } pub fn accountStatus(did: []const u8) !AccountStatus { const reader = try acquireReader(); defer reader.release(); return accountStatusFrom(reader.db, did); } pub fn sequenceAccountEvent(allocator: std.mem.Allocator, did: []const u8, status: AccountStatus) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const seq = try nextSeqLocked(); const frame = try accountEventFrame(allocator, seq, did, status); try insertSeqEventLocked(seq, did, "", frame); eventlog.publish(seq); } pub fn sequenceIdentityEvent(allocator: std.mem.Allocator, did: []const u8, handle: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const seq = try nextSeqLocked(); const frame = try zat.firehose.encodeIdentityEvent(allocator, .{ .seq = @intCast(seq), .did = did, .handle = handle, .time = try nowIso(allocator) }); try insertSeqEventLocked(seq, did, "", frame); eventlog.publish(seq); } /// Firehose announcement for a freshly created account, matching the official /// PDS sequencer order: #identity, #account, genesis #commit, #sync. The /// genesis commit is an empty-ops write so consumers see the repo exist /// before its first record. pub fn sequenceNewAccount(allocator: std.mem.Allocator, account: auth.Account) !void { try sequenceIdentityEvent(allocator, account.did, account.handle); try sequenceAccountEvent(allocator, account.did, .active); _ = try applyWritesWithOptions(allocator, account, &.{}, .{ .allow_empty_commit = true }); try sequenceSyncEvent(allocator, account.did); } /// Startup repair for accounts created before ZDS sequenced creation events: /// any active account with no commits gets its genesis announcement now, so /// relays stop reporting RepoNotFound for it. pub fn sequenceUnannouncedAccounts(allocator: std.mem.Allocator) !void { const unannounced = blk: { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT did, handle FROM accounts \\WHERE activated_at IS NOT NULL AND account_status = 'active' \\ AND did NOT IN (SELECT did FROM commits) , .{}); defer rows.deinit(); var found: std.ArrayList(auth.Account) = .empty; while (rows.next()) |row| { try found.append(allocator, .{ .did = try allocator.dupe(u8, row.text(0)), .handle = try allocator.dupe(u8, row.text(1)), .email = "", .password = "", }); } break :blk try found.toOwnedSlice(allocator); }; for (unannounced) |account| { sequenceNewAccount(allocator, account) catch |err| { log.err("store announce failed did={s} handle={s} err={s}\n", .{ account.did, account.handle, @errorName(err) }); continue; }; log.info("store announced pre-existing account did={s} handle={s}\n", .{ account.did, account.handle }); } } pub fn sequenceSyncEvent(allocator: std.mem.Allocator, did: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const seq = try nextSeqLocked(); const commit = try latestCommitRawLocked(allocator, did) orelse return Error.RepoNotFound; const frame = try zat.firehose.encodeSyncEvent(allocator, .{ .seq = @intCast(seq), .commit = commit.commit_data, .time = try nowIso(allocator) }); try insertSeqEventLocked(seq, did, commit.commit_cid_text, frame); eventlog.publish(seq); } pub fn isAccountActive(did: []const u8) bool { const reader = acquireReader() catch return false; defer reader.release(); return accountActiveFrom(reader.db, did) catch false; } pub fn create( allocator: std.mem.Allocator, account: auth.Account, collection: []const u8, maybe_rkey: ?[]const u8, value: std.json.Value, ) !Record { const rkey = maybe_rkey orelse try nextRkey(allocator); const result = try applyWrites(allocator, account, &.{.{ .create = .{ .collection = collection, .rkey = rkey, .value = value, } }}); if (result.records.len == 0) return Error.MissingRecord; return result.records[0]; } /// Browser credentials are independent of application OAuth grants. Store only /// the token digest; password changes and unavailable accounts invalidate reuse. pub fn putBrowserSession(token_hash: []const u8, account: auth.Account, expires_at: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.execNoArgs("DELETE FROM browser_sessions WHERE expires_at <= unixepoch()"); try conn.exec("INSERT INTO browser_sessions (token_hash, did, password_hash, expires_at) VALUES (?, ?, ?, ?)", .{ token_hash, account.did, account.password, expires_at }); } pub fn getBrowserSession(allocator: std.mem.Allocator, token_hash: []const u8, timestamp: i64) !?auth.Account { const reader = try acquireReader(); defer reader.release(); const row = try reader.db.row( \\SELECT a.handle, a.did, a.email, a.password_hash \\FROM browser_sessions s JOIN accounts a ON a.did = s.did \\WHERE s.token_hash = ? AND s.expires_at > ? \\AND a.password_hash = s.password_hash AND a.account_status = 'active' \\AND a.activated_at IS NOT NULL , .{ token_hash, timestamp }); if (row == null) return null; defer row.?.deinit(); return .{ .handle = try allocator.dupe(u8, row.?.text(0)), .did = try allocator.dupe(u8, row.?.text(1)), .email = try allocator.dupe(u8, row.?.text(2)), .password = try allocator.dupe(u8, row.?.text(3)), }; } pub fn deleteBrowserSession(token_hash: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM browser_sessions WHERE token_hash = ?", .{token_hash}); } /// Record that the browser holding the sign-in cookie with this digest has /// authenticated as `did` for one authorization request, pending consent. pub fn setOAuthSignIn(request_id: []const u8, signin_hash: []const u8, did: []const u8, method: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE oauth_requests SET signin_hash = ?, signin_did = ?, signin_method = ? WHERE request_id = ?", .{ signin_hash, did, method, request_id }); } pub fn setOAuthPrompt(request_id: []const u8, prompt: ?[]const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE oauth_requests SET prompt = ? WHERE request_id = ?", .{ prompt, request_id }); } pub fn putOAuthRequest( request_id: []const u8, client_id: []const u8, redirect_uri: []const u8, scope: []const u8, state: []const u8, code_challenge: []const u8, code_challenge_method: []const u8, response_mode: []const u8, login_hint: ?[]const u8, dpop_jkt: ?[]const u8, expires_at: i64, ) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\INSERT INTO oauth_requests \\ (request_id, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, response_mode, login_hint, dpop_jkt, expires_at) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , .{ request_id, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, response_mode, login_hint, dpop_jkt, expires_at }); } pub fn getOAuthRequest(allocator: std.mem.Allocator, request_id: []const u8) !?OAuthRequest { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT request_id, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, response_mode, login_hint, dpop_jkt, expires_at, sub, code, auth_method, prompt, signin_hash, signin_did, signin_method \\FROM oauth_requests \\WHERE request_id = ? , .{request_id}); if (row == null) return null; defer row.?.deinit(); return try oauthRequestFromRow(row.?, allocator); } pub fn authorizeOAuthRequest(request_id: []const u8, did: []const u8, code: []const u8, auth_method: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE oauth_requests \\SET sub = ?, code = ?, auth_method = ? \\WHERE request_id = ? , .{ did, code, auth_method, request_id }); } pub fn getOAuthRequestByCode(allocator: std.mem.Allocator, code: []const u8) !?OAuthRequest { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT request_id, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, response_mode, login_hint, dpop_jkt, expires_at, sub, code, auth_method, prompt, signin_hash, signin_did, signin_method \\FROM oauth_requests \\WHERE code = ? , .{code}); if (row == null) return null; defer row.?.deinit(); return try oauthRequestFromRow(row.?, allocator); } pub fn consumeOAuthCode(request_id: []const u8, code: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( "UPDATE oauth_requests SET code = NULL WHERE request_id = ? AND code = ?", .{ request_id, code }, ); const row = try conn.row("SELECT changes()", .{}); if (row == null) return false; defer row.?.deinit(); return row.?.int(0) == 1; } pub fn putOAuthToken( did: []const u8, client_id: []const u8, scope: []const u8, access_token: []const u8, refresh_token: []const u8, access_expires_at: i64, refresh_expires_at: i64, dpop_jkt: ?[]const u8, auth_method: ?[]const u8, ) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\INSERT INTO oauth_tokens (family_id, access_token, refresh_token, did, client_id, scope, expires_at, access_expires_at, refresh_expires_at, dpop_jkt, auth_method, session_started_at) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, unixepoch()) , .{ access_token, access_token, refresh_token, did, client_id, scope, access_expires_at, access_expires_at, refresh_expires_at, dpop_jkt, auth_method }); } pub fn getOAuthToken(allocator: std.mem.Allocator, token: []const u8) !?OAuthToken { const reader = try acquireReader(); defer reader.release(); const row = try reader.db.row( \\SELECT family_id, did, client_id, scope, access_token, refresh_token, access_expires_at, refresh_expires_at, \\ previous_refresh_token, previous_refresh_expires_at, revoked_at, dpop_jkt, auth_method, COALESCE(session_started_at, created_at) \\FROM oauth_tokens \\WHERE access_token = ? OR refresh_token = ? OR previous_refresh_token = ? \\ORDER BY created_at DESC \\LIMIT 1 , .{ token, token, token }); if (row == null) return try getOAuthTokenByUsedRefreshFrom(reader.db, allocator, token); defer row.?.deinit(); const access_token = row.?.text(4); const refresh_token = row.?.text(5); const previous_refresh_token = row.?.nullableText(8); const kind: OAuthToken.Kind = if (std.mem.eql(u8, token, access_token)) .access else if (std.mem.eql(u8, token, refresh_token)) .refresh else if (previous_refresh_token != null and std.mem.eql(u8, token, previous_refresh_token.?)) .previous_refresh else return null; return .{ .family_id = try allocator.dupe(u8, row.?.text(0)), .did = try allocator.dupe(u8, row.?.text(1)), .client_id = try allocator.dupe(u8, row.?.text(2)), .scope = try allocator.dupe(u8, row.?.text(3)), .access_token = try allocator.dupe(u8, access_token), .refresh_token = try allocator.dupe(u8, refresh_token), .access_expires_at = row.?.int(6), .refresh_expires_at = row.?.int(7), .previous_refresh_token = if (previous_refresh_token) |text| try allocator.dupe(u8, text) else null, .previous_refresh_expires_at = row.?.nullableInt(9), .revoked = row.?.nullableInt(10) != null, .dpop_jkt = if (row.?.nullableText(11)) |text| try allocator.dupe(u8, text) else null, .auth_method = if (row.?.nullableText(12)) |text| try allocator.dupe(u8, text) else null, .session_started_at = row.?.int(13), .kind = kind, }; } fn getOAuthTokenByUsedRefresh(allocator: std.mem.Allocator, token: []const u8) !?OAuthToken { return getOAuthTokenByUsedRefreshFrom(conn, allocator, token); } fn getOAuthTokenByUsedRefreshFrom(db: zqlite.Conn, allocator: std.mem.Allocator, token: []const u8) !?OAuthToken { const row = try db.row( \\SELECT t.family_id, t.did, t.client_id, t.scope, t.access_token, t.refresh_token, t.access_expires_at, t.refresh_expires_at, \\ t.previous_refresh_token, t.previous_refresh_expires_at, t.revoked_at, t.dpop_jkt, t.auth_method, COALESCE(t.session_started_at, t.created_at) \\FROM oauth_used_refresh_tokens u \\JOIN oauth_tokens t ON t.family_id = u.family_id \\WHERE u.refresh_token = ? \\LIMIT 1 , .{token}); if (row == null) return null; defer row.?.deinit(); return .{ .family_id = try allocator.dupe(u8, row.?.text(0)), .did = try allocator.dupe(u8, row.?.text(1)), .client_id = try allocator.dupe(u8, row.?.text(2)), .scope = try allocator.dupe(u8, row.?.text(3)), .access_token = try allocator.dupe(u8, row.?.text(4)), .refresh_token = try allocator.dupe(u8, row.?.text(5)), .access_expires_at = row.?.int(6), .refresh_expires_at = row.?.int(7), .previous_refresh_token = if (row.?.nullableText(8)) |text| try allocator.dupe(u8, text) else null, .previous_refresh_expires_at = row.?.nullableInt(9), .revoked = row.?.nullableInt(10) != null, .dpop_jkt = if (row.?.nullableText(11)) |text| try allocator.dupe(u8, text) else null, .auth_method = if (row.?.nullableText(12)) |text| try allocator.dupe(u8, text) else null, .session_started_at = row.?.int(13), .kind = .used_refresh, }; } /// rotates only while `current_refresh_token` is still the family's current /// refresh token and returns whether it did; a request that lost the race /// should re-read the row, which then reports the presented token as the /// winner's previous refresh token pub fn rotateOAuthToken( current_refresh_token: []const u8, new_access_token: []const u8, new_refresh_token: []const u8, access_expires_at: i64, refresh_expires_at: i64, previous_refresh_expires_at: i64, ) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try conn.exec( \\INSERT OR IGNORE INTO oauth_used_refresh_tokens (refresh_token, family_id, expires_at) \\SELECT refresh_token, family_id, refresh_expires_at \\FROM oauth_tokens \\WHERE refresh_token = ? AND revoked_at IS NULL , .{current_refresh_token}); try conn.exec( \\UPDATE oauth_tokens \\SET previous_refresh_token = refresh_token, \\ previous_refresh_expires_at = ?, \\ access_token = ?, \\ refresh_token = ?, \\ expires_at = ?, \\ access_expires_at = ?, \\ refresh_expires_at = ? \\WHERE refresh_token = ? AND revoked_at IS NULL , .{ previous_refresh_expires_at, new_access_token, new_refresh_token, access_expires_at, access_expires_at, refresh_expires_at, current_refresh_token }); if (conn.changes() != 1) { conn.rollback(); return false; } try conn.commit(); return true; } pub fn recordDpopJti(jti: []const u8, expires_at: i64) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM dpop_jtis WHERE expires_at < unixepoch()", .{}); const row = try conn.row("SELECT 1 FROM dpop_jtis WHERE jti = ?", .{jti}); if (row) |found| { found.deinit(); return false; } try conn.exec( \\INSERT INTO dpop_jtis (jti, expires_at) \\VALUES (?, ?) , .{ jti, expires_at }); return true; } pub fn revokeOAuthToken(token: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE oauth_tokens \\SET revoked_at = unixepoch() \\WHERE access_token = ? OR refresh_token = ? OR previous_refresh_token = ? \\ OR family_id IN (SELECT family_id FROM oauth_used_refresh_tokens WHERE refresh_token = ?) , .{ token, token, token, token }); } pub fn revokeOAuthTokenFamily(family_id: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE oauth_tokens \\SET revoked_at = unixepoch() \\WHERE family_id = ? , .{family_id}); } /// Revokes every live grant `did` gave the OAuth client `client_id`: the /// resident-facing "remove this app". Returns how many grants it ended. pub fn revokeOAuthClient(did: []const u8, client_id: []const u8) !usize { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE oauth_tokens \\SET revoked_at = unixepoch() \\WHERE did = ? AND client_id = ? AND revoked_at IS NULL , .{ did, client_id }); return conn.changes(); } pub const AccessQuery = struct { kind: enum { oauth, password } = .oauth, state: enum { authorized, past, all } = .authorized, group: ?[]const u8 = null, search: []const u8 = "", offset: i64 = 0, limit: i64 = 20, }; pub const AccessGroup = struct { key: []const u8, name: []const u8, count: i64, newest: i64, }; pub const AccessPage = struct { authorized: i64 = 0, total: i64 = 0, groups: []const AccessGroup = &.{}, grants: []const OAuthGrantInfo = &.{}, sessions: []const SessionInfo = &.{}, next: ?i64 = null, }; // Account-scoped summaries and bounded details; never select token values. pub fn listAccountAccess(allocator: std.mem.Allocator, did: []const u8, query: AccessQuery) !AccessPage { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const oauth = query.kind == .oauth; const base = if (oauth) "WITH access AS (SELECT rowid AS ordinal, did, client_id, scope, created_at, refresh_expires_at, revoked_at, auth_method, client_id AS group_key, client_id AS label, (revoked_at IS NULL AND refresh_expires_at > unixepoch()) AS active FROM oauth_tokens WHERE did = ?) " else "WITH access AS (SELECT rowid AS ordinal, id, did, auth_method, app_password_name, controller_did, created_at, updated_at, last_used_at, access_expires_at, refresh_expires_at, revoked_at, auth_method || ':' || COALESCE(app_password_name, '') AS group_key, COALESCE(app_password_name, 'Account password') AS label, (revoked_at IS NULL AND (access_expires_at > unixepoch() OR refresh_expires_at > unixepoch())) AS active FROM session_tokens WHERE did = ?) "; var page: AccessPage = .{}; const totals_sql = try std.fmt.allocPrint(allocator, "{s}SELECT count(*), COALESCE(sum(active),0) FROM access", .{base}); const totals = (try conn.row(totals_sql, .{did})).?; page.total = totals.int(0); page.authorized = totals.int(1); totals.deinit(); const filter = " WHERE (? = 'all' OR active = (? = 'authorized')) AND instr(lower(label),lower(?)) > 0 "; if (query.group) |group| { const columns = if (oauth) "SELECT t.did, a.handle, t.client_id, t.scope, t.created_at, t.refresh_expires_at, t.revoked_at, t.auth_method, t.active FROM access t JOIN accounts a ON a.did=t.did" else "SELECT t.id, t.did, a.handle, t.auth_method, t.app_password_name, t.controller_did, t.created_at, t.updated_at, t.last_used_at, t.access_expires_at, t.refresh_expires_at, t.revoked_at, t.active FROM access t JOIN accounts a ON a.did=t.did"; const sql = try std.fmt.allocPrint(allocator, "{s}{s}{s} AND group_key = ? ORDER BY t.created_at DESC, ordinal DESC LIMIT ? OFFSET ?", .{ base, columns, filter }); var rows = try conn.rows(sql, .{ did, @tagName(query.state), @tagName(query.state), query.search, group, query.limit + 1, query.offset }); defer rows.deinit(); if (oauth) { const items = try oauthGrantsFromRows(allocator, &rows); if (items.len > query.limit) page.next = query.offset + query.limit; page.grants = items[0..@min(items.len, @as(usize, @intCast(query.limit)))]; } else { const items = try sessionsFromRows(allocator, &rows); if (items.len > query.limit) page.next = query.offset + query.limit; page.sessions = items[0..@min(items.len, @as(usize, @intCast(query.limit)))]; } } else { const sql = try std.fmt.allocPrint(allocator, "{s}SELECT group_key,label,count(*),max(created_at) FROM access{s} GROUP BY group_key ORDER BY group_key LIMIT ? OFFSET ?", .{ base, filter }); var rows = try conn.rows(sql, .{ did, @tagName(query.state), @tagName(query.state), query.search, query.limit + 1, query.offset }); defer rows.deinit(); var groups: std.ArrayList(AccessGroup) = .empty; while (rows.next()) |row| { if (groups.items.len == query.limit) { page.next = query.offset + query.limit; break; } try groups.append(allocator, .{ .key = try allocator.dupe(u8, row.text(0)), .name = try allocator.dupe(u8, row.text(1)), .count = row.int(2), .newest = row.int(3) }); } page.groups = try groups.toOwnedSlice(allocator); } return page; } pub fn listOAuthGrantsForAccount(allocator: std.mem.Allocator, did: []const u8, active_only: bool, limit: i64) ![]OAuthGrantInfo { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const sql = if (active_only) \\SELECT t.did, a.handle, t.client_id, t.scope, t.created_at, t.refresh_expires_at, t.revoked_at, t.auth_method, \\ CASE WHEN t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() THEN 1 ELSE 0 END AS active \\FROM oauth_tokens t \\JOIN accounts a ON a.did = t.did \\WHERE t.did = ? AND t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() \\ORDER BY t.created_at DESC \\LIMIT ? else \\SELECT t.did, a.handle, t.client_id, t.scope, t.created_at, t.refresh_expires_at, t.revoked_at, t.auth_method, \\ CASE WHEN t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() THEN 1 ELSE 0 END AS active \\FROM oauth_tokens t \\JOIN accounts a ON a.did = t.did \\WHERE t.did = ? \\ORDER BY t.created_at DESC \\LIMIT ? ; var rows = try conn.rows(sql, .{ did, limit }); defer rows.deinit(); return try oauthGrantsFromRows(allocator, &rows); } pub fn listOAuthGrantsForAllAccounts(allocator: std.mem.Allocator, active_only: bool, limit: i64) ![]OAuthGrantInfo { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const sql = if (active_only) \\SELECT t.did, a.handle, t.client_id, t.scope, t.created_at, t.refresh_expires_at, t.revoked_at, t.auth_method, \\ CASE WHEN t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() THEN 1 ELSE 0 END AS active \\FROM oauth_tokens t \\JOIN accounts a ON a.did = t.did \\WHERE t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() \\ORDER BY t.created_at DESC \\LIMIT ? else \\SELECT t.did, a.handle, t.client_id, t.scope, t.created_at, t.refresh_expires_at, t.revoked_at, t.auth_method, \\ CASE WHEN t.revoked_at IS NULL AND t.refresh_expires_at > unixepoch() THEN 1 ELSE 0 END AS active \\FROM oauth_tokens t \\JOIN accounts a ON a.did = t.did \\ORDER BY t.created_at DESC \\LIMIT ? ; var rows = try conn.rows(sql, .{limit}); defer rows.deinit(); return try oauthGrantsFromRows(allocator, &rows); } pub fn createSessionTokenRow( allocator: std.mem.Allocator, did: []const u8, access_jti: []const u8, refresh_jti: []const u8, access_expires_at: i64, refresh_expires_at: i64, auth_method: []const u8, controller_did: ?[]const u8, app_password_name: ?[]const u8, ) ![]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const id = try randomToken(allocator, "ses-", 16); errdefer allocator.free(id); try conn.exec( \\INSERT INTO session_tokens \\ (id, did, access_jti, refresh_jti, access_expires_at, refresh_expires_at, auth_method, controller_did, app_password_name) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) , .{ id, did, access_jti, refresh_jti, access_expires_at, refresh_expires_at, auth_method, controller_did, app_password_name }); return id; } pub fn listSessionsForAccount(allocator: std.mem.Allocator, did: []const u8, active_only: bool, limit: i64) ![]SessionInfo { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const sql = if (active_only) \\SELECT s.id, s.did, a.handle, s.auth_method, s.app_password_name, s.controller_did, \\ s.created_at, s.updated_at, s.last_used_at, s.access_expires_at, s.refresh_expires_at, s.revoked_at, \\ CASE WHEN s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) THEN 1 ELSE 0 END AS active \\FROM session_tokens s \\JOIN accounts a ON a.did = s.did \\WHERE s.did = ? AND s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) \\ORDER BY COALESCE(s.last_used_at, s.updated_at, s.created_at) DESC \\LIMIT ? else \\SELECT s.id, s.did, a.handle, s.auth_method, s.app_password_name, s.controller_did, \\ s.created_at, s.updated_at, s.last_used_at, s.access_expires_at, s.refresh_expires_at, s.revoked_at, \\ CASE WHEN s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) THEN 1 ELSE 0 END AS active \\FROM session_tokens s \\JOIN accounts a ON a.did = s.did \\WHERE s.did = ? \\ORDER BY COALESCE(s.last_used_at, s.updated_at, s.created_at) DESC \\LIMIT ? ; var rows = try conn.rows(sql, .{ did, limit }); defer rows.deinit(); return try sessionsFromRows(allocator, &rows); } pub fn listSessionsForAllAccounts(allocator: std.mem.Allocator, active_only: bool, limit: i64) ![]SessionInfo { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const sql = if (active_only) \\SELECT s.id, s.did, a.handle, s.auth_method, s.app_password_name, s.controller_did, \\ s.created_at, s.updated_at, s.last_used_at, s.access_expires_at, s.refresh_expires_at, s.revoked_at, \\ CASE WHEN s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) THEN 1 ELSE 0 END AS active \\FROM session_tokens s \\JOIN accounts a ON a.did = s.did \\WHERE s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) \\ORDER BY COALESCE(s.last_used_at, s.updated_at, s.created_at) DESC \\LIMIT ? else \\SELECT s.id, s.did, a.handle, s.auth_method, s.app_password_name, s.controller_did, \\ s.created_at, s.updated_at, s.last_used_at, s.access_expires_at, s.refresh_expires_at, s.revoked_at, \\ CASE WHEN s.revoked_at IS NULL AND (s.access_expires_at > unixepoch() OR s.refresh_expires_at > unixepoch()) THEN 1 ELSE 0 END AS active \\FROM session_tokens s \\JOIN accounts a ON a.did = s.did \\ORDER BY COALESCE(s.last_used_at, s.updated_at, s.created_at) DESC \\LIMIT ? ; var rows = try conn.rows(sql, .{limit}); defer rows.deinit(); return try sessionsFromRows(allocator, &rows); } pub fn createAppPassword( allocator: std.mem.Allocator, did: []const u8, name: []const u8, password_hash: []const u8, privileged: bool, scopes: ?[]const u8, created_by_controller_did: ?[]const u8, ) !AppPassword { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\INSERT INTO app_passwords \\ (did, name, password_hash, privileged, scopes, created_by_controller_did) \\VALUES (?, ?, ?, ?, ?, ?) , .{ did, name, password_hash, privileged, scopes, created_by_controller_did }); const row = (try conn.row( \\SELECT name, password_hash, created_at, privileged, scopes, created_by_controller_did \\FROM app_passwords \\WHERE did = ? AND name = ? \\LIMIT 1 , .{ did, name })) orelse return error.MissingRecord; defer row.deinit(); return appPasswordFromRow(row, allocator); } pub fn listAppPasswords(allocator: std.mem.Allocator, did: []const u8) ![]AppPassword { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT name, password_hash, created_at, privileged, scopes, created_by_controller_did \\FROM app_passwords \\WHERE did = ? \\ORDER BY created_at DESC, name ASC , .{did}); defer rows.deinit(); var out: std.ArrayList(AppPassword) = .empty; while (rows.next()) |row| { try out.append(allocator, try appPasswordFromRow(row, allocator)); } return out.toOwnedSlice(allocator); } pub fn getAppPasswordByName(allocator: std.mem.Allocator, did: []const u8, name: []const u8) !?AppPassword { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT name, password_hash, created_at, privileged, scopes, created_by_controller_did \\FROM app_passwords \\WHERE did = ? AND name = ? \\LIMIT 1 , .{ did, name }); if (row == null) return null; defer row.?.deinit(); return try appPasswordFromRow(row.?, allocator); } pub fn findMatchingAppPassword(allocator: std.mem.Allocator, did: []const u8, password: []const u8) !?AppPassword { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT name, password_hash, created_at, privileged, scopes, created_by_controller_did \\FROM app_passwords \\WHERE did = ? \\ORDER BY created_at DESC , .{did}); defer rows.deinit(); while (rows.next()) |row| { const candidate = try appPasswordFromRow(row, allocator); if (auth.passwordHashMatches(candidate.password_hash, password)) return candidate; } return null; } pub fn sessionAuthMethod(allocator: std.mem.Allocator, did: []const u8, jti: []const u8) !?[]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT auth_method \\FROM session_tokens \\WHERE did = ? AND (access_jti = ? OR refresh_jti = ?) AND revoked_at IS NULL \\LIMIT 1 , .{ did, jti, jti }); if (row == null) return null; defer row.?.deinit(); return try allocator.dupe(u8, row.?.text(0)); } pub fn revokeAppPassword(did: []const u8, name: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE session_tokens \\SET revoked_at = unixepoch(), updated_at = unixepoch() \\WHERE did = ? AND app_password_name = ? AND revoked_at IS NULL , .{ did, name }); try conn.exec( \\DELETE FROM app_passwords \\WHERE did = ? AND name = ? , .{ did, name }); } pub const SessionTokenStatus = enum { active, expired, revoked, unknown }; /// for the refresh scope `jti` may also name the jti the last rotation retired, /// which stays usable until its grace window closes /// /// `+did` keeps SQLite off the did index so the OR resolves as two jti point /// lookups instead of a scan of every session the account has pub fn sessionTokenStatus(did: []const u8, jti: []const u8, scope: []const u8) !SessionTokenStatus { var id_buf: [128]u8 = undefined; const id = id: { const reader = try acquireReader(); defer reader.release(); switch (try sessionTokenLookup(reader.db, did, jti, scope, &id_buf)) { .active => |id| break :id id, .settled => |status| return status, } }; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE session_tokens SET last_used_at = unixepoch() WHERE id = ?", .{id}); return .active; } const SessionTokenLookup = union(enum) { /// the token's row id, to stamp its use on the writer active: []const u8, settled: SessionTokenStatus, }; fn sessionTokenLookup(db: zqlite.Conn, did: []const u8, jti: []const u8, scope: []const u8, id_buf: []u8) !SessionTokenLookup { const is_refresh = std.mem.eql(u8, scope, "com.atproto.refresh"); const row = if (is_refresh) try db.row( \\SELECT id, revoked_at IS NOT NULL, \\ (CASE WHEN refresh_jti = ? THEN refresh_expires_at ELSE previous_refresh_expires_at END) > unixepoch() \\FROM session_tokens \\WHERE +did = ? AND (refresh_jti = ? OR previous_refresh_jti = ?) \\LIMIT 1 , .{ jti, did, jti, jti }) else try db.row( \\SELECT id, revoked_at IS NOT NULL, access_expires_at > unixepoch() \\FROM session_tokens \\WHERE did = ? AND access_jti = ? \\LIMIT 1 , .{ did, jti }); const found = row orelse return .{ .settled = .unknown }; defer found.deinit(); if (found.int(1) != 0) return .{ .settled = .revoked }; if (found.int(2) == 0) return .{ .settled = .expired }; const id = found.text(0); if (id.len > id_buf.len) return Error.InvalidRefreshSession; @memcpy(id_buf[0..id.len], id); return .{ .active = id_buf[0..id.len] }; } pub const RefreshSession = struct { id: []const u8, auth_method: []const u8, access_jti: []const u8, refresh_jti: []const u8, access_expires_at: i64, refresh_expires_at: i64, /// the jti presented is the one the last rotation retired; the caller hands /// back the successor already on the row instead of rotating again replayed: bool, }; pub fn refreshSessionByJti(allocator: std.mem.Allocator, did: []const u8, jti: []const u8) !?RefreshSession { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT id, auth_method, access_jti, refresh_jti, access_expires_at, refresh_expires_at, refresh_jti <> ? \\FROM session_tokens \\WHERE +did = ? \\ AND (refresh_jti = ? OR (previous_refresh_jti = ? AND previous_refresh_expires_at > unixepoch())) \\ AND refresh_expires_at > unixepoch() AND revoked_at IS NULL \\LIMIT 1 , .{ jti, did, jti, jti }); const found = row orelse return null; defer found.deinit(); return .{ .id = try allocator.dupe(u8, found.text(0)), .auth_method = try allocator.dupe(u8, found.text(1)), .access_jti = try allocator.dupe(u8, found.text(2)), .refresh_jti = try allocator.dupe(u8, found.text(3)), .access_expires_at = found.int(4), .refresh_expires_at = found.int(5), .replayed = found.int(6) != 0, }; } /// rotates only while `old_refresh_jti` is still the row's current refresh jti; /// a request that lost the race sees `InvalidRefreshSession` and should re-read /// the row, which now reports the winner's successor as a replay pub fn rotateSessionToken( id: []const u8, old_refresh_jti: []const u8, new_access_jti: []const u8, new_refresh_jti: []const u8, access_expires_at: i64, refresh_expires_at: i64, previous_refresh_grace_secs: i64, ) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE session_tokens \\SET access_jti = ?, refresh_jti = ?, access_expires_at = ?, refresh_expires_at = ?, \\ previous_refresh_jti = ?, previous_refresh_expires_at = unixepoch() + ?, \\ updated_at = unixepoch(), last_used_at = unixepoch() \\WHERE id = ? AND refresh_jti = ? AND revoked_at IS NULL , .{ new_access_jti, new_refresh_jti, access_expires_at, refresh_expires_at, old_refresh_jti, previous_refresh_grace_secs, id, old_refresh_jti }); if (conn.changes() == 0) return error.InvalidRefreshSession; } pub fn revokeSessionToken(did: []const u8, jti: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE session_tokens \\SET revoked_at = unixepoch(), updated_at = unixepoch() \\WHERE did = ? AND (access_jti = ? OR refresh_jti = ?) , .{ did, jti, jti }); } pub fn recordAuditEvent( subject_did: []const u8, actor_did: []const u8, controller_did: ?[]const u8, action: []const u8, details_json: []const u8, ) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const id = try randomToken(std.heap.page_allocator, "aud-", 16); defer std.heap.page_allocator.free(id); try conn.exec( \\INSERT INTO account_audit_log \\ (id, subject_did, actor_did, controller_did, action, details_json) \\VALUES (?, ?, ?, ?, ?, ?) , .{ id, subject_did, actor_did, controller_did, action, details_json }); } pub fn putWebAuthnChallenge(did: []const u8, kind: []const u8, challenge: []const u8, state_json: []const u8, expires_at: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const id = try randomToken(std.heap.page_allocator, "wan-", 16); defer std.heap.page_allocator.free(id); try conn.exec("DELETE FROM webauthn_challenges WHERE did = ? AND kind = ?", .{ did, kind }); try conn.exec( \\INSERT INTO webauthn_challenges (id, did, challenge, kind, state_json, expires_at) \\VALUES (?, ?, ?, ?, ?, ?) , .{ id, did, challenge, kind, state_json, expires_at }); } pub fn getWebAuthnChallenge(allocator: std.mem.Allocator, did: []const u8, kind: []const u8) !?WebAuthnChallenge { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT id, did, challenge, kind, state_json, expires_at \\FROM webauthn_challenges \\WHERE did = ? AND kind = ? , .{ did, kind }); if (row == null) return null; defer row.?.deinit(); return .{ .id = try allocator.dupe(u8, row.?.text(0)), .did = try allocator.dupe(u8, row.?.text(1)), .challenge = try allocator.dupe(u8, row.?.text(2)), .kind = try allocator.dupe(u8, row.?.text(3)), .state_json = try allocator.dupe(u8, row.?.text(4)), .expires_at = row.?.int(5), }; } pub fn deleteWebAuthnChallenge(did: []const u8, kind: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM webauthn_challenges WHERE did = ? AND kind = ?", .{ did, kind }); } pub fn putDiscoverableWebAuthnChallenge(request_id: []const u8, challenge: []const u8, expires_at: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM webauthn_discoverable_challenges WHERE request_id = ?", .{request_id}); try conn.exec( \\INSERT INTO webauthn_discoverable_challenges (request_id, challenge, expires_at) \\VALUES (?, ?, ?) , .{ request_id, challenge, expires_at }); } pub fn getDiscoverableWebAuthnChallenge(allocator: std.mem.Allocator, request_id: []const u8) !?DiscoverableWebAuthnChallenge { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT request_id, challenge, expires_at \\FROM webauthn_discoverable_challenges \\WHERE request_id = ? , .{request_id}); if (row == null) return null; defer row.?.deinit(); return .{ .request_id = try allocator.dupe(u8, row.?.text(0)), .challenge = try allocator.dupe(u8, row.?.text(1)), .expires_at = row.?.int(2), }; } pub fn deleteDiscoverableWebAuthnChallenge(request_id: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM webauthn_discoverable_challenges WHERE request_id = ?", .{request_id}); } pub fn savePasskey(allocator: std.mem.Allocator, did: []const u8, credential_id: []const u8, public_key: []const u8, sign_count: u32, friendly_name: ?[]const u8) ![]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const id = try randomToken(allocator, "pk-", 16); try conn.exec( \\INSERT INTO passkeys (id, did, credential_id, public_key, sign_count, friendly_name) \\VALUES (?, ?, ?, ?, ?, ?) , .{ id, did, zqlite.blob(credential_id), zqlite.blob(public_key), @as(i64, @intCast(sign_count)), friendly_name }); return id; } pub fn getPasskeyByCredentialId(allocator: std.mem.Allocator, credential_id: []const u8) !?Passkey { const reader = try acquireReader(); defer reader.release(); const row = try reader.db.row( \\SELECT id, did, credential_id, public_key, sign_count, friendly_name, created_at, last_used \\FROM passkeys \\WHERE credential_id = ? , .{zqlite.blob(credential_id)}); if (row == null) return null; defer row.?.deinit(); return try passkeyFromRow(allocator, row.?); } pub fn listPasskeys(allocator: std.mem.Allocator, did: []const u8) ![]Passkey { const reader = try acquireReader(); defer reader.release(); var rows = try reader.db.rows( \\SELECT id, did, credential_id, public_key, sign_count, friendly_name, created_at, last_used \\FROM passkeys \\WHERE did = ? \\ORDER BY created_at DESC , .{did}); defer rows.deinit(); var out: std.ArrayList(Passkey) = .empty; while (rows.next()) |row| try out.append(allocator, try passkeyFromRow(allocator, row)); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } pub fn updatePasskeyUse(id: []const u8, sign_count: u32) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE passkeys \\SET sign_count = ?, last_used = unixepoch() \\WHERE id = ? , .{ @as(i64, @intCast(sign_count)), id }); } pub fn deletePasskey(did: []const u8, id: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM passkeys WHERE did = ? AND id = ?", .{ did, id }); } pub fn updatePasskeyName(did: []const u8, id: []const u8, friendly_name: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\UPDATE passkeys \\SET friendly_name = ? \\WHERE did = ? AND id = ? , .{ friendly_name, did, id }); } fn passkeyFromRow(allocator: std.mem.Allocator, row: zqlite.Row) !Passkey { return .{ .id = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .credential_id = try allocator.dupe(u8, row.blob(2)), .public_key = try allocator.dupe(u8, row.blob(3)), .sign_count = @intCast(row.int(4)), .friendly_name = if (row.nullableText(5)) |text| try allocator.dupe(u8, text) else null, .created_at = row.int(6), .last_used = row.nullableInt(7), }; } pub fn put( allocator: std.mem.Allocator, account: auth.Account, collection: []const u8, rkey: []const u8, value: std.json.Value, ) !Record { const result = try applyWrites(allocator, account, &.{.{ .update = .{ .collection = collection, .rkey = rkey, .value = value, } }}); if (result.records.len == 0) return Error.MissingRecord; return result.records[0]; } pub fn delete(allocator: std.mem.Allocator, account: auth.Account, collection: []const u8, rkey: []const u8) !CommitInfo { const result = try applyWrites(allocator, account, &.{.{ .delete = .{ .collection = collection, .rkey = rkey, } }}); return result.commit; } pub fn applyWrites(allocator: std.mem.Allocator, account: auth.Account, ops: []const WriteOp) !WriteResult { return applyWritesWithOptions(allocator, account, ops, .{}); } pub fn applyWritesWithOptions(allocator: std.mem.Allocator, account: auth.Account, ops: []const WriteOp, options: WriteOptions) !WriteResult { return applyWritesMeasured(allocator, account, ops, options, null); } pub fn initializeRepo(allocator: std.mem.Allocator, account: auth.Account) !void { _ = try applyWritesWithOptions(allocator, account, &.{}, .{ .allow_empty_commit = true, .initialize_only = true }); } pub fn applyWritesProfiled(allocator: std.mem.Allocator, account: auth.Account, ops: []const WriteOp, profile: *WriteProfile) !WriteResult { profile.* = .{}; return applyWritesMeasured(allocator, account, ops, .{}, profile); } fn applyWritesMeasured(allocator: std.mem.Allocator, account: auth.Account, ops: []const WriteOp, options: WriteOptions, profile: ?*WriteProfile) !WriteResult { const total_start = monotonicNs(); if (ops.len == 0 and !options.allow_empty_commit) return Error.MissingRecord; if (ops.len > max_commit_ops) return Error.TooManyWrites; const validation_start = monotonicNs(); for (ops) |op| switch (op) { .create => |create_op| { if (zat.Nsid.parse(create_op.collection) == null) return Error.InvalidCollection; if (create_op.rkey) |rkey| if (zat.Rkey.parse(rkey) == null) return Error.InvalidRecordKey; try validateRecordForWrite(create_op.collection, create_op.rkey, create_op.value, options.validate); }, .update => |update_op| { if (zat.Nsid.parse(update_op.collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(update_op.rkey) == null) return Error.InvalidRecordKey; try validateRecordForWrite(update_op.collection, update_op.rkey, update_op.value, options.validate); }, .delete => |delete_op| { if (zat.Nsid.parse(delete_op.collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(delete_op.rkey) == null) return Error.InvalidRecordKey; }, }; addProfile(profile, .validation_ns, elapsedNs(validation_start)); const lock_start = monotonicNs(); const lane = write_lanes.lock(store_io, account.did); defer lane.unlock(); addProfile(profile, .lock_wait_ns, elapsedNs(lock_start)); try requireInitialized(); const load_start = monotonicNs(); const loaded = blk: { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); const current = try latestCommitRawLocked(allocator, account.did); if (options.initialize_only) { if (current) |root| return .{ .commit = .{ .cid = root.commit_cid_text, .rev = root.rev }, .records = &.{}, }; } const seq = try nextSeqLocked(); const resolved_ops = try resolveWriteOpsLocked(allocator, ops); try requireWriteSwapsLocked(allocator, account.did, current, options, resolved_ops); break :blk .{ .seq = seq, .current = current, .rev = try revForSeq(allocator, seq, if (current) |root| root.rev else null), .keypair = try signingKeypairLocked(account.did), .ops = resolved_ops, }; }; const seq = loaded.seq; const current = loaded.current; const rev = loaded.rev; const keypair = loaded.keypair; const resolved_ops = loaded.ops; // each op's prev comes from the staged tree, not the index, so a batch // that touches one key twice reports what that op actually replaced const prev_record_cids = try allocator.alloc(?[]const u8, resolved_ops.len); var repo_block_reader = RepoBlockReader{ .allocator = allocator, .did = account.did, }; var tree = if (current) |root| try zat.mst.Mst.loadLazy(allocator, root.data_cid_raw, repo_block_reader.reader()) else zat.mst.Mst.init(allocator); addProfile(profile, .load_repo_ns, elapsedNs(load_start)); var records: std.ArrayList(Record) = .empty; var record_blocks: std.ArrayList(ImportedBlock) = .empty; var mst_blocks: std.ArrayList(zat.car.Block) = .empty; var blob_refs: std.ArrayList(BlobRef) = .empty; // half-open [start, end) into blob_refs for each staged record, parallel to // `records`. stageRecordWrite appends a record's refs contiguously, so the // index pass can find them without rescanning the whole list per record. var record_ref_spans: std.ArrayList(struct { start: usize, end: usize }) = .empty; const stage_start = monotonicNs(); for (resolved_ops, 0..) |op, op_idx| switch (op) { .create => |create_op| { const rkey = create_op.rkey orelse return Error.InvalidRecordKey; const refs_start = blob_refs.items.len; const record = try stageRecordWrite(allocator, &tree, account, create_op.collection, rkey, create_op.value, options.validate, rev, seq, &record_blocks, &blob_refs, .create, &prev_record_cids[op_idx]); try records.append(allocator, record); try record_ref_spans.append(allocator, .{ .start = refs_start, .end = blob_refs.items.len }); }, .update => |update_op| { const refs_start = blob_refs.items.len; const record = try stageRecordWrite(allocator, &tree, account, update_op.collection, update_op.rkey, update_op.value, options.validate, rev, seq, &record_blocks, &blob_refs, .update, &prev_record_cids[op_idx]); try records.append(allocator, record); try record_ref_spans.append(allocator, .{ .start = refs_start, .end = blob_refs.items.len }); }, .delete => |delete_op| { const path = try repoPath(allocator, delete_op.collection, delete_op.rkey); // a delete op must name a record the repo holds: sync 1.1 requires // its prev, and the reference PDS's MST refuses the same delete const previous = (try tree.deleteReturn(path)) orelse return Error.RecordNotFound; prev_record_cids[op_idx] = try zat.cbor.Cid.toString(.{ .raw = previous.raw }, allocator); }, }; addProfile(profile, .stage_records_ns, elapsedNs(stage_start)); const build_start = monotonicNs(); const data_cid = try tree.rootCid(); if (current) |root| try loadInversionProof(allocator, &tree, account.did, root.data_cid_raw, data_cid, resolved_ops, records.items, prev_record_cids); try writeMstBlocks(&tree, &mst_blocks); const commit = try signedCommitWithKeypair(allocator, account.did, rev, data_cid, if (current) |root| root.commit_cid_raw else null, &keypair); const event_car = try commitEventCar(allocator, commit.cid, commit.data, record_blocks.items, mst_blocks.items); if (event_car.len > max_commit_event_block_bytes) return Error.CommitTooLarge; addProfile(profile, .build_commit_ns, elapsedNs(build_start)); const sql_start = monotonicNs(); db_mutex.lockUncancelable(store_io); var db_locked = true; defer if (db_locked) db_mutex.unlock(store_io); try conn.exclusiveTransaction(); errdefer conn.rollback(); // The reference PDS refuses a record that names a blob this account never // uploaded or one under takedown (actor-store/blob/transactor.ts // verifyBlobAndMakePermanent). Checking inside the transaction keeps blob // GC from removing it before the reference is recorded. importRepo takes // its own path, where blobs may arrive after the records naming them. for (blob_refs.items) |ref| { if (try accountBlobAvailableLocked(account.did, ref.cid)) continue; if (options.missing_blob) |out| out.* = ref.cid; return Error.BlobNotFound; } for (record_blocks.items) |block| { try conn.exec( \\INSERT INTO repo_blocks (did, cid, data, repo_rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET \\ data = excluded.data, \\ repo_rev = COALESCE(repo_blocks.repo_rev, excluded.repo_rev) , .{ account.did, block.cid, zqlite.blob(block.data), rev }); } for (mst_blocks.items) |block| { try conn.exec( \\INSERT INTO repo_blocks (did, cid, data, repo_rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET \\ data = excluded.data, \\ repo_rev = COALESCE(repo_blocks.repo_rev, excluded.repo_rev) , .{ account.did, try zat.cbor.Cid.toString(.{ .raw = block.cid_raw }, allocator), zqlite.blob(block.data), rev }); } try conn.exec( \\INSERT INTO repo_blocks (did, cid, data, repo_rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET \\ data = excluded.data, \\ repo_rev = COALESCE(repo_blocks.repo_rev, excluded.repo_rev) , .{ account.did, commit.cid, zqlite.blob(commit.data), rev }); // Index the batch in op order, the way the tree was mutated. Applying every // delete before every create would let a same-key create+delete leave the row // behind while the MST says it is gone. The reference PDS walks its writes in // one ordered loop (actor-store/repo/transactor.ts indexWrites) and tranquil // resolves each delete against the running tree; this matches both. var staged_idx: usize = 0; for (resolved_ops) |op| switch (op) { .delete => |delete_op| { const uri = try std.fmt.allocPrint(allocator, "at://{s}/{s}/{s}", .{ account.did, delete_op.collection, delete_op.rkey }); try conn.exec("DELETE FROM expected_blobs WHERE record_uri = ?", .{uri}); try conn.exec("DELETE FROM records WHERE did = ? AND collection = ? AND rkey = ?", .{ account.did, delete_op.collection, delete_op.rkey }); }, else => { const record = records.items[staged_idx]; staged_idx += 1; const uri = try record.uri(allocator); try conn.exec( \\INSERT INTO records (did, collection, rkey, uri, cid, rev, seq) \\VALUES (?, ?, ?, ?, ?, ?, ?) \\ON CONFLICT(did, collection, rkey) DO UPDATE SET \\ uri = excluded.uri, \\ cid = excluded.cid, \\ rev = excluded.rev, \\ seq = excluded.seq , .{ record.did, record.collection, record.rkey, uri, record.cid, record.rev, @as(i64, @intCast(seq)) }); try conn.exec("DELETE FROM expected_blobs WHERE record_uri = ?", .{uri}); const span = record_ref_spans.items[staged_idx - 1]; for (blob_refs.items[span.start..span.end]) |ref| { try conn.exec( \\INSERT INTO expected_blobs (blob_cid, record_uri) \\VALUES (?, ?) \\ON CONFLICT(blob_cid, record_uri) DO UPDATE SET blob_cid = excluded.blob_cid , .{ ref.cid, uri }); } }, }; std.debug.assert(staged_idx == records.items.len); try conn.exec( \\INSERT INTO commits (seq, did, cid, rev, prev) \\VALUES (?, ?, ?, ?, ?) , .{ @as(i64, @intCast(seq)), account.did, commit.cid, rev, if (current) |root| root.commit_cid_text else null }); const publish_event = try accountActiveLocked(account.did); if (publish_event) { const event_frame = try commitEventFrame( allocator, seq, account.did, commit.cid, rev, if (current) |root| root.rev else null, if (current) |root| root.data_cid_raw else null, event_car, resolved_ops, records.items, prev_record_cids, ); try conn.exec( \\INSERT INTO seq_events (seq, did, commit_cid, evt) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(seq) DO UPDATE SET \\ did = excluded.did, \\ commit_cid = excluded.commit_cid, \\ evt = excluded.evt , .{ @as(i64, @intCast(seq)), account.did, commit.cid, zqlite.blob(event_frame) }); } try conn.commit(); db_mutex.unlock(store_io); db_locked = false; addProfile(profile, .sql_ns, elapsedNs(sql_start)); const publish_start = monotonicNs(); if (publish_event) eventlog.publish(seq); addProfile(profile, .event_publish_ns, elapsedNs(publish_start)); if (profile) |p| p.total_ns = elapsedNs(total_start); return .{ .commit = .{ .cid = commit.cid, .rev = rev }, .records = try records.toOwnedSlice(allocator), }; } /// Make the commit's blocks carry everything a consumer needs to invert it. /// /// Sync 1.1 consumers check a commit by undoing its ops on the blocks it /// ships and comparing the result with prevData. The lazily loaded tree only /// holds the nodes the forward write touched; undoing a delete can need more, /// such as the untouched subtree beside the key, which the inverse insert has /// to split. Undoing the ops on the tree itself and redoing them loads exactly /// those nodes, and `collectBlocks` emits every loaded node. The tree ends where /// it started, which is checked. fn loadInversionProof( allocator: std.mem.Allocator, tree: *zat.mst.Mst, did: []const u8, prev_data_raw: []const u8, data_cid: zat.cbor.Cid, ops: []const WriteOp, records: []const Record, prev_record_cids: []const ?[]const u8, ) !void { var mst_ops = try allocator.alloc(zat.mst.Operation, ops.len); var record_idx: usize = 0; for (ops, 0..) |op, idx| { const collection, const rkey, const value: ?[]const u8 = switch (op) { .create => |create_op| blk: { const record = records[record_idx]; record_idx += 1; break :blk .{ create_op.collection, record.rkey, record.cid }; }, .update => |update_op| blk: { const record = records[record_idx]; record_idx += 1; break :blk .{ update_op.collection, update_op.rkey, record.cid }; }, .delete => |delete_op| .{ delete_op.collection, delete_op.rkey, null }, }; mst_ops[idx] = .{ .path = try repoPath(allocator, collection, rkey), .value = if (value) |cid| (try zat.cbor.Cid.fromString(allocator, cid)).raw else null, .prev = if (prev_record_cids[idx]) |cid| (try zat.cbor.Cid.fromString(allocator, cid)).raw else null, }; } var undo = std.mem.reverseIterator(mst_ops); while (undo.next()) |op| try zat.mst.invertOp(tree, op); const undone = try tree.rootCid(); if (!std.mem.eql(u8, undone.raw, prev_data_raw)) { // the write itself is sound; only consumers' inversion check would // fail, so say so loudly rather than refuse the write log.err("commit inversion does not reach prevData did={s}\n", .{did}); } for (mst_ops) |op| { if (op.value) |value| { _ = try tree.putReturn(op.path, .{ .raw = value }); } else { _ = try tree.deleteReturn(op.path); } } const redone = try tree.rootCid(); if (!std.mem.eql(u8, redone.raw, data_cid.raw)) return Error.CommitTreeMismatch; } const ProfileField = enum { validation_ns, lock_wait_ns, load_repo_ns, stage_records_ns, build_commit_ns, sql_ns, event_publish_ns, }; fn addProfile(profile: ?*WriteProfile, field: ProfileField, ns: u64) void { const p = profile orelse return; switch (field) { .validation_ns => p.validation_ns += ns, .lock_wait_ns => p.lock_wait_ns += ns, .load_repo_ns => p.load_repo_ns += ns, .stage_records_ns => p.stage_records_ns += ns, .build_commit_ns => p.build_commit_ns += ns, .sql_ns => p.sql_ns += ns, .event_publish_ns => p.event_publish_ns += ns, } } fn monotonicNs() u64 { return clock.monotonicNs(); } fn elapsedNs(start: u64) u64 { const now = monotonicNs(); return if (now >= start) now - start else 0; } pub fn revForSeq(allocator: std.mem.Allocator, seq: u64, current_rev: ?[]const u8) ![]const u8 { var timestamp_us = nowMicros(); if (current_rev) |rev| { const current_timestamp = if (zat.Tid.parse(rev)) |tid| tid.timestamp() else 0; if (timestamp_us <= current_timestamp) timestamp_us = current_timestamp + 1; } const tid = zat.Tid.fromTimestamp(timestamp_us, @intCast(seq % 1024)).str(); return allocator.dupe(u8, &tid); } pub fn nowMicros() u64 { return @intCast(@max(0, @divTrunc(clock.nowNanos(), std.time.ns_per_us))); } pub fn get(allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8) ?Record { const reader = acquireReader() catch return null; defer reader.release(); const row = reader.db.row( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? AND r.rkey = ? \\ AND NOT EXISTS (SELECT 1 FROM record_takedowns t WHERE t.record_uri = r.uri) , .{ did, collection, rkey }) catch return null; if (row == null) return null; defer row.?.deinit(); return recordFromRow(row.?, allocator) catch null; } pub fn getCid(allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8) ?[]const u8 { const reader = acquireReader() catch return null; defer reader.release(); const row = reader.db.row( \\SELECT cid \\FROM records \\WHERE did = ? AND collection = ? AND rkey = ? , .{ did, collection, rkey }) catch return null; if (row == null) return null; defer row.?.deinit(); return allocator.dupe(u8, row.?.text(0)) catch null; } pub const RecordBlock = struct { cid: []const u8, data: []const u8, }; pub fn getRecordBlock(allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8) ?RecordBlock { const reader = acquireReader() catch return null; defer reader.release(); const row = reader.db.row( \\SELECT r.cid, rb.data \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? AND r.rkey = ? , .{ did, collection, rkey }) catch return null; if (row == null) return null; defer row.?.deinit(); return .{ .cid = allocator.dupe(u8, row.?.text(0)) catch return null, .data = allocator.dupe(u8, row.?.nullableBlob(1) orelse return null) catch return null, }; } pub fn listRecords( allocator: std.mem.Allocator, did: []const u8, collection: []const u8, limit: usize, ) ![]Record { const reader = try acquireReader(); defer reader.release(); var rows = try reader.db.rows( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? \\ORDER BY r.seq DESC \\LIMIT ? , .{ did, collection, @as(i64, @intCast(if (limit == 0) 100 else limit)) }); defer rows.deinit(); var records: std.ArrayList(Record) = .empty; while (rows.next()) |row| { try records.append(allocator, try recordFromRow(row, allocator)); } if (rows.err) |err| return err; return records.toOwnedSlice(allocator); } pub fn count(did: []const u8, collection: []const u8) usize { const reader = acquireReader() catch return 0; defer reader.release(); const row = reader.db.row("SELECT count(*) FROM records WHERE did = ? AND collection = ?", .{ did, collection }) catch return 0; if (row == null) return 0; defer row.?.deinit(); return @intCast(row.?.int(0)); } pub fn listCollectionsJson(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { const reader = try acquireReader(); defer reader.release(); var rows = try reader.db.rows( \\SELECT DISTINCT collection \\FROM records \\WHERE did = ? \\ORDER BY collection , .{did}); defer rows.deinit(); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.writeByte('['); var first = true; while (rows.next()) |row| { if (!first) try out.writer.writeByte(','); first = false; try out.writer.print("{f}", .{std.json.fmt(row.text(0), .{})}); } if (rows.err) |err| return err; try out.writer.writeByte(']'); return out.toOwnedSlice(); } pub fn getAppPreferences(allocator: std.mem.Allocator, did: []const u8, namespace: []const u8) ![]AppPreference { const reader = try acquireReader(); defer reader.release(); const like_pattern = try std.fmt.allocPrint(allocator, "{s}.%", .{namespace}); var rows = try reader.db.rows( \\SELECT name, value_json \\FROM app_preferences \\WHERE did = ? AND (name = ? OR name LIKE ?) \\ORDER BY id ASC , .{ did, namespace, like_pattern }); defer rows.deinit(); var preferences: std.ArrayList(AppPreference) = .empty; while (rows.next()) |row| { try preferences.append(allocator, .{ .name = try allocator.dupe(u8, row.text(0)), .value_json = try allocator.dupe(u8, row.text(1)), }); } if (rows.err) |err| return err; return preferences.toOwnedSlice(allocator); } pub fn replaceAppPreferences( allocator: std.mem.Allocator, did: []const u8, namespace: []const u8, preferences: []const AppPreference, ) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const like_pattern = try std.fmt.allocPrint(allocator, "{s}.%", .{namespace}); try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec( \\DELETE FROM app_preferences \\WHERE did = ? AND (name = ? OR name LIKE ?) , .{ did, namespace, like_pattern }); for (preferences) |preference| { try conn.exec( \\INSERT INTO app_preferences (did, name, value_json, updated_at) \\VALUES (?, ?, ?, unixepoch()) , .{ did, preference.name, preference.value_json }); } try conn.commit(); } pub fn putBlob( allocator: std.mem.Allocator, io: Io, account: auth.Account, data: []const u8, mime_type: []const u8, ) ![]const u8 { var reader: std.Io.Reader = .fixed(data); return (try putBlobReader(allocator, io, account, &reader, data.len, data.len, mime_type)).cid; } pub const StoredBlob = struct { cid: []const u8, size: usize, }; pub fn putBlobReader( allocator: std.mem.Allocator, io: Io, account: auth.Account, reader: *std.Io.Reader, expected_size: usize, max_size: usize, mime_type: []const u8, ) !StoredBlob { try requireInitialized(); const staged = try blobstore.stageUpload(allocator, io, account.did, reader, expected_size, max_size); errdefer blobstore.discard(staged.temp_path); const blob_cid = try zat.cbor.Cid.fromSha256Digest(allocator, zat.cbor.Codec.raw, &staged.digest); defer allocator.free(blob_cid.raw); const cid = try blob_cid.toString(allocator); if (try blobTakedown(allocator, account.did, cid) != null) return error.BlobTakendown; { const lane = blob_lanes.lock(store_io, cid); defer lane.unlock(); try blobstore.promote(allocator, staged.temp_path, account.did, cid); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( \\INSERT INTO blobs (cid, did, mime_type, size) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET \\ mime_type = excluded.mime_type, \\ size = excluded.size, \\ created_at = unixepoch() , .{ cid, account.did, mime_type, @as(i64, @intCast(staged.size)) }); } return .{ .cid = cid, .size = staged.size }; } const BlobGcCandidate = struct { cid: []const u8 }; pub fn collectBlobGarbage( allocator: std.mem.Allocator, grace_seconds: i64, limit: usize, ) !BlobGcResult { if (limit == 0) return .{}; var dids: std.ArrayList([]const u8) = .empty; { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT DISTINCT b.did \\FROM blobs b \\WHERE NOT EXISTS ( \\ SELECT 1 FROM expected_blobs eb \\ JOIN records r ON r.uri = eb.record_uri \\ WHERE r.did = b.did AND eb.blob_cid = b.cid \\) \\AND NOT EXISTS ( \\ SELECT 1 FROM permissioned_space_record_blobs psrb \\ WHERE psrb.repo_did = b.did AND psrb.blob_cid = b.cid \\) \\AND b.created_at <= unixepoch() - ? \\ORDER BY b.did , .{grace_seconds}); defer rows.deinit(); while (rows.next()) |row| try dids.append(allocator, try allocator.dupe(u8, row.text(0))); if (rows.err) |err| return err; } var result: BlobGcResult = .{}; for (dids.items) |did| { if (result.deleted + result.failed >= limit) break; { const lane = write_lanes.lock(store_io, did); defer lane.unlock(); const account_result = try collectBlobGarbageForDidInLane( allocator, did, grace_seconds, limit - result.deleted - result.failed, ); result.deleted += account_result.deleted; result.bytes += account_result.bytes; result.failed += account_result.failed; } } return result; } fn collectBlobGarbageForDidInLane( allocator: std.mem.Allocator, did: []const u8, grace_seconds: i64, limit: usize, ) !BlobGcResult { if (limit == 0) return .{}; var candidates: std.ArrayList(BlobGcCandidate) = .empty; { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT b.cid \\FROM blobs b \\WHERE b.did = ? \\AND NOT EXISTS ( \\ SELECT 1 FROM expected_blobs eb \\ JOIN records r ON r.uri = eb.record_uri \\ WHERE r.did = b.did AND eb.blob_cid = b.cid \\) \\AND NOT EXISTS ( \\ SELECT 1 FROM permissioned_space_record_blobs psrb \\ WHERE psrb.repo_did = b.did AND psrb.blob_cid = b.cid \\) \\AND b.created_at <= unixepoch() - ? \\ORDER BY b.created_at, b.cid \\LIMIT ? , .{ did, grace_seconds, @as(i64, @intCast(limit)) }); defer rows.deinit(); while (rows.next()) |row| { try candidates.append(allocator, .{ .cid = try allocator.dupe(u8, row.text(0)), }); } if (rows.err) |err| return err; } var result: BlobGcResult = .{}; for (candidates.items) |candidate| { const blob_lane = blob_lanes.lock(store_io, candidate.cid); defer blob_lane.unlock(); const size = blk: { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); const row = try conn.row( \\SELECT b.size \\FROM blobs b \\WHERE b.did = ? AND b.cid = ? \\AND NOT EXISTS ( \\ SELECT 1 FROM expected_blobs eb \\ JOIN records r ON r.uri = eb.record_uri \\ WHERE r.did = b.did AND eb.blob_cid = b.cid \\) \\AND NOT EXISTS ( \\ SELECT 1 FROM permissioned_space_record_blobs psrb \\ WHERE psrb.repo_did = b.did AND psrb.blob_cid = b.cid \\) \\AND b.created_at <= unixepoch() - ? , .{ did, candidate.cid, grace_seconds }); const found = row orelse continue; defer found.deinit(); break :blk @as(u64, @intCast(found.int(0))); }; blobstore.delete(allocator, did, candidate.cid) catch { result.failed += 1; continue; }; { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try conn.exec( \\DELETE FROM blobs \\WHERE did = ? AND cid = ? \\AND NOT EXISTS ( \\ SELECT 1 FROM expected_blobs eb \\ JOIN records r ON r.uri = eb.record_uri \\ WHERE r.did = blobs.did AND eb.blob_cid = blobs.cid \\) \\AND NOT EXISTS ( \\ SELECT 1 FROM permissioned_space_record_blobs psrb \\ WHERE psrb.repo_did = blobs.did AND psrb.blob_cid = blobs.cid \\) , .{ did, candidate.cid }); } result.deleted += 1; result.bytes += size; } return result; } pub fn startBlobGarbageCollector() void { if (blob_gc_worker_started.cmpxchgStrong(false, true, .acq_rel, .acquire) != null) return; const thread = std.Thread.spawn(.{}, collectBlobGarbageThread, .{}) catch |err| { blob_gc_worker_started.store(false, .release); log.err("blob gc spawn failed error={s}\n", .{@errorName(err)}); return; }; thread.detach(); } fn collectBlobGarbageThread() void { while (true) { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); const result = collectBlobGarbage(arena.allocator(), blob_gc_grace_seconds, blob_gc_batch) catch |err| blk: { log.err("blob gc failed error={s}\n", .{@errorName(err)}); break :blk null; }; if (result) |summary| { if (summary.deleted > 0 or summary.failed > 0) { log.info("blob gc deleted={d} bytes={d} failed={d}\n", .{ summary.deleted, summary.bytes, summary.failed }); } } arena.deinit(); store_io.sleep(Io.Duration.fromSeconds(blob_gc_interval_seconds), .awake) catch {}; } } pub const BlobToMirror = struct { rowid: i64, did: []const u8, cid: []const u8, size: usize }; /// Blobs the mirror bucket has no copy of yet (`storage = 'disk'`), in row /// order after `after_rowid`. pub fn blobsToMirror(allocator: std.mem.Allocator, after_rowid: i64, limit: usize) ![]BlobToMirror { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT rowid, did, cid, size FROM blobs \\WHERE storage = 'disk' AND rowid > ? \\ORDER BY rowid \\LIMIT ? , .{ after_rowid, @as(i64, @intCast(limit)) }); defer rows.deinit(); var out: std.ArrayList(BlobToMirror) = .empty; while (rows.next()) |row| try out.append(allocator, .{ .rowid = row.int(0), .did = try allocator.dupe(u8, row.text(1)), .cid = try allocator.dupe(u8, row.text(2)), .size = @intCast(@max(row.int(3), 0)), }); if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// Records that the bucket holds a copy of the blob. False when the blob's /// row is gone (it was deleted while it uploaded), so the caller removes the /// copy it just made. pub fn markBlobMirrored(did: []const u8, cid: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE blobs SET storage = 's3' WHERE did = ? AND cid = ?", .{ did, cid }); return conn.changes() > 0; } pub fn blobExists(did: []const u8, cid: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row("SELECT 1 FROM blobs WHERE did = ? AND cid = ?", .{ did, cid })) orelse return false; row.deinit(); return true; } pub const BlobMirrorCounts = struct { mirrored: u64 = 0, pending: u64 = 0 }; pub fn blobMirrorCounts() !BlobMirrorCounts { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row( "SELECT coalesce(sum(storage = 's3'), 0), coalesce(sum(storage = 'disk'), 0) FROM blobs", .{}, )).?; defer row.deinit(); return .{ .mirrored = @intCast(row.int(0)), .pending = @intCast(row.int(1)) }; } pub const SubjectTakedown = struct { applied: bool, ref: ?[]const u8, }; pub const RecordTakedownStatus = struct { cid: []const u8, takedown: SubjectTakedown, }; /// Take down (`applied`) or restore one record. Its takedown ends when the /// record is deleted, as on the reference PDS; updates keep it. pub fn setRecordTakedown(uri: []const u8, applied: bool, ref: ?[]const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (!applied) return conn.exec("DELETE FROM record_takedowns WHERE record_uri = ?", .{uri}); const exists = try conn.row("SELECT 1 FROM records WHERE uri = ?", .{uri}); if (exists) |row| row.deinit() else return error.SubjectNotFound; try conn.exec( \\INSERT INTO record_takedowns (record_uri, ref) VALUES (?, ?) \\ON CONFLICT(record_uri) DO UPDATE SET ref = excluded.ref , .{ uri, ref }); } pub fn recordTakedownStatus(allocator: std.mem.Allocator, uri: []const u8) !?RecordTakedownStatus { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row( \\SELECT r.cid, t.record_uri IS NOT NULL, t.ref \\FROM records r \\LEFT JOIN record_takedowns t ON t.record_uri = r.uri \\WHERE r.uri = ? , .{uri})) orelse return null; defer row.deinit(); return .{ .cid = try allocator.dupe(u8, row.text(0)), .takedown = .{ .applied = row.int(1) != 0, .ref = if (row.nullableText(2)) |ref| try allocator.dupe(u8, ref) else null, }, }; } /// Take down (`applied`) or restore one blob of an account. The blob stays on /// disk but is not served, from the public or the space blob routes, and the /// account cannot upload it again until the takedown is lifted. pub fn setBlobTakedown(did: []const u8, cid: []const u8, applied: bool, ref: ?[]const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (!applied) return conn.exec("DELETE FROM blob_takedowns WHERE did = ? AND cid = ?", .{ did, cid }); const exists = try conn.row("SELECT 1 FROM accounts WHERE did = ?", .{did}); if (exists) |row| row.deinit() else return error.SubjectNotFound; try conn.exec( \\INSERT INTO blob_takedowns (did, cid, ref) VALUES (?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET ref = excluded.ref , .{ did, cid, ref }); } /// The takedown of a blob, or null when the blob is not taken down. pub fn blobTakedown(allocator: std.mem.Allocator, did: []const u8, cid: []const u8) !?SubjectTakedown { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row("SELECT ref FROM blob_takedowns WHERE did = ? AND cid = ?", .{ did, cid })) orelse return null; defer row.deinit(); return .{ .applied = true, .ref = if (row.nullableText(0)) |ref| try allocator.dupe(u8, ref) else null }; } pub const AccountAdminStatus = struct { takedown: SubjectTakedown, deactivated: bool, }; pub fn accountAdminStatus(allocator: std.mem.Allocator, did: []const u8) !?AccountAdminStatus { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row( \\SELECT account_status, account_status_ref, deactivated_beneath_takedown \\FROM accounts \\WHERE did = ? , .{did})) orelse return null; defer row.deinit(); const status = row.nullableText(0) orelse "active"; const takendown = std.mem.eql(u8, status, "takendown"); return .{ .takedown = .{ .applied = takendown, .ref = if (takendown) if (row.nullableText(1)) |ref| try allocator.dupe(u8, ref) else null else null, }, .deactivated = std.mem.eql(u8, status, "deactivated") or (takendown and row.int(2) != 0), }; } pub fn getBlob( allocator: std.mem.Allocator, did: []const u8, cid: []const u8, ) ?BlobRecord { const metadata = blobMetadata(allocator, did, cid, false) orelse return null; return .{ .mime_type = metadata.mime_type, .data = blobstore.get(allocator, did, cid, metadata.size + 1) catch return null, }; } pub fn permissionedBlobReferenced(space: []const u8, repo_did: []const u8, cid: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( \\SELECT 1 \\FROM permissioned_space_record_blobs \\WHERE space = ? AND repo_did = ? AND blob_cid = ? \\LIMIT 1 , .{ space, repo_did, cid }); if (row) |found| { found.deinit(); return true; } return false; } pub fn listPermissionedBlobs( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, cursor: ?[]const u8, limit: usize, ) ![][]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const capped_limit: i64 = @intCast(@min(if (limit == 0) 500 else limit, 1000)); var rows = if (cursor) |after| try conn.rows( \\SELECT DISTINCT blob_cid \\FROM permissioned_space_record_blobs \\WHERE space = ? AND repo_did = ? AND blob_cid > ? \\ORDER BY blob_cid ASC LIMIT ? , .{ space, repo_did, after, capped_limit }) else try conn.rows( \\SELECT DISTINCT blob_cid \\FROM permissioned_space_record_blobs \\WHERE space = ? AND repo_did = ? \\ORDER BY blob_cid ASC LIMIT ? , .{ space, repo_did, capped_limit }); defer rows.deinit(); var cids: std.ArrayList([]const u8) = .empty; while (rows.next()) |row| try cids.append(allocator, try allocator.dupe(u8, row.text(0))); if (rows.err) |err| return err; return cids.toOwnedSlice(allocator); } pub fn getPublicBlob( allocator: std.mem.Allocator, did: []const u8, cid: []const u8, ) ?BlobRecord { const metadata = blobMetadata(allocator, did, cid, true) orelse return null; return .{ .mime_type = metadata.mime_type, .data = blobstore.get(allocator, did, cid, metadata.size + 1) catch return null, }; } pub const BlobMetadata = struct { mime_type: []const u8, size: usize, }; /// A blob's type and size if a public record references it, for streaming /// it from the blobstore. pub fn publicBlobMetadata(allocator: std.mem.Allocator, did: []const u8, cid: []const u8) ?BlobMetadata { return blobMetadata(allocator, did, cid, true); } fn blobMetadata( allocator: std.mem.Allocator, did: []const u8, cid: []const u8, require_public_reference: bool, ) ?BlobMetadata { const reader = acquireReader() catch return null; defer reader.release(); const row = if (require_public_reference) reader.db.row( \\SELECT b.mime_type, b.size \\FROM blobs b \\WHERE b.did = ? AND b.cid = ? \\ AND NOT EXISTS (SELECT 1 FROM blob_takedowns t WHERE t.did = b.did AND t.cid = b.cid) \\ AND EXISTS ( \\ SELECT 1 \\ FROM expected_blobs eb \\ JOIN records r ON r.uri = eb.record_uri \\ WHERE r.did = b.did AND eb.blob_cid = b.cid \\ ) , .{ did, cid }) catch return null else reader.db.row( \\SELECT b.mime_type, b.size \\FROM blobs b \\WHERE b.did = ? AND b.cid = ? \\ AND NOT EXISTS (SELECT 1 FROM blob_takedowns t WHERE t.did = b.did AND t.cid = b.cid) , .{ did, cid }) catch return null; const found = row orelse return null; defer found.deinit(); return .{ .mime_type = allocator.dupe(u8, found.text(0)) catch return null, .size = @intCast(found.int(1)), }; } pub fn writeBlobListJson(allocator: std.mem.Allocator, did: []const u8, since: ?[]const u8, cursor: ?[]const u8, limit: usize) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const page_limit = @as(i64, @intCast(if (limit == 0) 500 else limit)); var rows = if (since) |since_rev| blk: { if (cursor) |after_cid| { break :blk try reader.db.rows( \\SELECT DISTINCT b.cid \\FROM blobs b \\JOIN expected_blobs eb ON eb.blob_cid = b.cid \\JOIN records r ON r.uri = eb.record_uri AND r.did = b.did \\WHERE b.did = ? AND r.rev > ? AND b.cid > ? \\ORDER BY b.cid ASC \\LIMIT ? , .{ did, since_rev, after_cid, page_limit }); } break :blk try reader.db.rows( \\SELECT DISTINCT b.cid \\FROM blobs b \\JOIN expected_blobs eb ON eb.blob_cid = b.cid \\JOIN records r ON r.uri = eb.record_uri AND r.did = b.did \\WHERE b.did = ? AND r.rev > ? \\ORDER BY b.cid ASC \\LIMIT ? , .{ did, since_rev, page_limit }); } else blk: { if (cursor) |after_cid| { break :blk try reader.db.rows( \\SELECT DISTINCT b.cid \\FROM blobs b \\JOIN expected_blobs eb ON eb.blob_cid = b.cid \\JOIN records r ON r.uri = eb.record_uri AND r.did = b.did \\WHERE b.did = ? AND b.cid > ? \\ORDER BY b.cid ASC \\LIMIT ? , .{ did, after_cid, page_limit }); } break :blk try reader.db.rows( \\SELECT DISTINCT b.cid \\FROM blobs b \\JOIN expected_blobs eb ON eb.blob_cid = b.cid \\JOIN records r ON r.uri = eb.record_uri AND r.did = b.did \\WHERE b.did = ? \\ORDER BY b.cid ASC \\LIMIT ? , .{ did, page_limit }); }; defer rows.deinit(); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.writeAll("{\"cids\":["); var first = true; var last_cid: ?[]const u8 = null; while (rows.next()) |row| { if (!first) try out.writer.writeByte(','); first = false; const cid = row.text(0); last_cid = try allocator.dupe(u8, cid); try out.writer.print("{f}", .{std.json.fmt(cid, .{})}); } if (rows.err) |err| return err; try out.writer.writeByte(']'); if (last_cid) |cid| try out.writer.print(",\"cursor\":{f}", .{std.json.fmt(cid, .{})}); try out.writer.writeByte('}'); return out.toOwnedSlice(); } pub fn writeMissingBlobsJson(allocator: std.mem.Allocator, did: []const u8, cursor: ?[]const u8, limit: usize) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const page_limit = @as(i64, @intCast(if (limit == 0) 500 else limit)); var rows = if (cursor) |after_cid| try reader.db.rows( \\SELECT rb.blob_cid, rb.record_uri \\FROM expected_blobs rb \\JOIN records r ON r.uri = rb.record_uri \\LEFT JOIN blobs b ON b.cid = rb.blob_cid AND b.did = r.did \\WHERE r.did = ? AND b.cid IS NULL AND rb.blob_cid > ? \\ORDER BY rb.blob_cid ASC \\LIMIT ? , .{ did, after_cid, page_limit }) else try reader.db.rows( \\SELECT rb.blob_cid, rb.record_uri \\FROM expected_blobs rb \\JOIN records r ON r.uri = rb.record_uri \\LEFT JOIN blobs b ON b.cid = rb.blob_cid AND b.did = r.did \\WHERE r.did = ? AND b.cid IS NULL \\ORDER BY rb.blob_cid ASC \\LIMIT ? , .{ did, page_limit }); defer rows.deinit(); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.writeAll("{\"blobs\":["); var first = true; var last_cid: ?[]const u8 = null; while (rows.next()) |row| { if (!first) try out.writer.writeByte(','); first = false; const cid = row.text(0); last_cid = try allocator.dupe(u8, cid); try out.writer.print( "{{\"cid\":{f},\"recordUri\":{f}}}", .{ std.json.fmt(cid, .{}), std.json.fmt(row.text(1), .{}) }, ); } if (rows.err) |err| return err; try out.writer.writeByte(']'); if (last_cid) |cid| try out.writer.print(",\"cursor\":{f}", .{std.json.fmt(cid, .{})}); try out.writer.writeByte('}'); return out.toOwnedSlice(); } pub fn writeAccountStatusJson(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const record_count = try scalarCountFrom( reader.db, "SELECT COUNT(*) FROM records WHERE did = ?", did, ); const blob_count = try scalarCountFrom( reader.db, "SELECT COUNT(*) FROM blobs WHERE did = ?", did, ); const expected_blob_count = try scalarCountFrom( reader.db, \\SELECT COUNT(*) \\FROM expected_blobs rb \\JOIN records r ON r.uri = rb.record_uri \\WHERE r.did = ? , did, ); const block_count = try scalarCountFrom( reader.db, "SELECT COUNT(*) FROM repo_blocks WHERE did = ?", did, ); const status = try accountStatusFrom(reader.db, did); const active = status.isActive(); const root = try latestRootFrom(reader.db, allocator, did); return std.fmt.allocPrint( allocator, "{{\"activated\":{},\"validDid\":{},\"status\":{f},\"repoCommit\":{f},\"repoRev\":{f},\"repoBlocks\":{d},\"indexedRecords\":{d},\"privateStateValues\":0,\"expectedBlobs\":{d},\"importedBlobs\":{d}}}", .{ active, active, std.json.fmt(status.asString(), .{}), std.json.fmt(root.cid, .{}), std.json.fmt(root.rev, .{}), block_count, record_count, expected_blob_count, blob_count, }, ); } pub fn importRepo( allocator: std.mem.Allocator, account: auth.Account, commit_cid: []const u8, rev: []const u8, records: []const ImportedRecord, blocks: []const ImportedBlock, ) !void { const lane = write_lanes.lock(store_io, account.did); defer lane.unlock(); var publish_event = false; var seq: u64 = 0; db_mutex.lockUncancelable(store_io); var db_locked = true; defer if (db_locked) db_mutex.unlock(store_io); try requireInitialized(); seq = try nextSeqLocked(); try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec("DELETE FROM expected_blobs WHERE record_uri IN (SELECT uri FROM records WHERE did = ?)", .{account.did}); try conn.exec("DELETE FROM records WHERE did = ?", .{account.did}); try conn.exec("DELETE FROM repo_blocks WHERE did = ?", .{account.did}); try conn.exec("DELETE FROM commits WHERE did = ?", .{account.did}); for (blocks) |block| { try conn.exec( \\INSERT INTO repo_blocks (did, cid, data, repo_rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(did, cid) DO UPDATE SET \\ data = excluded.data, \\ repo_rev = COALESCE(repo_blocks.repo_rev, excluded.repo_rev) , .{ account.did, block.cid, zqlite.blob(block.data), rev }); } for (records) |record| { const uri = try std.fmt.allocPrint(std.heap.page_allocator, "at://{s}/{s}/{s}", .{ account.did, record.collection, record.rkey }); defer std.heap.page_allocator.free(uri); try conn.exec( \\INSERT INTO records (did, collection, rkey, uri, cid, rev, seq) \\VALUES (?, ?, ?, ?, ?, ?, ?) , .{ account.did, record.collection, record.rkey, uri, record.cid, rev, @as(i64, @intCast(seq)) }); for (record.blob_cids) |blob_cid| { try conn.exec( \\INSERT INTO expected_blobs (blob_cid, record_uri) \\VALUES (?, ?) \\ON CONFLICT(blob_cid, record_uri) DO UPDATE SET blob_cid = excluded.blob_cid , .{ blob_cid, uri }); } } try conn.exec( \\INSERT INTO commits (seq, did, cid, rev, prev) \\VALUES (?, ?, ?, ?, NULL) , .{ @as(i64, @intCast(seq)), account.did, commit_cid, rev }); publish_event = try accountActiveLocked(account.did); if (publish_event) { const event_frame = try importedCommitEventFrame(allocator, seq, account.did, commit_cid, rev, blocks); try conn.exec( \\INSERT INTO seq_events (seq, did, commit_cid, evt) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(seq) DO UPDATE SET \\ did = excluded.did, \\ commit_cid = excluded.commit_cid, \\ evt = excluded.evt , .{ @as(i64, @intCast(seq)), account.did, commit_cid, zqlite.blob(event_frame) }); } try conn.commit(); db_mutex.unlock(store_io); db_locked = false; if (publish_event) eventlog.publish(seq); } pub fn writeRepoCar(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { return writeRepoCarSince(allocator, did, null); } pub fn writeRepoCarSince(allocator: std.mem.Allocator, did: []const u8, since: ?[]const u8) ![]const u8 { return writeRepoCarData(allocator, try collectRepoCar(allocator, did, since)); } /// The root and blocks of a repo CAR (all of it, or since a rev), read in /// one transaction but not yet encoded, so a caller can stream the CAR /// instead of holding it. pub fn collectRepoCar(allocator: std.mem.Allocator, did: []const u8, since: ?[]const u8) !RepoCarData { if (since == null) return collectRepoCarFull(allocator, did); const path = database_path orelse return Error.StoreNotInitialized; if (std.mem.eql(u8, path, ":memory:")) { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return collectRepoCarSinceFrom(allocator, conn, did, since.?); } const read_conn = try openRepoReadConnection(path); defer read_conn.close(); try read_conn.transaction(); errdefer read_conn.rollback(); const data = try collectRepoCarSinceFrom(allocator, read_conn, did, since.?); try read_conn.commit(); return data; } const since_export_newer_sql = \\SELECT cid, data \\FROM repo_blocks \\WHERE did = ? AND repo_rev > ? \\ORDER BY repo_rev DESC, cid DESC ; // Blocks written before revisions were tracked and not yet backfilled; a // diff keeps them since it cannot tell whether they are newer. const since_export_unknown_rev_sql = \\SELECT cid, data \\FROM repo_blocks \\WHERE did = ? AND repo_rev IS NULL \\ORDER BY cid DESC ; fn appendRepoBlockRows( allocator: std.mem.Allocator, read_conn: zqlite.Conn, blocks: *std.ArrayList(zat.car.Block), comptime sql: []const u8, args: anytype, ) !void { var rows = try read_conn.rows(sql, args); defer rows.deinit(); while (rows.next()) |row| { const cid_text = row.text(0); const data = row.nullableBlob(1) orelse ""; const cid_raw = try zat.multibase.base32lower.decode(allocator, cid_text[1..]); try blocks.append(allocator, .{ .cid_raw = cid_raw, .data = try allocator.dupe(u8, data), }); } if (rows.err) |err| return err; } fn collectRepoCarSinceFrom( allocator: std.mem.Allocator, read_conn: zqlite.Conn, did: []const u8, since_rev: []const u8, ) !RepoCarData { const root = try latestRootFrom(read_conn, allocator, did); const root_raw = try zat.multibase.base32lower.decode(allocator, root.cid[1..]); // Two range reads rather than one `repo_rev IS NULL OR repo_rev > ?`: // SQLite cannot turn that OR into a range on repo_blocks_rev_idx, so it // constrained the index by DID alone and walked every block the account // ever wrote to answer a one-commit diff. Revisions sort above NULL under // DESC, so the blocks come out in the order the single query gave. var blocks: std.ArrayList(zat.car.Block) = .empty; try appendRepoBlockRows(allocator, read_conn, &blocks, since_export_newer_sql, .{ did, since_rev }); try appendRepoBlockRows(allocator, read_conn, &blocks, since_export_unknown_rev_sql, .{did}); return .{ .root_cid_raw = root_raw, .blocks = try blocks.toOwnedSlice(allocator), }; } pub fn writeRecordProofCar( allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8, ) ![]const u8 { return writeRepoCarData(allocator, try collectRecordProof(allocator, did, collection, rkey)); } /// The sync.getRecord proof's root and blocks, not yet encoded. pub fn collectRecordProof( allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8, ) !RepoCarData { // The commit and the MST path come from one snapshot. The proof is a // handful of blocks, so a pooled reader serves it; full exports keep // their own connection. const reader = try acquireReader(); defer reader.release(); try reader.db.transaction(); errdefer reader.db.rollback(); const data = try collectRecordProofFrom(allocator, reader.db, did, collection, rkey); try reader.db.commit(); return data; } /// Collect the sync.getRecord proof CAR: the signed commit block plus the MST /// node path from the root down to where (collection, rkey) lives, and the /// record block itself when present. For an absent key the path alone is the /// exclusion proof, matching the reference implementation's `cidsForPath`. fn collectRecordProofFrom( allocator: std.mem.Allocator, read_conn: zqlite.Conn, did: []const u8, collection: []const u8, rkey: []const u8, ) !RepoCarData { const commit = try latestCommitRawFrom(read_conn, allocator, did) orelse return Error.RepoNotFound; var blocks: std.ArrayList(zat.car.Block) = .empty; try blocks.append(allocator, .{ .cid_raw = commit.commit_cid_raw, .data = commit.commit_data, }); const key = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ collection, rkey }); var current: ?[]const u8 = commit.data_cid_raw; var record_cid_raw: ?[]const u8 = null; var depth: usize = 0; walk: while (current) |node_cid_raw| { depth += 1; if (depth > 256) return Error.InvalidDagCbor; const node_cid_text = try zat.cbor.Cid.toString(.{ .raw = node_cid_raw }, allocator); const node_data = try repoBlockDataFrom(read_conn, allocator, did, node_cid_text) orelse return Error.MissingRecordBlock; try blocks.append(allocator, .{ .cid_raw = try allocator.dupe(u8, node_cid_raw), .data = node_data, }); const node = try zat.mst.decodeMstNode(allocator, node_data); var next: ?[]const u8 = node.left; var prev_key: []const u8 = ""; for (node.entries) |entry| { if (entry.prefix_len > prev_key.len) return Error.InvalidDagCbor; const entry_key = try allocator.alloc(u8, entry.prefix_len + entry.key_suffix.len); @memcpy(entry_key[0..entry.prefix_len], prev_key[0..entry.prefix_len]); @memcpy(entry_key[entry.prefix_len..], entry.key_suffix); prev_key = entry_key; switch (std.mem.order(u8, key, entry_key)) { .lt => { current = next; continue :walk; }, .eq => { record_cid_raw = entry.value; break :walk; }, .gt => next = entry.tree, } } current = next; } if (record_cid_raw) |raw| { const record_cid_text = try zat.cbor.Cid.toString(.{ .raw = raw }, allocator); const record_data = try repoBlockDataFrom(read_conn, allocator, did, record_cid_text) orelse return Error.MissingRecordBlock; try blocks.append(allocator, .{ .cid_raw = try allocator.dupe(u8, raw), .data = record_data, }); } return .{ .root_cid_raw = commit.commit_cid_raw, .blocks = try blocks.toOwnedSlice(allocator), }; } fn collectRepoCarFull(allocator: std.mem.Allocator, did: []const u8) !RepoCarData { const path = database_path orelse return Error.StoreNotInitialized; if (std.mem.eql(u8, path, ":memory:")) { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return collectRepoCarFullFrom(allocator, conn, did); } const read_conn = try openRepoReadConnection(path); defer read_conn.close(); try read_conn.transaction(); errdefer read_conn.rollback(); const data = try collectRepoCarFullFrom(allocator, read_conn, did); try read_conn.commit(); return data; } pub const RepoCarData = struct { root_cid_raw: []const u8, blocks: []zat.car.Block, }; fn collectRepoCarFullFrom( allocator: std.mem.Allocator, read_conn: zqlite.Conn, did: []const u8, ) !RepoCarData { const root = try latestCommitRawFrom(read_conn, allocator, did) orelse return Error.RepoNotFound; var blocks: std.ArrayList(zat.car.Block) = .empty; try blocks.append(allocator, .{ .cid_raw = root.commit_cid_raw, .data = root.commit_data, }); var prefetched = PrefetchedBlocks{ .fallback = .{ .allocator = allocator, .did = did, .connection = read_conn, } }; try prefetched.loadReachable(allocator, read_conn, did, root.data_cid_raw); try zat.mst.collectReachableBlocks( allocator, root.data_cid_raw, prefetched.reader(), &blocks, .{ .include_records = true }, ); return .{ .root_cid_raw = root.commit_cid_raw, .blocks = try blocks.toOwnedSlice(allocator), }; } /// CIDs per prefetch query. Each is still a primary-key probe; batching /// saves preparing and stepping one statement per block. const prefetch_batch_cids = 512; /// The blocks a full export will walk, read a tree level at a time with one /// query per batch of CIDs instead of one per block (a 30k-record repo took /// about 60k statements). zat then walks them from memory, so the CAR keeps /// its depth-first order; anything the prefetch did not find falls through /// to a direct read, which is where a missing block is reported. const PrefetchedBlocks = struct { by_cid: std.StringHashMapUnmanaged([]const u8) = .empty, fallback: RepoBlockReader, fn reader(self: *PrefetchedBlocks) zat.mst.BlockReader { return .{ .ctx = self, .getFn = getBlock }; } fn getBlock(ctx: *anyopaque, cid_raw: []const u8) anyerror!?[]const u8 { const self: *PrefetchedBlocks = @ptrCast(@alignCast(ctx)); if (self.by_cid.get(cid_raw)) |data| return data; return RepoBlockReader.getBlock(&self.fallback, cid_raw); } fn loadReachable( self: *PrefetchedBlocks, allocator: std.mem.Allocator, read_conn: zqlite.Conn, did: []const u8, root_cid_raw: []const u8, ) !void { var queued: std.StringHashMapUnmanaged(void) = .empty; var nodes: std.ArrayList([]const u8) = .empty; var records: std.ArrayList([]const u8) = .empty; try enqueue(allocator, &queued, &nodes, root_cid_raw); var depth: usize = 0; while (nodes.items.len > 0 and depth <= 256) : (depth += 1) { try self.load(allocator, read_conn, did, nodes.items); var next: std.ArrayList([]const u8) = .empty; for (nodes.items) |cid_raw| { const data = self.by_cid.get(cid_raw) orelse continue; // A node that does not decode is left for the walk to report. const node = zat.mst.decodeMstNode(allocator, data) catch continue; if (node.left) |left| try enqueue(allocator, &queued, &next, left); for (node.entries) |entry| { try enqueue(allocator, &queued, &records, entry.value); if (entry.tree) |right| try enqueue(allocator, &queued, &next, right); } } nodes = next; } try self.load(allocator, read_conn, did, records.items); } fn enqueue( allocator: std.mem.Allocator, queued: *std.StringHashMapUnmanaged(void), list: *std.ArrayList([]const u8), cid_raw: []const u8, ) !void { const entry = try queued.getOrPut(allocator, cid_raw); if (!entry.found_existing) try list.append(allocator, cid_raw); } fn load( self: *PrefetchedBlocks, allocator: std.mem.Allocator, read_conn: zqlite.Conn, did: []const u8, cids_raw: []const []const u8, ) !void { var start: usize = 0; while (start < cids_raw.len) : (start += prefetch_batch_cids) { const batch = cids_raw[start..@min(start + prefetch_batch_cids, cids_raw.len)]; var list: std.Io.Writer.Allocating = .init(allocator); defer list.deinit(); try list.writer.writeByte('['); for (batch, 0..) |cid_raw, i| { if (i > 0) try list.writer.writeByte(','); try list.writer.print("\"{s}\"", .{try zat.cbor.Cid.toString(.{ .raw = cid_raw }, allocator)}); } try list.writer.writeByte(']'); var rows = try read_conn.rows( \\SELECT cid, data \\FROM repo_blocks \\WHERE did = ? AND cid IN (SELECT value FROM json_each(?)) , .{ did, list.written() }); defer rows.deinit(); while (rows.next()) |row| { const cid_text = row.text(0); const data = row.nullableBlob(1) orelse continue; const cid_raw = try zat.multibase.base32lower.decode(allocator, cid_text[1..]); try self.by_cid.put(allocator, cid_raw, try allocator.dupe(u8, data)); } if (rows.err) |err| return err; } } }; pub fn writeRepoCarData(allocator: std.mem.Allocator, data: RepoCarData) ![]const u8 { return zat.car.writeAlloc(allocator, .{ .roots = &.{.{ .raw = data.root_cid_raw }}, .blocks = data.blocks, }); } pub const RepoBlocks = struct { blocks: []zat.car.Block, missing: []const []const u8, }; /// Blocks of one repo by CID, for sync.getBlocks; CIDs it does not hold come /// back in `missing`. pub fn getRepoBlocks(allocator: std.mem.Allocator, did: []const u8, cids: []const []const u8) !RepoBlocks { const reader = try acquireReader(); defer reader.release(); var blocks: std.ArrayList(zat.car.Block) = .empty; var missing: std.ArrayList([]const u8) = .empty; for (cids) |cid| { const cid_raw = (zat.cbor.Cid.fromString(allocator, cid) catch return error.InvalidCid).raw; const row = try reader.db.row("SELECT data FROM repo_blocks WHERE did = ? AND cid = ?", .{ did, cid }); if (row) |r| { defer r.deinit(); try blocks.append(allocator, .{ .cid_raw = cid_raw, .data = try allocator.dupe(u8, r.nullableBlob(0) orelse "") }); } else { try missing.append(allocator, cid); } } return .{ .blocks = try blocks.toOwnedSlice(allocator), .missing = try missing.toOwnedSlice(allocator) }; } fn openRepoReadConnection(path: [:0]const u8) !zqlite.Conn { const read_conn = try zqlite.open(path.ptr, zqlite.OpenFlags.ReadOnly); errdefer read_conn.close(); try read_conn.busyTimeout(5000); try read_conn.execNoArgs("PRAGMA query_only=ON"); return read_conn; } pub fn writeRepoListJson(allocator: std.mem.Allocator, cursor: ?[]const u8, limit: usize) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const actual_limit = if (limit == 0) 500 else @min(limit, 1000); const parsed_cursor = try parseRepoListCursor(cursor); var rows = if (parsed_cursor) |after| try reader.db.rows( \\SELECT a.did, c.cid, c.rev, a.account_status, a.created_at \\FROM accounts a \\JOIN ( \\ SELECT did, MAX(seq) AS seq \\ FROM commits \\ GROUP BY did \\) latest ON latest.did = a.did \\JOIN commits c ON c.did = latest.did AND c.seq = latest.seq \\WHERE a.created_at > ? OR (a.created_at = ? AND a.did > ?) \\ORDER BY a.created_at ASC, a.did ASC \\LIMIT ? , .{ after.created_at, after.created_at, after.did, @as(i64, @intCast(actual_limit)) }) else try reader.db.rows( \\SELECT a.did, c.cid, c.rev, a.account_status, a.created_at \\FROM accounts a \\JOIN ( \\ SELECT did, MAX(seq) AS seq \\ FROM commits \\ GROUP BY did \\) latest ON latest.did = a.did \\JOIN commits c ON c.did = latest.did AND c.seq = latest.seq \\ORDER BY a.created_at ASC, a.did ASC \\LIMIT ? , .{@as(i64, @intCast(actual_limit))}); defer rows.deinit(); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); var json: std.json.Stringify = .{ .writer = &out.writer }; try json.beginObject(); try json.objectField("repos"); try json.beginArray(); var last_created_at: ?i64 = null; var last_did: ?[]const u8 = null; while (rows.next()) |row| { const did = row.text(0); last_did = try allocator.dupe(u8, did); last_created_at = row.int(4); const status = AccountStatus.parse(row.text(3)); const active = status.isActive(); try json.beginObject(); try json.objectField("did"); try json.write(did); try json.objectField("head"); try json.write(row.text(1)); try json.objectField("rev"); try json.write(row.text(2)); try json.objectField("active"); try json.write(active); if (!active) { try json.objectField("status"); try json.write(status.asString()); } try json.endObject(); } if (rows.err) |err| return err; try json.endArray(); if (last_created_at) |created_at| { try json.objectField("cursor"); try json.write(try std.fmt.allocPrint(allocator, "{d}:{s}", .{ created_at, last_did.? })); } try json.endObject(); return out.toOwnedSlice(); } const RepoListCursor = struct { created_at: i64, did: []const u8, }; fn parseRepoListCursor(cursor: ?[]const u8) Error!?RepoListCursor { const raw = cursor orelse return null; const sep = std.mem.indexOfScalar(u8, raw, ':') orelse return Error.InvalidCursor; if (sep == 0 or sep + 1 >= raw.len) return Error.InvalidCursor; return .{ .created_at = std.fmt.parseInt(i64, raw[0..sep], 10) catch return Error.InvalidCursor, .did = raw[sep + 1 ..], }; } /// The CID of the repo's current commit, the sync routes' cache validator. pub fn latestCommitCid(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { const reader = try acquireReader(); defer reader.release(); return (try latestRootFrom(reader.db, allocator, did)).cid; } pub fn writeLatestCommitJson(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const root = try latestRootFrom(reader.db, allocator, did); return std.fmt.allocPrint( allocator, "{{\"cid\":{f},\"rev\":{f}}}", .{ std.json.fmt(root.cid, .{}), std.json.fmt(root.rev, .{}) }, ); } pub fn listSeqEvents(allocator: std.mem.Allocator, cursor: u64, limit: usize) ![]SeqEvent { const reader = try acquireReader(); defer reader.release(); return listSeqEventsFrom(reader.db, allocator, cursor, limit); } fn listSeqEventsFrom( read_conn: zqlite.Conn, allocator: std.mem.Allocator, cursor: u64, limit: usize, ) ![]SeqEvent { var rows = try read_conn.rows( \\SELECT seq, evt \\FROM seq_events \\WHERE seq > ? \\ORDER BY seq ASC \\LIMIT ? , .{ @as(i64, @intCast(cursor)), @as(i64, @intCast(@min(limit, 1000))) }); defer rows.deinit(); var events: std.ArrayList(SeqEvent) = .empty; while (rows.next()) |row| { try events.append(allocator, .{ .seq = @intCast(row.int(0)), .frame = try allocator.dupe(u8, row.nullableBlob(1) orelse ""), }); } if (rows.err) |err| return err; return events.toOwnedSlice(allocator); } fn insertSeqEventLocked(seq: u64, did: []const u8, commit_cid: []const u8, frame: []const u8) !void { try conn.exec( \\INSERT INTO seq_events (seq, did, commit_cid, evt) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(seq) DO UPDATE SET \\ did = excluded.did, \\ commit_cid = excluded.commit_cid, \\ evt = excluded.evt , .{ @as(i64, @intCast(seq)), did, commit_cid, zqlite.blob(frame) }); } pub fn writeRepoStatusJson(allocator: std.mem.Allocator, did: []const u8) ![]const u8 { const reader = try acquireReader(); defer reader.release(); const status = try accountStatusFrom(reader.db, did); const active = status.isActive(); const root = if (active) try latestRootFrom(reader.db, allocator, did) else null; var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); var json: std.json.Stringify = .{ .writer = &out.writer }; try json.beginObject(); try json.objectField("did"); try json.write(did); try json.objectField("active"); try json.write(active); if (!active) { try json.objectField("status"); try json.write(status.asString()); } else { try json.objectField("rev"); try json.write(root.?.rev); } try json.endObject(); return out.toOwnedSlice(); } pub fn writeRecordJson(allocator: std.mem.Allocator, record: Record) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); const uri = try record.uri(allocator); defer allocator.free(uri); try out.writer.print( "{{\"uri\":{f},\"cid\":{f},\"value\":{s}}}", .{ std.json.fmt(uri, .{}), std.json.fmt(record.cid, .{}), record.value_json }, ); return out.toOwnedSlice(); } pub fn writeListJson( allocator: std.mem.Allocator, did: []const u8, collection: []const u8, cursor: ?[]const u8, reverse: bool, limit: usize, ) ![]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const page_limit = @as(i64, @intCast(if (limit == 0) 100 else limit)); var rows = if (cursor) |rkey_cursor| blk: { if (reverse) { break :blk try conn.rows( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? AND r.rkey > ? \\ AND NOT EXISTS (SELECT 1 FROM record_takedowns t WHERE t.record_uri = r.uri) \\ORDER BY r.rkey ASC \\LIMIT ? , .{ did, collection, rkey_cursor, page_limit }); } break :blk try conn.rows( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? AND r.rkey < ? \\ AND NOT EXISTS (SELECT 1 FROM record_takedowns t WHERE t.record_uri = r.uri) \\ORDER BY r.rkey DESC \\LIMIT ? , .{ did, collection, rkey_cursor, page_limit }); } else blk: { if (reverse) { break :blk try conn.rows( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? \\ AND NOT EXISTS (SELECT 1 FROM record_takedowns t WHERE t.record_uri = r.uri) \\ORDER BY r.rkey ASC \\LIMIT ? , .{ did, collection, page_limit }); } break :blk try conn.rows( \\SELECT r.did, r.collection, r.rkey, r.cid, rb.data, r.rev, r.seq \\FROM records r \\JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE r.did = ? AND r.collection = ? \\ AND NOT EXISTS (SELECT 1 FROM record_takedowns t WHERE t.record_uri = r.uri) \\ORDER BY r.rkey DESC \\LIMIT ? , .{ did, collection, page_limit }); }; defer rows.deinit(); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.writeAll("{\"records\":["); var first = true; var last_rkey: ?[]const u8 = null; while (rows.next()) |row| { const record = try recordFromRow(row, allocator); if (!first) try out.writer.writeByte(','); first = false; last_rkey = try allocator.dupe(u8, row.text(2)); const uri = try record.uri(allocator); defer allocator.free(uri); try out.writer.print( "{{\"uri\":{f},\"cid\":{f},\"value\":{s}}}", .{ std.json.fmt(uri, .{}), std.json.fmt(record.cid, .{}), record.value_json }, ); } if (rows.err) |err| return err; try out.writer.writeByte(']'); if (last_rkey) |rkey| { try out.writer.print(",\"cursor\":{f}", .{std.json.fmt(rkey, .{})}); } try out.writer.writeByte('}'); return out.toOwnedSlice(); } pub fn generateRkey(allocator: std.mem.Allocator) ![]const u8 { return nextRkey(allocator); } pub fn prepareRecordValue( allocator: std.mem.Allocator, collection: []const u8, rkey: []const u8, value: std.json.Value, ) !PreparedRecord { if (zat.Nsid.parse(collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(rkey) == null) return Error.InvalidRecordKey; try validateRecordForWrite(collection, rkey, value, .known); const cbor_value = try recordValueFromJson(allocator, value); const record_bytes = try zat.cbor.encodeAlloc(allocator, cbor_value); if (record_bytes.len > max_space_record_bytes) return Error.RecordTooLarge; const record_cid = try zat.cbor.Cid.forDagCbor(allocator, record_bytes); return .{ .cid = try zat.cbor.Cid.toString(.{ .raw = record_cid.raw }, allocator), .value_json = try stringifyValue(allocator, value), .validation_status = validationStatusForRecord(collection, .known), .blob_cids = try blobCidTexts(allocator, cbor_value), }; } pub const CreateSpaceInput = struct { actor_did: []const u8, authority_did: []const u8, space_type: []const u8, skey: []const u8, is_authority: bool, /// Required for the authority. A host that only keeps a writer's repo in /// another authority's space has no policy for it. policy: ?space_policy.Policy = null, }; pub fn createSpace(allocator: std.mem.Allocator, input: CreateSpaceInput) !SpaceConfig { if (zat.Did.parse(input.actor_did) == null) return Error.InvalidRepoPath; if (zat.Did.parse(input.authority_did) == null) return Error.InvalidRepoPath; if (zat.Nsid.parse(input.space_type) == null) return Error.InvalidCollection; if (zat.Rkey.parse(input.skey) == null) return Error.InvalidRecordKey; const policy_json: ?[]const u8 = if (input.is_authority) try space_policy.toJson(allocator, input.policy orelse return Error.InvalidRecordType) else null; const uri = try zat.space.SpaceUri.formatRef(allocator, input.authority_did, input.space_type, input.skey); const lane = if (input.is_authority) write_lanes.lock(store_io, input.authority_did) else null; defer if (lane) |held| held.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (try getSpaceLocked(allocator, input.actor_did, uri) != null) return Error.InvalidRefreshSession; try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec( \\INSERT INTO permissioned_spaces (uri, authority_did, space_type, skey, policy_json) \\VALUES (?, ?, ?, ?, ?) \\ON CONFLICT(uri) DO NOTHING , .{ uri, input.authority_did, input.space_type, input.skey, policy_json }); if (input.is_authority) { // Recreating a previously deleted space is allowed (per the lexicon), so // the authority's policy is replaced and the tombstone cleared. A live // space never reaches here: the guard above returns SpaceAlreadyExists. try conn.exec("UPDATE permissioned_spaces SET policy_json = ?, deleted_at = NULL WHERE uri = ?", .{ policy_json, uri }); } try conn.exec( \\INSERT INTO permissioned_space_repos (space, repo_did, set_hash, rev) \\VALUES (?, ?, NULL, NULL) \\ON CONFLICT(space, repo_did) DO NOTHING , .{ uri, input.authority_did }); // Upsert, not plain insert: a recreate lands on the actor row left behind by // the prior delete, which must be reactivated rather than collide. try conn.exec( \\INSERT INTO permissioned_space_actor_state (space, actor_did, is_authority) \\VALUES (?, ?, ?) \\ON CONFLICT(space, actor_did) DO UPDATE SET \\ is_authority = excluded.is_authority, \\ deleted_at = NULL , .{ uri, input.actor_did, @as(i64, if (input.is_authority) 1 else 0) }); if (input.policy) |policy| if (input.is_authority) { var ops: std.ArrayList(SpaceWriteOp) = .empty; try appendPolicyRecordOpLocked(allocator, &ops, uri, input.authority_did, policy); if (ops.items.len > 0) _ = try applySpaceWritesLocked(allocator, uri, input.authority_did, ops.items); }; try conn.commit(); return (try getSpaceLocked(allocator, input.actor_did, uri)) orelse Error.RepoNotFound; } pub fn getSpace(allocator: std.mem.Allocator, actor_did: []const u8, uri: []const u8) !?SpaceConfig { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return getSpaceLocked(allocator, actor_did, uri); } /// Whether the authority deleted this space and has not recreated it. getSpace /// hides tombstones, so credential issuance asks separately to answer SpaceDeleted. pub fn spaceDeleted(authority_did: []const u8, uri: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row("SELECT 1 FROM permissioned_spaces WHERE uri = ? AND authority_did = ? AND deleted_at IS NOT NULL", .{ uri, authority_did }); if (row) |r| { r.deinit(); return true; } return false; } /// The space's latest revision of its writer set, held by its authority. pub fn latestSpaceRev(allocator: std.mem.Allocator, space: []const u8) !?[]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row("SELECT rev FROM permissioned_space_sequence WHERE space = ?", .{space})) orelse return null; defer row.deinit(); return try allocator.dupe(u8, row.text(0)); } pub fn listSpaces( allocator: std.mem.Allocator, actor_did: []const u8, maybe_did: ?[]const u8, maybe_type: ?[]const u8, maybe_cursor: ?[]const u8, limit: usize, ) ![]SpaceConfig { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const capped_limit: i64 = @intCast(@min(if (limit == 0) 50 else limit, 100)); var query: std.Io.Writer.Allocating = .init(allocator); defer query.deinit(); try query.writer.writeAll( \\SELECT s.uri, a.is_authority \\FROM permissioned_spaces s \\JOIN permissioned_space_actor_state a ON a.space = s.uri \\WHERE a.actor_did = ? AND a.deleted_at IS NULL AND s.deleted_at IS NULL ); if (maybe_did != null) try query.writer.writeAll(" AND s.authority_did = ?"); if (maybe_type != null) try query.writer.writeAll(" AND s.space_type = ?"); if (maybe_cursor != null) try query.writer.writeAll(" AND s.uri > ?"); try query.writer.writeAll(" ORDER BY s.uri ASC LIMIT ?"); var rows = if (maybe_did) |did| if (maybe_type) |space_type| if (maybe_cursor) |cursor| try conn.rows(query.written(), .{ actor_did, did, space_type, cursor, capped_limit }) else try conn.rows(query.written(), .{ actor_did, did, space_type, capped_limit }) else if (maybe_cursor) |cursor| try conn.rows(query.written(), .{ actor_did, did, cursor, capped_limit }) else try conn.rows(query.written(), .{ actor_did, did, capped_limit }) else if (maybe_type) |space_type| if (maybe_cursor) |cursor| try conn.rows(query.written(), .{ actor_did, space_type, cursor, capped_limit }) else try conn.rows(query.written(), .{ actor_did, space_type, capped_limit }) else if (maybe_cursor) |cursor| try conn.rows(query.written(), .{ actor_did, cursor, capped_limit }) else try conn.rows(query.written(), .{ actor_did, capped_limit }); defer rows.deinit(); var spaces: std.ArrayList(SpaceConfig) = .empty; while (rows.next()) |row| { var space = (try getSpaceConfigLocked(allocator, row.text(0))) orelse continue; space.is_authority = row.int(1) != 0; try spaces.append(allocator, space); } if (rows.err) |err| return err; return spaces.toOwnedSlice(allocator); } pub const SpaceAccess = enum { read, write }; pub const MemberAccess = struct { read: bool, write: bool }; /// Replaces a space's policy wholesale. A different `$type` migrates the space /// and discards the previous policy's state, so migrating back does not /// restore a member list. Returns whether the authority's repo changed. pub fn updateSpacePolicy(allocator: std.mem.Allocator, space: []const u8, next: space_policy.Policy) !bool { const authority_did = (zat.space.SpaceUri.parse(space) orelse return Error.RepoNotFound).space_did; const lane = write_lanes.lock(store_io, authority_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const current = try activeSpacePolicyLocked(allocator, space); try conn.exclusiveTransaction(); errdefer conn.rollback(); var ops: std.ArrayList(SpaceWriteOp) = .empty; try appendPolicyRecordOpLocked(allocator, &ops, space, authority_did, next); // Only memberspace has a member list, so this also withdraws a published // list when a migration discards it. if (space_policy.areMembersPublished(current) != space_policy.areMembersPublished(next)) { for (try allSpaceMembersLocked(allocator, space)) |member| { try appendMemberRecordOpLocked(allocator, &ops, space, authority_did, next, member.did, .{ .read = member.read, .write = member.write }); } } try conn.exec("UPDATE permissioned_spaces SET policy_json = ? WHERE uri = ?", .{ try space_policy.toJson(allocator, next), space }); if (!space_policy.sameType(current, next)) try conn.exec("DELETE FROM memberspace_members WHERE space = ?", .{space}); if (ops.items.len > 0) _ = try applySpaceWritesLocked(allocator, space, authority_did, ops.items); try conn.commit(); return ops.items.len > 0; } pub const MemberChange = struct { did: []const u8, /// null removes the member. access: ?MemberAccess, }; /// Applies several member changes in one commit, as `putMembers` and /// `removeMembers` do: a DID listed twice takes its last entry, and removing a /// DID that is not a member leaves it alone, writer set included. A member /// removed or denied write leaves the writer set. pub fn changeSpaceMembers(allocator: std.mem.Allocator, space: []const u8, changes: []const MemberChange) !MemberChanges { var latest: std.ArrayList(MemberChange) = .empty; outer: for (changes, 0..) |change, idx| { for (changes[idx + 1 ..]) |later| if (std.mem.eql(u8, later.did, change.did)) continue :outer; try latest.append(allocator, change); } return mutateSpaceMembers(allocator, space, latest.items); } pub const MemberChanges = struct { /// Whether the authority's repo changed. changed: bool, /// The writers this removed, whose removal registered syncers are owed. removed_writers: []const RemovedWriter, }; pub const RemovedWriter = struct { repo_did: []const u8, update: SpaceUpdate, }; fn mutateSpaceMembers(allocator: std.mem.Allocator, space: []const u8, changes: []const MemberChange) !MemberChanges { for (changes) |change| if (zat.Did.parse(change.did) == null) return Error.InvalidRepoPath; const authority_did = (zat.space.SpaceUri.parse(space) orelse return Error.RepoNotFound).space_did; const lane = write_lanes.lock(store_io, authority_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const policy = try activeSpacePolicyLocked(allocator, space); if (policy != .member) return error.PolicyMismatch; try conn.exclusiveTransaction(); errdefer conn.rollback(); var ops: std.ArrayList(SpaceWriteOp) = .empty; var removed: std.ArrayList(RemovedWriter) = .empty; for (changes) |change| { if (change.access) |access| { try conn.exec( \\INSERT INTO memberspace_members (space, member_did, read, write) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(space, member_did) DO UPDATE SET read = excluded.read, write = excluded.write , .{ space, change.did, @as(i64, @intFromBool(access.read)), @as(i64, @intFromBool(access.write)) }); } else { try conn.exec("DELETE FROM memberspace_members WHERE space = ? AND member_did = ?", .{ space, change.did }); if (conn.changes() == 0) continue; } if (change.access == null or !change.access.?.write) { if (try removeSpaceWriterLocked(allocator, space, change.did)) |update| try removed.append(allocator, .{ .repo_did = change.did, .update = update }); } try appendMemberRecordOpLocked(allocator, &ops, space, authority_did, policy, change.did, change.access); } if (ops.items.len > 0) _ = try applySpaceWritesLocked(allocator, space, authority_did, ops.items); try conn.commit(); return .{ .changed = ops.items.len > 0, .removed_writers = removed.items }; } pub fn listSpaceMembers( allocator: std.mem.Allocator, space: []const u8, maybe_cursor: ?[]const u8, limit: usize, ) ![]SpaceMember { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); if (try activeSpacePolicyLocked(allocator, space) != .member) return error.PolicyMismatch; const capped_limit: i64 = @intCast(@min(if (limit == 0) 100 else limit, 1000)); var rows = if (maybe_cursor) |cursor| try conn.rows( \\SELECT member_did, created_at, read, write \\FROM memberspace_members \\WHERE space = ? AND member_did > ? \\ORDER BY member_did ASC \\LIMIT ? , .{ space, cursor, capped_limit }) else try conn.rows( \\SELECT member_did, created_at, read, write \\FROM memberspace_members \\WHERE space = ? \\ORDER BY member_did ASC \\LIMIT ? , .{ space, capped_limit }); defer rows.deinit(); return collectSpaceMembers(allocator, &rows); } fn allSpaceMembersLocked(allocator: std.mem.Allocator, space: []const u8) ![]SpaceMember { var rows = try conn.rows( \\SELECT member_did, created_at, read, write \\FROM memberspace_members \\WHERE space = ? \\ORDER BY member_did ASC , .{space}); defer rows.deinit(); return collectSpaceMembers(allocator, &rows); } fn collectSpaceMembers(allocator: std.mem.Allocator, rows: anytype) ![]SpaceMember { var members: std.ArrayList(SpaceMember) = .empty; while (rows.next()) |row| { try members.append(allocator, .{ .did = try allocator.dupe(u8, row.text(0)), .created_at = row.int(1), .read = row.int(2) != 0, .write = row.int(3) != 0, }); } if (rows.err) |err| return err; return members.toOwnedSlice(allocator); } pub fn spaceMemberAllows(space: []const u8, did: []const u8, access: SpaceAccess) !bool { return try spaceMemberAccess(space, did, access) orelse false; } /// A member's read or write access, or null when `did` is not a member. pub fn spaceMemberAccess(space: []const u8, did: []const u8, access: SpaceAccess) !?bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( "SELECT read, write FROM memberspace_members WHERE space = ? AND member_did = ?", .{ space, did }, ); if (row == null) return null; defer row.?.deinit(); return row.?.int(if (access == .read) 0 else 1) != 0; } /// Removes an account from a space's writer set at the next space revision; it /// rejoins on its next authorized write. Null when it was not a writer. pub fn removeSpaceWriter(allocator: std.mem.Allocator, space: []const u8, did: []const u8) !?SpaceUpdate { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); _ = try activeSpacePolicyLocked(allocator, space); try conn.transaction(); errdefer conn.rollback(); const update = try removeSpaceWriterLocked(allocator, space, did); try conn.commit(); return update; } /// The row stays, marked removed, so listWriters can report the removal. fn removeSpaceWriterLocked(allocator: std.mem.Allocator, space: []const u8, did: []const u8) !?SpaceUpdate { const writer = (try conn.row("SELECT removed_at IS NULL FROM permissioned_space_writers WHERE space = ? AND repo_did = ?", .{ space, did })) orelse return null; const current = writer.int(0) != 0; writer.deinit(); if (!current) return null; const update = try nextSpaceRevLocked(allocator, space); try conn.exec("UPDATE permissioned_space_writers SET space_rev = ?, removed_at = unixepoch(), updated_at = unixepoch() WHERE space = ? AND repo_did = ?", .{ update.space_rev, space, did }); return update; } fn nextSpaceRevLocked(allocator: std.mem.Allocator, space: []const u8) !SpaceUpdate { const previous = try conn.row("SELECT rev FROM permissioned_space_sequence WHERE space = ?", .{space}); const prev: ?[]const u8 = if (previous) |row| blk: { defer row.deinit(); break :blk try allocator.dupe(u8, row.text(0)); } else null; const next = try revForSeq(allocator, 0, prev); try conn.exec("INSERT INTO permissioned_space_sequence(space, rev) VALUES (?, ?) ON CONFLICT(space) DO UPDATE SET rev = excluded.rev", .{ space, next }); return .{ .space_rev = next, .prev_space_rev = prev }; } fn activeSpacePolicyLocked(allocator: std.mem.Allocator, space: []const u8) !space_policy.Policy { const config = (try getSpaceConfigLocked(allocator, space)) orelse return Error.RepoNotFound; if (!config.is_authority) return Error.RepoNotFound; return (try config.policy(allocator)) orelse Error.RepoNotFound; } fn appendPolicyRecordOpLocked( allocator: std.mem.Allocator, ops: *std.ArrayList(SpaceWriteOp), space: []const u8, authority_did: []const u8, policy: space_policy.Policy, ) !void { const collection = space_policy.policy_collection; const rkey = space_policy.policy_rkey; if (space_policy.isPolicyPublished(policy)) { const prepared = try preparePublishedRecord(allocator, collection, rkey, try space_policy.policyRecordJson(allocator, policy)); try ops.append(allocator, .{ .put = .{ .collection = collection, .rkey = rkey, .prepared = prepared } }); } else if (try getSpaceRecordLocked(allocator, space, authority_did, collection, rkey) != null) { try ops.append(allocator, .{ .delete = .{ .collection = collection, .rkey = rkey } }); } } fn appendMemberRecordOpLocked( allocator: std.mem.Allocator, ops: *std.ArrayList(SpaceWriteOp), space: []const u8, authority_did: []const u8, policy: space_policy.Policy, did: []const u8, maybe_access: ?MemberAccess, ) !void { const collection = space_policy.member_collection; const rkey = try space_policy.didToRecordKey(allocator, did); if (maybe_access != null and space_policy.areMembersPublished(policy)) { const access = maybe_access.?; const prepared = try preparePublishedRecord(allocator, collection, rkey, try space_policy.memberRecordJson(allocator, access.read, access.write)); try ops.append(allocator, .{ .put = .{ .collection = collection, .rkey = rkey, .prepared = prepared } }); } else if (try getSpaceRecordLocked(allocator, space, authority_did, collection, rkey) != null) { try ops.append(allocator, .{ .delete = .{ .collection = collection, .rkey = rkey } }); } } fn preparePublishedRecord(allocator: std.mem.Allocator, collection: []const u8, rkey: []const u8, json: []const u8) !PreparedRecord { const value = try std.json.parseFromSliceLeaky(std.json.Value, allocator, json, .{}); return prepareRecordValue(allocator, collection, rkey, value); } pub const CollectionCheck = struct { context: *const anyopaque, allows: *const fn (context: *const anyopaque, collection: []const u8) bool, }; /// Deletes every record in a repo in one transaction and advances it to a new /// rev with a cleared oplog. The authority's own repo keeps the records /// published from the space's configuration. `check` must allow every /// collection being deleted. Returns false when there was nothing to delete. pub fn clearSpaceRecords(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, check: ?CollectionCheck) !bool { const keep_published = std.mem.eql(u8, (zat.space.SpaceUri.parse(space) orelse return Error.RepoNotFound).space_did, repo_did); const lane = write_lanes.lock(store_io, repo_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exclusiveTransaction(); errdefer conn.rollback(); var set_hash = zat.space.LtHash{}; var cleared = false; { var rows = try conn.rows("SELECT collection, rkey, cid FROM permissioned_space_records WHERE space = ? AND repo_did = ? ORDER BY collection", .{ space, repo_did }); defer rows.deinit(); var checked: ?[]const u8 = null; while (rows.next()) |row| { const collection = row.text(0); if (keep_published and space_policy.isPublishedCollection(collection)) { try addRecordElement(allocator, &set_hash, collection, row.text(1), row.text(2)); continue; } cleared = true; if (checked != null and std.mem.eql(u8, checked.?, collection)) continue; if (check) |c| if (!c.allows(c.context, collection)) return error.InsufficientScope; checked = try allocator.dupe(u8, collection); } if (rows.err) |err| return err; } if (!cleared) { try conn.commit(); return false; } const published_filter = " AND collection NOT IN ('" ++ space_policy.policy_collection ++ "', '" ++ space_policy.member_collection ++ "')"; if (keep_published) { try conn.exec("DELETE FROM permissioned_space_record_blobs WHERE space = ? AND repo_did = ?" ++ published_filter, .{ space, repo_did }); try conn.exec("DELETE FROM permissioned_space_records WHERE space = ? AND repo_did = ?" ++ published_filter, .{ space, repo_did }); } else { try conn.exec("DELETE FROM permissioned_space_record_blobs WHERE space = ? AND repo_did = ?", .{ space, repo_did }); try conn.exec("DELETE FROM permissioned_space_records WHERE space = ? AND repo_did = ?", .{ space, repo_did }); } try conn.exec("DELETE FROM permissioned_space_record_oplog WHERE space = ? AND repo_did = ?", .{ space, repo_did }); const state = try getRepoStateLocked(allocator, space, repo_did); const rev = try revForSeq(allocator, try nextSeqLocked(), state.rev); try conn.exec("UPDATE permissioned_space_repos SET set_hash = ?, rev = ? WHERE space = ? AND repo_did = ?", .{ zqlite.blob(&set_hash.bytes), rev, space, repo_did }); try queueSpaceNotifyLocked(space, repo_did, rev, set_hash.digest()); try conn.commit(); return true; } pub const SpaceMeta = struct { policy: space_policy.Policy, members: []SpaceMember, writers: []SpaceWriterState, }; /// The space's host-side state, for migrating its authority's account. pub fn exportSpaceMeta(allocator: std.mem.Allocator, space: []const u8) !SpaceMeta { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const policy = try activeSpacePolicyLocked(allocator, space); // Removed writers stay behind: the archive carries only the writer set. var rows = try conn.rows("SELECT repo_did, hash, rev, space_rev FROM permissioned_space_writers WHERE space = ? AND removed_at IS NULL ORDER BY repo_did", .{space}); defer rows.deinit(); var writers: std.ArrayList(SpaceWriterState) = .empty; while (rows.next()) |row| { try writers.append(allocator, .{ .repo_did = try allocator.dupe(u8, row.text(0)), .hash = try allocator.dupe(u8, row.blob(1)), .rev = try allocator.dupe(u8, row.text(2)), .space_rev = try allocator.dupe(u8, row.text(3)), }); } if (rows.err) |err| return err; return .{ .policy = policy, .members = if (policy == .member) try allSpaceMembersLocked(allocator, space) else &.{}, .writers = try writers.toOwnedSlice(allocator), }; } pub const ImportedSpaceMember = struct { did: []const u8, access: MemberAccess }; /// Replace whatever this host holds for a space with an archive from its /// authority's previous host, in one transaction. Nothing is republished: the /// authority's published records arrive with its repo. pub fn importSpaceMeta( allocator: std.mem.Allocator, space: []const u8, policy: space_policy.Policy, members: []const ImportedSpaceMember, writers: []const SpaceWriterState, ) !void { const parsed = zat.space.SpaceUri.parse(space) orelse return Error.RepoNotFound; if (policy != .member and members.len > 0) return error.PolicyMismatch; for (members) |member| if (zat.Did.parse(member.did) == null) return Error.InvalidRepoPath; const policy_json = try space_policy.toJson(allocator, policy); const lane = write_lanes.lock(store_io, parsed.space_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec( \\INSERT INTO permissioned_spaces (uri, authority_did, space_type, skey, policy_json) \\VALUES (?, ?, ?, ?, ?) \\ON CONFLICT(uri) DO UPDATE SET policy_json = excluded.policy_json, deleted_at = NULL , .{ space, parsed.space_did, parsed.space_type, parsed.skey, policy_json }); try conn.exec( \\INSERT INTO permissioned_space_actor_state (space, actor_did, is_authority) \\VALUES (?, ?, 1) \\ON CONFLICT(space, actor_did) DO UPDATE SET is_authority = 1, deleted_at = NULL , .{ space, parsed.space_did }); try conn.exec( \\INSERT INTO permissioned_space_repos (space, repo_did, set_hash, rev) \\VALUES (?, ?, NULL, NULL) \\ON CONFLICT(space, repo_did) DO NOTHING , .{ space, parsed.space_did }); try conn.exec("DELETE FROM memberspace_members WHERE space = ?", .{space}); for (members) |member| { try conn.exec( "INSERT INTO memberspace_members (space, member_did, read, write) VALUES (?, ?, ?, ?)", .{ space, member.did, @as(i64, @intFromBool(member.access.read)), @as(i64, @intFromBool(member.access.write)) }, ); } try conn.exec("DELETE FROM permissioned_space_writers WHERE space = ?", .{space}); var latest: ?[]const u8 = null; for (writers) |writer| { try conn.exec( "INSERT INTO permissioned_space_writers (space, repo_did, rev, hash, space_rev) VALUES (?, ?, ?, ?, ?)", .{ space, writer.repo_did, writer.rev, zqlite.blob(writer.hash), writer.space_rev }, ); if (latest == null or std.mem.order(u8, writer.space_rev, latest.?) == .gt) latest = writer.space_rev; } // Writer entries keep their spaceRev, so the next one must sort after them // for listWriters cursors to stay valid. if (latest) |rev| { try conn.exec( \\INSERT INTO permissioned_space_sequence (space, rev) VALUES (?, ?) \\ON CONFLICT(space) DO UPDATE SET rev = CASE WHEN excluded.rev > rev THEN excluded.rev ELSE rev END , .{ space, rev }); } try conn.commit(); } pub const ImportedSpaceRecord = struct { collection: []const u8, rkey: []const u8, prepared: PreparedRecord }; /// Replace an account's repo in a space with an imported archive, in one /// transaction: records, oplog, and blob links. The repo gets a new rev; its /// records must fold to `data`. The authority's own spaces must already exist /// here (importSpaceMeta first); a repo in another authority's space needs no /// prior knowledge of it. pub fn importSpaceRepo( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, records: []const ImportedSpaceRecord, data: [zat.space.lthash.digest_bytes]u8, ) !void { const parsed = zat.space.SpaceUri.parse(space) orelse return Error.RepoNotFound; if (zat.Did.parse(repo_did) == null) return Error.InvalidRepoPath; const lane = write_lanes.lock(store_io, repo_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exclusiveTransaction(); errdefer conn.rollback(); if (std.mem.eql(u8, parsed.space_did, repo_did)) { _ = try activeSpacePolicyLocked(allocator, space); } else { try conn.exec( \\INSERT INTO permissioned_spaces (uri, authority_did, space_type, skey, policy_json) \\VALUES (?, ?, ?, ?, NULL) \\ON CONFLICT(uri) DO NOTHING , .{ space, parsed.space_did, parsed.space_type, parsed.skey }); try conn.exec( \\INSERT INTO permissioned_space_actor_state (space, actor_did, is_authority) \\VALUES (?, ?, 0) \\ON CONFLICT(space, actor_did) DO UPDATE SET deleted_at = NULL , .{ space, repo_did }); } try conn.exec("DELETE FROM permissioned_space_record_blobs WHERE space = ? AND repo_did = ?", .{ space, repo_did }); try conn.exec("DELETE FROM permissioned_space_records WHERE space = ? AND repo_did = ?", .{ space, repo_did }); try conn.exec("DELETE FROM permissioned_space_record_oplog WHERE space = ? AND repo_did = ?", .{ space, repo_did }); const state = try getRepoStateLocked(allocator, space, repo_did); const rev = try revForSeq(allocator, try nextSeqLocked(), state.rev); var set_hash = zat.space.LtHash{}; for (records) |record| { try upsertSpaceRecordLocked(space, repo_did, record.collection, record.rkey, record.prepared, rev); try syncSpaceRecordBlobRefsLocked(space, repo_did, record.collection, record.rkey, record.prepared.blob_cids); try addRecordElement(allocator, &set_hash, record.collection, record.rkey, record.prepared.cid); } if (!std.mem.eql(u8, &set_hash.digest(), &data)) return error.InvalidRepoIndex; try conn.exec( \\INSERT INTO permissioned_space_repos (space, repo_did, set_hash, rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(space, repo_did) DO UPDATE SET set_hash = excluded.set_hash, rev = excluded.rev , .{ space, repo_did, zqlite.blob(&set_hash.bytes), rev }); try conn.commit(); } pub fn listSpaceWriters(allocator: std.mem.Allocator, space: []const u8, maybe_cursor: ?[]const u8, limit: usize) ![]SpaceWriterState { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const authority = try conn.row( \\SELECT 1 \\FROM permissioned_spaces s \\JOIN permissioned_space_actor_state a \\ ON a.space = s.uri AND a.actor_did = s.authority_did \\WHERE s.uri = ? AND s.deleted_at IS NULL AND a.deleted_at IS NULL AND a.is_authority = 1 , .{space}); if (authority == null) return Error.RepoNotFound; authority.?.deinit(); const capped_limit: i64 = @intCast(@min(if (limit == 0) 100 else limit, 1000)); var rows = if (maybe_cursor) |cursor| try conn.rows( \\SELECT repo_did, hash, rev, space_rev, removed_at IS NOT NULL \\FROM permissioned_space_writers \\WHERE space = ? AND space_rev > ? \\ORDER BY space_rev ASC \\LIMIT ? , .{ space, cursor, capped_limit }) else try conn.rows( \\SELECT repo_did, hash, rev, space_rev, removed_at IS NOT NULL \\FROM permissioned_space_writers \\WHERE space = ? \\ORDER BY space_rev ASC \\LIMIT ? , .{ space, capped_limit }); defer rows.deinit(); var repos: std.ArrayList(SpaceWriterState) = .empty; while (rows.next()) |row| { try repos.append(allocator, .{ .repo_did = try allocator.dupe(u8, row.text(0)), .hash = try allocator.dupe(u8, row.blob(1)), .rev = try allocator.dupe(u8, row.text(2)), .space_rev = try allocator.dupe(u8, row.text(3)), .removed = row.int(4) != 0, }); } if (rows.err) |err| return err; return repos.toOwnedSlice(allocator); } pub fn revokeSpaceCredentials(space: []const u8, ids: []const []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try conn.execNoArgs("DELETE FROM permissioned_space_revocations WHERE expires_at <= unixepoch()"); for (ids) |id| try conn.exec("INSERT INTO permissioned_space_revocations(space, jti, expires_at) VALUES (?, ?, unixepoch() + 3610) ON CONFLICT(space, jti) DO UPDATE SET expires_at = excluded.expires_at", .{ space, id }); try conn.commit(); } pub fn spaceCredentialRevoked(space: []const u8, jti: []const u8) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row("SELECT 1 FROM permissioned_space_revocations WHERE space = ? AND jti = ? AND expires_at > unixepoch()", .{ space, jti }); if (row) |r| { r.deinit(); return true; } return false; } pub const SpaceUpdate = struct { space_rev: []const u8, prev_space_rev: ?[]const u8, }; pub fn recordSpaceWriter(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, rev: []const u8, hash: []const u8) !?SpaceUpdate { if (hash.len != zat.space.lthash.digest_bytes) return Error.InvalidRecordType; const timestamp = (zat.Tid.parse(rev) orelse return Error.InvalidRecordType).timestamp(); if (timestamp > nowMicros() + 300 * std.time.us_per_s) return error.FutureRev; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); const space_row = try conn.row("SELECT 1 FROM permissioned_spaces WHERE uri = ? AND deleted_at IS NULL", .{space}); if (space_row) |row| row.deinit() else return Error.RepoNotFound; const existing = try conn.row("SELECT rev FROM permissioned_space_writers WHERE space = ? AND repo_did = ?", .{ space, repo_did }); if (existing) |row| { const stale = std.mem.order(u8, rev, row.text(0)) != .gt; row.deinit(); if (stale) { try conn.commit(); return null; } } const update = try nextSpaceRevLocked(allocator, space); // A newer write from a removed writer adds it back to the writer set. try conn.exec("INSERT INTO permissioned_space_writers (space, repo_did, rev, hash, space_rev) VALUES (?, ?, ?, ?, ?) ON CONFLICT(space, repo_did) DO UPDATE SET rev = excluded.rev, hash = excluded.hash, space_rev = excluded.space_rev, removed_at = NULL, updated_at = unixepoch()", .{ space, repo_did, rev, zqlite.blob(hash), update.space_rev }); try conn.commit(); return update; } pub fn markSpaceDeleted(actor_did: []const u8, space: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE permissioned_space_actor_state SET deleted_at = unixepoch() WHERE space = ? AND actor_did = ?", .{ space, actor_did }); try conn.exec("UPDATE permissioned_spaces SET deleted_at = unixepoch() WHERE uri = ? AND authority_did = ?", .{ space, actor_did }); } // Deletion purges everything the authority hosts for the space — its own // records/oplog/repo state and blob links, the member list, the writer // registry, and notify registrations — while the tombstoned space row keeps // answering SpaceDeleted. Members' records live in their own writer repos and // are theirs to keep (proposal: deletion does not entitle the authority to // delete all data in it); the reference PDS purges the same set. A later // recreate of the type+skey therefore starts genuinely empty. pub fn purgeAuthoritySpaceData(space: []const u8, authority_did: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec("DELETE FROM permissioned_space_record_blobs WHERE space = ? AND repo_did = ?", .{ space, authority_did }); try conn.exec("DELETE FROM permissioned_space_records WHERE space = ? AND repo_did = ?", .{ space, authority_did }); try conn.exec("DELETE FROM permissioned_space_record_oplog WHERE space = ? AND repo_did = ?", .{ space, authority_did }); try conn.exec("DELETE FROM permissioned_space_repos WHERE space = ? AND repo_did = ?", .{ space, authority_did }); try conn.exec("DELETE FROM memberspace_members WHERE space = ?", .{space}); try conn.exec("DELETE FROM permissioned_space_writers WHERE space = ?", .{space}); // Every syncer registered at deletion, lapsed or not, is owed a deletion // notice. It replaces whatever notice was pending for that registration. try conn.exec( \\INSERT INTO permissioned_space_syncer_outbox (space, endpoint, token, notice, space_rev, expires_at) \\SELECT space, endpoint, token, 'deleted', NULL, unixepoch() + ? FROM permissioned_space_notify_registrations \\WHERE space = ? \\ON CONFLICT(space, endpoint, token) DO UPDATE SET \\ notice = 'deleted', space_rev = NULL, attempts = 0, retry_at = 0, lease_until = 0, expires_at = excluded.expires_at , .{ syncer_notice_window_s, space }); try conn.exec("DELETE FROM permissioned_space_notify_registrations WHERE space = ?", .{space}); try conn.exec("DELETE FROM permissioned_space_notify_outbox WHERE space = ?", .{space}); try conn.commit(); } /// Mirrors the reference admin.deleteAccount: unlink the account and every /// row it owns, replace its firehose history with a single #account deleted /// event, then remove blob bytes. Spaces the account is authority for go with /// it, and their syncers learn of that from the account event. pub fn deleteAccount(allocator: std.mem.Allocator, did: []const u8) !void { const blob_cids = blk: { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); _ = try accountStatusLocked(did); var cids: std.ArrayList([]const u8) = .empty; { var rows = try conn.rows("SELECT cid FROM blobs WHERE did = ?", .{did}); defer rows.deinit(); while (rows.next()) |row| try cids.append(allocator, try allocator.dupe(u8, row.text(0))); if (rows.err) |err| return err; } try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.exec("DELETE FROM permissioned_space_notify_registrations WHERE space IN (SELECT uri FROM permissioned_spaces WHERE authority_did = ?)", .{did}); try conn.exec("DELETE FROM permissioned_space_syncer_outbox WHERE space IN (SELECT uri FROM permissioned_spaces WHERE authority_did = ?)", .{did}); try conn.exec("DELETE FROM permissioned_spaces WHERE authority_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_record_blobs WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_records WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_record_oplog WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_repos WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_writers WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_notify_outbox WHERE repo_did = ?", .{did}); try conn.exec("DELETE FROM permissioned_space_actor_state WHERE actor_did = ?", .{did}); try conn.exec("DELETE FROM memberspace_members WHERE member_did = ?", .{did}); try conn.exec("DELETE FROM reserved_signing_keys WHERE did = ?", .{did}); try conn.exec("DELETE FROM accounts WHERE did = ?", .{did}); try conn.exec("DELETE FROM seq_events WHERE did = ?", .{did}); const seq = try nextSeqLocked(); const frame = try accountEventFrame(allocator, seq, did, .deleted); try insertSeqEventLocked(seq, did, "", frame); try conn.commit(); eventlog.publish(seq); break :blk cids.items; }; for (blob_cids) |cid| { blobstore.delete(allocator, did, cid) catch |err| { log.err("delete account blob did={s} cid={s} err={s}\n", .{ did, cid, @errorName(err) }); }; } blobstore.deleteActorDir(allocator, did); } /// Whether the registration exists, lapsed or not. Only a new registration is /// confirmed with its syncer; a renewal is not. pub fn notifyRegistrationExists(space: []const u8, registration: NotifyRegistration) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = try conn.row( "SELECT 1 FROM permissioned_space_notify_registrations WHERE space = ? AND endpoint = ? AND token = ?", .{ space, registration.endpoint, registration.token }, ); if (row) |r| { r.deinit(); return true; } return false; } /// Registers, or renews, until `expires_at`: the expiry of the credential the /// registration was last authorized with. The space is checked in the same /// transaction, so a registration cannot outlive a concurrent deletion. pub fn putNotifyRegistration(space: []const u8, registration: NotifyRegistration, expires_at: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); const live = try conn.row("SELECT 1 FROM permissioned_spaces WHERE uri = ? AND deleted_at IS NULL", .{space}); if (live) |row| row.deinit() else return Error.RepoNotFound; try conn.exec( \\INSERT INTO permissioned_space_notify_registrations (space, endpoint, token, expires_at, expiry_notified) \\VALUES (?, ?, ?, ?, 0) \\ON CONFLICT(space, endpoint, token) DO UPDATE SET expires_at = excluded.expires_at, expiry_notified = 0 , .{ space, registration.endpoint, registration.token, expires_at }); try conn.commit(); } /// Withdraws a registration whose syncer was refused the space. pub fn removeNotifyRegistration(space: []const u8, registration: NotifyRegistration) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( "DELETE FROM permissioned_space_notify_registrations WHERE space = ? AND endpoint = ? AND token = ?", .{ space, registration.endpoint, registration.token }, ); } /// The registrations whose credential has not yet expired. pub fn activeNotifyRegistrations(allocator: std.mem.Allocator, space: []const u8) ![]NotifyRegistration { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT endpoint, token FROM permissioned_space_notify_registrations \\WHERE space = ? AND expires_at > unixepoch() \\ORDER BY endpoint, token , .{space}); defer rows.deinit(); var out: std.ArrayList(NotifyRegistration) = .empty; while (rows.next()) |row| { try out.append(allocator, .{ .endpoint = try allocator.dupe(u8, row.text(0)), .token = try allocator.dupe(u8, row.text(1)), }); } if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } /// Queues an expiry notice at `space_rev`, in place of that update, for each /// registration that has lapsed since it was last renewed. Each lapse is /// noticed once; after that the registration hears only of the space's /// deletion until it is renewed. A pending notice is never replaced by one. pub fn queueExpiryNotices(space: []const u8, space_rev: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try conn.exec( \\INSERT INTO permissioned_space_syncer_outbox (space, endpoint, token, notice, space_rev, expires_at) \\SELECT space, endpoint, token, 'expired', ?, unixepoch() + ? FROM permissioned_space_notify_registrations \\WHERE space = ? AND expires_at <= unixepoch() AND expiry_notified = 0 \\ON CONFLICT(space, endpoint, token) DO NOTHING , .{ space_rev, syncer_notice_window_s, space }); try conn.exec( "UPDATE permissioned_space_notify_registrations SET expiry_notified = 1 WHERE space = ? AND expires_at <= unixepoch() AND expiry_notified = 0", .{space}, ); try conn.commit(); } pub fn loadSpaceRepoBlocks(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8) ![]zat.space.archive.RepoRecordBlock { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); var rows = try conn.rows( \\SELECT collection, rkey, cid, value_json \\FROM permissioned_space_records \\WHERE space = ? AND repo_did = ? \\ORDER BY collection ASC, rkey ASC , .{ space, repo_did }); defer rows.deinit(); var blocks: std.ArrayList(zat.space.archive.RepoRecordBlock) = .empty; var scratch = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer scratch.deinit(); while (rows.next()) |row| { _ = scratch.reset(.retain_capacity); const scratch_allocator = scratch.allocator(); const parsed = try std.json.parseFromSlice(std.json.Value, scratch_allocator, row.blob(3), .{}); const value = try recordValueFromJson(scratch_allocator, parsed.value); const data = try zat.cbor.encodeAlloc(allocator, value); const cid_text = row.text(2); if (cid_text.len == 0 or cid_text[0] != 'b') return Error.InvalidDagCbor; try blocks.append(allocator, .{ .path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ row.text(0), row.text(1) }), .cid = .{ .raw = try zat.multibase.base32lower.decode(allocator, cid_text[1..]) }, .data = data, }); } if (rows.err) |err| return err; return blocks.toOwnedSlice(allocator); } pub const SpaceReplayToken = struct { jti: []const u8, expires_at: i64, }; pub fn consumeSpaceCredentialExchange(delegation: ?SpaceReplayToken, attestation: ?SpaceReplayToken) !bool { if (delegation == null and attestation == null) return true; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.execNoArgs("DELETE FROM permissioned_space_used_delegations WHERE expires_at < unixepoch()"); try conn.execNoArgs("DELETE FROM permissioned_space_used_client_attestations WHERE expires_at < unixepoch()"); if (delegation) |token| { conn.exec( \\INSERT INTO permissioned_space_used_delegations (jti, expires_at) \\VALUES (?, ?) , .{ token.jti, token.expires_at }) catch |err| switch (err) { error.Constraint => { conn.rollback(); return false; }, else => return err, }; } if (attestation) |token| { conn.exec( \\INSERT INTO permissioned_space_used_client_attestations (jti, expires_at) \\VALUES (?, ?) , .{ token.jti, token.expires_at }) catch |err| switch (err) { error.Constraint => { conn.rollback(); return false; }, else => return err, }; } try conn.commit(); return true; } pub fn putSpaceRecord( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, prepared: PreparedRecord, ) !SpaceRecord { const results = try applySpaceWrites(allocator, space, repo_did, &.{.{ .put = .{ .collection = collection, .rkey = rkey, .prepared = prepared, } }}); return switch (results[0]) { .create => |record| record, .update => |record| record, .delete => Error.MissingRecord, }; } pub fn createSpaceRecord( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, prepared: PreparedRecord, ) !SpaceRecord { const results = try applySpaceWrites(allocator, space, repo_did, &.{.{ .create = .{ .collection = collection, .rkey = rkey, .prepared = prepared, } }}); return switch (results[0]) { .create => |record| record, .update => |record| record, .delete => Error.MissingRecord, }; } pub fn deleteSpaceRecord( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, ) !void { _ = try applySpaceWrites(allocator, space, repo_did, &.{.{ .delete = .{ .collection = collection, .rkey = rkey } }}); } pub fn getSpaceRecord( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, ) !?SpaceRecord { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return getSpaceRecordLocked(allocator, space, repo_did, collection, rkey); } pub fn listSpaceRecords( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, maybe_collection: ?[]const u8, maybe_cursor: ?[]const u8, limit: usize, include_values: bool, ) ![]SpaceRecordRef { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const capped_limit: i64 = @intCast(@min(if (limit == 0) 50 else limit, 100)); const op = "<"; const direction = "DESC"; const cursor = maybe_cursor orelse ""; const parsed_cursor = parseSpaceRecordCursor(cursor); const cursor_collection = parsed_cursor.collection; const cursor_rkey = parsed_cursor.rkey; var query = std.Io.Writer.Allocating.init(allocator); defer query.deinit(); try query.writer.print( "SELECT collection, rkey, cid, {s} FROM permissioned_space_records WHERE space = ? AND repo_did = ?", .{if (include_values) "value_json" else "NULL"}, ); if (maybe_collection != null) try query.writer.writeAll(" AND collection = ?"); if (maybe_cursor != null and cursor_collection.len > 0 and cursor_rkey.len > 0) { try query.writer.print(" AND (collection, rkey) {s} (?, ?)", .{op}); } try query.writer.print(" ORDER BY collection {s}, rkey {s} LIMIT ?", .{ direction, direction }); var rows = if (maybe_collection) |collection| if (maybe_cursor != null and cursor_collection.len > 0 and cursor_rkey.len > 0) try conn.rows(query.written(), .{ space, repo_did, collection, cursor_collection, cursor_rkey, capped_limit }) else try conn.rows(query.written(), .{ space, repo_did, collection, capped_limit }) else if (maybe_cursor != null and cursor_collection.len > 0 and cursor_rkey.len > 0) try conn.rows(query.written(), .{ space, repo_did, cursor_collection, cursor_rkey, capped_limit }) else try conn.rows(query.written(), .{ space, repo_did, capped_limit }); defer rows.deinit(); var records: std.ArrayList(SpaceRecordRef) = .empty; while (rows.next()) |row| { try records.append(allocator, .{ .collection = try allocator.dupe(u8, row.text(0)), .rkey = try allocator.dupe(u8, row.text(1)), .cid = try allocator.dupe(u8, row.text(2)), .value_json = if (row.nullableText(3)) |value| try allocator.dupe(u8, value) else null, }); } if (rows.err) |err| return err; return records.toOwnedSlice(allocator); } pub fn applySpaceWrites( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, ops: []const SpaceWriteOp, ) ![]SpaceWriteResult { if (zat.Did.parse(repo_did) == null) return Error.InvalidRepoPath; for (ops) |op| switch (op) { inline .create, .put, .update => |write| { if (zat.Nsid.parse(write.collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(write.rkey) == null) return Error.InvalidRecordKey; }, .delete => |write| { if (zat.Nsid.parse(write.collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(write.rkey) == null) return Error.InvalidRecordKey; }, }; const lane = write_lanes.lock(store_io, repo_did); defer lane.unlock(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); _ = (try getSpaceConfigLocked(allocator, space)) orelse return Error.RepoNotFound; try conn.exclusiveTransaction(); errdefer conn.rollback(); const results = try applySpaceWritesLocked(allocator, space, repo_did, ops); try conn.commit(); return results; } /// Applies writes as one commit inside the caller's transaction. fn applySpaceWritesLocked( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, ops: []const SpaceWriteOp, ) ![]SpaceWriteResult { var results: std.ArrayList(SpaceWriteResult) = .empty; const state = try getRepoStateLocked(allocator, space, repo_did); var set_hash = if (state.set_hash) |bytes| try zat.space.LtHash.fromBytes(bytes) else zat.space.LtHash{}; const rev = try revForSeq(allocator, try nextSeqLocked(), state.rev); var idx: i64 = 0; for (ops) |op| { switch (op) { .create => |write| { if (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey) != null) return Error.InvalidRefreshSession; try addRecordElement(allocator, &set_hash, write.collection, write.rkey, write.prepared.cid); try upsertSpaceRecordLocked(space, repo_did, write.collection, write.rkey, write.prepared, rev); try syncSpaceRecordBlobRefsLocked(space, repo_did, write.collection, write.rkey, write.prepared.blob_cids); try insertRecordOplogLocked(space, repo_did, rev, idx, "create", write.collection, write.rkey, write.prepared.cid, null); try results.append(allocator, .{ .create = (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey)) orelse return Error.MissingRecord }); }, .put => |write| { const existing = try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey); if (existing) |old| try removeRecordElement(allocator, &set_hash, old.collection, old.rkey, old.cid); try addRecordElement(allocator, &set_hash, write.collection, write.rkey, write.prepared.cid); try upsertSpaceRecordLocked(space, repo_did, write.collection, write.rkey, write.prepared, rev); try syncSpaceRecordBlobRefsLocked(space, repo_did, write.collection, write.rkey, write.prepared.blob_cids); const action = if (existing == null) "create" else "update"; try insertRecordOplogLocked(space, repo_did, rev, idx, action, write.collection, write.rkey, write.prepared.cid, if (existing) |old| old.cid else null); try results.append(allocator, .{ .update = (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey)) orelse return Error.MissingRecord }); }, .update => |write| { const existing = (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey)) orelse return Error.MissingRecord; try removeRecordElement(allocator, &set_hash, existing.collection, existing.rkey, existing.cid); try addRecordElement(allocator, &set_hash, write.collection, write.rkey, write.prepared.cid); try upsertSpaceRecordLocked(space, repo_did, write.collection, write.rkey, write.prepared, rev); try syncSpaceRecordBlobRefsLocked(space, repo_did, write.collection, write.rkey, write.prepared.blob_cids); try insertRecordOplogLocked(space, repo_did, rev, idx, "update", write.collection, write.rkey, write.prepared.cid, existing.cid); try results.append(allocator, .{ .update = (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey)) orelse return Error.MissingRecord }); }, .delete => |write| { const existing = (try getSpaceRecordLocked(allocator, space, repo_did, write.collection, write.rkey)) orelse return Error.MissingRecord; try removeRecordElement(allocator, &set_hash, existing.collection, existing.rkey, existing.cid); try deleteSpaceRecordBlobRefsLocked(space, repo_did, write.collection, write.rkey); try conn.exec( \\DELETE FROM permissioned_space_records \\WHERE space = ? AND repo_did = ? AND collection = ? AND rkey = ? , .{ space, repo_did, write.collection, write.rkey }); try insertRecordOplogLocked(space, repo_did, rev, idx, "delete", write.collection, write.rkey, null, existing.cid); try results.append(allocator, .{ .delete = {} }); }, } idx += 1; } try conn.exec( \\INSERT INTO permissioned_space_repos (space, repo_did, set_hash, rev) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(space, repo_did) DO UPDATE SET \\ set_hash = excluded.set_hash, \\ rev = excluded.rev , .{ space, repo_did, zqlite.blob(&set_hash.bytes), rev }); try conn.exec( \\INSERT INTO permissioned_space_actor_state (space, actor_did, is_authority) \\VALUES (?, ?, 0) \\ON CONFLICT(space, actor_did) DO NOTHING , .{ space, repo_did }); try queueSpaceNotifyLocked(space, repo_did, rev, set_hash.digest()); return results.toOwnedSlice(allocator); } /// Queued already leased: the request that made the write delivers it inline, /// and the worker must not claim the same row meanwhile and deliver it twice. /// The inline delivery finishes the row, or releases it on a transient failure. fn queueSpaceNotifyLocked(space: []const u8, repo_did: []const u8, rev: []const u8, digest: [zat.space.lthash.digest_bytes]u8) !void { try conn.exec("INSERT INTO permissioned_space_notify_outbox(space, repo_did, rev, hash, lease_until, expires_at) VALUES (?, ?, ?, ?, unixepoch() + ?, unixepoch() + 86400) ON CONFLICT(space, repo_did) DO UPDATE SET rev = excluded.rev, hash = excluded.hash, attempts = 0, retry_at = 0, lease_until = excluded.lease_until, expires_at = excluded.expires_at", .{ space, repo_did, rev, zqlite.blob(&digest), inline_notify_lease_seconds }); } const inline_notify_lease_seconds: i64 = 120; pub fn getSpaceRepoState(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8) !SpaceState { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return getRepoStateLocked(allocator, space, repo_did); } pub fn listSpaceRecordOplog(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, since: ?[]const u8, cursor: ?OplogCursor, limit: usize, include_values: bool) ![]SpaceRecordOplogEntry { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const capped_limit: i64 = @intCast(@min(if (limit == 0) 100 else limit, 1000)); var rows = if (cursor) |position| try conn.rows( \\SELECT o.rev, o.idx, o.action, o.repo_did, o.collection, o.rkey, o.cid, o.prev, \\ CASE WHEN ? AND r.cid = o.cid THEN r.value_json END \\FROM permissioned_space_record_oplog o \\LEFT JOIN permissioned_space_records r \\ ON r.space = o.space AND r.repo_did = o.repo_did \\ AND r.collection = o.collection AND r.rkey = o.rkey \\WHERE o.space = ? AND o.repo_did = ? AND (o.rev > ? OR (o.rev = ? AND o.idx > ?)) \\ORDER BY o.rev ASC, o.idx ASC \\LIMIT ? , .{ include_values, space, repo_did, position.rev, position.rev, position.idx, capped_limit }) else if (since) |rev| try conn.rows( \\SELECT o.rev, o.idx, o.action, o.repo_did, o.collection, o.rkey, o.cid, o.prev, \\ CASE WHEN ? AND r.cid = o.cid THEN r.value_json END \\FROM permissioned_space_record_oplog o \\LEFT JOIN permissioned_space_records r \\ ON r.space = o.space AND r.repo_did = o.repo_did \\ AND r.collection = o.collection AND r.rkey = o.rkey \\WHERE o.space = ? AND o.repo_did = ? AND o.rev > ? \\ORDER BY o.rev ASC, o.idx ASC \\LIMIT ? , .{ include_values, space, repo_did, rev, capped_limit }) else try conn.rows( \\SELECT o.rev, o.idx, o.action, o.repo_did, o.collection, o.rkey, o.cid, o.prev, \\ CASE WHEN ? AND r.cid = o.cid THEN r.value_json END \\FROM permissioned_space_record_oplog o \\LEFT JOIN permissioned_space_records r \\ ON r.space = o.space AND r.repo_did = o.repo_did \\ AND r.collection = o.collection AND r.rkey = o.rkey \\WHERE o.space = ? AND o.repo_did = ? \\ORDER BY o.rev ASC, o.idx ASC \\LIMIT ? , .{ include_values, space, repo_did, capped_limit }); defer rows.deinit(); var out: std.ArrayList(SpaceRecordOplogEntry) = .empty; while (rows.next()) |row| { try out.append(allocator, .{ .rev = try allocator.dupe(u8, row.text(0)), .idx = row.int(1), .action = try allocator.dupe(u8, row.text(2)), .repo_did = try allocator.dupe(u8, row.text(3)), .collection = try allocator.dupe(u8, row.text(4)), .rkey = try allocator.dupe(u8, row.text(5)), .cid = if (row.nullableText(6)) |cid| try allocator.dupe(u8, cid) else null, .prev = if (row.nullableText(7)) |prev| try allocator.dupe(u8, prev) else null, .value_json = if (row.nullableBlob(8)) |value| try allocator.dupe(u8, value) else null, }); } if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } fn getRepoStateLocked(allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8) !SpaceState { const row = try conn.row( \\SELECT set_hash, rev \\FROM permissioned_space_repos \\WHERE space = ? AND repo_did = ? , .{ space, repo_did }); if (row == null) return .{ .set_hash = null, .rev = null }; defer row.?.deinit(); return .{ .set_hash = if (row.?.nullableBlob(0)) |bytes| try allocator.dupe(u8, bytes) else null, .rev = if (row.?.nullableText(1)) |rev| try allocator.dupe(u8, rev) else null, }; } fn addRecordElement(allocator: std.mem.Allocator, hash: *zat.space.LtHash, collection: []const u8, rkey: []const u8, cid: []const u8) !void { const element = try zat.space.recordElement(allocator, collection, rkey, cid); hash.add(element); } fn removeRecordElement(allocator: std.mem.Allocator, hash: *zat.space.LtHash, collection: []const u8, rkey: []const u8, cid: []const u8) !void { const element = try zat.space.recordElement(allocator, collection, rkey, cid); hash.remove(element); } fn upsertSpaceRecordLocked( space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, prepared: PreparedRecord, rev: []const u8, ) !void { try conn.exec( \\INSERT INTO permissioned_space_records (space, repo_did, collection, rkey, cid, value_json, validation_status, repo_rev, updated_at) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, unixepoch()) \\ON CONFLICT(space, repo_did, collection, rkey) DO UPDATE SET \\ cid = excluded.cid, \\ value_json = excluded.value_json, \\ validation_status = excluded.validation_status, \\ repo_rev = excluded.repo_rev, \\ updated_at = excluded.updated_at , .{ space, repo_did, collection, rkey, prepared.cid, prepared.value_json, prepared.validation_status, rev }); } fn syncSpaceRecordBlobRefsLocked( space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, blob_cids: []const []const u8, ) !void { try deleteSpaceRecordBlobRefsLocked(space, repo_did, collection, rkey); for (blob_cids) |blob_cid| { try conn.exec( \\INSERT INTO permissioned_space_record_blobs (space, repo_did, collection, rkey, blob_cid) \\VALUES (?, ?, ?, ?, ?) \\ON CONFLICT(space, repo_did, collection, rkey, blob_cid) DO UPDATE SET \\ blob_cid = excluded.blob_cid , .{ space, repo_did, collection, rkey, blob_cid }); } } fn deleteSpaceRecordBlobRefsLocked( space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, ) !void { try conn.exec( \\DELETE FROM permissioned_space_record_blobs \\WHERE space = ? AND repo_did = ? AND collection = ? AND rkey = ? , .{ space, repo_did, collection, rkey }); } fn insertRecordOplogLocked( space: []const u8, repo_did: []const u8, rev: []const u8, idx: i64, action: []const u8, collection: []const u8, rkey: []const u8, cid: ?[]const u8, prev: ?[]const u8, ) !void { try conn.exec( \\INSERT INTO permissioned_space_record_oplog (space, repo_did, rev, idx, action, collection, rkey, cid, prev) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) , .{ space, repo_did, rev, idx, action, collection, rkey, cid, prev }); } fn getSpaceConfigLocked(allocator: std.mem.Allocator, uri: []const u8) !?SpaceConfig { const row = try conn.row( \\SELECT s.uri, s.authority_did, s.space_type, s.skey, s.policy_json, \\ EXISTS ( \\ SELECT 1 FROM permissioned_space_actor_state a \\ WHERE a.space = s.uri AND a.actor_did = s.authority_did \\ AND a.is_authority = 1 AND a.deleted_at IS NULL \\ ) \\FROM permissioned_spaces s \\WHERE s.uri = ? AND s.deleted_at IS NULL , .{uri}); if (row == null) return null; defer row.?.deinit(); return .{ .uri = try allocator.dupe(u8, row.?.text(0)), .authority_did = try allocator.dupe(u8, row.?.text(1)), .space_type = try allocator.dupe(u8, row.?.text(2)), .skey = try allocator.dupe(u8, row.?.text(3)), .policy_json = if (row.?.nullableText(4)) |value| try allocator.dupe(u8, value) else null, .is_authority = row.?.int(5) != 0, .deleted_at = null, }; } fn getSpaceLocked(allocator: std.mem.Allocator, actor_did: []const u8, uri: []const u8) !?SpaceConfig { const row = try conn.row( \\SELECT s.uri, s.authority_did, s.space_type, s.skey, s.policy_json, a.is_authority, a.deleted_at \\FROM permissioned_spaces s \\JOIN permissioned_space_actor_state a ON a.space = s.uri AND a.actor_did = ? \\WHERE s.uri = ? AND s.deleted_at IS NULL AND a.deleted_at IS NULL , .{ actor_did, uri }); if (row == null) return null; defer row.?.deinit(); return .{ .uri = try allocator.dupe(u8, row.?.text(0)), .authority_did = try allocator.dupe(u8, row.?.text(1)), .space_type = try allocator.dupe(u8, row.?.text(2)), .skey = try allocator.dupe(u8, row.?.text(3)), .policy_json = if (row.?.nullableText(4)) |value| try allocator.dupe(u8, value) else null, .is_authority = row.?.int(5) != 0, .deleted_at = if (row.?.nullableInt(6)) |value| value else null, }; } fn getSpaceRecordLocked( allocator: std.mem.Allocator, space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, ) !?SpaceRecord { const row = try conn.row( \\SELECT space, repo_did, collection, rkey, cid, value_json, validation_status, repo_rev, updated_at \\FROM permissioned_space_records \\WHERE space = ? AND repo_did = ? AND collection = ? AND rkey = ? , .{ space, repo_did, collection, rkey }); if (row == null) return null; defer row.?.deinit(); return .{ .space = try allocator.dupe(u8, row.?.text(0)), .repo_did = try allocator.dupe(u8, row.?.text(1)), .collection = try allocator.dupe(u8, row.?.text(2)), .rkey = try allocator.dupe(u8, row.?.text(3)), .cid = try allocator.dupe(u8, row.?.text(4)), .value_json = try allocator.dupe(u8, row.?.text(5)), .validation_status = try allocator.dupe(u8, row.?.text(6)), .repo_rev = try allocator.dupe(u8, row.?.text(7)), .updated_at = row.?.int(8), }; } fn parseSpaceRecordCursor(cursor: []const u8) struct { collection: []const u8, rkey: []const u8 } { const slash = std.mem.indexOfScalar(u8, cursor, '/') orelse return .{ .collection = "", .rkey = "" }; if (slash == 0 or slash + 1 >= cursor.len) return .{ .collection = "", .rkey = "" }; return .{ .collection = cursor[0..slash], .rkey = cursor[slash + 1 ..] }; } fn spaceWriterColumnExists(column: []const u8) !bool { var rows = try conn.rows("PRAGMA table_info(permissioned_space_writers)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), column)) return true; } if (rows.err) |err| return err; return false; } pub const PendingSpaceNotification = struct { space: []const u8, repo: []const u8, rev: []const u8, hash: []const u8, attempts: i64, }; pub fn claimSpaceNotification(allocator: std.mem.Allocator) !?PendingSpaceNotification { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try conn.execNoArgs("DELETE FROM permissioned_space_notify_outbox WHERE expires_at <= unixepoch()"); const row = try conn.row("SELECT space, repo_did, rev, hash, attempts FROM permissioned_space_notify_outbox WHERE retry_at <= unixepoch() AND lease_until <= unixepoch() ORDER BY retry_at LIMIT 1", .{}); if (row == null) { try conn.commit(); return null; } const pending = blk: { const r = row.?; defer r.deinit(); break :blk PendingSpaceNotification{ .space = try allocator.dupe(u8, r.text(0)), .repo = try allocator.dupe(u8, r.text(1)), .rev = try allocator.dupe(u8, r.text(2)), .hash = try allocator.dupe(u8, r.blob(3)), .attempts = r.int(4) + 1, }; }; try conn.exec("UPDATE permissioned_space_notify_outbox SET attempts = ?, lease_until = unixepoch() + 120 WHERE space = ? AND repo_did = ?", .{ pending.attempts, pending.space, pending.repo }); try conn.commit(); return pending; } pub fn finishSpaceNotification(space: []const u8, repo: []const u8, rev: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM permissioned_space_notify_outbox WHERE space = ? AND repo_did = ? AND rev = ?", .{ space, repo, rev }); } pub fn releaseSpaceNotification(space: []const u8, repo: []const u8, rev: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE permissioned_space_notify_outbox SET lease_until = 0 WHERE space = ? AND repo_did = ? AND rev = ? AND attempts = 0", .{ space, repo, rev }); } /// Notices to syncers are retried for at least this long. const syncer_notice_window_s = 72 * 60 * 60; /// A notice the space host owes a registered syncer, delivered reliably. pub const SyncerNotice = enum { /// `#spaceDeleted`: the space was deleted. deleted, /// `#registrationExpired`: the registration lapsed before `space_rev`. expired, }; pub const PendingSyncerNotice = struct { space: []const u8, registration: NotifyRegistration, notice: SyncerNotice, /// The update an expiry notice was sent in place of. space_rev: ?[]const u8, attempts: i64, expires_at: i64, }; pub fn claimSyncerNotice(allocator: std.mem.Allocator) !?PendingSyncerNotice { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); const row = try conn.row( \\SELECT space, endpoint, token, notice, space_rev, attempts, expires_at FROM permissioned_space_syncer_outbox \\WHERE retry_at <= unixepoch() AND lease_until <= unixepoch() ORDER BY retry_at LIMIT 1 , .{}); if (row == null) { try conn.commit(); return null; } const pending = blk: { const r = row.?; defer r.deinit(); break :blk PendingSyncerNotice{ .space = try allocator.dupe(u8, r.text(0)), .registration = .{ .endpoint = try allocator.dupe(u8, r.text(1)), .token = try allocator.dupe(u8, r.text(2)) }, .notice = std.meta.stringToEnum(SyncerNotice, r.text(3)) orelse .deleted, .space_rev = if (r.nullableText(4)) |rev| try allocator.dupe(u8, rev) else null, .attempts = r.int(5) + 1, .expires_at = r.int(6), }; }; try conn.exec( "UPDATE permissioned_space_syncer_outbox SET attempts = ?, lease_until = unixepoch() + 120 WHERE space = ? AND endpoint = ? AND token = ?", .{ pending.attempts, pending.space, pending.registration.endpoint, pending.registration.token }, ); try conn.commit(); return pending; } /// Clears a delivered notice, unless another has replaced it since it was claimed. pub fn finishSyncerNotice(pending: PendingSyncerNotice) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try finishSyncerNoticeLocked(pending); } fn finishSyncerNoticeLocked(pending: PendingSyncerNotice) !void { try conn.exec( "DELETE FROM permissioned_space_syncer_outbox WHERE space = ? AND endpoint = ? AND token = ? AND notice = ? AND attempts = ?", .{ pending.space, pending.registration.endpoint, pending.registration.token, @tagName(pending.notice), pending.attempts }, ); } /// Schedules the next attempt: one minute, ten minutes, an hour, six hours, /// then every twelve hours. pub fn retrySyncerNotice(pending: PendingSyncerNotice) !void { const delays = [_]i64{ 60, 600, 3600, 6 * 3600, 12 * 3600 }; const delay = delays[@intCast(@min(@max(pending.attempts, 1), delays.len) - 1)]; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec( "UPDATE permissioned_space_syncer_outbox SET retry_at = unixepoch() + ?, lease_until = 0 WHERE space = ? AND endpoint = ? AND token = ? AND notice = ? AND attempts = ?", .{ delay, pending.space, pending.registration.endpoint, pending.registration.token, @tagName(pending.notice), pending.attempts }, ); } /// Gives up on a notice. A syncer that rejects its expiry notice, or cannot be /// reached within the retry window, is unregistered unless it renewed since. /// A deleted space's registrations went with it. pub fn abandonSyncerNotice(pending: PendingSyncerNotice) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try finishSyncerNoticeLocked(pending); if (pending.notice == .expired) try conn.exec( "DELETE FROM permissioned_space_notify_registrations WHERE space = ? AND endpoint = ? AND token = ? AND expires_at <= unixepoch()", .{ pending.space, pending.registration.endpoint, pending.registration.token }, ); try conn.commit(); } /// Whether a notice still applies: an expiry notice only while the /// registration is still lapsed, a deletion notice only while the space is /// still deleted and not recreated. pub fn syncerNoticeOwed(pending: PendingSyncerNotice) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = switch (pending.notice) { .expired => try conn.row( "SELECT 1 FROM permissioned_space_notify_registrations WHERE space = ? AND endpoint = ? AND token = ? AND expires_at <= unixepoch()", .{ pending.space, pending.registration.endpoint, pending.registration.token }, ), .deleted => try conn.row("SELECT 1 FROM permissioned_spaces WHERE uri = ? AND deleted_at IS NOT NULL", .{pending.space}), }; if (row) |r| { r.deinit(); return true; } return false; } fn retryDelay(attempts: i64) i64 { return @min(3600, @as(i64, 5) << @as(u6, @intCast(@min(attempts - 1, 10)))); } pub fn retrySpaceNotification(pending: PendingSpaceNotification) !void { const delay = retryDelay(pending.attempts); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE permissioned_space_notify_outbox SET retry_at = unixepoch() + ?, lease_until = 0 WHERE space = ? AND repo_did = ? AND rev = ? AND attempts = ?", .{ delay, pending.space, pending.repo, pending.rev, pending.attempts }); } fn migrateSpaceSequence() !void { try conn.execNoArgs("CREATE TABLE IF NOT EXISTS permissioned_space_sequence(space TEXT PRIMARY KEY REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, rev TEXT NOT NULL)"); try conn.execNoArgs("CREATE TABLE IF NOT EXISTS permissioned_space_notify_outbox(space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, repo_did TEXT NOT NULL, rev TEXT NOT NULL, hash BLOB NOT NULL, attempts INTEGER NOT NULL DEFAULT 0, retry_at INTEGER NOT NULL DEFAULT 0, lease_until INTEGER NOT NULL DEFAULT 0, expires_at INTEGER NOT NULL, PRIMARY KEY(space, repo_did))"); if (!try spaceWriterColumnExists("space_rev")) { try conn.transaction(); errdefer conn.rollback(); try conn.execNoArgs("ALTER TABLE permissioned_space_writers ADD COLUMN space_rev TEXT"); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const a = arena.allocator(); var rows = try conn.rows("SELECT space, repo_did FROM permissioned_space_writers ORDER BY space, repo_did", .{}); defer rows.deinit(); var previous: ?[]const u8 = null; while (rows.next()) |row| { const rev = try revForSeq(a, 0, previous); try conn.exec("UPDATE permissioned_space_writers SET space_rev = ? WHERE space = ? AND repo_did = ?", .{ rev, row.text(0), row.text(1) }); try conn.exec("INSERT INTO permissioned_space_sequence(space, rev) VALUES (?, ?) ON CONFLICT(space) DO UPDATE SET rev = excluded.rev", .{ row.text(0), rev }); previous = rev; } if (rows.err) |err| return err; try conn.commit(); } try conn.execNoArgs("CREATE INDEX IF NOT EXISTS permissioned_space_writer_sequence ON permissioned_space_writers(space, space_rev)"); if (!try spaceWriterColumnExists("removed_at")) try conn.execNoArgs("ALTER TABLE permissioned_space_writers ADD COLUMN removed_at INTEGER"); } fn migrateSpaceUriScheme() !void { const name = "permissioned-space-at-space-uri"; if (try migrationApplied(name)) return; try conn.execNoArgs("PRAGMA foreign_keys = OFF"); errdefer conn.execNoArgs("PRAGMA foreign_keys = ON") catch {}; try conn.exclusiveTransaction(); errdefer conn.rollback(); // Revocations and queued syncer notices may name a removed or remote space. inline for (.{ "permissioned_spaces", "permissioned_space_actor_state", "memberspace_members", "permissioned_space_records", "permissioned_space_record_blobs", "permissioned_space_repos", "permissioned_space_writers", "permissioned_space_record_oplog", "permissioned_space_notify_registrations", "permissioned_space_sequence", "permissioned_space_notify_outbox", "permissioned_space_syncer_outbox", "permissioned_space_revocations", }) |table| { const column = if (comptime std.mem.eql(u8, table, "permissioned_spaces")) "uri" else "space"; const authority_len = "instr(substr(" ++ column ++ ", 6), '/') - 1"; try conn.execNoArgs("UPDATE " ++ table ++ " SET " ++ column ++ " = 'at:space/' || substr(" ++ column ++ ", 6, " ++ authority_len ++ ") || '/' || substr(" ++ column ++ ", 13 + " ++ authority_len ++ ")" ++ " WHERE substr(" ++ column ++ ", 1, 5) = 'at://' AND substr(" ++ column ++ ", 6 + " ++ authority_len ++ ", 7) = '/space/'"); } if (try conn.row("PRAGMA foreign_key_check", .{})) |row| { defer row.deinit(); return error.SpaceUriMigrationForeignKeyMismatch; } try markMigrationApplied(name); try conn.commit(); try conn.execNoArgs("PRAGMA foreign_keys = ON"); } fn migrate() !void { inline for (schema_statements) |sql| try conn.execNoArgs(sql); try conn.execNoArgs("CREATE TABLE IF NOT EXISTS permissioned_space_revocations (space TEXT NOT NULL, jti TEXT NOT NULL, expires_at INTEGER NOT NULL, PRIMARY KEY(space, jti))"); try migratePermissionedDataTables(); try migrateSpaceSequence(); try migrateSpaceUriScheme(); const had_deactivated_at = try accountsColumnExistsLocked("deactivated_at"); conn.execNoArgs("ALTER TABLE accounts ADD COLUMN email TEXT") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN email_confirmed_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN auth_code TEXT") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN auth_code_expires_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN pending_email TEXT") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN invites_disabled INTEGER NOT NULL DEFAULT 0") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN activated_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN account_status TEXT NOT NULL DEFAULT 'active'") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN account_status_ref TEXT") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN deactivated_beneath_takedown INTEGER NOT NULL DEFAULT 0") catch {}; conn.execNoArgs("UPDATE accounts SET account_status = 'deactivated' WHERE activated_at IS NULL AND account_status = 'active'") catch {}; if (had_deactivated_at) { conn.execNoArgs("UPDATE accounts SET account_status = 'deactivated' WHERE deactivated_at IS NOT NULL AND account_status = 'active'") catch {}; } conn.execNoArgs("ALTER TABLE accounts ADD COLUMN signing_key_type TEXT") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN signing_key BLOB") catch {}; conn.execNoArgs("ALTER TABLE accounts ADD COLUMN kem_key BLOB") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN login_hint TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN response_mode TEXT NOT NULL DEFAULT 'query'") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN auth_method TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN prompt TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN signin_hash TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN signin_did TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_requests ADD COLUMN signin_method TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN auth_method TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN dpop_jkt TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN family_id TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN access_expires_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN refresh_expires_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN previous_refresh_token TEXT") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN previous_refresh_expires_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE oauth_tokens ADD COLUMN session_started_at INTEGER") catch {}; conn.execNoArgs("UPDATE oauth_tokens SET family_id = access_token WHERE family_id IS NULL") catch {}; conn.execNoArgs("UPDATE oauth_tokens SET access_expires_at = expires_at WHERE access_expires_at IS NULL") catch {}; conn.execNoArgs("UPDATE oauth_tokens SET refresh_expires_at = expires_at WHERE refresh_expires_at IS NULL") catch {}; conn.execNoArgs("UPDATE oauth_tokens SET session_started_at = unixepoch() WHERE session_started_at IS NULL") catch {}; conn.execNoArgs("ALTER TABLE session_tokens ADD COLUMN app_password_name TEXT") catch {}; conn.execNoArgs("ALTER TABLE session_tokens ADD COLUMN previous_refresh_jti TEXT") catch {}; conn.execNoArgs("ALTER TABLE session_tokens ADD COLUMN previous_refresh_expires_at INTEGER") catch {}; conn.execNoArgs("ALTER TABLE permissioned_space_records ADD COLUMN repo_rev TEXT NOT NULL DEFAULT ''") catch {}; try migrateBlobTable(); try migrateAppPreferencesTable(); conn.execNoArgs("ALTER TABLE repo_blocks ADD COLUMN repo_rev TEXT") catch {}; inline for (post_schema_statements) |sql| try conn.execNoArgs(sql); try migrateRepoBlockRevs(); try migrateRecordsTable(); try validateRecordBlocksPresent(); try migrateSeqEventsSync11(); try migrateSeqEventsSinceRev(); try migrateSeqEventsWithoutAccountFk(); try migrateSeqEventsRequiredCommitFields(); } fn migratePermissionedDataTables() !void { try ensureMigrationTable(); try migratePermissionedSpacesAuthorityColumns(); try migratePermissionedSpaceAuthorityFlag(); try migrateSimpleSpaceConfig(); try migratePermissionedRecordOplogRepo(); try migratePermissionedSpaceUris(); try migratePermissionedSpaceBlobRefs(); try migratePermissionedSpaceRepoHashes(); try migratePermissionedSpaceWriters(); try migrateSpacePolicies(); try migrateServiceRefs(); try migrateNotifyWebhooks(); } /// The November 12 alpha registers a syncer's endpoint and token through /// authorizeSpaceSync in place of a registerNotify service, and sends it /// notices through notifySpaceUpdate in place of notifySpaceStatus. Neither /// the old registrations nor pending deletion notices name a token, so they /// are dropped, as the reference PDS's migrations drop theirs; syncers /// re-register on their next authorization. fn migrateNotifyWebhooks() !void { const name = "permissioned-space-notify-webhook-v1"; if (try migrationApplied(name)) return; try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_space_credential_recipients"); try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_space_notify_registrations"); try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_space_deleted_outbox"); try conn.execNoArgs( \\CREATE TABLE permissioned_space_notify_registrations ( \\ space TEXT NOT NULL, \\ endpoint TEXT NOT NULL, \\ token TEXT NOT NULL, \\ expires_at INTEGER NOT NULL, \\ expiry_notified INTEGER NOT NULL DEFAULT 0, \\ PRIMARY KEY (space, endpoint, token) \\) ); try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS permissioned_space_syncer_outbox ( \\ space TEXT NOT NULL, \\ endpoint TEXT NOT NULL, \\ token TEXT NOT NULL, \\ notice TEXT NOT NULL, \\ space_rev TEXT, \\ attempts INTEGER NOT NULL DEFAULT 0, \\ retry_at INTEGER NOT NULL DEFAULT 0, \\ lease_until INTEGER NOT NULL DEFAULT 0, \\ expires_at INTEGER NOT NULL, \\ PRIMARY KEY (space, endpoint, token) \\) ); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS permissioned_space_syncer_outbox_retry ON permissioned_space_syncer_outbox(retry_at)"); try markMigrationApplied(name); try conn.commit(); } /// The lexicon review made `managingApp` a `did#serviceId` reference. A bare /// DID used to resolve to the DID's PDS endpoint, so it becomes /// `did#atproto_pds`, which resolves to the same place. (It rewrote /// registerNotify services the same way; migrateNotifyWebhooks drops those.) fn migrateServiceRefs() !void { const name = "service-refs-v1"; if (try migrationApplied(name)) return; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const a = arena.allocator(); try conn.execNoArgs("BEGIN IMMEDIATE"); errdefer conn.execNoArgs("ROLLBACK") catch {}; var updates: std.ArrayList(struct { uri: []const u8, policy_json: []const u8 }) = .empty; { var rows = try conn.rows("SELECT uri, policy_json FROM permissioned_spaces WHERE policy_json LIKE '%managingApp%'", .{}); defer rows.deinit(); while (rows.next()) |row| { var parsed = std.json.parseFromSliceLeaky(std.json.Value, a, row.text(1), .{}) catch continue; if (parsed != .object) continue; const app = parsed.object.get("managingApp") orelse continue; if (app != .string or std.mem.indexOfScalar(u8, app.string, '#') != null) continue; try parsed.object.put(a, "managingApp", .{ .string = try std.fmt.allocPrint(a, "{s}#atproto_pds", .{app.string}) }); try updates.append(a, .{ .uri = try a.dupe(u8, row.text(0)), .policy_json = try std.fmt.allocPrint(a, "{f}", .{std.json.fmt(parsed, .{})}) }); } if (rows.err) |err| return err; } for (updates.items) |update| try conn.exec("UPDATE permissioned_spaces SET policy_json = ? WHERE uri = ?", .{ update.policy_json, update.uri }); try markMigrationApplied(name); try conn.execNoArgs("COMMIT"); } // Converts each simplespace configuration to a space policy the way the // reference PDS's 006-space-policy does: a managing app on either side becomes // a managedspace, whose member list and app allow-list are dropped. The // authority's own member rows go as well, since the authority is authorized // under every policy. fn migrateSpacePolicies() !void { const name = "space-policy-v1"; if (try migrationApplied(name)) return; if (!try permissionedSpacesColumnExistsLocked("read_policy")) { try markMigrationApplied(name); return; } var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const a = arena.allocator(); try conn.exclusiveTransaction(); errdefer conn.rollback(); if (!try permissionedSpacesColumnExistsLocked("policy_json")) { try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN policy_json TEXT"); } const Converted = struct { uri: []const u8, policy_json: []const u8, keeps_members: bool, mixed: bool, allow_list_dropped: bool }; var converted: std.ArrayList(Converted) = .empty; { var rows = try conn.rows( \\SELECT uri, read_policy, read_managing_app, write_policy, write_managing_app, app_access_json \\FROM permissioned_spaces s \\WHERE EXISTS ( \\ SELECT 1 FROM permissioned_space_actor_state a \\ WHERE a.space = s.uri AND a.actor_did = s.authority_did AND a.is_authority = 1 \\) , .{}); defer rows.deinit(); while (rows.next()) |row| { const policy = space_policy.fromLegacy(a, .{ .read_policy = row.text(1), .read_managing_app = row.nullableText(2), .write_policy = row.text(3), .write_managing_app = row.nullableText(4), .app_access_json = row.text(5), }) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => return Error.InvalidRecordType, }; const read_app = std.mem.eql(u8, row.text(1), "managing-app"); const write_app = std.mem.eql(u8, row.text(3), "managing-app"); try converted.append(a, .{ .uri = try a.dupe(u8, row.text(0)), .policy_json = try space_policy.toJson(a, policy), .keeps_members = policy == .member, .mixed = read_app != write_app, .allow_list_dropped = policy == .managed and std.mem.indexOf(u8, row.text(5), "allowList") != null, }); } if (rows.err) |err| return err; } var members_dropped: i64 = 0; for (converted.items) |space| { try conn.exec("UPDATE permissioned_spaces SET policy_json = ? WHERE uri = ?", .{ space.policy_json, space.uri }); var dropped: i64 = 0; if (!space.keeps_members and try tableExistsLocked("simplespace_members")) { const member_row = (try conn.row( "SELECT COUNT(*) FROM simplespace_members m JOIN permissioned_spaces s ON s.uri = m.space WHERE m.space = ? AND m.member_did != s.authority_did", .{space.uri}, )).?; dropped = member_row.int(0); member_row.deinit(); try conn.exec("DELETE FROM simplespace_members WHERE space = ?", .{space.uri}); } members_dropped += dropped; // The operator's record of every conversion that changes who has access. if (space.mixed or space.allow_list_dropped or dropped > 0) { log.info("space policy migration changed access uri={s} policy={s} mixed={} allow_list_dropped={} members_dropped={d}\n", .{ space.uri, space.policy_json, space.mixed, space.allow_list_dropped, dropped }); } } log.info("space policy migration spaces={d} members_dropped={d}\n", .{ converted.items.len, members_dropped }); if (try tableExistsLocked("simplespace_members")) { try conn.execNoArgs( \\INSERT OR IGNORE INTO memberspace_members (space, member_did, read, write, created_at) \\SELECT m.space, m.member_did, m.read, m.write, m.created_at \\FROM simplespace_members m \\JOIN permissioned_spaces s ON s.uri = m.space \\WHERE m.member_did != s.authority_did ); try conn.execNoArgs("DROP TABLE simplespace_members"); } inline for (.{ "read_policy", "read_managing_app", "write_policy", "write_managing_app", "app_access_json" }) |column| { try conn.execNoArgs("ALTER TABLE permissioned_spaces DROP COLUMN " ++ column); } try markMigrationApplied(name); try conn.commit(); } fn tableExistsLocked(name: []const u8) !bool { const row = try conn.row("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?", .{name}); if (row == null) return false; row.?.deinit(); return true; } fn migratePermissionedSpaceBlobRefs() !void { const name = "permissioned-space-blob-refs-v1"; if (try migrationApplied(name)) return; const ExistingRecord = struct { space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, value_json: []const u8, }; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var records: std.ArrayList(ExistingRecord) = .empty; { var rows = try conn.rows( \\SELECT space, repo_did, collection, rkey, value_json \\FROM permissioned_space_records , .{}); defer rows.deinit(); while (rows.next()) |row| { try records.append(allocator, .{ .space = try allocator.dupe(u8, row.text(0)), .repo_did = try allocator.dupe(u8, row.text(1)), .collection = try allocator.dupe(u8, row.text(2)), .rkey = try allocator.dupe(u8, row.text(3)), .value_json = try allocator.dupe(u8, row.blob(4)), }); } if (rows.err) |err| return err; } try conn.exclusiveTransaction(); errdefer conn.rollback(); for (records.items) |record| { const parsed = try std.json.parseFromSlice(std.json.Value, allocator, record.value_json, .{}); const value = try recordValueFromJson(allocator, parsed.value); try syncSpaceRecordBlobRefsLocked(record.space, record.repo_did, record.collection, record.rkey, try blobCidTexts(allocator, value)); } try markMigrationApplied(name); try conn.commit(); } fn migratePermissionedSpaceUris() !void { const name = "permissioned-space-at-uri"; if (try migrationApplied(name)) return; try conn.execNoArgs("PRAGMA foreign_keys = OFF"); errdefer conn.execNoArgs("PRAGMA foreign_keys = ON") catch {}; try conn.exclusiveTransaction(); errdefer conn.rollback(); const tables = [_][]const u8{ "permissioned_space_actor_state", "simplespace_members", "memberspace_members", "permissioned_space_records", "permissioned_space_record_blobs", "permissioned_space_repos", "permissioned_space_writers", "permissioned_space_record_oplog", "permissioned_space_notify_registrations", }; inline for (tables) |table| if (try tableExistsLocked(table)) { try conn.execNoArgs( "UPDATE " ++ table ++ " SET space = (" ++ "SELECT 'at://' || authority_did || '/space/' || space_type || '/' || skey " ++ "FROM permissioned_spaces WHERE uri = " ++ table ++ ".space" ++ ") WHERE space LIKE 'ats://%'", ); }; try conn.execNoArgs( \\UPDATE permissioned_spaces \\SET uri = 'at://' || authority_did || '/space/' || space_type || '/' || skey \\WHERE uri LIKE 'ats://%' ); try markMigrationApplied(name); try conn.commit(); try conn.execNoArgs("PRAGMA foreign_keys = ON"); } fn migratePermissionedSpaceRepoHashes() !void { const name = "permissioned-space-record-element-v1"; if (try migrationApplied(name)) return; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); const RepoIdentity = struct { space: []const u8, did: []const u8, has_rev: bool }; var identities: std.ArrayList(RepoIdentity) = .empty; var repos = try conn.rows("SELECT space, repo_did, rev FROM permissioned_space_repos", .{}); while (repos.next()) |repo| { try identities.append(allocator, .{ .space = try allocator.dupe(u8, repo.text(0)), .did = try allocator.dupe(u8, repo.text(1)), .has_rev = repo.nullableText(2) != null, }); } if (repos.err) |err| return err; repos.deinit(); for (identities.items) |identity| { var hash: zat.space.LtHash = .{}; var record_count: usize = 0; var records = try conn.rows( \\SELECT collection, rkey, cid \\FROM permissioned_space_records \\WHERE space = ? AND repo_did = ? , .{ identity.space, identity.did }); defer records.deinit(); while (records.next()) |record| { try addRecordElement(allocator, &hash, record.text(0), record.text(1), record.text(2)); record_count += 1; } if (records.err) |err| return err; if (!identity.has_rev and record_count == 0) continue; try conn.exec( \\UPDATE permissioned_space_repos \\SET set_hash = ? \\WHERE space = ? AND repo_did = ? , .{ zqlite.blob(&hash.bytes), identity.space, identity.did }); } try markMigrationApplied(name); } fn migratePermissionedSpaceWriters() !void { const name = "permissioned-space-writer-set-v1"; if (try migrationApplied(name)) return; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); const ExistingRepo = struct { space: []const u8, did: []const u8, rev: []const u8, state: []const u8, }; var existing: std.ArrayList(ExistingRepo) = .empty; { var rows = try conn.rows( \\SELECT r.space, r.repo_did, r.rev, r.set_hash \\FROM permissioned_space_repos r \\JOIN permissioned_spaces s ON s.uri = r.space \\JOIN permissioned_space_actor_state a \\ ON a.space = s.uri AND a.actor_did = s.authority_did \\WHERE r.rev IS NOT NULL AND r.set_hash IS NOT NULL \\ AND s.deleted_at IS NULL AND a.deleted_at IS NULL AND a.is_authority = 1 , .{}); defer rows.deinit(); while (rows.next()) |row| { try existing.append(allocator, .{ .space = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .rev = try allocator.dupe(u8, row.text(2)), .state = try allocator.dupe(u8, row.blob(3)), }); } if (rows.err) |err| return err; } for (existing.items) |repo| { const state = try zat.space.LtHash.fromBytes(repo.state); const digest = state.digest(); try conn.exec( \\INSERT INTO permissioned_space_writers (space, repo_did, rev, hash) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(space, repo_did) DO UPDATE SET \\ rev = excluded.rev, \\ hash = excluded.hash, \\ updated_at = unixepoch() , .{ repo.space, repo.did, repo.rev, zqlite.blob(&digest) }); } try markMigrationApplied(name); } fn migrateSimpleSpaceConfig() !void { if (try permissionedSpacesColumnExistsLocked("read_policy")) return; if (try permissionedSpacesColumnExistsLocked("policy_json")) return; try conn.exclusiveTransaction(); errdefer conn.rollback(); if (!try permissionedSpacesColumnExistsLocked("policy")) { try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN policy TEXT NOT NULL DEFAULT 'member-list'"); if (try permissionedSpacesColumnExistsLocked("is_public")) { try conn.execNoArgs("UPDATE permissioned_spaces SET policy = CASE WHEN is_public = 1 THEN 'public' ELSE 'member-list' END"); } } if (!try permissionedSpacesColumnExistsLocked("app_access_json")) { try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN app_access_json TEXT NOT NULL DEFAULT '{\"type\":\"open\"}'"); if (try permissionedSpacesColumnExistsLocked("app_access_mode")) { try conn.execNoArgs( \\UPDATE permissioned_spaces SET app_access_json = CASE \\WHEN app_access_mode = 'deny' AND app_exceptions_json != '[]' \\THEN '{"type":"allowList","allowed":[]}' ELSE '{"type":"open"}' END ); } } try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS simplespace_members ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ member_did TEXT NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (space, member_did) \\) ); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS simplespace_members_member_idx ON simplespace_members (member_did, space)"); try conn.execNoArgs( \\INSERT OR IGNORE INTO simplespace_members (space, member_did) \\SELECT uri, authority_did \\FROM permissioned_spaces ); try conn.execNoArgs("ALTER TABLE permissioned_spaces RENAME COLUMN policy TO read_policy"); try conn.execNoArgs("ALTER TABLE permissioned_spaces RENAME COLUMN managing_app TO read_managing_app"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN write_policy TEXT NOT NULL DEFAULT 'member-list'"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN write_managing_app TEXT"); try conn.execNoArgs("UPDATE permissioned_spaces SET write_policy = read_policy, write_managing_app = read_managing_app"); try conn.execNoArgs("ALTER TABLE simplespace_members ADD COLUMN read INTEGER NOT NULL DEFAULT 1"); try conn.execNoArgs("ALTER TABLE simplespace_members ADD COLUMN write INTEGER NOT NULL DEFAULT 1"); try conn.commit(); } fn ensureMigrationTable() !void { try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS zds_migrations ( \\ name TEXT PRIMARY KEY, \\ applied_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) ); } fn accountsColumnExistsLocked(column: []const u8) !bool { var rows = try conn.rows("PRAGMA table_info(accounts)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), column)) return true; } if (rows.err) |err| return err; return false; } fn permissionedSpacesColumnExistsLocked(column: []const u8) !bool { var rows = try conn.rows("PRAGMA table_info(permissioned_spaces)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), column)) return true; } if (rows.err) |err| return err; return false; } fn permissionedSpaceActorStateColumnExistsLocked(column: []const u8) !bool { var rows = try conn.rows("PRAGMA table_info(permissioned_space_actor_state)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), column)) return true; } if (rows.err) |err| return err; return false; } fn migrationApplied(name: []const u8) !bool { const row = try conn.row("SELECT 1 FROM zds_migrations WHERE name = ?", .{name}); if (row == null) return false; defer row.?.deinit(); return true; } fn markMigrationApplied(name: []const u8) !void { try conn.exec("INSERT OR IGNORE INTO zds_migrations (name) VALUES (?)", .{name}); } fn migratePermissionedSpacesAuthorityColumns() !void { const name = "permissioned-spaces-authority-columns"; if (try migrationApplied(name)) return; if (!try permissionedSpacesColumnExistsLocked("owner_did")) { try conn.execNoArgs("CREATE INDEX IF NOT EXISTS permissioned_spaces_authority_idx ON permissioned_spaces (authority_did, space_type, uri)"); try markMigrationApplied(name); return; } try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_spaces_next"); try conn.execNoArgs( \\CREATE TABLE permissioned_spaces_next ( \\ uri TEXT PRIMARY KEY, \\ authority_did TEXT NOT NULL, \\ space_type TEXT NOT NULL, \\ skey TEXT NOT NULL, \\ managing_app TEXT, \\ is_public INTEGER NOT NULL DEFAULT 0, \\ app_access_mode TEXT NOT NULL DEFAULT 'allow', \\ app_exceptions_json TEXT NOT NULL DEFAULT '[]', \\ is_authority INTEGER NOT NULL DEFAULT 1, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ deleted_at INTEGER, \\ UNIQUE(authority_did, space_type, skey) \\) ); try conn.execNoArgs( \\INSERT INTO permissioned_spaces_next ( \\ uri, authority_did, space_type, skey, managing_app, is_public, app_access_mode, \\ app_exceptions_json, is_authority, created_at, deleted_at \\) \\SELECT uri, owner_did, space_type, skey, managing_app, is_public, app_access_mode, \\ app_exceptions_json, is_owner, created_at, deleted_at \\FROM permissioned_spaces ); try conn.execNoArgs("PRAGMA foreign_keys = OFF"); errdefer conn.execNoArgs("PRAGMA foreign_keys = ON") catch {}; try conn.execNoArgs("DROP TABLE permissioned_spaces"); try conn.execNoArgs("ALTER TABLE permissioned_spaces_next RENAME TO permissioned_spaces"); try conn.execNoArgs("PRAGMA foreign_keys = ON"); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS permissioned_spaces_authority_idx ON permissioned_spaces (authority_did, space_type, uri)"); try markMigrationApplied(name); } fn migratePermissionedSpaceAuthorityFlag() !void { const name = "permissioned-space-authority-flag"; if (try migrationApplied(name)) return; if (try permissionedSpaceActorStateColumnExistsLocked("is_owner")) { try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_space_actor_state_next"); try conn.execNoArgs( \\CREATE TABLE permissioned_space_actor_state_next ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ actor_did TEXT NOT NULL, \\ is_authority INTEGER NOT NULL DEFAULT 0, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ deleted_at INTEGER, \\ PRIMARY KEY (space, actor_did) \\) ); try conn.execNoArgs( \\INSERT INTO permissioned_space_actor_state_next ( \\ space, actor_did, is_authority, created_at, deleted_at \\) \\SELECT space, actor_did, is_owner, created_at, deleted_at \\FROM permissioned_space_actor_state ); try conn.execNoArgs("PRAGMA foreign_keys = OFF"); errdefer conn.execNoArgs("PRAGMA foreign_keys = ON") catch {}; try conn.execNoArgs("DROP TABLE permissioned_space_actor_state"); try conn.execNoArgs("ALTER TABLE permissioned_space_actor_state_next RENAME TO permissioned_space_actor_state"); try conn.execNoArgs("PRAGMA foreign_keys = ON"); } try conn.execNoArgs( \\INSERT OR IGNORE INTO permissioned_space_actor_state (space, actor_did, is_authority, deleted_at) \\SELECT s.uri, \\ CASE WHEN s.is_authority = 1 THEN s.authority_did ELSE COALESCE(r.repo_did, s.authority_did) END, \\ s.is_authority, \\ s.deleted_at \\FROM permissioned_spaces s \\LEFT JOIN permissioned_space_repos r ON r.space = s.uri \\WHERE s.is_authority = 1 ); try markMigrationApplied(name); } fn migratePermissionedRecordOplogRepo() !void { const name = "permissioned-record-oplog-repo"; if (try migrationApplied(name)) return; try conn.execNoArgs("DROP TABLE IF EXISTS permissioned_space_record_oplog_next"); try conn.execNoArgs( \\CREATE TABLE permissioned_space_record_oplog_next ( \\ space TEXT NOT NULL, \\ repo_did TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ idx INTEGER NOT NULL, \\ action TEXT NOT NULL, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ cid TEXT, \\ prev TEXT, \\ PRIMARY KEY (space, repo_did, rev, idx) \\) ); try conn.execNoArgs( \\INSERT INTO permissioned_space_record_oplog_next ( \\ space, repo_did, rev, idx, action, collection, rkey, cid, prev \\) \\SELECT space, '', rev, idx, action, collection, rkey, cid, prev \\FROM permissioned_space_record_oplog ); try conn.execNoArgs("DROP TABLE permissioned_space_record_oplog"); try conn.execNoArgs("ALTER TABLE permissioned_space_record_oplog_next RENAME TO permissioned_space_record_oplog"); try markMigrationApplied(name); } fn migrateSeqEventsSync11() !void { const name = "sync11-seq-events"; if (try migrationApplied(name)) return; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try conn.exclusiveTransaction(); errdefer conn.rollback(); try rebuildSeqEventsSync11Locked(allocator); try markMigrationApplied(name); try conn.commit(); } fn migrateSeqEventsSinceRev() !void { const name = "sync-since-rev-events"; if (try migrationApplied(name)) return; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try conn.exclusiveTransaction(); errdefer conn.rollback(); try rebuildSeqEventsSync11Locked(allocator); try markMigrationApplied(name); try conn.commit(); } // Before zat 0.6.4, #commit frames left out since (when null), rebase and // tooBig, all required by subscribeRepos; consumers replaying from an old // cursor reject those frames, so rewrite them in place. fn migrateSeqEventsRequiredCommitFields() !void { const name = "seq-events-required-commit-fields"; if (try migrationApplied(name)) return; db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try conn.exclusiveTransaction(); errdefer conn.rollback(); var cursor: i64 = -1; while (true) { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var rewrites: std.ArrayList(struct { seq: i64, frame: []const u8 }) = .empty; var page_last: ?i64 = null; { var rows = try conn.rows( \\SELECT seq, evt \\FROM seq_events \\WHERE seq > ? \\ORDER BY seq ASC \\LIMIT 500 , .{cursor}); defer rows.deinit(); while (rows.next()) |row| { const seq = row.int(0); page_last = seq; const frame = row.nullableBlob(1) orelse continue; if (try withRequiredCommitFields(allocator, frame)) |fixed| { try rewrites.append(allocator, .{ .seq = seq, .frame = fixed }); } } if (rows.err) |err| return err; } for (rewrites.items) |rewrite| { try conn.exec("UPDATE seq_events SET evt = ? WHERE seq = ?", .{ zqlite.blob(rewrite.frame), rewrite.seq }); } cursor = page_last orelse break; } try markMigrationApplied(name); try conn.commit(); } /// Returns the frame with since, rebase and tooBig added when it is a #commit /// frame missing any of them, or null when it needs no change. fn withRequiredCommitFields(allocator: std.mem.Allocator, frame: []const u8) !?[]const u8 { const header = zat.cbor.decode(allocator, frame) catch return null; const tag = header.value.getString("t") orelse return null; if (!std.mem.eql(u8, tag, "#commit")) return null; const payload = zat.cbor.decodeAll(allocator, frame[header.consumed..]) catch return null; const entries = switch (payload) { .map => |map| map, else => return null, }; var fixed: std.ArrayList(zat.cbor.Value.MapEntry) = .empty; try fixed.appendSlice(allocator, entries); if (payload.get("since") == null) try fixed.append(allocator, .{ .key = "since", .value = .null }); if (payload.get("rebase") == null) try fixed.append(allocator, .{ .key = "rebase", .value = .{ .boolean = false } }); if (payload.get("tooBig") == null) try fixed.append(allocator, .{ .key = "tooBig", .value = .{ .boolean = false } }); if (fixed.items.len == entries.len) return null; const body = try zat.cbor.encodeAlloc(allocator, .{ .map = fixed.items }); return try std.mem.concat(allocator, u8, &.{ frame[0..header.consumed], body }); } // Account deletion keeps one #account deleted event per DID after the // account row is gone, so seq_events cannot cascade from accounts. fn migrateSeqEventsWithoutAccountFk() !void { const name = "seq-events-without-account-fk"; if (try migrationApplied(name)) return; try conn.exclusiveTransaction(); errdefer conn.rollback(); try conn.execNoArgs("DROP TABLE IF EXISTS seq_events_next"); try conn.execNoArgs( \\CREATE TABLE seq_events_next ( \\ seq INTEGER PRIMARY KEY, \\ did TEXT NOT NULL, \\ commit_cid TEXT NOT NULL, \\ evt BLOB NOT NULL \\) ); try conn.execNoArgs("INSERT INTO seq_events_next (seq, did, commit_cid, evt) SELECT seq, did, commit_cid, evt FROM seq_events"); try conn.execNoArgs("DROP TABLE seq_events"); try conn.execNoArgs("ALTER TABLE seq_events_next RENAME TO seq_events"); try markMigrationApplied(name); try conn.commit(); } fn migrateRecordsTable() !void { if (!try recordsTableHasValueJsonColumn()) return; try conn.execNoArgs("DROP TABLE IF EXISTS records_next"); try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS records_next ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ uri TEXT NOT NULL UNIQUE, \\ cid TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ seq INTEGER NOT NULL, \\ PRIMARY KEY (did, collection, rkey) \\) ); try conn.execNoArgs( \\INSERT INTO records_next (did, collection, rkey, uri, cid, rev, seq) \\SELECT did, collection, rkey, uri, cid, rev, seq \\FROM records ); try conn.execNoArgs("PRAGMA foreign_keys = OFF"); errdefer conn.execNoArgs("PRAGMA foreign_keys = ON") catch {}; try conn.execNoArgs("DROP TABLE records"); try conn.execNoArgs("ALTER TABLE records_next RENAME TO records"); try conn.execNoArgs("PRAGMA foreign_keys = ON"); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS records_collection_idx ON records (did, collection, seq DESC)"); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS records_cid_idx ON records (cid)"); } fn migrateRepoBlockRevs() !void { try conn.execNoArgs( \\UPDATE repo_blocks \\SET repo_rev = ( \\ SELECT r.rev \\ FROM records r \\ WHERE r.did = repo_blocks.did AND r.cid = repo_blocks.cid \\ LIMIT 1 \\) \\WHERE repo_rev IS NULL \\ AND EXISTS ( \\ SELECT 1 FROM records r \\ WHERE r.did = repo_blocks.did AND r.cid = repo_blocks.cid \\ ) ); try conn.execNoArgs( \\UPDATE repo_blocks \\SET repo_rev = ( \\ SELECT c.rev \\ FROM commits c \\ WHERE c.did = repo_blocks.did AND c.cid = repo_blocks.cid \\ LIMIT 1 \\) \\WHERE repo_rev IS NULL \\ AND EXISTS ( \\ SELECT 1 FROM commits c \\ WHERE c.did = repo_blocks.did AND c.cid = repo_blocks.cid \\ ) ); try conn.execNoArgs("CREATE INDEX IF NOT EXISTS repo_blocks_rev_idx ON repo_blocks (did, repo_rev DESC, cid DESC)"); } fn recordsTableHasValueJsonColumn() !bool { var rows = try conn.rows("PRAGMA table_info(records)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), "value_json")) return true; } if (rows.err) |err| return err; return false; } fn validateRecordBlocksPresent() !void { const row = try conn.row( \\SELECT COUNT(*) \\FROM records r \\LEFT JOIN repo_blocks rb ON rb.did = r.did AND rb.cid = r.cid \\WHERE rb.cid IS NULL , .{}); if (row == null) return; defer row.?.deinit(); if (row.?.int(0) != 0) return Error.MissingRecordBlock; } fn migrateAppPreferencesTable() !void { if (!try appPreferencesTableHasPreferencesJsonColumn()) return; try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS app_preferences_next ( \\ id INTEGER PRIMARY KEY AUTOINCREMENT, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ name TEXT NOT NULL, \\ value_json BLOB NOT NULL, \\ updated_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) ); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); { var rows = try conn.rows( \\SELECT did, namespace, preferences_json \\FROM app_preferences , .{}); defer rows.deinit(); var preferences: std.ArrayList(AppPreference) = .empty; while (rows.next()) |row| { const did = try allocator.dupe(u8, row.text(0)); const namespace = try allocator.dupe(u8, row.text(1)); const preferences_json = try allocator.dupe(u8, row.text(2)); const parsed = std.json.parseFromSlice(std.json.Value, allocator, preferences_json, .{}) catch continue; if (parsed.value != .array) continue; for (parsed.value.array.items) |preference| { if (preference != .object) continue; const name = jsonValueString(preference, "$type") orelse continue; if (!preferenceNameInNamespace(name, namespace)) continue; const value_json = try stringifyValue(allocator, preference); try preferences.append(allocator, .{ .name = name, .value_json = value_json, }); } for (preferences.items) |preference| { try conn.exec( \\INSERT INTO app_preferences_next (did, name, value_json, updated_at) \\VALUES (?, ?, ?, unixepoch()) , .{ did, preference.name, preference.value_json }); } preferences.clearRetainingCapacity(); } if (rows.err) |err| return err; } try conn.execNoArgs("DROP TABLE app_preferences"); try conn.execNoArgs("ALTER TABLE app_preferences_next RENAME TO app_preferences"); } fn appPreferencesTableHasPreferencesJsonColumn() !bool { var rows = try conn.rows("PRAGMA table_info(app_preferences)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), "preferences_json")) return true; } if (rows.err) |err| return err; return false; } fn preferenceNameInNamespace(name: []const u8, namespace: []const u8) bool { return std.mem.eql(u8, name, namespace) or (std.mem.startsWith(u8, name, namespace) and name.len > namespace.len and name[namespace.len] == '.'); } fn jsonValueString(value: std.json.Value, key: []const u8) ?[]const u8 { return switch (value) { .object => |object| switch (object.get(key) orelse return null) { .string => |string| string, else => null, }, else => null, }; } fn migrateBlobTable() !void { if (!try blobTableHasDataColumn()) return; try conn.execNoArgs( \\CREATE TABLE IF NOT EXISTS blobs_next ( \\ cid TEXT NOT NULL, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ mime_type TEXT NOT NULL, \\ size INTEGER NOT NULL, \\ storage TEXT NOT NULL DEFAULT 'disk', \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (did, cid) \\) ); try conn.execNoArgs( \\INSERT OR IGNORE INTO blobs_next (cid, did, mime_type, size, storage, created_at) \\SELECT cid, did, mime_type, size, 'disk', created_at \\FROM blobs ); try conn.execNoArgs("DROP TABLE blobs"); try conn.execNoArgs("ALTER TABLE blobs_next RENAME TO blobs"); } fn blobTableHasDataColumn() !bool { var rows = try conn.rows("PRAGMA table_info(blobs)", .{}); defer rows.deinit(); while (rows.next()) |row| { if (std.mem.eql(u8, row.text(1), "data")) return true; } if (rows.err) |err| return err; return false; } pub fn getEmailInfo(allocator: std.mem.Allocator, did: []const u8) ?EmailInfo { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); requireInitialized() catch return null; const row = conn.row( \\SELECT email, email_confirmed_at, pending_email \\FROM accounts \\WHERE did = ? , .{did}) catch return null; if (row == null) return null; defer row.?.deinit(); return .{ .email = allocator.dupe(u8, row.?.text(0)) catch return null, .email_confirmed = row.?.nullableInt(1) != null, .pending_email = if (row.?.nullableText(2)) |text| allocator.dupe(u8, text) catch null else null, }; } /// What an emailed code is for, as the reference PDS keys its email_token table: /// a code issued for one purpose never authorizes another. pub const EmailTokenPurpose = enum { confirm_email, update_email, reset_password, delete_account, plc_operation, }; pub const OutboxMail = struct { id: i64, to: []const u8, subject: []const u8, text: []const u8, code: ?[]const u8, created_at: i64, }; const outbox_retention_seconds = 24 * 60 * 60; /// The outbox mail provider's sink: keeps a message for a day instead of /// sending it. pub fn recordOutboxMail(to: []const u8, handle: []const u8, subject: []const u8, text: []const u8, code: ?[]const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM mail_outbox WHERE created_at < unixepoch() - ?", .{@as(i64, outbox_retention_seconds)}); try conn.exec( "INSERT INTO mail_outbox (to_address, handle, subject, text, code) VALUES (?, ?, ?, ?, ?)", .{ to, handle, subject, text, code }, ); } /// Kept mail, newest first, optionally for one address. pub fn listOutboxMail(allocator: std.mem.Allocator, to: ?[]const u8, limit: usize) ![]OutboxMail { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const capped: i64 = @intCast(@min(if (limit == 0) 50 else limit, 100)); var rows = if (to) |address| try conn.rows("SELECT id, to_address, subject, text, code, created_at FROM mail_outbox WHERE to_address = ? COLLATE NOCASE ORDER BY id DESC LIMIT ?", .{ address, capped }) else try conn.rows("SELECT id, to_address, subject, text, code, created_at FROM mail_outbox ORDER BY id DESC LIMIT ?", .{capped}); defer rows.deinit(); var out: std.ArrayList(OutboxMail) = .empty; while (rows.next()) |row| { try out.append(allocator, .{ .id = row.int(0), .to = try allocator.dupe(u8, row.text(1)), .subject = try allocator.dupe(u8, row.text(2)), .text = try allocator.dupe(u8, row.text(3)), .code = if (row.nullableText(4)) |code| try allocator.dupe(u8, code) else null, .created_at = row.int(5), }); } if (rows.err) |err| return err; return out.toOwnedSlice(allocator); } pub fn setEmailToken(did: []const u8, purpose: EmailTokenPurpose, code: []const u8, expires_at_ms: i64) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return setEmailTokenLocked(did, purpose, code, expires_at_ms); } pub fn setPasswordResetToken(did: []const u8, email: []const u8, code: []const u8, expires_at_ms: i64) !bool { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); const row = (try conn.row("SELECT email FROM accounts WHERE did = ? AND account_status != 'deleted'", .{did})) orelse return false; defer row.deinit(); if (!std.ascii.eqlIgnoreCase(row.text(0), email)) return false; try setEmailTokenLocked(did, .reset_password, code, expires_at_ms); return true; } fn setEmailTokenLocked(did: []const u8, purpose: EmailTokenPurpose, code: []const u8, expires_at_ms: i64) !void { try conn.exec( \\INSERT INTO email_tokens (did, purpose, token, expires_at_ms) VALUES (?, ?, ?, ?) \\ON CONFLICT(did, purpose) DO UPDATE SET token = excluded.token, expires_at_ms = excluded.expires_at_ms , .{ did, @tagName(purpose), code, expires_at_ms }); } pub fn validateEmailToken(did: []const u8, purpose: EmailTokenPurpose, code: []const u8, now_ms: i64) CodeStatus { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); requireInitialized() catch return .invalid; const row = conn.row( "SELECT token, expires_at_ms FROM email_tokens WHERE did = ? AND purpose = ?", .{ did, @tagName(purpose) }, ) catch return .invalid; if (row == null) return .invalid; defer row.?.deinit(); if (!std.ascii.eqlIgnoreCase(row.?.text(0), code)) return .invalid; if (now_ms >= row.?.int(1)) return .expired; return .valid; } pub fn clearEmailToken(did: []const u8, purpose: EmailTokenPurpose) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("DELETE FROM email_tokens WHERE did = ? AND purpose = ?", .{ did, @tagName(purpose) }); } pub const PasswordTarget = union(enum) { did: []const u8, reset_token: []const u8 }; fn passwordResetDidLocked(allocator: std.mem.Allocator, token: []const u8) ![]const u8 { const row = (try conn.row("SELECT did, expires_at_ms, count(*) FROM email_tokens WHERE purpose = 'reset_password' AND token = ? COLLATE NOCASE", .{token})).?; defer row.deinit(); if (row.int(2) != 1) return error.InvalidToken; if (nowMs() >= row.int(1)) return error.ExpiredToken; return allocator.dupe(u8, row.text(0)); } pub fn changePassword(allocator: std.mem.Allocator, target: PasswordTarget, password: []const u8, actor_did: []const u8) !void { if (target == .reset_token) { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); allocator.free(try passwordResetDidLocked(allocator, target.reset_token)); } var salt: [16]u8 = undefined; randomBytes(&salt); const hash = try auth.hashPassword(allocator, password, salt); defer allocator.free(hash); const audit_id = try randomToken(allocator, "aud-", 16); defer allocator.free(audit_id); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); const did = switch (target) { .did => |did| try allocator.dupe(u8, did), .reset_token => |token| try passwordResetDidLocked(allocator, token), }; defer allocator.free(did); const account = try conn.row("SELECT did FROM accounts WHERE did = ? AND account_status != 'deleted'", .{did}); if (account == null) return error.AccountNotFound; account.?.deinit(); try conn.exec("UPDATE accounts SET password_hash = ? WHERE did = ?", .{ hash, did }); try conn.exec("DELETE FROM email_tokens WHERE did = ? AND purpose = 'reset_password'", .{did}); try conn.exec("UPDATE session_tokens SET revoked_at = unixepoch(), updated_at = unixepoch() WHERE did = ? AND revoked_at IS NULL", .{did}); try conn.exec("DELETE FROM browser_sessions WHERE did = ?", .{did}); try conn.exec("INSERT INTO account_audit_log (id, subject_did, actor_did, action, details_json) VALUES (?, ?, ?, ?, '{}')", .{ audit_id, did, if (target == .reset_token) did else actor_did, if (target == .reset_token) "password_reset" else "password_updated", }); try conn.commit(); } pub fn confirmEmail(did: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.exec("UPDATE accounts SET email_confirmed_at = unixepoch() WHERE did = ?", .{did}); try conn.exec("DELETE FROM email_tokens WHERE did = ? AND purpose = 'confirm_email'", .{did}); } pub fn updateEmail(did: []const u8, email: []const u8) !void { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); try conn.transaction(); errdefer conn.rollback(); try conn.exec( \\UPDATE accounts \\SET email = ?, \\ email_confirmed_at = NULL, \\ pending_email = NULL \\WHERE did = ? , .{ email, did }); try conn.exec("DELETE FROM email_tokens WHERE did = ? AND purpose IN ('update_email', 'reset_password', 'confirm_email')", .{did}); try conn.commit(); } fn loadNextSeqLocked() !u64 { const row = try conn.row( \\SELECT COALESCE(MAX(seq), 0) + 1 \\FROM ( \\ SELECT MAX(seq) AS seq FROM commits \\ UNION ALL \\ SELECT MAX(seq) AS seq FROM seq_events \\) , .{}); if (row == null) return 1; defer row.?.deinit(); return @intCast(row.?.int(0)); } fn nextSeqLocked() !u64 { _ = try requireInitialized(); const seq = next_seq; next_seq += 1; return seq; } fn nextRkey(allocator: std.mem.Allocator) ![]const u8 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try requireInitialized(); return nextRkeyLocked(allocator); } fn nextRkeyLocked(allocator: std.mem.Allocator) ![]const u8 { const seq = try nextSeqLocked(); const tid = zat.Tid.fromTimestamp(nowMicros(), @intCast(seq % 1024)).str(); return allocator.dupe(u8, &tid); } fn resolveWriteOpsLocked(allocator: std.mem.Allocator, ops: []const WriteOp) ![]WriteOp { var resolved = try allocator.alloc(WriteOp, ops.len); for (ops, 0..) |op, i| { resolved[i] = switch (op) { .create => |create_op| .{ .create = .{ .collection = create_op.collection, .rkey = create_op.rkey orelse try nextRkeyLocked(allocator), .value = create_op.value, } }, .update => |update_op| .{ .update = update_op }, .delete => |delete_op| .{ .delete = delete_op }, }; } return resolved; } fn requireWriteSwapsLocked(allocator: std.mem.Allocator, did: []const u8, current: ?CurrentCommit, options: WriteOptions, ops: []const WriteOp) !void { if (options.swap_commit) |expected| { if (current == null or !std.mem.eql(u8, current.?.commit_cid_text, expected)) return Error.InvalidSwap; } for (ops) |op| switch (op) { .create => {}, .update => |update_op| try requireRecordSwapLocked(allocator, did, update_op.collection, update_op.rkey, update_op.swap), .delete => |delete_op| try requireRecordSwapLocked(allocator, did, delete_op.collection, delete_op.rkey, delete_op.swap), }; } fn requireRecordSwapLocked(allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8, swap: RecordSwap) !void { switch (swap) { .none => {}, .missing => { if (try currentRecordCidLocked(allocator, did, collection, rkey) != null) return Error.InvalidSwap; }, .cid => |expected| { const actual = try currentRecordCidLocked(allocator, did, collection, rkey) orelse return Error.InvalidSwap; if (!std.mem.eql(u8, actual, expected)) return Error.InvalidSwap; }, } } fn currentRecordCidLocked(allocator: std.mem.Allocator, did: []const u8, collection: []const u8, rkey: []const u8) !?[]const u8 { const row = try conn.row( \\SELECT cid \\FROM records \\WHERE did = ? AND collection = ? AND rkey = ? \\LIMIT 1 , .{ did, collection, rkey }); if (row == null) return null; defer row.?.deinit(); return try allocator.dupe(u8, row.?.text(0)); } fn validateRecordForWrite(collection: []const u8, rkey: ?[]const u8, value: std.json.Value, mode: ValidationMode) Error!void { const object = switch (value) { .object => |object| object, else => return Error.InvalidRecordType, }; const record_type = switch (object.get("$type") orelse return Error.InvalidRecordType) { .string => |record_type| record_type, else => return Error.InvalidRecordType, }; if (!std.mem.eql(u8, record_type, collection)) return Error.InvalidRecordType; if (mode == .require and !knownRecordType(record_type)) return Error.ValidationRequired; if (rkey) |key| try validateKnownRecordKey(record_type, key); } fn validateKnownRecordKey(record_type: []const u8, rkey: []const u8) Error!void { if (knownRecordKeyRule(record_type)) |rule| switch (rule) { .tid => { if (zat.Tid.parse(rkey) == null) return Error.InvalidRecordKey; }, .literal_self => { if (!std.mem.eql(u8, rkey, "self")) return Error.InvalidRecordKey; }, }; } const KnownRecordKeyRule = enum { tid, literal_self }; fn validationStatusForRecord(record_type: []const u8, mode: ValidationMode) []const u8 { return if (mode != .skip and knownRecordType(record_type)) "valid" else "unknown"; } fn knownRecordType(record_type: []const u8) bool { if (knownRecordKeyRule(record_type) != null) return true; const any_records = [_][]const u8{ "app.bsky.feed.generator", "com.atproto.lexicon.schema", }; for (any_records) |known| { if (std.mem.eql(u8, record_type, known)) return true; } return false; } fn knownRecordKeyRule(record_type: []const u8) ?KnownRecordKeyRule { const tid_records = [_][]const u8{ "app.bsky.feed.post", "app.bsky.feed.like", "app.bsky.feed.repost", "app.bsky.feed.threadgate", "app.bsky.feed.postgate", "app.bsky.graph.follow", "app.bsky.graph.block", "app.bsky.graph.list", "app.bsky.graph.listblock", "app.bsky.graph.listitem", "app.bsky.graph.starterpack", "app.bsky.graph.verification", }; for (tid_records) |known| { if (std.mem.eql(u8, record_type, known)) return .tid; } const self_records = [_][]const u8{ "app.bsky.actor.profile", "app.bsky.actor.status", "app.bsky.labeler.service", "app.bsky.notification.declaration", "chat.bsky.actor.declaration", "com.germnetwork.declaration", }; for (self_records) |known| { if (std.mem.eql(u8, record_type, known)) return .literal_self; } return null; } fn repoPath(allocator: std.mem.Allocator, collection: []const u8, rkey: []const u8) ![]const u8 { if (zat.Nsid.parse(collection) == null) return Error.InvalidCollection; if (zat.Rkey.parse(rkey) == null) return Error.InvalidRecordKey; return std.fmt.allocPrint(allocator, "{s}/{s}", .{ collection, rkey }); } pub const max_record_bytes = 1_000_000; /// subscribeRepos `#commit` caps `ops` at 200 entries and `blocks` at /// 2,000,000 bytes, and relays drop frames past either, so a write that would /// exceed them is refused before anything is stored pub const max_commit_ops = 200; pub const max_commit_event_block_bytes = 2_000_000; /// The Spaces alpha bounds a space record's DRISL encoding at 1 MiB, separately /// from the public repo limit. pub const max_space_record_bytes = zat.space.archive.max_space_record_bytes; fn accountBlobAvailableLocked(did: []const u8, cid: []const u8) !bool { const row = try conn.row( \\SELECT 1 FROM blobs b \\WHERE b.did = ? AND b.cid = ? \\ AND NOT EXISTS (SELECT 1 FROM blob_takedowns t WHERE t.did = b.did AND t.cid = b.cid) , .{ did, cid }); const found = row orelse return false; found.deinit(); return true; } fn stageRecordWrite( allocator: std.mem.Allocator, tree: *zat.mst.Mst, account: auth.Account, collection: []const u8, rkey: []const u8, value: std.json.Value, validation_mode: ValidationMode, rev: []const u8, seq: u64, blocks: *std.ArrayList(ImportedBlock), blob_refs: *std.ArrayList(BlobRef), action: enum { create, update }, previous_cid: *?[]const u8, ) !Record { const cbor_value = try recordValueFromJson(allocator, value); const record_bytes = try zat.cbor.encodeAlloc(allocator, cbor_value); if (record_bytes.len > max_record_bytes) return Error.RecordTooLarge; const record_cid = try zat.cbor.Cid.forDagCbor(allocator, record_bytes); const record_cid_text = try zat.cbor.Cid.toString(.{ .raw = record_cid.raw }, allocator); const path = try repoPath(allocator, collection, rkey); // the reference PDS's MST refuses to add a key it already holds; checking // the staged tree rather than the index lets one batch delete then recreate const previous = try tree.putReturn(path, record_cid); if (action == .create and previous != null) return Error.RecordAlreadyExists; previous_cid.* = if (previous) |cid| try zat.cbor.Cid.toString(.{ .raw = cid.raw }, allocator) else null; try blocks.append(allocator, .{ .cid = record_cid_text, .data = record_bytes, }); const uri = try std.fmt.allocPrint(allocator, "at://{s}/{s}/{s}", .{ account.did, collection, rkey }); for (try blobCidTexts(allocator, cbor_value)) |blob_cid| { try blob_refs.append(allocator, .{ .cid = blob_cid, .uri = uri }); } return .{ .did = try allocator.dupe(u8, account.did), .collection = try allocator.dupe(u8, collection), .rkey = try allocator.dupe(u8, rkey), .cid = record_cid_text, .value_json = try stringifyValue(allocator, value), .validation_status = validationStatusForRecord(collection, validation_mode), .rev = try allocator.dupe(u8, rev), .seq = seq, }; } const CurrentCommit = struct { commit_cid_text: []const u8, commit_cid_raw: []const u8, data_cid_raw: []const u8, rev: []const u8, commit_data: []const u8, }; fn latestCommitRawLocked(allocator: std.mem.Allocator, did: []const u8) !?CurrentCommit { return latestCommitRawFrom(conn, allocator, did); } fn latestCommitRawFrom( read_conn: zqlite.Conn, allocator: std.mem.Allocator, did: []const u8, ) !?CurrentCommit { const row = try read_conn.row( \\SELECT c.cid, c.rev, rb.data \\FROM commits c \\JOIN repo_blocks rb ON rb.did = c.did AND rb.cid = c.cid \\WHERE c.did = ? \\ORDER BY c.seq DESC \\LIMIT 1 , .{did}); if (row == null) return null; defer row.?.deinit(); const commit_cid_text = try allocator.dupe(u8, row.?.text(0)); const commit_cid_raw = (zat.cbor.Cid.fromString(allocator, commit_cid_text) catch return Error.InvalidDagCbor).raw; const rev = try allocator.dupe(u8, row.?.text(1)); const commit_data = row.?.nullableBlob(2) orelse return null; const commit_value = try zat.cbor.decodeAll(allocator, commit_data); const data = commit_value.getCid("data") orelse return error.InvalidRepoPath; return .{ .commit_cid_text = commit_cid_text, .commit_cid_raw = commit_cid_raw, .data_cid_raw = try allocator.dupe(u8, data.raw), .rev = rev, .commit_data = try allocator.dupe(u8, commit_data), }; } fn writeMstBlocks(tree: *zat.mst.Mst, out: *std.ArrayList(zat.car.Block)) !void { try tree.collectBlocks(out); } const SignedCommit = struct { cid: []const u8, data: []const u8, }; fn signedCommitWithKeypair( allocator: std.mem.Allocator, did: []const u8, rev: []const u8, data_cid: zat.cbor.Cid, prev_cid_raw: ?[]const u8, keypair: *const zat.Keypair, ) !SignedCommit { const signed = try zat.signCommit(allocator, .{ .did = did, .rev = rev, .data = data_cid, .prev = if (prev_cid_raw) |raw| .{ .raw = raw } else null, }, keypair); defer allocator.free(signed.cid.raw); return .{ .cid = try zat.cbor.Cid.toString(.{ .raw = signed.cid.raw }, allocator), .data = signed.bytes, }; } fn signingKeypairLocked(did: []const u8) !zat.Keypair { const row = try conn.row( \\SELECT signing_key_type, signing_key \\FROM accounts \\WHERE did = ? , .{did}); if (row == null) return Error.MissingRecord; defer row.?.deinit(); const existing = row.?.nullableBlob(1); const key_type_text = row.?.nullableText(0) orelse "secp256k1"; const key_bytes = if (existing) |bytes| blk: { if (bytes.len != 32) return Error.InvalidDagCbor; break :blk bytes[0..32].*; } else blk: { const generated = try generateSigningKey(); try conn.exec( \\UPDATE accounts \\SET signing_key_type = 'secp256k1', signing_key = ? \\WHERE did = ? , .{ zqlite.blob(&generated), did }); break :blk generated; }; const key_type: zat.multicodec.KeyType = if (std.mem.eql(u8, key_type_text, "p256")) .p256 else .secp256k1; return zat.Keypair.fromSecretKey(key_type, key_bytes); } fn consumeReservedSigningKeyLocked(signing_key_or_did: []const u8, did: []const u8) ![32]u8 { const row = try conn.row( \\SELECT signing_key, secret_key, expires_at \\FROM reserved_signing_keys \\WHERE used_at IS NULL \\ AND expires_at > ? \\ AND (signing_key = ? OR did = ?) \\ORDER BY created_at DESC \\LIMIT 1 , .{ nowMs(), signing_key_or_did, did }); if (row == null) return Error.MissingReservedSigningKey; defer row.?.deinit(); const signing_key = row.?.text(0); const secret = row.?.blob(1); if (secret.len != 32) return Error.InvalidReservedSigningKey; const key_bytes = secret[0..32].*; try conn.exec( \\UPDATE reserved_signing_keys \\SET used_at = ? \\WHERE signing_key = ? , .{ nowMs(), signing_key }); return key_bytes; } fn generateKemKey() [32]u8 { var key: [32]u8 = undefined; while (true) { store_io.random(&key); if (zat.Keypair.fromSecretKey(.p256, key)) |_| return key else |_| {} } } fn generateSigningKey() ![32]u8 { var key: [32]u8 = undefined; while (true) { store_io.random(&key); if (zat.Keypair.fromSecretKey(.secp256k1, key)) |_| return key else |_| {} } } /// Record data as DAG-CBOR. zat keeps fractional JSON numbers as floats, which /// the data model allows elsewhere but forbids in records. pub fn recordValueFromJson(allocator: std.mem.Allocator, value: std.json.Value) !zat.cbor.Value { const cbor_value = zat.cbor.fromJson(allocator, value) catch |err| return switch (err) { error.OutOfMemory => error.OutOfMemory, error.InvalidBytesSentinel, error.InvalidLinkSentinel, error.UnsupportedJsonValue => Error.InvalidDagCbor, }; if (containsFloat(cbor_value)) return Error.InvalidDagCbor; return cbor_value; } fn containsFloat(value: zat.cbor.Value) bool { return switch (value) { .float => true, .array => |items| for (items) |item| { if (containsFloat(item)) break true; } else false, .map => |entries| for (entries) |entry| { if (containsFloat(entry.value)) break true; } else false, else => false, }; } /// The CIDs of the blobs a record references, as the text the blob tables key on. pub fn blobCidTexts(allocator: std.mem.Allocator, value: zat.cbor.Value) ![]const []const u8 { var refs: std.ArrayList(zat.cbor.Cid) = .empty; defer refs.deinit(allocator); try zat.cbor.collectBlobRefs(allocator, value, &refs); const texts = try allocator.alloc([]const u8, refs.items.len); for (refs.items, texts) |ref, *text| text.* = try ref.toString(allocator); return texts; } fn recordFromRow(row: zqlite.Row, allocator: std.mem.Allocator) !Record { const record_bytes = row.nullableBlob(4) orelse return Error.MissingRecordBlock; return .{ .did = try allocator.dupe(u8, row.text(0)), .collection = try allocator.dupe(u8, row.text(1)), .rkey = try allocator.dupe(u8, row.text(2)), .cid = try allocator.dupe(u8, row.text(3)), .value_json = try recordJsonFromBlock(allocator, record_bytes), .validation_status = validationStatusForRecord(row.text(1), .known), .rev = try allocator.dupe(u8, row.text(5)), .seq = @intCast(row.int(6)), }; } fn recordJsonFromBlock(allocator: std.mem.Allocator, data: []const u8) ![]const u8 { const value = zat.cbor.decodeAll(allocator, data) catch return Error.InvalidDagCbor; return zat.cbor.toJsonAlloc(allocator, value); } pub fn recordJsonFromDagCbor(allocator: std.mem.Allocator, data: []const u8) ![]const u8 { return recordJsonFromBlock(allocator, data); } fn oauthRequestFromRow(row: zqlite.Row, allocator: std.mem.Allocator) !OAuthRequest { return .{ .request_id = try allocator.dupe(u8, row.text(0)), .client_id = try allocator.dupe(u8, row.text(1)), .redirect_uri = try allocator.dupe(u8, row.text(2)), .scope = try allocator.dupe(u8, row.text(3)), .state = try allocator.dupe(u8, row.text(4)), .code_challenge = try allocator.dupe(u8, row.text(5)), .code_challenge_method = try allocator.dupe(u8, row.text(6)), .response_mode = try allocator.dupe(u8, row.text(7)), .login_hint = if (row.nullableText(8)) |text| try allocator.dupe(u8, text) else null, .dpop_jkt = if (row.nullableText(9)) |text| try allocator.dupe(u8, text) else null, .expires_at = row.int(10), .sub = if (row.nullableText(11)) |text| try allocator.dupe(u8, text) else null, .code = if (row.nullableText(12)) |text| try allocator.dupe(u8, text) else null, .auth_method = if (row.nullableText(13)) |text| try allocator.dupe(u8, text) else null, .prompt = if (row.nullableText(14)) |text| try allocator.dupe(u8, text) else null, .signin_hash = if (row.nullableText(15)) |text| try allocator.dupe(u8, text) else null, .signin_did = if (row.nullableText(16)) |text| try allocator.dupe(u8, text) else null, .signin_method = if (row.nullableText(17)) |text| try allocator.dupe(u8, text) else null, }; } fn appPasswordFromRow(row: zqlite.Row, allocator: std.mem.Allocator) !AppPassword { return .{ .name = try allocator.dupe(u8, row.text(0)), .password_hash = try allocator.dupe(u8, row.text(1)), .created_at = row.int(2), .privileged = row.int(3) != 0, .scopes = if (row.nullableText(4)) |text| try allocator.dupe(u8, text) else null, .created_by_controller_did = if (row.nullableText(5)) |text| try allocator.dupe(u8, text) else null, }; } fn sessionsFromRows(allocator: std.mem.Allocator, rows: anytype) ![]SessionInfo { var out: std.ArrayList(SessionInfo) = .empty; while (rows.next()) |row| { try out.append(allocator, .{ .id = try allocator.dupe(u8, row.text(0)), .did = try allocator.dupe(u8, row.text(1)), .handle = try allocator.dupe(u8, row.text(2)), .auth_method = try allocator.dupe(u8, row.text(3)), .app_password_name = if (row.nullableText(4)) |text| try allocator.dupe(u8, text) else null, .controller_did = if (row.nullableText(5)) |text| try allocator.dupe(u8, text) else null, .created_at = row.int(6), .updated_at = row.int(7), .last_used_at = row.nullableInt(8), .access_expires_at = row.int(9), .refresh_expires_at = row.int(10), .revoked_at = row.nullableInt(11), .active = row.int(12) != 0, }); } return out.toOwnedSlice(allocator); } fn oauthGrantsFromRows(allocator: std.mem.Allocator, rows: anytype) ![]OAuthGrantInfo { var out: std.ArrayList(OAuthGrantInfo) = .empty; while (rows.next()) |row| { try out.append(allocator, .{ .did = try allocator.dupe(u8, row.text(0)), .handle = try allocator.dupe(u8, row.text(1)), .client_id = try allocator.dupe(u8, row.text(2)), .scope = try allocator.dupe(u8, row.text(3)), .created_at = row.int(4), .expires_at = row.int(5), .revoked_at = row.nullableInt(6), .auth_method = if (row.nullableText(7)) |text| try allocator.dupe(u8, text) else null, .active = row.int(8) != 0, }); } return out.toOwnedSlice(allocator); } fn stringifyValue(allocator: std.mem.Allocator, value: std.json.Value) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.print("{f}", .{std.json.fmt(value, .{})}); return out.toOwnedSlice(); } const Root = struct { cid: []const u8, rev: []const u8, }; fn latestRootLocked(allocator: std.mem.Allocator, did: []const u8) !Root { return latestRootFrom(conn, allocator, did); } fn latestRootFrom( read_conn: zqlite.Conn, allocator: std.mem.Allocator, did: []const u8, ) !Root { const row = try read_conn.row( \\SELECT cid, rev \\FROM commits \\WHERE did = ? \\ORDER BY seq DESC \\LIMIT 1 , .{did}); if (row == null) return Error.RepoNotFound; defer row.?.deinit(); return .{ .cid = try allocator.dupe(u8, row.?.text(0)), .rev = try allocator.dupe(u8, row.?.text(1)), }; } fn rebuildSeqEventsSync11Locked(allocator: std.mem.Allocator) !void { var rows = try conn.rows( \\SELECT c.seq, c.did, c.cid, c.rev, c.prev, p.rev \\FROM commits c \\LEFT JOIN commits p ON p.did = c.did AND p.cid = c.prev \\ORDER BY c.seq ASC , .{}); defer rows.deinit(); var commits: std.ArrayList(struct { seq: u64, did: []const u8, cid: []const u8, rev: []const u8, prev: ?[]const u8, since_rev: ?[]const u8, }) = .empty; while (rows.next()) |row| { try commits.append(allocator, .{ .seq = @intCast(row.int(0)), .did = try allocator.dupe(u8, row.text(1)), .cid = try allocator.dupe(u8, row.text(2)), .rev = try allocator.dupe(u8, row.text(3)), .prev = if (row.nullableText(4)) |prev| try allocator.dupe(u8, prev) else null, .since_rev = if (row.nullableText(5)) |rev| try allocator.dupe(u8, rev) else null, }); } if (rows.err) |err| return err; for (commits.items) |commit| { { var commit_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer commit_arena.deinit(); const commit_allocator = commit_arena.allocator(); const repo_car = writeRepoCarFromLocked(commit_allocator, commit.did, commit.cid) catch continue; const prev_data_raw = if (commit.prev) |prev_commit| commitDataCidRawLocked(commit_allocator, commit.did, prev_commit) catch null else null; const frame = commitEventFrameFromCar(commit_allocator, commit.seq, commit.did, commit.cid, commit.rev, commit.since_rev, prev_data_raw, repo_car, &.{}) catch continue; try conn.exec( \\INSERT INTO seq_events (seq, did, commit_cid, evt) \\VALUES (?, ?, ?, ?) \\ON CONFLICT(seq) DO UPDATE SET \\ did = excluded.did, \\ commit_cid = excluded.commit_cid, \\ evt = excluded.evt , .{ @as(i64, @intCast(commit.seq)), commit.did, commit.cid, zqlite.blob(frame) }); } } } fn commitDataCidRawLocked(allocator: std.mem.Allocator, did: []const u8, commit_cid: []const u8) ![]const u8 { const row = try conn.row( \\SELECT data \\FROM repo_blocks \\WHERE did = ? AND cid = ? \\LIMIT 1 , .{ did, commit_cid }); if (row == null) return Error.MissingRecordBlock; defer row.?.deinit(); const commit_data = row.?.nullableBlob(0) orelse return Error.MissingRecordBlock; const commit_value = try zat.cbor.decodeAll(allocator, commit_data); const data = commit_value.getCid("data") orelse return Error.InvalidRepoPath; return try allocator.dupe(u8, data.raw); } fn writeRepoCarFromLocked(allocator: std.mem.Allocator, did: []const u8, root_cid: []const u8) ![]const u8 { const root_raw = try zat.multibase.base32lower.decode(allocator, root_cid[1..]); const car_root = zat.cbor.Cid{ .raw = root_raw }; var rows = try conn.rows( \\SELECT cid, data \\FROM repo_blocks \\WHERE did = ? \\ORDER BY cid ASC , .{did}); defer rows.deinit(); var blocks: std.ArrayList(zat.car.Block) = .empty; while (rows.next()) |row| { const cid_text = row.text(0); const data = row.nullableBlob(1) orelse ""; const cid_raw = try zat.multibase.base32lower.decode(allocator, cid_text[1..]); try blocks.append(allocator, .{ .cid_raw = cid_raw, .data = try allocator.dupe(u8, data), }); } if (rows.err) |err| return err; return zat.car.writeAlloc(allocator, .{ .roots = &.{car_root}, .blocks = blocks.items, }); } /// the CAR a write's `#commit` event carries in its `blocks` field fn commitEventCar( allocator: std.mem.Allocator, commit_cid: []const u8, commit_data: []const u8, record_blocks: []const ImportedBlock, mst_blocks: []const zat.car.Block, ) ![]const u8 { const root_raw = (zat.cbor.Cid.fromString(allocator, commit_cid) catch return Error.InvalidDagCbor).raw; var blocks: std.ArrayList(zat.car.Block) = .empty; try blocks.append(allocator, .{ .cid_raw = root_raw, .data = commit_data }); for (record_blocks) |block| { try blocks.append(allocator, .{ .cid_raw = (zat.cbor.Cid.fromString(allocator, block.cid) catch return Error.InvalidDagCbor).raw, .data = block.data, }); } for (mst_blocks) |block| try blocks.append(allocator, block); return zat.car.writeAlloc(allocator, .{ .roots = &.{.{ .raw = root_raw }}, .blocks = blocks.items, }); } fn commitEventFrame( allocator: std.mem.Allocator, seq: u64, did: []const u8, commit_cid: []const u8, rev: []const u8, since_rev: ?[]const u8, prev_data_raw: ?[]const u8, car_bytes: []const u8, ops: []const WriteOp, records: []const Record, prev_record_cids: []const ?[]const u8, ) ![]const u8 { var op_values = try allocator.alloc(zat.firehose.CommitEventOp, ops.len); var record_idx: usize = 0; for (ops, 0..) |op, idx| switch (op) { .create => |create_op| { const record = records[record_idx]; record_idx += 1; op_values[idx] = try firehoseOp(allocator, .create, create_op.collection, record.rkey, record.cid, null); }, .update => |update_op| { const record = records[record_idx]; record_idx += 1; // an update of a key the repo did not hold (putRecord on a new // rkey) is a create on the wire: sync 1.1 updates carry a prev const action: zat.CommitAction = if (prev_record_cids[idx] == null) .create else .update; op_values[idx] = try firehoseOp(allocator, action, update_op.collection, update_op.rkey, record.cid, prev_record_cids[idx]); }, .delete => |delete_op| { op_values[idx] = try firehoseOp(allocator, .delete, delete_op.collection, delete_op.rkey, null, prev_record_cids[idx]); }, }; return commitEventFrameFromCar(allocator, seq, did, commit_cid, rev, since_rev, prev_data_raw, car_bytes, op_values); } fn importedCommitEventFrame( allocator: std.mem.Allocator, seq: u64, did: []const u8, commit_cid: []const u8, rev: []const u8, blocks: []const ImportedBlock, ) ![]const u8 { var car_blocks: std.ArrayList(zat.car.Block) = .empty; for (blocks) |block| { try car_blocks.append(allocator, .{ .cid_raw = (zat.cbor.Cid.fromString(allocator, block.cid) catch return Error.InvalidDagCbor).raw, .data = block.data, }); } const root_raw = (zat.cbor.Cid.fromString(allocator, commit_cid) catch return Error.InvalidDagCbor).raw; const car_bytes = try zat.car.writeAlloc(allocator, .{ .roots = &.{.{ .raw = root_raw }}, .blocks = car_blocks.items, }); return commitEventFrameFromCar(allocator, seq, did, commit_cid, rev, null, null, car_bytes, &.{}); } fn accountEventFrame(allocator: std.mem.Allocator, seq: u64, did: []const u8, status: AccountStatus) ![]const u8 { return zat.firehose.encodeAccountEvent(allocator, .{ .seq = @intCast(seq), .did = did, .time = try nowIso(allocator), .active = status.isActive(), .status = zat.AccountStatus.parse(status.asString()), }); } fn commitEventFrameFromCar( allocator: std.mem.Allocator, seq: u64, did: []const u8, commit_cid: []const u8, rev: []const u8, since_rev: ?[]const u8, prev_data_raw: ?[]const u8, car_bytes: []const u8, ops: []const zat.firehose.CommitEventOp, ) ![]const u8 { const commit_raw = (zat.cbor.Cid.fromString(allocator, commit_cid) catch return Error.InvalidDagCbor).raw; return zat.firehose.encodeCommitEvent(allocator, .{ .seq = @intCast(seq), .repo_did = did, .commit_cid = .{ .raw = commit_raw }, .rev = rev, .since_rev = since_rev, .prev_data = if (prev_data_raw) |raw| .{ .raw = raw } else null, .blocks = car_bytes, .ops = ops, .time = try nowIso(allocator), }); } fn firehoseOp( allocator: std.mem.Allocator, action: zat.CommitAction, collection: []const u8, rkey: []const u8, cid: ?[]const u8, prev: ?[]const u8, ) !zat.firehose.CommitEventOp { return .{ .action = action, .collection = collection, .rkey = rkey, .cid = if (cid) |c| try zat.cbor.Cid.fromString(allocator, c) else null, .prev = if (prev) |p| try zat.cbor.Cid.fromString(allocator, p) else null, }; } fn nowIso(allocator: std.mem.Allocator) ![]const u8 { return clock.isoMillis(allocator, clock.nowSeconds()); } fn scalarCountLocked(sql: [:0]const u8, did: []const u8) !u64 { return scalarCountFrom(conn, sql, did); } fn scalarCountFrom(db: zqlite.Conn, sql: [:0]const u8, did: []const u8) !u64 { const row = try db.row(sql, .{did}); if (row == null) return 0; defer row.?.deinit(); return @intCast(row.?.int(0)); } fn accountActiveLocked(did: []const u8) !bool { return (try accountStatusLocked(did)).isActive(); } fn accountActiveFrom(db: zqlite.Conn, did: []const u8) !bool { return (try accountStatusFrom(db, did)).isActive(); } fn accountStatusLocked(did: []const u8) !AccountStatus { return accountStatusFrom(conn, did); } fn accountStatusFrom(db: zqlite.Conn, did: []const u8) !AccountStatus { const row = try db.row( \\SELECT account_status \\FROM accounts \\WHERE did = ? , .{did}); if (row == null) return Error.RepoNotFound; defer row.?.deinit(); return AccountStatus.parse(row.?.text(0)); } fn cidForBlob(allocator: std.mem.Allocator, data: []const u8) ![]const u8 { const cid = try zat.cbor.Cid.create(allocator, 1, 0x55, 0x12, data); defer allocator.free(cid.raw); return zat.multibase.base32lower.encode(allocator, cid.raw); } fn requireInitialized() !void { if (!initialized) return Error.StoreNotInitialized; } fn insertInviteCodeLocked(code: []const u8, use_count: i64, for_account: []const u8, created_by: []const u8) !void { try conn.exec( \\INSERT INTO invite_codes (code, available_uses, disabled, for_account, created_by, created_at) \\VALUES (?, ?, 0, ?, ?, unixepoch()) , .{ code, use_count, for_account, created_by }); } fn ensureInviteAvailableLocked(code: []const u8) !void { const row = try conn.row( \\SELECT available_uses, disabled, \\ (SELECT COUNT(*) FROM invite_code_uses u WHERE u.code = invite_codes.code) AS uses \\FROM invite_codes \\WHERE code = ? \\LIMIT 1 , .{code}); if (row == null) return error.InvalidInviteCode; defer row.?.deinit(); if (row.?.int(1) != 0) return error.InvalidInviteCode; if (row.?.int(2) >= row.?.int(0)) return error.InvalidInviteCode; } fn recordInviteUseLocked(code: []const u8, used_by: []const u8) !void { try conn.exec( \\INSERT INTO invite_code_uses (code, used_by, used_at) \\VALUES (?, ?, unixepoch()) , .{ code, used_by }); } fn inviteCodeUsesLocked(allocator: std.mem.Allocator, code: []const u8) ![]InviteCodeUse { var rows = try conn.rows( \\SELECT used_by, used_at \\FROM invite_code_uses \\WHERE code = ? \\ORDER BY used_at DESC, used_by , .{code}); defer rows.deinit(); var uses: std.ArrayList(InviteCodeUse) = .empty; while (rows.next()) |row| { try uses.append(allocator, .{ .used_by = try allocator.dupe(u8, row.text(0)), .used_at = row.int(1), }); } if (rows.err) |err| return err; return uses.toOwnedSlice(allocator); } fn generateInviteCode(allocator: std.mem.Allocator, public_url: []const u8) ![]const u8 { const host = publicHost(public_url); const normalized = try normalizedInviteHost(allocator, host); var first: [5]u8 = undefined; var second: [5]u8 = undefined; fillBase32(&first); fillBase32(&second); return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ normalized, &first, &second }); } fn publicHost(public_url: []const u8) []const u8 { const after_scheme = if (std.mem.indexOf(u8, public_url, "://")) |idx| public_url[idx + 3 ..] else public_url; const end = std.mem.indexOfAny(u8, after_scheme, "/?#") orelse after_scheme.len; const host_port = after_scheme[0..end]; if (std.mem.lastIndexOfScalar(u8, host_port, ':')) |idx| return host_port[0..idx]; return host_port; } fn normalizedInviteHost(allocator: std.mem.Allocator, host: []const u8) ![]const u8 { const out = try allocator.alloc(u8, host.len); for (host, 0..) |c, i| { out[i] = switch (c) { '.' => '-', else => std.ascii.toLower(c), }; } return out; } fn fillBase32(out: *[5]u8) void { const alphabet = "abcdefghijklmnopqrstuvwxyz234567"; var bytes: [5]u8 = undefined; randomBytes(&bytes); for (&bytes, 0..) |b, i| out[i] = alphabet[b % alphabet.len]; } const schema_statements = [_][*:0]const u8{ \\CREATE TABLE IF NOT EXISTS accounts ( \\ did TEXT PRIMARY KEY, \\ handle TEXT NOT NULL UNIQUE, \\ email TEXT NOT NULL UNIQUE, \\ email_confirmed_at INTEGER, \\ auth_code TEXT, \\ auth_code_expires_at INTEGER, \\ pending_email TEXT, \\ invites_disabled INTEGER NOT NULL DEFAULT 0, \\ activated_at INTEGER, \\ account_status TEXT NOT NULL DEFAULT 'active', \\ account_status_ref TEXT, \\ signing_key_type TEXT, \\ signing_key BLOB, \\ kem_key BLOB, \\ password_hash TEXT NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , "CREATE UNIQUE INDEX IF NOT EXISTS accounts_handle_nocase_idx ON accounts (lower(handle))", \\CREATE TABLE IF NOT EXISTS rate_limit_events ( \\ id INTEGER PRIMARY KEY AUTOINCREMENT, \\ subject TEXT NOT NULL, \\ action TEXT NOT NULL, \\ occurred_at INTEGER NOT NULL \\) , "CREATE INDEX IF NOT EXISTS rate_limit_events_lookup_idx ON rate_limit_events (subject, action, occurred_at)", \\CREATE TABLE IF NOT EXISTS records ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ uri TEXT NOT NULL UNIQUE, \\ cid TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ seq INTEGER NOT NULL, \\ PRIMARY KEY (did, collection, rkey) \\) , "CREATE INDEX IF NOT EXISTS records_collection_idx ON records (did, collection, seq DESC)", "CREATE INDEX IF NOT EXISTS records_cid_idx ON records (cid)", \\CREATE TABLE IF NOT EXISTS commits ( \\ seq INTEGER PRIMARY KEY, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ cid TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ prev TEXT \\) , "CREATE INDEX IF NOT EXISTS commits_did_idx ON commits (did, seq DESC)", // Startup revision backfill and commit-parent lookups match both columns. "CREATE INDEX IF NOT EXISTS commits_cid_idx ON commits (did, cid)", \\CREATE TABLE IF NOT EXISTS seq_events ( \\ seq INTEGER PRIMARY KEY, \\ did TEXT NOT NULL, \\ commit_cid TEXT NOT NULL, \\ evt BLOB NOT NULL \\) , \\CREATE TABLE IF NOT EXISTS blocks ( \\ cid TEXT PRIMARY KEY, \\ data BLOB NOT NULL \\) , \\CREATE TABLE IF NOT EXISTS blobs ( \\ cid TEXT NOT NULL, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ mime_type TEXT NOT NULL, \\ size INTEGER NOT NULL, \\ storage TEXT NOT NULL DEFAULT 'disk', \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (did, cid) \\) , \\CREATE TABLE IF NOT EXISTS record_blobs ( \\ blob_cid TEXT NOT NULL, \\ record_uri TEXT NOT NULL REFERENCES records(uri) ON DELETE CASCADE, \\ PRIMARY KEY (blob_cid, record_uri) \\) , \\CREATE TABLE IF NOT EXISTS mail_outbox ( \\ id INTEGER PRIMARY KEY, \\ to_address TEXT NOT NULL, \\ handle TEXT NOT NULL, \\ subject TEXT NOT NULL, \\ text TEXT NOT NULL, \\ code TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , "CREATE INDEX IF NOT EXISTS mail_outbox_to_idx ON mail_outbox (to_address, id DESC)", \\CREATE TABLE IF NOT EXISTS email_tokens ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ purpose TEXT NOT NULL, \\ token TEXT NOT NULL, \\ expires_at_ms INTEGER NOT NULL, \\ PRIMARY KEY (did, purpose) \\) , \\CREATE TABLE IF NOT EXISTS record_takedowns ( \\ record_uri TEXT PRIMARY KEY REFERENCES records(uri) ON DELETE CASCADE, \\ ref TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS blob_takedowns ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ cid TEXT NOT NULL, \\ ref TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (did, cid) \\) , \\CREATE TABLE IF NOT EXISTS app_preferences ( \\ id INTEGER PRIMARY KEY AUTOINCREMENT, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ name TEXT NOT NULL, \\ value_json BLOB NOT NULL, \\ updated_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS browser_sessions ( \\ token_hash TEXT PRIMARY KEY, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ password_hash TEXT NOT NULL, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS oauth_requests ( \\ request_id TEXT PRIMARY KEY, \\ client_id TEXT NOT NULL, \\ redirect_uri TEXT NOT NULL, \\ scope TEXT NOT NULL, \\ state TEXT NOT NULL, \\ code_challenge TEXT NOT NULL, \\ code_challenge_method TEXT NOT NULL, \\ response_mode TEXT NOT NULL DEFAULT 'query', \\ login_hint TEXT, \\ dpop_jkt TEXT, \\ expires_at INTEGER NOT NULL, \\ sub TEXT, \\ code TEXT UNIQUE, \\ auth_method TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS oauth_tokens ( \\ family_id TEXT NOT NULL UNIQUE, \\ access_token TEXT PRIMARY KEY, \\ refresh_token TEXT NOT NULL UNIQUE, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ client_id TEXT NOT NULL, \\ scope TEXT NOT NULL, \\ expires_at INTEGER NOT NULL, \\ access_expires_at INTEGER NOT NULL, \\ refresh_expires_at INTEGER NOT NULL, \\ previous_refresh_token TEXT, \\ previous_refresh_expires_at INTEGER, \\ dpop_jkt TEXT, \\ auth_method TEXT, \\ revoked_at INTEGER, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ session_started_at INTEGER \\) , \\CREATE TABLE IF NOT EXISTS dpop_jtis ( \\ jti TEXT PRIMARY KEY, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS session_tokens ( \\ id TEXT PRIMARY KEY, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ access_jti TEXT NOT NULL UNIQUE, \\ refresh_jti TEXT NOT NULL UNIQUE, \\ access_expires_at INTEGER NOT NULL, \\ refresh_expires_at INTEGER NOT NULL, \\ auth_method TEXT NOT NULL, \\ controller_did TEXT, \\ app_password_name TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ updated_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ last_used_at INTEGER, \\ revoked_at INTEGER, \\ previous_refresh_jti TEXT, \\ previous_refresh_expires_at INTEGER \\) , "CREATE INDEX IF NOT EXISTS session_tokens_did_idx ON session_tokens (did, created_at DESC)", "CREATE INDEX IF NOT EXISTS session_tokens_access_jti_idx ON session_tokens (access_jti)", "CREATE INDEX IF NOT EXISTS session_tokens_refresh_jti_idx ON session_tokens (refresh_jti)", "CREATE INDEX IF NOT EXISTS session_tokens_controller_idx ON session_tokens (controller_did) WHERE controller_did IS NOT NULL", "CREATE INDEX IF NOT EXISTS session_tokens_app_password_idx ON session_tokens (did, app_password_name) WHERE app_password_name IS NOT NULL", \\CREATE TABLE IF NOT EXISTS app_passwords ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ name TEXT NOT NULL, \\ password_hash TEXT NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ privileged INTEGER NOT NULL DEFAULT 0, \\ scopes TEXT, \\ created_by_controller_did TEXT, \\ PRIMARY KEY (did, name) \\) , "CREATE INDEX IF NOT EXISTS app_passwords_did_created_idx ON app_passwords (did, created_at DESC)", \\CREATE TABLE IF NOT EXISTS account_audit_log ( \\ id TEXT PRIMARY KEY, \\ subject_did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ actor_did TEXT NOT NULL, \\ controller_did TEXT, \\ action TEXT NOT NULL, \\ details_json TEXT NOT NULL DEFAULT '{}', \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , "CREATE INDEX IF NOT EXISTS account_audit_subject_idx ON account_audit_log (subject_did, created_at DESC)", "CREATE INDEX IF NOT EXISTS account_audit_controller_idx ON account_audit_log (controller_did, created_at DESC) WHERE controller_did IS NOT NULL", \\CREATE TABLE IF NOT EXISTS passkeys ( \\ id TEXT PRIMARY KEY, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ credential_id BLOB NOT NULL UNIQUE, \\ public_key BLOB NOT NULL, \\ sign_count INTEGER NOT NULL DEFAULT 0, \\ friendly_name TEXT, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ last_used INTEGER \\) , "CREATE INDEX IF NOT EXISTS passkeys_did_idx ON passkeys (did)", \\CREATE TABLE IF NOT EXISTS webauthn_challenges ( \\ id TEXT PRIMARY KEY, \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ challenge TEXT NOT NULL, \\ kind TEXT NOT NULL, \\ state_json TEXT NOT NULL, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , "CREATE INDEX IF NOT EXISTS webauthn_challenges_did_kind_idx ON webauthn_challenges (did, kind)", \\CREATE TABLE IF NOT EXISTS webauthn_discoverable_challenges ( \\ request_id TEXT PRIMARY KEY REFERENCES oauth_requests(request_id) ON DELETE CASCADE, \\ challenge TEXT NOT NULL, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS invite_codes ( \\ code TEXT PRIMARY KEY, \\ available_uses INTEGER NOT NULL, \\ disabled INTEGER NOT NULL DEFAULT 0, \\ for_account TEXT NOT NULL, \\ created_by TEXT NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , "CREATE INDEX IF NOT EXISTS invite_codes_for_account_idx ON invite_codes (for_account)", \\CREATE TABLE IF NOT EXISTS invite_code_uses ( \\ code TEXT NOT NULL REFERENCES invite_codes(code) ON DELETE CASCADE, \\ used_by TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ used_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (code, used_by) \\) , \\CREATE TABLE IF NOT EXISTS reserved_signing_keys ( \\ id INTEGER PRIMARY KEY AUTOINCREMENT, \\ did TEXT, \\ signing_key TEXT NOT NULL UNIQUE, \\ signing_key_type TEXT NOT NULL, \\ secret_key BLOB NOT NULL, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000), \\ used_at INTEGER \\) , "CREATE INDEX IF NOT EXISTS reserved_signing_keys_did_idx ON reserved_signing_keys (did) WHERE did IS NOT NULL", "CREATE INDEX IF NOT EXISTS reserved_signing_keys_expires_idx ON reserved_signing_keys (expires_at) WHERE used_at IS NULL", \\CREATE TABLE IF NOT EXISTS permissioned_spaces ( \\ uri TEXT PRIMARY KEY, \\ authority_did TEXT NOT NULL, \\ space_type TEXT NOT NULL, \\ skey TEXT NOT NULL, \\ policy_json TEXT, \\ is_authority INTEGER NOT NULL DEFAULT 1, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ deleted_at INTEGER, \\ UNIQUE(authority_did, space_type, skey) \\) , \\CREATE TABLE IF NOT EXISTS permissioned_space_actor_state ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ actor_did TEXT NOT NULL, \\ is_authority INTEGER NOT NULL DEFAULT 0, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ deleted_at INTEGER, \\ PRIMARY KEY (space, actor_did) \\) , \\CREATE TABLE IF NOT EXISTS memberspace_members ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ member_did TEXT NOT NULL, \\ read INTEGER NOT NULL DEFAULT 0, \\ write INTEGER NOT NULL DEFAULT 0, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (space, member_did) \\) , "CREATE INDEX IF NOT EXISTS memberspace_members_member_idx ON memberspace_members (member_did, space)", "CREATE INDEX IF NOT EXISTS permissioned_space_actor_state_actor_idx ON permissioned_space_actor_state (actor_did, space)", \\CREATE TABLE IF NOT EXISTS permissioned_space_records ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ repo_did TEXT NOT NULL, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ cid TEXT NOT NULL, \\ value_json BLOB NOT NULL, \\ validation_status TEXT NOT NULL DEFAULT 'unknown', \\ repo_rev TEXT NOT NULL DEFAULT '', \\ updated_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (space, repo_did, collection, rkey) \\) , "CREATE INDEX IF NOT EXISTS permissioned_space_records_list_idx ON permissioned_space_records (space, repo_did, collection, rkey)", \\CREATE TABLE IF NOT EXISTS permissioned_space_record_blobs ( \\ space TEXT NOT NULL, \\ repo_did TEXT NOT NULL, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ blob_cid TEXT NOT NULL, \\ PRIMARY KEY (space, repo_did, collection, rkey, blob_cid), \\ FOREIGN KEY (space, repo_did, collection, rkey) \\ REFERENCES permissioned_space_records(space, repo_did, collection, rkey) \\ ON DELETE CASCADE \\) , "CREATE INDEX IF NOT EXISTS permissioned_space_record_blobs_blob_idx ON permissioned_space_record_blobs (repo_did, blob_cid)", \\CREATE TABLE IF NOT EXISTS permissioned_space_repos ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ repo_did TEXT NOT NULL, \\ set_hash BLOB, \\ rev TEXT, \\ PRIMARY KEY (space, repo_did) \\) , \\CREATE TABLE IF NOT EXISTS permissioned_space_writers ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ repo_did TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ hash BLOB NOT NULL, \\ updated_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (space, repo_did) \\) , \\CREATE TABLE IF NOT EXISTS permissioned_space_record_oplog ( \\ space TEXT NOT NULL, \\ repo_did TEXT NOT NULL, \\ rev TEXT NOT NULL, \\ idx INTEGER NOT NULL, \\ action TEXT NOT NULL, \\ collection TEXT NOT NULL, \\ rkey TEXT NOT NULL, \\ cid TEXT, \\ prev TEXT, \\ PRIMARY KEY (space, repo_did, rev, idx) \\) , }; test "record activity times writes by TID key, scoped and bucketed by range" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const hour = std.time.us_per_hour; const now: u64 = 1_794_600_000 * std.time.us_per_s + 30 * std.time.us_per_min; const alice = try createAccount(a, "alice.example.com", "a@example.com", "password", "did:plc:activityalice", true); const bob = try createAccount(a, "bob.example.com", "b@example.com", "password", "did:plc:activitybob", true); const hidden = try createAccount(a, "carol.test", "c@example.com", "password", "did:plc:activitycarol", true); const cid = "bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm"; const at = struct { fn key(alloc: std.mem.Allocator, time: u64) ![]const u8 { const tid = zat.Tid.fromTimestamp(time, 0).str(); return alloc.dupe(u8, &tid); } }; try importRepo(a, alice, cid, "3m7gxplizuk2a", &.{ .{ .collection = "app.bsky.feed.post", .rkey = try at.key(a, now - hour), .cid = cid, .blob_cids = &.{} }, .{ .collection = "app.bsky.feed.post", .rkey = try at.key(a, now - 3 * hour), .cid = cid, .blob_cids = &.{} }, .{ .collection = "app.bsky.feed.like", .rkey = try at.key(a, now - 400 * 24 * hour), .cid = cid, .blob_cids = &.{} }, .{ .collection = "app.bsky.actor.profile", .rkey = "self", .cid = cid, .blob_cids = &.{} }, }, &.{}); try importRepo(a, bob, cid, "3m7gxplizuk2b", &.{ .{ .collection = "app.bsky.feed.like", .rkey = try at.key(a, now - 2 * hour), .cid = cid, .blob_cids = &.{} }, // A key ahead of the clock is not a write time. .{ .collection = "app.bsky.feed.like", .rkey = try at.key(a, now + 48 * hour), .cid = cid, .blob_cids = &.{} }, }, &.{}); try importRepo(a, hidden, cid, "3m7gxplizuk2c", &.{ .{ .collection = "app.bsky.feed.post", .rkey = try at.key(a, now - hour), .cid = cid, .blob_cids = &.{} }, }, &.{}); const day = try listRecordActivity(a, .day, null, now); try std.testing.expectEqual(@as(usize, 24), day.buckets.len); var total: u64 = 0; for (day.buckets) |bucket| total += bucket.count; try std.testing.expectEqual(@as(u64, 3), total); try std.testing.expect(day.buckets[23].start_us <= now and now < day.buckets[23].end_us); try std.testing.expectEqual(@as(u64, 1), day.buckets[22].count); const scoped = try listRecordActivity(a, .day, alice.did, now); total = 0; for (scoped.buckets) |bucket| total += bucket.count; try std.testing.expectEqual(@as(u64, 2), total); const all = try listRecordActivity(a, .all, null, now); try std.testing.expect(all.monthly); try std.testing.expect(all.buckets.len >= 13 and all.buckets.len <= 15); total = 0; for (all.buckets) |bucket| total += bucket.count; try std.testing.expectEqual(@as(u64, 4), total); try std.testing.expectEqual(@as(u64, 1), all.buckets[0].count); const everyone = try listCollectionSummaries(a, null, 10); try std.testing.expectEqualStrings("app.bsky.feed.like", everyone[0].collection); try std.testing.expectEqual(@as(u64, 3), everyone[0].count); const just_alice = try listCollectionSummaries(a, alice.did, 10); try std.testing.expectEqual(@as(usize, 3), just_alice.len); try std.testing.expectEqualStrings("app.bsky.feed.post", just_alice[0].collection); } test "telemetry rows add up across flushes and prune by age" { try init(std.Options.debug_io, ":memory:"); defer close(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); var hist: [telemetry_buckets]u32 = @splat(0); hist[3] = 2; const minute: i64 = 29_000_000; const hour: i64 = @divFloor(minute, 60); for (0..2) |_| try addTelemetry(.{ .minutes = &.{.{ .minute = minute, .kind = 1, .count = 2, .failures = 1, .hist = hist }}, .hours = &.{.{ .hour = hour, .route = "sync_get_repo", .method = "GET", .count = 2, .failures = 0, .hist = hist }}, }); const minutes = try listTelemetryMinutes(a, minute, minute + 1); try std.testing.expectEqual(@as(usize, 1), minutes.len); try std.testing.expectEqual(@as(u32, 4), minutes[0].count); try std.testing.expectEqual(@as(u32, 2), minutes[0].failures); try std.testing.expectEqual(@as(u32, 4), minutes[0].hist[3]); const hours = try listTelemetryHours(a, hour, hour + 1); try std.testing.expectEqual(@as(usize, 1), hours.len); try std.testing.expectEqualStrings("sync_get_repo", hours[0].route); try std.testing.expectEqual(@as(u32, 4), hours[0].count); try addTelemetry(.{ .minutes = &.{}, .hours = &.{}, .apps = &.{ .{ .hour = hour, .did = "did:plc:alice", .app = "bsky.app", .count = 3 }, .{ .hour = hour, .did = "did:plc:bob", .app = "plyr.fm", .count = 1 } } }); try addTelemetry(.{ .minutes = &.{}, .hours = &.{}, .apps = &.{.{ .hour = hour, .did = "did:plc:alice", .app = "bsky.app", .count = 2 }} }); const alice_apps = try listTelemetryApps(a, hour, hour + 1, "did:plc:alice"); try std.testing.expectEqual(@as(usize, 1), alice_apps.len); try std.testing.expectEqual(@as(u32, 5), alice_apps[0].count); try std.testing.expectEqual(@as(usize, 2), (try listTelemetryApps(a, hour, hour + 1, null)).len); try pruneTelemetry(minute * 60 + 3 * 24 * 3600); try std.testing.expectEqual(@as(usize, 0), (try listTelemetryMinutes(a, minute, minute + 1)).len); try std.testing.expectEqual(@as(usize, 1), (try listTelemetryHours(a, hour, hour + 1)).len); } const post_schema_statements = [_][*:0]const u8{ \\CREATE TABLE IF NOT EXISTS telemetry_storage ( \\ hour INTEGER PRIMARY KEY, \\ db_bytes INTEGER NOT NULL, \\ blob_bytes INTEGER NOT NULL, \\ records INTEGER NOT NULL, \\ accounts INTEGER NOT NULL \\) , \\CREATE TABLE IF NOT EXISTS telemetry_apps ( \\ hour INTEGER NOT NULL, \\ did TEXT NOT NULL, \\ app TEXT NOT NULL, \\ count INTEGER NOT NULL, \\ PRIMARY KEY (hour, did, app) \\) WITHOUT ROWID , \\CREATE TABLE IF NOT EXISTS telemetry_minutes ( \\ minute INTEGER NOT NULL, \\ kind INTEGER NOT NULL, \\ count INTEGER NOT NULL, \\ failures INTEGER NOT NULL, \\ hist BLOB NOT NULL, \\ PRIMARY KEY (minute, kind) \\) WITHOUT ROWID , \\CREATE TABLE IF NOT EXISTS telemetry_hours ( \\ hour INTEGER NOT NULL, \\ route TEXT NOT NULL, \\ method TEXT NOT NULL, \\ count INTEGER NOT NULL, \\ failures INTEGER NOT NULL, \\ hist BLOB NOT NULL, \\ PRIMARY KEY (hour, route, method) \\) WITHOUT ROWID , "CREATE INDEX IF NOT EXISTS records_rkey_idx ON records (rkey, did)", \\CREATE TABLE IF NOT EXISTS expected_blobs ( \\ blob_cid TEXT NOT NULL, \\ record_uri TEXT NOT NULL REFERENCES records(uri) ON DELETE CASCADE, \\ PRIMARY KEY (blob_cid, record_uri) \\) , "CREATE INDEX IF NOT EXISTS blobs_gc_idx ON blobs (created_at, did, cid)", "CREATE UNIQUE INDEX IF NOT EXISTS oauth_tokens_family_id_idx ON oauth_tokens (family_id)", "CREATE UNIQUE INDEX IF NOT EXISTS oauth_tokens_previous_refresh_token_idx ON oauth_tokens (previous_refresh_token) WHERE previous_refresh_token IS NOT NULL", "CREATE INDEX IF NOT EXISTS session_tokens_previous_refresh_jti_idx ON session_tokens (previous_refresh_jti) WHERE previous_refresh_jti IS NOT NULL", \\CREATE TABLE IF NOT EXISTS oauth_used_refresh_tokens ( \\ refresh_token TEXT PRIMARY KEY, \\ family_id TEXT NOT NULL REFERENCES oauth_tokens(family_id) ON DELETE CASCADE, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS permissioned_space_used_delegations ( \\ jti TEXT PRIMARY KEY, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS permissioned_space_used_client_attestations ( \\ jti TEXT PRIMARY KEY, \\ expires_at INTEGER NOT NULL, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()) \\) , \\CREATE TABLE IF NOT EXISTS repo_blocks ( \\ did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, \\ cid TEXT NOT NULL, \\ data BLOB NOT NULL, \\ repo_rev TEXT, \\ PRIMARY KEY (did, cid) \\) , "CREATE INDEX IF NOT EXISTS repo_blocks_rev_idx ON repo_blocks (did, repo_rev DESC, cid DESC)", // Backfill must not scan payload-bearing rows whose revisions are already known. "CREATE INDEX IF NOT EXISTS repo_blocks_missing_rev_idx ON repo_blocks (did, cid) WHERE repo_rev IS NULL", }; test "persists records in sqlite" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "alice.test", "alice@test.com", "password", "did:plc:cmadossymmii3izkabdbp5en", true, ); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"hello\"}", .{}); defer parsed.deinit(); const rkey = "3jzfcijpj2z2a"; const record = try create(allocator, account, "app.bsky.feed.post", rkey, parsed.value); try std.testing.expectEqualStrings(rkey, record.rkey); try std.testing.expect(!try recordsTableHasValueJsonColumn()); const block_row = try conn.row( "SELECT count(*) FROM repo_blocks WHERE did = ? AND cid = ?", .{ account.did, record.cid }, ); try std.testing.expect(block_row != null); defer block_row.?.deinit(); try std.testing.expectEqual(@as(i64, 1), block_row.?.int(0)); const fetched = get(allocator, account.did, "app.bsky.feed.post", rkey).?; try std.testing.expectEqualStrings(record.cid, fetched.cid); try std.testing.expect(std.mem.indexOf(u8, fetched.value_json, "hello") != null); const listed = try writeListJson(allocator, account.did, "app.bsky.feed.post", null, false, 10); try std.testing.expect(std.mem.indexOf(u8, listed, "hello") != null); const repo_car = try writeRepoCar(allocator, account.did); const loaded = try zat.loadCommitFromCAR(allocator, repo_car); try std.testing.expectEqualStrings(account.did, loaded.commit.did); try std.testing.expectEqualStrings(fetched.rev, loaded.commit.rev); try std.testing.expectEqualStrings((try latestRootLocked(allocator, account.did)).cid, try zat.cbor.Cid.toString(.{ .raw = loaded.commit_cid }, allocator)); // Legacy blocks must still recover their record and commit revisions on restart. try conn.exec("UPDATE repo_blocks SET repo_rev = NULL WHERE did = ?", .{account.did}); try migrateRepoBlockRevs(); const restored = try conn.row("SELECT repo_rev FROM repo_blocks WHERE did = ? AND cid = ?", .{ account.did, record.cid }); try std.testing.expect(restored != null); defer restored.?.deinit(); try std.testing.expectEqualStrings(fetched.rev, restored.?.text(0)); const root = try latestRootLocked(allocator, account.did); const restored_commit = try conn.row("SELECT repo_rev FROM repo_blocks WHERE did = ? AND cid = ?", .{ account.did, root.cid }); try std.testing.expect(restored_commit != null); defer restored_commit.?.deinit(); try std.testing.expectEqualStrings(root.rev, restored_commit.?.text(0)); const tree = try zat.mst.Mst.loadFromBlocks(allocator, loaded.repo_car, loaded.commit.data_cid); const found = tree.get("app.bsky.feed.post/3jzfcijpj2z2a") orelse return error.MissingRecord; try std.testing.expectEqualStrings(record.cid, try zat.cbor.Cid.toString(.{ .raw = found.raw }, allocator)); try conn.exec( "DELETE FROM repo_blocks WHERE did = ? AND cid = ?", .{ account.did, record.cid }, ); try std.testing.expect(get(allocator, account.did, "app.bsky.feed.post", rkey) == null); } test "active account resolution excludes unavailable identities" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "Alice.Example.COM", "alice-active@test.com", "password", "did:plc:activehandle", true, ); try std.testing.expect((try findActiveAccount(allocator, "alice.example.com")) != null); try setAccountActive(account.did, false); try std.testing.expect((try findAccount(allocator, "alice.example.com")) != null); try std.testing.expect((try findActiveAccount(allocator, "alice.example.com")) == null); } test "durable rate limits are isolated by subject and window" { try init(std.Options.debug_io, ":memory:"); defer close(); try consumeRateLimit("did:plc:alice", "identity.updateHandle", 1_000, 1_000, 2); try consumeRateLimit("did:plc:alice", "identity.updateHandle", 1_500, 1_000, 2); try std.testing.expectError( error.RateLimitExceeded, consumeRateLimit("did:plc:alice", "identity.updateHandle", 1_750, 1_000, 2), ); try consumeRateLimit("did:plc:bob", "identity.updateHandle", 1_750, 1_000, 2); try consumeRateLimit("did:plc:alice", "identity.updateHandle", 2_100, 1_000, 2); } test "repo writes enforce swap and explicit validation preconditions" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "swaps.test", "swaps@test.com", "password", "did:plc:swapscheck", true, ); const first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"before\"}", .{}); defer first.deinit(); const rkey = "3jzfcijpj2z2b"; const created_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = first.value, } }}, .{}); const first_cid = created_result.records[0].cid; const second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"after\"}", .{}); defer second.deinit(); try std.testing.expectError(Error.InvalidSwap, applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = second.value, } }}, .{ .swap_commit = "bafkreiwrongcommit" })); try std.testing.expectError(Error.InvalidSwap, applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = second.value, .swap = .{ .cid = "bafkreiwrongrecord" }, } }}, .{})); const updated_result = try applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = second.value, .swap = .{ .cid = first_cid }, } }}, .{ .swap_commit = created_result.commit.cid }); try std.testing.expectEqualStrings("after", (try std.json.parseFromSlice(std.json.Value, allocator, updated_result.records[0].value_json, .{})).value.object.get("text").?.string); const custom = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"example.test.record\",\"text\":\"custom\"}", .{}); defer custom.deinit(); try std.testing.expectError(Error.ValidationRequired, applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "example.test.record", .rkey = "custom", .value = custom.value, } }}, .{ .validate = .require })); const custom_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "example.test.record", .rkey = "custom", .value = custom.value, } }}, .{ .validate = .skip }); try std.testing.expectEqualStrings("unknown", custom_result.records[0].validation_status); } test "repo writes lazily load existing MST blocks for mutation" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "lazy.test", "lazy@test.com", "password", "did:plc:lazywritecheck", true, ); var create_ops: [160]WriteOp = undefined; var create_parsed: [160]std.json.Parsed(std.json.Value) = undefined; var create_rkeys: [160]zat.Tid.Str = undefined; for (&create_ops, &create_parsed, 0..) |*op, *parsed, i| { parsed.* = try std.json.parseFromSlice( std.json.Value, allocator, try std.fmt.allocPrint(allocator, "{{\"$type\":\"app.bsky.feed.post\",\"text\":\"record {d}\"}}", .{i}), .{}, ); create_rkeys[i] = zat.Tid.fromTimestamp(1_700_000_000_000_000 + i, 0).str(); op.* = .{ .create = .{ .collection = "app.bsky.feed.post", .rkey = &create_rkeys[i], .value = parsed.value, } }; } defer for (&create_parsed) |*parsed| parsed.deinit(); _ = try applyWritesWithOptions(allocator, account, &create_ops, .{}); const update = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"updated\"}", .{}); defer update.deinit(); const update_rkey = create_rkeys[77]; const update_result = try applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.feed.post", .rkey = &update_rkey, .value = update.value, } }}, .{}); const delete_rkey = create_rkeys[12]; _ = try applyWritesWithOptions(allocator, account, &.{.{ .delete = .{ .collection = "app.bsky.feed.post", .rkey = &delete_rkey, } }}, .{}); const repo_car = try writeRepoCar(allocator, account.did); const loaded = try zat.loadCommitFromCAR(allocator, repo_car); var tree = try zat.mst.Mst.loadFromBlocks(allocator, loaded.repo_car, loaded.commit.data_cid); const updated_path = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{update_rkey}); const updated = tree.get(updated_path) orelse return error.MissingRecord; try std.testing.expectEqualStrings(update_result.records[0].cid, try zat.cbor.Cid.toString(.{ .raw = updated.raw }, allocator)); const deleted_path = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{delete_rkey}); try std.testing.expect(tree.get(deleted_path) == null); const retained_rkey = create_rkeys[159]; const retained_path = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{retained_rkey}); const retained = tree.get(retained_path) orelse return error.MissingRecord; try std.testing.expectEqualStrings((get(allocator, account.did, "app.bsky.feed.post", &retained_rkey) orelse return error.MissingRecord).cid, try zat.cbor.Cid.toString(.{ .raw = retained.raw }, allocator)); } test "commit firehose since uses previous repo rev" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "firehose-since.test", "firehose-since@test.com", "password", "did:plc:firehosesince", true, ); const first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"first\"}", .{}); defer first.deinit(); const first_rkey = "3jzfcijpj2z2c"; const first_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = first_rkey, .value = first.value, } }}, .{}); const second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"second\"}", .{}); defer second.deinit(); const second_rkey = "3jzfcijpj2z2d"; const second_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = second_rkey, .value = second.value, } }}, .{}); try conn.exec("DELETE FROM seq_events WHERE did = ?", .{account.did}); try rebuildSeqEventsSync11Locked(allocator); const events = try listSeqEvents(allocator, 0, 20); var found_second = false; for (events) |event| { const decoded = zat.firehose.decodeFrame(allocator, event.frame) catch continue; switch (decoded) { .commit => |commit| { if (!std.mem.eql(u8, commit.repo, account.did)) continue; if (!std.mem.eql(u8, commit.rev, second_result.commit.rev)) continue; found_second = true; try std.testing.expect(commit.since != null); try std.testing.expectEqualStrings(first_result.commit.rev, commit.since.?); try std.testing.expect(!std.mem.eql(u8, first_result.commit.cid, commit.since.?)); }, else => {}, } } try std.testing.expect(found_second); } test "commit ops carry the prev the staged tree replaced" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "commit-op-prev.test", "commit-op-prev@test.com", "password", "did:plc:commitopprev", true, ); const profile = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.actor.profile\",\"displayName\":\"one\"}", .{}); defer profile.deinit(); const edited = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.actor.profile\",\"displayName\":\"two\"}", .{}); defer edited.deinit(); // putRecord on a key the repo does not hold: a create on the wire const first = try applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.actor.profile", .rkey = "self", .value = profile.value, } }}, .{}); // putRecord again: an update whose prev is the first record const second = try applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.actor.profile", .rkey = "self", .value = edited.value, } }}, .{}); // one batch that deletes the record, then puts it back: the put's prev // comes from the staged tree, where the record is already gone const third = try applyWritesWithOptions(allocator, account, &.{ .{ .delete = .{ .collection = "app.bsky.actor.profile", .rkey = "self" } }, .{ .update = .{ .collection = "app.bsky.actor.profile", .rkey = "self", .value = profile.value } }, }, .{}); // deleting a record the repo does not hold writes nothing try std.testing.expectError(Error.RecordNotFound, applyWritesWithOptions(allocator, account, &.{.{ .delete = .{ .collection = "app.bsky.feed.post", .rkey = "3jzfcijpj2z2e", } }}, .{})); const latest = (try latestCommitRawLocked(allocator, account.did)).?; try std.testing.expectEqualStrings(third.commit.rev, latest.rev); var seen: usize = 0; for (try listSeqEvents(allocator, 0, 20)) |event| { const decoded = zat.firehose.decodeFrame(allocator, event.frame) catch continue; const commit = switch (decoded) { .commit => |commit| commit, else => continue, }; if (!std.mem.eql(u8, commit.repo, account.did)) continue; if (std.mem.eql(u8, commit.rev, first.commit.rev)) { try std.testing.expectEqual(@as(usize, 1), commit.ops.len); try std.testing.expectEqual(zat.CommitAction.create, commit.ops[0].action); try std.testing.expect(commit.ops[0].prev == null); seen += 1; } else if (std.mem.eql(u8, commit.rev, second.commit.rev)) { try std.testing.expectEqual(zat.CommitAction.update, commit.ops[0].action); const prev = try zat.cbor.Cid.toString(commit.ops[0].prev.?, allocator); try std.testing.expectEqualStrings(first.records[0].cid, prev); seen += 1; } else if (std.mem.eql(u8, commit.rev, third.commit.rev)) { try std.testing.expectEqual(@as(usize, 2), commit.ops.len); try std.testing.expectEqual(zat.CommitAction.delete, commit.ops[0].action); const prev = try zat.cbor.Cid.toString(commit.ops[0].prev.?, allocator); try std.testing.expectEqualStrings(second.records[0].cid, prev); try std.testing.expectEqual(zat.CommitAction.create, commit.ops[1].action); try std.testing.expect(commit.ops[1].prev == null); seen += 1; } } try std.testing.expectEqual(@as(usize, 3), seen); } test "every commit inverts to prevData from its own blocks" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "commit-inversion.test", "commit-inversion@test.com", "password", "did:plc:commitinversion", true); const keypair = try signingKeypair(account.did); const public_key = try keypair.publicKey(); const value = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"com.example.churn\",\"n\":1}", .{}); defer value.deinit(); // random TIDs give the tree keys at several layers, which is where a // delete's inverse insert has to split a subtree the write never loaded var prng = std.Random.DefaultPrng.init(0x5eed); const random = prng.random(); const tid_first = "234567abcdefghij"; const tid_rest = "234567abcdefghijklmnopqrstuvwxyz"; var rkeys: std.ArrayList([]const u8) = .empty; for (0..160) |_| { const rkey = try allocator.alloc(u8, 13); rkey[0] = tid_first[random.uintLessThan(usize, tid_first.len)]; for (rkey[1..]) |*c| c.* = tid_rest[random.uintLessThan(usize, tid_rest.len)]; try rkeys.append(allocator, rkey); } var batch: std.ArrayList(WriteOp) = .empty; for (rkeys.items, 0..) |rkey, idx| { try batch.append(allocator, .{ .create = .{ .collection = "com.example.churn", .rkey = rkey, .value = value.value } }); if (batch.items.len == 8 or idx == rkeys.items.len - 1) { _ = try applyWritesWithOptions(allocator, account, batch.items, .{}); batch.clearRetainingCapacity(); } } random.shuffle([]const u8, rkeys.items); // single deletes, then mixed batches that delete, update and recreate for (rkeys.items[0..60]) |rkey| { _ = try applyWritesWithOptions(allocator, account, &.{.{ .delete = .{ .collection = "com.example.churn", .rkey = rkey } }}, .{}); } var idx: usize = 60; while (idx + 4 <= rkeys.items.len) : (idx += 4) { _ = try applyWritesWithOptions(allocator, account, &.{ .{ .delete = .{ .collection = "com.example.churn", .rkey = rkeys.items[idx] } }, .{ .delete = .{ .collection = "com.example.churn", .rkey = rkeys.items[idx + 1] } }, .{ .update = .{ .collection = "com.example.churn", .rkey = rkeys.items[idx + 2], .value = value.value } }, .{ .create = .{ .collection = "com.example.churn", .rkey = rkeys.items[idx - 60], .value = value.value } }, }, .{}); } var checked: usize = 0; var cursor: u64 = 0; while (true) { const events = try listSeqEvents(allocator, cursor, 100); if (events.len == 0) break; for (events) |event| { cursor = event.seq; const decoded = zat.firehose.decodeFrame(allocator, event.frame) catch continue; const commit = switch (decoded) { .commit => |commit| commit, else => continue, }; if (!std.mem.eql(u8, commit.repo, account.did)) continue; const prev_data = commit.prev_data orelse continue; _ = try zat.verifyCommitDiff(allocator, commit.blocks, try commit.toMstOperations(allocator), prev_data.raw, .{ .key_type = keypair.key_type, .raw = &public_key }, .{ .expected_did = account.did }); checked += 1; } } try std.testing.expect(checked > 100); } test "listing sequenced events does not perform event repair" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "firehose-read.test", "firehose-read@test.com", "password", "did:plc:firehoseread", true, ); const value = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"sequenced at write time\"}", .{}, ); defer value.deinit(); _ = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = "3jzfcijpj2z2e", .value = value.value, } }}, .{}); const before = try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did); try std.testing.expect(before > 0); try conn.exec("DELETE FROM seq_events WHERE did = ?", .{account.did}); const events = try listSeqEvents(allocator, 0, 100); for (events) |event| { const decoded = zat.firehose.decodeFrame(allocator, event.frame) catch continue; switch (decoded) { .commit => |commit| try std.testing.expect(!std.mem.eql(u8, commit.repo, account.did)), else => {}, } } try std.testing.expectEqual( @as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did), ); } test "getRepo since filters repo blocks by revision" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "since.test", "since@test.com", "password", "did:plc:sincecheck", true, ); const first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"first\"}", .{}); defer first.deinit(); const first_rkey = "3jzfcijpj2z2e"; const first_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = first_rkey, .value = first.value, } }}, .{}); const second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"second\"}", .{}); defer second.deinit(); const second_rkey = "3jzfcijpj2z2f"; const second_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = second_rkey, .value = second.value, } }}, .{}); const full_car = try zat.car.read(allocator, try writeRepoCar(allocator, account.did)); const diff_car = try zat.car.read(allocator, try writeRepoCarSince(allocator, account.did, first_result.commit.rev)); const empty_diff_car = try zat.car.read(allocator, try writeRepoCarSince(allocator, account.did, second_result.commit.rev)); try std.testing.expect(diff_car.blocks.len < full_car.blocks.len); try std.testing.expect(diff_car.blocks.len > 0); try std.testing.expectEqual(@as(usize, 0), empty_diff_car.blocks.len); try std.testing.expectEqualStrings(second_result.commit.cid, try zat.cbor.Cid.toString(.{ .raw = empty_diff_car.roots[0].raw }, allocator)); } test "getRepo since reads repo_blocks by revision range, not every block" { try init(std.Options.debug_io, ":memory:"); defer close(); // The diff query must bound the index by revision; constrained by DID // alone it read every block the account ever wrote. try expectQueryPlanUses(since_export_newer_sql, .{ "did:plc:plan", "3jzfcijpj2z2a" }, "repo_rev>?"); try expectQueryPlanUses(since_export_unknown_rev_sql, .{"did:plc:plan"}, "repo_rev=?"); } test "getRepo since keeps blocks whose revision is unknown" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "since-legacy.test", "since-legacy@test.com", "password", "did:plc:sincelegacy", true); const value = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"legacy\"}", .{}); defer value.deinit(); const first = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = "3jzfcijpj2z2e", .value = value.value, } }}, .{}); const newer = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"newer\"}", .{}); defer newer.deinit(); const second = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = "3jzfcijpj2z2f", .value = newer.value, } }}, .{}); const legacy_cid = first.records[0].cid; try conn.exec("UPDATE repo_blocks SET repo_rev = NULL WHERE did = ? AND cid = ?", .{ account.did, legacy_cid }); const diff = try zat.car.read(allocator, try writeRepoCarSince(allocator, account.did, first.commit.rev)); try std.testing.expect(try carContainsCid(allocator, diff, legacy_cid)); try std.testing.expect(try carContainsCid(allocator, diff, second.records[0].cid)); // Known revisions come first, newest first, as the single query ordered them. try std.testing.expectEqualStrings(legacy_cid, try zat.cbor.Cid.toString(.{ .raw = diff.blocks[diff.blocks.len - 1].cid_raw }, allocator)); } fn expectQueryPlanUses(comptime sql: []const u8, args: anytype, needle: []const u8) !void { var rows = try conn.rows("EXPLAIN QUERY PLAN " ++ sql, args); defer rows.deinit(); var found = false; while (rows.next()) |row| { const detail = row.text(3); if (std.mem.indexOf(u8, detail, "repo_blocks_rev_idx") != null and std.mem.indexOf(u8, detail, needle) != null) found = true; } if (rows.err) |err| return err; try std.testing.expect(found); } fn carContainsCid(allocator: std.mem.Allocator, c: zat.car.Car, cid: []const u8) !bool { for (c.blocks) |block| { if (std.mem.eql(u8, cid, try zat.cbor.Cid.toString(.{ .raw = block.cid_raw }, allocator))) return true; } return false; } test "full getRepo exports only current reachable record blocks" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "reachable.test", "reachable@test.com", "password", "did:plc:reachablecheck", true, ); const first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"first\"}", .{}); defer first.deinit(); const rkey = "3jzfcijpj2z2g"; const first_result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = first.value, } }}, .{}); const first_cid = first_result.records[0].cid; const second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"second\"}", .{}); defer second.deinit(); const second_result = try applyWritesWithOptions(allocator, account, &.{.{ .update = .{ .collection = "app.bsky.feed.post", .rkey = rkey, .value = second.value, } }}, .{}); const second_cid = second_result.records[0].cid; const updated_car = try zat.car.read(allocator, try writeRepoCar(allocator, account.did)); try std.testing.expect(!try carContainsCid(allocator, updated_car, first_cid)); try std.testing.expect(try carContainsCid(allocator, updated_car, second_cid)); _ = try applyWritesWithOptions(allocator, account, &.{.{ .delete = .{ .collection = "app.bsky.feed.post", .rkey = rkey, } }}, .{}); const deleted_car = try zat.car.read(allocator, try writeRepoCar(allocator, account.did)); try std.testing.expect(!try carContainsCid(allocator, deleted_car, first_cid)); try std.testing.expect(!try carContainsCid(allocator, deleted_car, second_cid)); } test "full getRepo prefetch matches a block-at-a-time walk" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "prefetch.test", "prefetch@test.com", "password", "did:plc:prefetchcheck", true); // Enough records for a few MST levels and more than one prefetch batch. for (0..prefetch_batch_cids + 200) |i| { const value = try std.json.parseFromSlice(std.json.Value, allocator, try std.fmt.allocPrint(allocator, "{{\"$type\":\"dev.zds.test.item\",\"n\":{d}}}", .{i}), .{}); const rkey = try std.fmt.allocPrint(allocator, "item{d:0>5}", .{i}); _ = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "dev.zds.test.item", .rkey = rkey, .value = value.value, } }}, .{}); } const root = (try latestCommitRawFrom(conn, allocator, account.did)).?; var expected: std.ArrayList(zat.car.Block) = .empty; try expected.append(allocator, .{ .cid_raw = root.commit_cid_raw, .data = root.commit_data }); var direct = RepoBlockReader{ .allocator = allocator, .did = account.did, .connection = conn }; try zat.mst.collectReachableBlocks(allocator, root.data_cid_raw, direct.reader(), &expected, .{ .include_records = true }); const expected_car = try zat.car.writeAlloc(allocator, .{ .roots = &.{.{ .raw = root.commit_cid_raw }}, .blocks = expected.items }); var prefetched = PrefetchedBlocks{ .fallback = .{ .allocator = allocator, .did = account.did, .connection = conn } }; try prefetched.loadReachable(allocator, conn, account.did, root.data_cid_raw); // Every reachable block but the commit came from the batched reads. try std.testing.expectEqual(expected.items.len - 1, prefetched.by_cid.count()); try std.testing.expectEqualSlices(u8, expected_car, try writeRepoCar(allocator, account.did)); } test "record proof car carries commit, mst path, and record" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "proof.test", "proof@test.com", "password", "did:plc:proofcheck", true, ); var last_commit_cid: []const u8 = ""; var target_cid: []const u8 = ""; const target_rkey = "3jzfcijpj2z2m"; for (0..24) |i| { const value = try std.json.parseFromSlice(std.json.Value, allocator, try std.fmt.allocPrint(allocator, "{{\"$type\":\"app.bsky.feed.post\",\"text\":\"post {d}\"}}", .{i}), .{}); const rkey = try std.fmt.allocPrint(allocator, "3jzfcijpj2a{c}{c}", .{ 'a' + @as(u8, @intCast(i / 12)), 'a' + @as(u8, @intCast(i % 12)) }); const result = try applyWritesWithOptions(allocator, account, &.{.{ .create = .{ .collection = "app.bsky.feed.post", .rkey = if (i == 11) target_rkey else rkey, .value = value.value, } }}, .{}); last_commit_cid = result.commit.cid; if (i == 11) target_cid = result.records[0].cid; } const proof_car = try zat.car.read(allocator, try writeRecordProofCar(allocator, account.did, "app.bsky.feed.post", target_rkey)); try std.testing.expectEqualStrings(last_commit_cid, try zat.cbor.Cid.toString(.{ .raw = proof_car.roots[0].raw }, allocator)); try std.testing.expect(try carContainsCid(allocator, proof_car, target_cid)); const full_car = try zat.car.read(allocator, try writeRepoCar(allocator, account.did)); try std.testing.expect(proof_car.blocks.len < full_car.blocks.len); const commit_data = zat.car.findBlock(proof_car, proof_car.roots[0].raw) orelse return error.TestUnexpectedResult; const commit_value = try zat.cbor.decodeAll(allocator, commit_data); const data_cid = commit_value.getCid("data") orelse return error.TestUnexpectedResult; const tree = try zat.mst.Mst.loadFromBlocks(allocator, proof_car, data_cid.raw); const key = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{target_rkey}); const found = tree.get(key) orelse return error.TestUnexpectedResult; try std.testing.expectEqualStrings(target_cid, try zat.cbor.Cid.toString(.{ .raw = found.raw }, allocator)); const exclusion_car = try zat.car.read(allocator, try writeRecordProofCar(allocator, account.did, "app.bsky.feed.post", "3jzfcijpj2zzz")); try std.testing.expectEqualStrings(last_commit_cid, try zat.cbor.Cid.toString(.{ .raw = exclusion_car.roots[0].raw }, allocator)); try std.testing.expect(exclusion_car.blocks.len >= 2); const exclusion_tree = try zat.mst.Mst.loadFromBlocks(allocator, exclusion_car, data_cid.raw); try std.testing.expect(exclusion_tree.get("app.bsky.feed.post/3jzfcijpj2zzz") == null); try std.testing.expectError(Error.RepoNotFound, writeRecordProofCar(allocator, "did:plc:noproofrepo", "app.bsky.feed.post", "3jzfcijpj2z2m")); } fn testDatabasePath(allocator: std.mem.Allocator, tmp: *std.testing.TmpDir) ![]const u8 { var path_buf: [std.fs.max_path_bytes]u8 = undefined; const root_len = try tmp.dir.realPath(std.testing.io, &path_buf); return std.fmt.allocPrint(allocator, "{s}/zds.sqlite3", .{path_buf[0..root_len]}); } test "file-backed reads are served while the writer lock is held" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try init(std.Options.debug_io, try testDatabasePath(allocator, &tmp)); defer close(); try std.testing.expectEqual(app_config.dbReaders(), readers.?.conns.len); const account = try createAccount(allocator, "reader-pool.test", "reader-pool@test.com", "password", "did:plc:readerpool", true); const post = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"pooled\"}", .{}); defer post.deinit(); _ = try create(allocator, account, "app.bsky.feed.post", "3jzfcijpj2z2g", post.value); // A long write or blob GC pass holds db_mutex; the request-path reads // must not queue behind it. Were any of these still on the writer // connection, this thread would deadlock on its own lock. db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); const record = get(allocator, account.did, "app.bsky.feed.post", "3jzfcijpj2z2g") orelse return error.TestUnexpectedResult; try std.testing.expectEqualStrings(account.did, record.did); try std.testing.expectEqual(@as(usize, 1), (try listRecords(allocator, account.did, "app.bsky.feed.post", 10)).len); try std.testing.expectEqual(@as(usize, 1), count(account.did, "app.bsky.feed.post")); try std.testing.expect(getRecordBlock(allocator, account.did, "app.bsky.feed.post", "3jzfcijpj2z2g") != null); try std.testing.expect((try findAccount(allocator, "reader-pool.test")) != null); try std.testing.expect(isAccountActive(account.did)); try std.testing.expect((try listSeqEvents(allocator, 0, 10)).len > 0); _ = try writeLatestCommitJson(allocator, account.did); _ = try writeRepoStatusJson(allocator, account.did); _ = try writeRecordProofCar(allocator, account.did, "app.bsky.feed.post", "3jzfcijpj2z2g"); _ = try listResidents(allocator, 10); _ = try listRecordActivity(allocator, .all, null, nowMicros()); } test "file-backed reads see a write as soon as it returns" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try init(std.Options.debug_io, try testDatabasePath(allocator, &tmp)); defer close(); const account = try createAccount(allocator, "reader-fresh.test", "reader-fresh@test.com", "password", "did:plc:readerfresh", true); const rkeys = [_][]const u8{ "3jzfcijpj2z2a", "3jzfcijpj2z2b", "3jzfcijpj2z2c", "3jzfcijpj2z2d", "3jzfcijpj2z2e", "3jzfcijpj2z2f" }; // More writes than readers, so every reader serves a read after a // commit it did not make. for (rkeys, 1..) |rkey, written| { const post = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"fresh\"}", .{}); defer post.deinit(); const created = try create(allocator, account, "app.bsky.feed.post", rkey, post.value); try std.testing.expectEqualStrings(created.cid, getCid(allocator, account.did, "app.bsky.feed.post", rkey) orelse return error.TestUnexpectedResult); try std.testing.expectEqual(written, count(account.did, "app.bsky.feed.post")); } _ = try delete(allocator, account, "app.bsky.feed.post", rkeys[0]); try std.testing.expect(get(allocator, account.did, "app.bsky.feed.post", rkeys[0]) == null); try std.testing.expectEqual(rkeys.len - 1, count(account.did, "app.bsky.feed.post")); } const ReadWhileBusy = struct { allocator: std.mem.Allocator, did: []const u8, found: std.atomic.Value(bool) = .init(false), fn run(self: *ReadWhileBusy) void { if (findAccount(self.allocator, self.did) catch null) |_| self.found.store(true, .release); } }; test "a read waits for a reader when every reader is busy" { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try init(std.Options.debug_io, try testDatabasePath(allocator, &tmp)); defer close(); const pool = readers orelse return error.TestUnexpectedResult; const account = try createAccount(allocator, "reader-busy.test", "reader-busy@test.com", "password", "did:plc:readerbusy", true); var leases: [max_db_readers]ReadLease = undefined; const busy = leases[0..pool.conns.len]; for (busy) |*lease| lease.* = try acquireReader(); var released: usize = 0; defer for (busy[released..]) |lease| lease.release(); var waiter: ReadWhileBusy = .{ .allocator = allocator, .did = account.did }; const thread = try std.Thread.spawn(.{}, ReadWhileBusy.run, .{&waiter}); try store_io.sleep(.fromMilliseconds(50), .awake); try std.testing.expect(!waiter.found.load(.acquire)); leases[0].release(); released = 1; thread.join(); try std.testing.expect(waiter.found.load(.acquire)); } test "ZDS_DB_READERS=0 keeps every read on the writer connection" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const previous = app_config.dbReaders(); app_config.setDbReaders(0); defer app_config.setDbReaders(previous); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try init(std.Options.debug_io, try testDatabasePath(allocator, &tmp)); defer close(); try std.testing.expect(readers == null); const account = try createAccount(allocator, "reader-off.test", "reader-off@test.com", "password", "did:plc:readeroff", true); try std.testing.expect((try findAccount(allocator, account.did)) != null); } test "file-backed repo export snapshot does not block writes" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const root_len = try tmp.dir.realPath(std.testing.io, &path_buf); const db_path = try std.fmt.allocPrint(allocator, "{s}/zds.sqlite3", .{path_buf[0..root_len]}); try init(std.Options.debug_io, db_path); defer close(); const account = try createAccount( allocator, "export-snapshot.test", "export-snapshot@test.com", "password", "did:plc:exportsnapshot", true, ); const first = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"before snapshot\"}", .{}, ); defer first.deinit(); const first_rkey = "3jzfcijpj2z2g"; _ = try create(allocator, account, "app.bsky.feed.post", first_rkey, first.value); const read_conn = try openRepoReadConnection(database_path.?); defer read_conn.close(); try read_conn.transaction(); errdefer read_conn.rollback(); const snapshot = try collectRepoCarFullFrom(allocator, read_conn, account.did); const second = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"during snapshot\"}", .{}, ); defer second.deinit(); const second_rkey = "3jzfcijpj2z2h"; _ = try create(allocator, account, "app.bsky.feed.post", second_rkey, second.value); try read_conn.commit(); const first_path = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{first_rkey}); const second_path = try std.fmt.allocPrint(allocator, "app.bsky.feed.post/{s}", .{second_rkey}); const snapshot_car = try writeRepoCarData(allocator, snapshot); const loaded_snapshot = try zat.loadCommitFromCAR(allocator, snapshot_car); var snapshot_tree = try zat.mst.Mst.loadFromBlocks( allocator, loaded_snapshot.repo_car, loaded_snapshot.commit.data_cid, ); try std.testing.expect(snapshot_tree.get(first_path) != null); try std.testing.expect(snapshot_tree.get(second_path) == null); const current_car = try writeRepoCar(allocator, account.did); const loaded_current = try zat.loadCommitFromCAR(allocator, current_car); var current_tree = try zat.mst.Mst.loadFromBlocks( allocator, loaded_current.repo_car, loaded_current.commit.data_cid, ); try std.testing.expect(current_tree.get(second_path) != null); } test "permissioned spaces store self-owned records outside public repo" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "space.test", "space@test.com", "password", "did:plc:spaceauthorityalice", true, ); const space = try createSpace(allocator, .{ .actor_did = account.did, .authority_did = account.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); try std.testing.expectEqualStrings("at:space/did:plc:spaceauthorityalice/fm.plyr.privateMedia/self", space.uri); try std.testing.expect(space.is_authority); try std.testing.expectError(Error.InvalidRefreshSession, createSpace(allocator, .{ .actor_did = account.did, .authority_did = account.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, })); const parsed = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"fm.plyr.track\",\"title\":\"secret track\",\"audioBlob\":{\"$type\":\"blob\",\"ref\":{\"$link\":\"bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku\"},\"mimeType\":\"audio/mpeg\",\"size\":12}}", .{}, ); defer parsed.deinit(); const prepared = try prepareRecordValue(allocator, "fm.plyr.track", "track-one", parsed.value); const stored = try putSpaceRecord(allocator, space.uri, account.did, "fm.plyr.track", "track-one", prepared); try std.testing.expectEqualStrings("track-one", stored.rkey); try std.testing.expectEqualStrings("unknown", stored.validation_status); try std.testing.expect(get(allocator, account.did, "fm.plyr.track", "track-one") == null); try expectSpaceBlobRefCount(space.uri, account.did, "fm.plyr.track", "track-one", "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", 1); try std.testing.expect(try permissionedBlobReferenced(space.uri, account.did, "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")); try std.testing.expect(getPublicBlob(allocator, account.did, "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku") == null); const found = (try getSpaceRecord(allocator, space.uri, account.did, "fm.plyr.track", "track-one")).?; try std.testing.expectEqualStrings(stored.cid, found.cid); try std.testing.expect(std.mem.indexOf(u8, found.value_json, "secret track") != null); const listed = try listSpaceRecords(allocator, space.uri, account.did, "fm.plyr.track", null, 50, true); try std.testing.expectEqual(@as(usize, 1), listed.len); try std.testing.expectEqualStrings("fm.plyr.track", listed[0].collection); try std.testing.expectEqualStrings("track-one", listed[0].rkey); try std.testing.expect(listed[0].value_json != null); try std.testing.expect(std.mem.indexOf(u8, listed[0].value_json.?, "secret track") != null); const listed_refs = try listSpaceRecords(allocator, space.uri, account.did, "fm.plyr.track", null, 50, false); try std.testing.expectEqual(@as(usize, 1), listed_refs.len); try std.testing.expect(listed_refs[0].value_json == null); const updated_parsed = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"fm.plyr.track\",\"title\":\"secret track\",\"audioBlob\":{\"$type\":\"blob\",\"ref\":{\"$link\":\"bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu\"},\"mimeType\":\"audio/mpeg\",\"size\":12}}", .{}, ); defer updated_parsed.deinit(); const updated_prepared = try prepareRecordValue(allocator, "fm.plyr.track", "track-one", updated_parsed.value); _ = try putSpaceRecord(allocator, space.uri, account.did, "fm.plyr.track", "track-one", updated_prepared); try expectSpaceBlobRefCount(space.uri, account.did, "fm.plyr.track", "track-one", "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", 0); try expectSpaceBlobRefCount(space.uri, account.did, "fm.plyr.track", "track-one", "bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu", 1); try std.testing.expect(!try permissionedBlobReferenced(space.uri, account.did, "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")); try std.testing.expect(try permissionedBlobReferenced(space.uri, account.did, "bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu")); try deleteSpaceRecord(allocator, space.uri, account.did, "fm.plyr.track", "track-one"); try expectSpaceBlobRefCount(space.uri, account.did, "fm.plyr.track", "track-one", "bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu", 0); try std.testing.expect(!try permissionedBlobReferenced(space.uri, account.did, "bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu")); } test "permissioned blob ref migration backfills existing records" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const did = "did:plc:spaceblobmigration"; const space = try createSpace(allocator, .{ .actor_did = did, .authority_did = did, .space_type = "fm.example.private", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); const parsed = try std.json.parseFromSlice( std.json.Value, allocator, "{\"$type\":\"fm.example.note\",\"media\":{\"$type\":\"blob\",\"ref\":{\"$link\":\"bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku\"},\"mimeType\":\"text/plain\",\"size\":12}}", .{}, ); defer parsed.deinit(); const prepared = try prepareRecordValue(allocator, "fm.example.note", "one", parsed.value); _ = try putSpaceRecord(allocator, space.uri, did, "fm.example.note", "one", prepared); try conn.execNoArgs("DELETE FROM permissioned_space_record_blobs"); try conn.exec("DELETE FROM zds_migrations WHERE name = ?", .{"permissioned-space-blob-refs-v1"}); try migratePermissionedSpaceBlobRefs(); try expectSpaceBlobRefCount(space.uri, did, "fm.example.note", "one", "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", 1); } test "permissioned space foundation migration preserves rows and rebuilds hashes" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const did = "did:plc:spacemigrationalice"; const canonical = "at:space/did:plc:spacemigrationalice/fm.example.private/self"; const legacy = "ats://did:plc:spacemigrationalice/fm.example.private/self"; const prior = "at://did:plc:spacemigrationalice/space/fm.example.private/self"; const space = try createSpace(allocator, .{ .actor_did = did, .authority_did = did, .space_type = "fm.example.private", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); try std.testing.expectEqualStrings(canonical, space.uri); _ = try changeSpaceMembers(allocator, canonical, &.{.{ .did = "did:plc:spacemigrationbob", .access = .{ .read = true, .write = true } }}); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"fm.example.note\",\"text\":\"private\"}", .{}); defer parsed.deinit(); const prepared = try prepareRecordValue(allocator, "fm.example.note", "one", parsed.value); const record = try putSpaceRecord(allocator, canonical, did, "fm.example.note", "one", prepared); try conn.exec( \\INSERT INTO permissioned_space_record_blobs (space, repo_did, collection, rkey, blob_cid) \\VALUES (?, ?, ?, ?, ?) , .{ canonical, did, "fm.example.note", "one", "bafkreiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }); try conn.exec( \\INSERT INTO permissioned_space_notify_registrations (space, endpoint, token, expires_at) \\VALUES (?, ?, ?, 4102444800) , .{ canonical, "https://sync.example", "secret" }); const tombstone_before = "at://did:web:remote.example/space/com.example.public/deleted"; const tombstone_after = "at:space/did:web:remote.example/com.example.public/deleted"; try conn.exec("INSERT INTO permissioned_space_revocations (space, jti, expires_at) VALUES (?, 'jti', 4102444800)", .{tombstone_before}); try conn.exec("INSERT INTO permissioned_space_syncer_outbox (space, endpoint, token, notice, expires_at) VALUES (?, 'https://sync.example', 'secret', 'spaceDeleted', 4102444800)", .{tombstone_before}); try conn.execNoArgs("PRAGMA foreign_keys = OFF"); inline for (.{ "permissioned_space_actor_state", "memberspace_members", "permissioned_space_records", "permissioned_space_record_blobs", "permissioned_space_repos", "permissioned_space_writers", "permissioned_space_record_oplog", "permissioned_space_notify_registrations", }) |table| { try conn.exec("UPDATE " ++ table ++ " SET space = ? WHERE space = ?", .{ legacy, canonical }); } inline for (.{ "permissioned_space_sequence", "permissioned_space_notify_outbox" }) |table| { try conn.exec("UPDATE " ++ table ++ " SET space = ? WHERE space = ?", .{ prior, canonical }); } try conn.exec("UPDATE permissioned_spaces SET uri = ? WHERE uri = ?", .{ legacy, canonical }); try conn.exec("UPDATE permissioned_space_repos SET set_hash = zeroblob(?)", .{zat.space.lthash.lthash_state_bytes}); try conn.exec("DELETE FROM zds_migrations WHERE name IN (?, ?, ?)", .{ "permissioned-space-at-uri", "permissioned-space-record-element-v1", "permissioned-space-writer-set-v1" }); try conn.execNoArgs("DELETE FROM zds_migrations WHERE name = 'permissioned-space-at-space-uri'"); try conn.execNoArgs("PRAGMA foreign_keys = ON"); try migratePermissionedDataTables(); try migrateSpaceUriScheme(); try migrateSpaceUriScheme(); inline for (.{ "permissioned_space_revocations", "permissioned_space_syncer_outbox" }) |table| { const row = (try conn.row("SELECT space FROM " ++ table, .{})).?; defer row.deinit(); try std.testing.expectEqualStrings(tombstone_after, row.text(0)); } inline for (.{ "permissioned_spaces", "permissioned_space_actor_state", "memberspace_members", "permissioned_space_records", "permissioned_space_record_blobs", "permissioned_space_repos", "permissioned_space_writers", "permissioned_space_record_oplog", "permissioned_space_notify_registrations", }) |table| { const column = comptime if (std.mem.eql(u8, table, "permissioned_spaces")) "uri" else "space"; const row = (try conn.row("SELECT count(*) FROM " ++ table ++ " WHERE " ++ column ++ " = ?", .{canonical})).?; defer row.deinit(); try std.testing.expect(row.int(0) > 0); } const state = try getSpaceRepoState(allocator, canonical, did); var expected: zat.space.LtHash = .{}; try addRecordElement(allocator, &expected, record.collection, record.rkey, record.cid); try std.testing.expectEqualSlices(u8, &expected.bytes, state.set_hash.?); const writers = try listSpaceWriters(allocator, canonical, null, 50); try std.testing.expectEqual(@as(usize, 1), writers.len); try std.testing.expectEqualStrings(did, writers[0].repo_did); try std.testing.expectEqualSlices(u8, &expected.digest(), writers[0].hash); } fn expectSyncerNotice(allocator: std.mem.Allocator, endpoint: []const u8, notice: SyncerNotice, space_rev: ?[]const u8) !PendingSyncerNotice { const pending = (try claimSyncerNotice(allocator)) orelse return error.TestExpectedEqual; try std.testing.expectEqualStrings(endpoint, pending.registration.endpoint); try std.testing.expectEqual(notice, pending.notice); if (space_rev) |rev| try std.testing.expectEqualStrings(rev, pending.space_rev.?) else try std.testing.expect(pending.space_rev == null); return pending; } test "a lapsed registration is noticed once, and renewing it starts over" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "webhook.test", "webhook@test.com", "password", "did:plc:webhookowner", true); const space = (try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.webhook.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{} } })).uri; const live: NotifyRegistration = .{ .endpoint = "https://live.example", .token = "one" }; const lapsed: NotifyRegistration = .{ .endpoint = "https://lapsed.example", .token = "two" }; const far = clock.nowSeconds() + 600; try std.testing.expect(!try notifyRegistrationExists(space, live)); try putNotifyRegistration(space, live, far); try putNotifyRegistration(space, lapsed, far); // Another token on the same endpoint is another registration. try putNotifyRegistration(space, .{ .endpoint = "https://live.example", .token = "three" }, far); try std.testing.expectEqual(@as(usize, 3), (try activeNotifyRegistrations(a, space)).len); try std.testing.expectError(Error.RepoNotFound, putNotifyRegistration("at:space/did:plc:webhookowner/test.webhook.space/none", live, far)); try putNotifyRegistration(space, lapsed, 1); try std.testing.expect(try notifyRegistrationExists(space, lapsed)); try std.testing.expectEqual(@as(usize, 2), (try activeNotifyRegistrations(a, space)).len); try queueExpiryNotices(space, "3m3tfkwy3ii22"); try queueExpiryNotices(space, "3m3tfkwy4ii22"); const notice = try expectSyncerNotice(a, "https://lapsed.example", .expired, "3m3tfkwy3ii22"); try std.testing.expect(try syncerNoticeOwed(notice)); // Leased, and the second update queued nothing more. try std.testing.expect(try claimSyncerNotice(a) == null); try finishSyncerNotice(notice); try conn.execNoArgs("UPDATE permissioned_space_syncer_outbox SET lease_until = 0"); try std.testing.expect(try claimSyncerNotice(a) == null); // A renewal clears the lapse, so the next lapse is noticed again; a // notice queued before the renewal is no longer owed. try putNotifyRegistration(space, lapsed, 1); try queueExpiryNotices(space, "3m3tfkwy5ii22"); const stale = try expectSyncerNotice(a, "https://lapsed.example", .expired, "3m3tfkwy5ii22"); try putNotifyRegistration(space, lapsed, far); try std.testing.expect(!try syncerNoticeOwed(stale)); // Rejecting the notice unregisters a syncer that is still lapsed. try putNotifyRegistration(space, lapsed, 1); try abandonSyncerNotice(stale); try std.testing.expect(!try notifyRegistrationExists(space, lapsed)); try removeNotifyRegistration(space, live); try std.testing.expect(!try notifyRegistrationExists(space, live)); } test "space deletion owes every registration a notice, lapsed ones too" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "deleted.test", "deleted@test.com", "password", "did:plc:deletedowner", true); const space = (try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.deleted.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{} } })).uri; try putNotifyRegistration(space, .{ .endpoint = "https://live.example", .token = "one" }, clock.nowSeconds() + 600); try putNotifyRegistration(space, .{ .endpoint = "https://lapsed.example", .token = "two" }, 1); // A pending expiry notice gives way to the deletion notice. try queueExpiryNotices(space, "3m3tfkwy3ii22"); try markSpaceDeleted(owner.did, space); try purgeAuthoritySpaceData(space, owner.did); try std.testing.expectEqual(@as(usize, 0), (try activeNotifyRegistrations(a, space)).len); var endpoints: std.ArrayList([]const u8) = .empty; for (0..2) |_| { const pending = (try claimSyncerNotice(a)).?; try std.testing.expectEqual(SyncerNotice.deleted, pending.notice); try std.testing.expect(pending.space_rev == null); try std.testing.expectEqual(@as(i64, 1), pending.attempts); try std.testing.expect(pending.expires_at >= clock.nowSeconds() + syncer_notice_window_s - 5); try std.testing.expect(try syncerNoticeOwed(pending)); try endpoints.append(a, pending.registration.endpoint); if (std.mem.eql(u8, pending.registration.endpoint, "https://live.example")) { try retrySyncerNotice(pending); } else { try finishSyncerNotice(pending); } } try std.testing.expect(try claimSyncerNotice(a) == null); try conn.execNoArgs("UPDATE permissioned_space_syncer_outbox SET retry_at = 0"); const retried = try expectSyncerNotice(a, "https://live.example", .deleted, null); try std.testing.expectEqual(@as(i64, 2), retried.attempts); try finishSyncerNotice(retried); try conn.execNoArgs("UPDATE permissioned_space_syncer_outbox SET retry_at = 0, lease_until = 0"); try std.testing.expect(try claimSyncerNotice(a) == null); } test "the webhook migration drops registerNotify registrations and pending deletion notices" { try init(std.Options.debug_io, ":memory:"); defer close(); try conn.execNoArgs("DROP TABLE permissioned_space_notify_registrations"); try conn.execNoArgs("CREATE TABLE permissioned_space_notify_registrations (space TEXT NOT NULL, service_id TEXT NOT NULL, service_endpoint TEXT NOT NULL, expires_at INTEGER NOT NULL, PRIMARY KEY (space, service_id))"); try conn.execNoArgs("INSERT INTO permissioned_space_notify_registrations VALUES ('at:space/did:plc:old/fm.old/x', 'did:web:sync.example#sync', 'https://sync.example', 4102444800)"); try conn.execNoArgs("CREATE TABLE permissioned_space_deleted_outbox (space TEXT NOT NULL, service_id TEXT NOT NULL, service_endpoint TEXT NOT NULL, expires_at INTEGER NOT NULL)"); try conn.execNoArgs("INSERT INTO permissioned_space_deleted_outbox VALUES ('at:space/did:plc:old/fm.old/x', 'did:web:sync.example#sync', 'https://sync.example', 4102444800)"); try conn.exec("DELETE FROM zds_migrations WHERE name = ?", .{"permissioned-space-notify-webhook-v1"}); try migrateNotifyWebhooks(); try std.testing.expect(!try tableExistsLocked("permissioned_space_deleted_outbox")); const left = (try conn.row("SELECT count(*) FROM permissioned_space_notify_registrations", .{})).?; defer left.deinit(); try std.testing.expectEqual(@as(i64, 0), left.int(0)); try conn.execNoArgs("INSERT INTO permissioned_space_notify_registrations (space, endpoint, token, expires_at) VALUES ('at:space/did:plc:new/fm.new/x', 'https://sync.example', 'secret', 4102444800)"); } test "permissioned credential exchange tokens are one use" { try init(std.Options.debug_io, ":memory:"); defer close(); const first = SpaceReplayToken{ .jti = "delegation-one", .expires_at = 4_000_000_000 }; const attestation = SpaceReplayToken{ .jti = "attestation-one", .expires_at = 4_000_000_000 }; try std.testing.expect(try consumeSpaceCredentialExchange(first, attestation)); try std.testing.expect(!try consumeSpaceCredentialExchange(first, null)); const second = SpaceReplayToken{ .jti = "delegation-two", .expires_at = 4_000_000_000 }; try std.testing.expect(!try consumeSpaceCredentialExchange(second, attestation)); try std.testing.expect(try consumeSpaceCredentialExchange(second, null)); } fn expectSpaceBlobRefCount( space: []const u8, repo_did: []const u8, collection: []const u8, rkey: []const u8, blob_cid: []const u8, expected: i64, ) !void { const row = try conn.row( \\SELECT COUNT(*) \\FROM permissioned_space_record_blobs \\WHERE space = ? AND repo_did = ? AND collection = ? AND rkey = ? AND blob_cid = ? , .{ space, repo_did, collection, rkey, blob_cid }); defer row.?.deinit(); try std.testing.expectEqual(expected, row.?.int(0)); } test "permissioned spaces keep authority-local state per space URI" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount( allocator, "authority-space.test", "authority-space@test.com", "password", "did:plc:spaceauthoritylocal", true, ); const created = try createSpace(allocator, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); const owner_view = (try getSpace(allocator, owner.did, created.uri)).?; try std.testing.expect(owner_view.is_authority); try std.testing.expect(owner_view.deleted_at == null); const owner_spaces = try listSpaces(allocator, owner.did, owner.did, "fm.plyr.privateMedia", null, 50); try std.testing.expectEqual(@as(usize, 1), owner_spaces.len); try std.testing.expect(owner_spaces[0].is_authority); try markSpaceDeleted(owner.did, created.uri); try std.testing.expect((try getSpace(allocator, owner.did, created.uri)) == null); } test "permissioned space create is duplicate-checked per actor" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(allocator, "authority-first.test", "authority-first@test.com", "password", "did:plc:ownerfirst", true); const viewer = try createAccount(allocator, "viewer-first.test", "viewer-first@test.com", "password", "did:plc:viewerfirst", true); const member = try createAccount(allocator, "member-only.test", "member-only@test.com", "password", "did:plc:memberonly", true); const viewer_row = try createSpace(allocator, .{ .actor_did = viewer.did, .authority_did = owner.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = false, .policy = .{ .member = .{} }, }); try std.testing.expect(!viewer_row.is_authority); const owner_row = try createSpace(allocator, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = true, .policy = .{ .member = .{ .public_read = true, .public_write = true, .allowed_apps = &.{"did:web:allowed.example"} } }, }); try std.testing.expect(owner_row.is_authority); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true,\"publicWrite\":true,\"allowedApps\":[\"did:web:allowed.example\"],\"publishPolicy\":false,\"publishMembers\":false}", owner_row.policy_json.?, ); try std.testing.expect(viewer_row.policy_json == null); _ = try changeSpaceMembers(allocator, owner_row.uri, &.{.{ .did = member.did, .access = .{ .read = true, .write = true } }}); const member_spaces = try listSpaces(allocator, member.did, owner.did, "fm.plyr.privateMedia", null, 50); try std.testing.expectEqual(@as(usize, 0), member_spaces.len); const viewer_spaces = try listSpaces(allocator, viewer.did, owner.did, "fm.plyr.privateMedia", null, 50); try std.testing.expectEqual(@as(usize, 1), viewer_spaces.len); try std.testing.expect(!viewer_spaces[0].is_authority); try std.testing.expectError(Error.InvalidRefreshSession, createSpace(allocator, .{ .actor_did = viewer.did, .authority_did = owner.did, .space_type = "fm.plyr.privateMedia", .skey = "self", .is_authority = false, .policy = .{ .member = .{} }, })); } test "migration account has a signed empty repo without publication and initialization preserves existing data" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "migration.test", "migration@test.com", "password", "did:plc:migration", false); try initializeRepo(allocator, account); const status = try std.json.parseFromSlice(std.json.Value, allocator, try writeAccountStatusJson(allocator, account.did), .{}); try std.testing.expect(!status.value.object.get("activated").?.bool); try std.testing.expectEqual(@as(i64, 0), status.value.object.get("indexedRecords").?.integer); const car = try writeRepoCar(allocator, account.did); const keypair = try signingKeypair(account.did); const public_key = try keypair.publicKey(); _ = try zat.verifyCommitCar(allocator, car, .{ .key_type = keypair.key_type, .raw = &public_key }, .{ .expected_did = account.did, .require_complete_repo = true }); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did)); const record = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"dev.zds.migration.check\",\"text\":\"preserved\"}", .{}); const result = try create(allocator, account, "dev.zds.migration.check", "check", record.value); const root = try latestRootLocked(allocator, account.did); try initializeRepo(allocator, account); try std.testing.expectEqualStrings(root.cid, (try latestRootLocked(allocator, account.did)).cid); try std.testing.expectEqualStrings(result.cid, getCid(allocator, account.did, "dev.zds.migration.check", "check").?); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did)); } test "inactive repo import does not publish firehose event" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "import-inactive.test", "import-inactive@test.com", "password", "did:plc:importinactive", true, ); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"hello\"}", .{}); defer parsed.deinit(); const record = try create(allocator, account, "app.bsky.feed.post", "3jzfcijpj2z2h", parsed.value); const repo_car = try writeRepoCar(allocator, account.did); const loaded = try zat.loadCommitFromCAR(allocator, repo_car); var imported_blocks: std.ArrayList(ImportedBlock) = .empty; for (loaded.repo_car.blocks) |block| { try imported_blocks.append(allocator, .{ .cid = try zat.cbor.Cid.toString(.{ .raw = block.cid_raw }, allocator), .data = block.data, }); } const imported_records = [_]ImportedRecord{.{ .collection = record.collection, .rkey = record.rkey, .cid = record.cid, .blob_cids = &.{}, }}; try setAccountActive(account.did, false); const before = try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did); try importRepo( allocator, account, try zat.cbor.Cid.toString(.{ .raw = loaded.commit_cid }, allocator), loaded.commit.rev, &imported_records, imported_blocks.items, ); const after = try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", account.did); try std.testing.expectEqual(before, after); try std.testing.expectEqualStrings((try latestRootLocked(allocator, account.did)).cid, try zat.cbor.Cid.toString(.{ .raw = loaded.commit_cid }, allocator)); } test "put updates record index while content comes from repo blocks" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "alice.test", "alice@test.com", "password", "did:plc:cmadossymmii3izkabdbp5en", true, ); var first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"before\"}", .{}); defer first.deinit(); const rkey = "3jzfcijpj2z2i"; _ = try create(allocator, account, "app.bsky.feed.post", rkey, first.value); var second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"after\"}", .{}); defer second.deinit(); const updated = try put(allocator, account, "app.bsky.feed.post", rkey, second.value); const fetched = get(allocator, account.did, "app.bsky.feed.post", rkey).?; try std.testing.expectEqualStrings(updated.cid, fetched.cid); try std.testing.expect(std.mem.indexOf(u8, fetched.value_json, "after") != null); try std.testing.expect(std.mem.indexOf(u8, fetched.value_json, "before") == null); const block_row = try conn.row( "SELECT count(*) FROM repo_blocks WHERE did = ? AND cid = ?", .{ account.did, updated.cid }, ); try std.testing.expect(block_row != null); defer block_row.?.deinit(); try std.testing.expectEqual(@as(i64, 1), block_row.?.int(0)); } test "stores blob metadata in sqlite and bytes in disk blobstore" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); const blob_root = path_buf[0..path_len]; blobstore.init(std.Options.debug_io, blob_root); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "alice.test", "alice@test.com", "password", "did:plc:cmadossymmii3izkabdbp5en", true, ); const cid = try putBlob(allocator, std.Options.debug_io, account, "hello blob", "text/plain"); const row = try conn.row("SELECT mime_type, size FROM blobs WHERE did = ? AND cid = ?", .{ account.did, cid }); try std.testing.expect(row != null); defer row.?.deinit(); try std.testing.expectEqualStrings("text/plain", row.?.text(0)); try std.testing.expectEqual(@as(i64, 10), row.?.int(1)); const blob = getBlob(allocator, account.did, cid) orelse return error.MissingRecord; try std.testing.expectEqualStrings("text/plain", blob.mime_type); try std.testing.expectEqualStrings("hello blob", blob.data); } test "stores larger blob metadata in sqlite and bytes in disk blobstore" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); const blob_root = path_buf[0..path_len]; blobstore.init(std.Options.debug_io, blob_root); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "large-blob.test", "large-blob@test.com", "password", "did:plc:largeblob", true, ); const data = try allocator.alloc(u8, 571_880); @memset(data, 0xaa); const cid = try putBlob(allocator, std.Options.debug_io, account, data, "image/jpeg"); const expected_cid = try cidForBlob(allocator, data); try std.testing.expectEqualStrings(expected_cid, cid); const row = try conn.row("SELECT mime_type, size FROM blobs WHERE did = ? AND cid = ?", .{ account.did, cid }); try std.testing.expect(row != null); defer row.?.deinit(); try std.testing.expectEqualStrings("image/jpeg", row.?.text(0)); try std.testing.expectEqual(@as(i64, 571_880), row.?.int(1)); const blob = getBlob(allocator, account.did, cid) orelse return error.MissingRecord; try std.testing.expectEqualStrings("image/jpeg", blob.mime_type); try std.testing.expectEqualSlices(u8, data, blob.data); } test "blob gc preserves fresh uploads through the untethered grace period" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "gc-grace.test", "gc-grace@test.com", "password", "did:plc:gcgrace", true); const cid = try putBlob(allocator, std.Options.debug_io, account, "untethered", "text/plain"); const protected = try collectBlobGarbage(allocator, 60, 10); try std.testing.expectEqual(@as(usize, 0), protected.deleted); try std.testing.expect(getBlob(allocator, account.did, cid) != null); const expired = try collectBlobGarbage(allocator, 0, 10); try std.testing.expectEqual(@as(usize, 1), expired.deleted); try std.testing.expectEqual(@as(u64, 10), expired.bytes); try std.testing.expect(getBlob(allocator, account.did, cid) == null); } test "account deletion leaves one deleted event and spares other accounts" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const doomed = try createAccount(allocator, "doomed.test", "doomed@test.com", "password", "did:plc:doomed", true); const kept = try createAccount(allocator, "kept.test", "kept@test.com", "password", "did:plc:kept", true); try sequenceNewAccount(allocator, doomed); try sequenceNewAccount(allocator, kept); const record = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"dev.zds.delete.check\",\"text\":\"bye\"}", .{}); _ = try create(allocator, doomed, "dev.zds.delete.check", "one", record.value); _ = try create(allocator, kept, "dev.zds.delete.check", "one", record.value); const doomed_blob = try putBlob(allocator, std.Options.debug_io, doomed, "doomed", "text/plain"); const kept_blob = try putBlob(allocator, std.Options.debug_io, kept, "kept", "text/plain"); const kept_events = try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", kept.did); try deleteAccount(allocator, doomed.did); try std.testing.expectError(Error.RepoNotFound, accountStatus(doomed.did)); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", doomed.did)); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM commits WHERE did = ?", doomed.did)); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM blobs WHERE did = ?", doomed.did)); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", doomed.did)); { const row = (try conn.row("SELECT evt FROM seq_events WHERE did = ?", .{doomed.did})).?; defer row.deinit(); const frame = row.blob(0); try std.testing.expect(std.mem.indexOf(u8, frame, "#account") != null); try std.testing.expect(std.mem.indexOf(u8, frame, "deleted") != null); } try std.testing.expectError(error.FileNotFound, blobstore.get(allocator, doomed.did, doomed_blob, 64)); try std.testing.expectError(error.FileNotFound, tmp.dir.openDir(std.testing.io, doomed.did, .{})); try std.testing.expectEqual(AccountStatus.active, try accountStatus(kept.did)); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", kept.did)); try std.testing.expectEqual(kept_events, try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", kept.did)); try std.testing.expect(getBlob(allocator, kept.did, kept_blob) != null); try std.testing.expectError(Error.RepoNotFound, deleteAccount(allocator, doomed.did)); } test "oauth families from before session tracking start their lifetime at the migration" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); try conn.execNoArgs("INSERT INTO accounts (did, handle, email, password_hash) VALUES ('did:plc:legacyoauth', 'legacyoauth.test', 'legacyoauth@example.test', 'unused')"); try conn.execNoArgs( \\INSERT INTO oauth_tokens (family_id, access_token, refresh_token, did, client_id, scope, expires_at, access_expires_at, refresh_expires_at, created_at) \\VALUES ('legacy-access', 'legacy-access', 'legacy-refresh', 'did:plc:legacyoauth', 'https://legacy.example', 'atproto', 1, 1, 4102444800, 1000) ); try migrate(); const row = (try getOAuthToken(allocator, "legacy-refresh")) orelse return error.MissingRecord; try std.testing.expect(row.session_started_at > 1000); try std.testing.expect(row.session_started_at >= clock.nowSeconds() - 60); } test "seq_events migration drops the account cascade and keeps history" { try init(std.Options.debug_io, ":memory:"); defer close(); try conn.execNoArgs("DROP TABLE seq_events"); try conn.execNoArgs("CREATE TABLE seq_events (seq INTEGER PRIMARY KEY, did TEXT NOT NULL REFERENCES accounts(did) ON DELETE CASCADE, commit_cid TEXT NOT NULL, evt BLOB NOT NULL)"); try conn.execNoArgs("DELETE FROM zds_migrations WHERE name = 'seq-events-without-account-fk'"); try conn.execNoArgs("INSERT INTO accounts (did, handle, email, password_hash) VALUES ('did:plc:legacyseq', 'legacyseq.test', 'legacyseq@example.test', 'unused')"); try conn.execNoArgs("INSERT INTO seq_events (seq, did, commit_cid, evt) VALUES (5, 'did:plc:legacyseq', 'commit', X'A0')"); try migrateSeqEventsWithoutAccountFk(); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM pragma_foreign_key_list('seq_events') WHERE ? = ?", "")); try conn.execNoArgs("DELETE FROM accounts WHERE did = 'did:plc:legacyseq'"); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", "did:plc:legacyseq")); } test "seq_events migration adds required fields to legacy commit frames" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const cid = "bafyreifmpxapiafzeml4ns5nedutwbsnjw5obdganycdy6mhmb2mxth5aq"; const cid_raw = (zat.cbor.Cid.fromString(allocator, cid) catch return Error.InvalidDagCbor).raw; const rev = zat.Tid.fromTimestamp(1_700_000_123_456_789, 1).str(); const legacy_header = try zat.cbor.encodeAlloc(allocator, .{ .map = &.{ .{ .key = "op", .value = .{ .unsigned = 1 } }, .{ .key = "t", .value = .{ .text = "#commit" } }, } }); const legacy = try std.mem.concat(allocator, u8, &.{ legacy_header, try zat.cbor.encodeAlloc(allocator, .{ .map = &.{ .{ .key = "blocks", .value = .{ .bytes = "legacy car bytes" } }, .{ .key = "blobs", .value = .{ .array = &.{} } }, .{ .key = "commit", .value = .{ .cid = .{ .raw = cid_raw } } }, .{ .key = "ops", .value = .{ .array = &.{} } }, .{ .key = "prevData", .value = .null }, .{ .key = "repo", .value = .{ .text = "did:plc:legacyframe" } }, .{ .key = "rev", .value = .{ .text = &rev } }, .{ .key = "seq", .value = .{ .unsigned = 1 } }, .{ .key = "time", .value = .{ .text = "2026-10-01T00:00:00.000Z" } }, } }) }); const current = try commitEventFrameFromCar(allocator, 2, "did:plc:legacyframe", cid, &rev, null, null, "current car bytes", &.{}); const account = try accountEventFrame(allocator, 3, "did:plc:legacyframe", .active); try insertSeqEventLocked(1, "did:plc:legacyframe", cid, legacy); try insertSeqEventLocked(2, "did:plc:legacyframe", cid, current); try insertSeqEventLocked(3, "did:plc:legacyframe", "", account); try conn.execNoArgs("DELETE FROM zds_migrations WHERE name = 'seq-events-required-commit-fields'"); try migrateSeqEventsRequiredCommitFields(); const events = try listSeqEventsFrom(conn, allocator, 0, 10); try std.testing.expectEqual(@as(usize, 3), events.len); const header = try zat.cbor.decode(allocator, events[0].frame); try std.testing.expectEqualStrings("#commit", header.value.getString("t").?); const payload = try zat.cbor.decodeAll(allocator, events[0].frame[header.consumed..]); try std.testing.expect(payload.get("since").? == .null); try std.testing.expectEqual(false, payload.getBool("rebase").?); try std.testing.expectEqual(false, payload.getBool("tooBig").?); try std.testing.expectEqualStrings("legacy car bytes", payload.getBytes("blocks").?); const decoded = try zat.firehose.decodeFrame(allocator, events[0].frame); try std.testing.expectEqualStrings(&rev, decoded.commit.rev); try std.testing.expectEqualSlices(u8, cid_raw, decoded.commit.commit.?.raw); try std.testing.expectEqualSlices(u8, current, events[1].frame); try std.testing.expectEqualSlices(u8, account, events[2].frame); } test "blob gc waits for the last public record reference" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "gc-public.test", "gc-public@test.com", "password", "did:plc:gcpublic", true); const cid = try putBlob(allocator, std.Options.debug_io, account, "public", "text/plain"); const json = try std.fmt.allocPrint( allocator, "{{\"$type\":\"fm.example.blob\",\"media\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"text/plain\",\"size\":6}}}}", .{cid}, ); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json, .{}); defer parsed.deinit(); _ = try create(allocator, account, "fm.example.blob", "one", parsed.value); _ = try create(allocator, account, "fm.example.blob", "two", parsed.value); _ = try delete(allocator, account, "fm.example.blob", "one"); try std.testing.expect(getBlob(allocator, account.did, cid) != null); _ = try delete(allocator, account, "fm.example.blob", "two"); const gc = try collectBlobGarbage(allocator, 0, 10); try std.testing.expectEqual(@as(usize, 1), gc.deleted); try std.testing.expect(getBlob(allocator, account.did, cid) == null); } test "blob gc follows permissioned record references" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "gc-space.test", "gc-space@test.com", "password", "did:plc:gcspace", true); const cid = try putBlob(allocator, std.Options.debug_io, account, "private", "audio/mpeg"); const space = try createSpace(allocator, .{ .actor_did = account.did, .authority_did = account.did, .space_type = "fm.example.private", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); const json = try std.fmt.allocPrint( allocator, "{{\"$type\":\"fm.example.privateBlob\",\"media\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"audio/mpeg\",\"size\":7}}}}", .{cid}, ); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json, .{}); defer parsed.deinit(); const prepared = try prepareRecordValue(allocator, "fm.example.privateBlob", "one", parsed.value); _ = try putSpaceRecord(allocator, space.uri, account.did, "fm.example.privateBlob", "one", prepared); try std.testing.expect(getBlob(allocator, account.did, cid) != null); try deleteSpaceRecord(allocator, space.uri, account.did, "fm.example.privateBlob", "one"); const gc = try collectBlobGarbage(allocator, 0, 10); try std.testing.expectEqual(@as(usize, 1), gc.deleted); try std.testing.expect(getBlob(allocator, account.did, cid) == null); } test "blob visibility follows public and permissioned references independently" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "blob-access.test", "blob-access@test.com", "password", "did:plc:blobaccess", true); const cid = try putBlob(allocator, std.Options.debug_io, account, "shared", "text/plain"); const space = try createSpace(allocator, .{ .actor_did = account.did, .authority_did = account.did, .space_type = "fm.example.private", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); const json = try std.fmt.allocPrint( allocator, "{{\"$type\":\"fm.example.blob\",\"media\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"text/plain\",\"size\":6}}}}", .{cid}, ); const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json, .{}); defer parsed.deinit(); const prepared = try prepareRecordValue(allocator, "fm.example.blob", "private", parsed.value); try std.testing.expect(getPublicBlob(allocator, account.did, cid) == null); try std.testing.expect(!try permissionedBlobReferenced(space.uri, account.did, cid)); try std.testing.expectEqual(@as(usize, 0), (try collectBlobGarbage(allocator, 60, 10)).deleted); _ = try putSpaceRecord(allocator, space.uri, account.did, "fm.example.blob", "private", prepared); try std.testing.expect(getPublicBlob(allocator, account.did, cid) == null); try std.testing.expect(try permissionedBlobReferenced(space.uri, account.did, cid)); try std.testing.expectEqual(@as(usize, 0), (try collectBlobGarbage(allocator, 0, 10)).deleted); _ = try create(allocator, account, "fm.example.blob", "public", parsed.value); try std.testing.expect(getPublicBlob(allocator, account.did, cid) != null); try std.testing.expect(try permissionedBlobReferenced(space.uri, account.did, cid)); try deleteSpaceRecord(allocator, space.uri, account.did, "fm.example.blob", "private"); try std.testing.expect(getPublicBlob(allocator, account.did, cid) != null); try std.testing.expect(!try permissionedBlobReferenced(space.uri, account.did, cid)); try std.testing.expectEqual(@as(usize, 0), (try collectBlobGarbage(allocator, 0, 10)).deleted); _ = try putSpaceRecord(allocator, space.uri, account.did, "fm.example.blob", "private", prepared); _ = try delete(allocator, account, "fm.example.blob", "public"); try std.testing.expect(getPublicBlob(allocator, account.did, cid) == null); try std.testing.expect(try permissionedBlobReferenced(space.uri, account.did, cid)); try std.testing.expectEqual(@as(usize, 0), (try collectBlobGarbage(allocator, 0, 10)).deleted); try deleteSpaceRecord(allocator, space.uri, account.did, "fm.example.blob", "private"); try std.testing.expectEqual(@as(usize, 1), (try collectBlobGarbage(allocator, 0, 10)).deleted); try std.testing.expect(getBlob(allocator, account.did, cid) == null); } test "re-upload refreshes the untethered grace period" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "gc-reupload.test", "gc-reupload@test.com", "password", "did:plc:gcreupload", true); const cid = try putBlob(allocator, std.Options.debug_io, account, "re-uploaded", "text/plain"); try conn.exec("UPDATE blobs SET created_at = unixepoch() - 120 WHERE did = ? AND cid = ?", .{ account.did, cid }); const same_cid = try putBlob(allocator, std.Options.debug_io, account, "re-uploaded", "text/plain"); try std.testing.expectEqualStrings(cid, same_cid); const gc = try collectBlobGarbage(allocator, 60, 10); try std.testing.expectEqual(@as(usize, 0), gc.deleted); try std.testing.expect(getBlob(allocator, account.did, cid) != null); } test "accounts carry a KEM key unless created without one" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const keyed = try createAccount(allocator, "keyed.test", "keyed@test.com", "password", "did:plc:keyed", true); const kem_key = (try kemSecretKey(keyed.did)).?; _ = try zat.Keypair.fromSecretKey(.p256, kem_key); try std.testing.expect(std.mem.startsWith(u8, (try kemDidKey(allocator, keyed.did)).?, "did:key:zDnae")); var signing_key: [32]u8 = @splat(0); signing_key[31] = 1; _ = try createAccountWithSigningKeyAndInvite(allocator, "keyless.test", "keyless@test.com", "password", "did:plc:keyless", true, signing_key, null, null); try std.testing.expect(try kemSecretKey("did:plc:keyless") == null); try std.testing.expect(try kemDidKey(allocator, "did:plc:keyless") == null); const missing = try listDidsWithoutKemKey(allocator, null, 10); try std.testing.expectEqual(@as(usize, 1), missing.len); try std.testing.expectEqualStrings("did:plc:keyless", missing[0]); try std.testing.expectEqual(@as(usize, 0), (try listDidsWithoutKemKey(allocator, "did:plc:keyless", 10)).len); const backfilled = try generateKemSecretKey(); try std.testing.expect(try setMissingKemSecretKey("did:plc:keyless", backfilled)); try std.testing.expect(!try setMissingKemSecretKey("did:plc:keyless", try generateKemSecretKey())); try std.testing.expect(!try setMissingKemSecretKey(keyed.did, backfilled)); try std.testing.expectEqualSlices(u8, &backfilled, &(try kemSecretKey("did:plc:keyless")).?); try std.testing.expectEqualSlices(u8, &kem_key, &(try kemSecretKey(keyed.did)).?); try std.testing.expectEqual(@as(usize, 0), (try listDidsWithoutKemKey(allocator, null, 10)).len); } test "invite codes gate account creation and record uses" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const code = try createInviteCode(allocator, "https://pds.example.com", 1, "admin", "admin"); try std.testing.expect(std.mem.startsWith(u8, code, "pds-example-com-")); try std.testing.expect(try inviteCodeIsAvailable(code)); var signing_key: [32]u8 = @splat(0); signing_key[31] = 1; const account = try createAccountWithSigningKeyAndInvite( allocator, "invited.test", "invited@test.com", "password", "did:plc:invited", true, signing_key, try generateKemSecretKey(), code, ); try std.testing.expectEqualStrings("did:plc:invited", account.did); try std.testing.expect(!try inviteCodeIsAvailable(code)); var other_key: [32]u8 = @splat(0); other_key[31] = 2; try std.testing.expectError(error.InvalidInviteCode, createAccountWithSigningKeyAndInvite( allocator, "second.test", "second@test.com", "password", "did:plc:second", true, other_key, try generateKemSecretKey(), code, )); const codes = try getAccountInviteCodes(allocator, "admin", true); try std.testing.expectEqual(@as(usize, 1), codes.len); try std.testing.expectEqual(@as(usize, 1), codes[0].uses.len); try std.testing.expectEqualStrings("did:plc:invited", codes[0].uses[0].used_by); } test "account handle updates are unique and immediately resolvable" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const alice = try createAccount( allocator, "alice.test", "alice@test.com", "password", "did:plc:alice", true, ); _ = try createAccount( allocator, "bob.test", "bob@test.com", "password", "did:plc:bob", true, ); try updateAccountHandle(alice.did, "alice-new.test"); try std.testing.expect((try findAccount(allocator, "alice.test")) == null); const updated = (try findAccount(allocator, "ALICE-NEW.TEST")) orelse return error.AccountNotFound; try std.testing.expectEqualStrings(alice.did, updated.did); try std.testing.expectError(error.HandleNotAvailable, updateAccountHandle(alice.did, "BOB.TEST")); try std.testing.expectError(error.AccountNotFound, updateAccountHandle("did:plc:missing", "missing.test")); } test "reserved signing keys are one-use account signing keys" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const did = "did:plc:reserved"; const reserved = try reserveSigningKey(allocator, did); try std.testing.expectEqualStrings(did, reserved.did.?); try std.testing.expect(std.mem.startsWith(u8, reserved.signing_key, "did:key:")); const consumed = try consumeReservedSigningKey(reserved.signing_key, did); try std.testing.expectEqualSlices(u8, &reserved.secret_key, &consumed); try std.testing.expectError(error.MissingReservedSigningKey, consumeReservedSigningKey(reserved.signing_key, did)); } test "stores discoverable passkey challenges by oauth request" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); try putOAuthRequest( "req-discoverable", "https://client.example.com/oauth-client-metadata.json", "https://client.example.com/callback", "atproto", "state", "challenge", "S256", "query", null, null, 4102444800, ); try putDiscoverableWebAuthnChallenge("req-discoverable", "webauthn-challenge", 4102444800); const stored = (try getDiscoverableWebAuthnChallenge(allocator, "req-discoverable")) orelse return error.MissingRecord; try std.testing.expectEqualStrings("req-discoverable", stored.request_id); try std.testing.expectEqualStrings("webauthn-challenge", stored.challenge); try std.testing.expectEqual(@as(i64, 4102444800), stored.expires_at); try deleteDiscoverableWebAuthnChallenge("req-discoverable"); try std.testing.expect(try getDiscoverableWebAuthnChallenge(allocator, "req-discoverable") == null); } test "oauth authorization codes remain readable until atomically consumed" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); try putOAuthRequest( "req-code", "https://client.example.com/oauth-client-metadata.json", "https://client.example.com/callback", "atproto", "state", "challenge", "S256", "query", null, "dpop-thumbprint", 4102444800, ); try authorizeOAuthRequest("req-code", "did:plc:code", "authorization-code", "passkey"); const before = (try getOAuthRequestByCode(allocator, "authorization-code")) orelse return error.MissingRecord; try std.testing.expectEqualStrings("req-code", before.request_id); try std.testing.expectEqualStrings("authorization-code", before.code.?); try std.testing.expect(!try consumeOAuthCode("wrong-request", "authorization-code")); try std.testing.expect((try getOAuthRequestByCode(allocator, "authorization-code")) != null); try std.testing.expect(try consumeOAuthCode("req-code", "authorization-code")); try std.testing.expect((try getOAuthRequestByCode(allocator, "authorization-code")) == null); try std.testing.expect(!try consumeOAuthCode("req-code", "authorization-code")); } test "oauth app sessions stay active after access token expiry until revoked" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "oauth-session.test", "oauth-session@test.com", "password", "did:plc:oauthsession", true, ); try putOAuthToken( account.did, "https://client.example.com/oauth-client-metadata.json", "atproto", "expired-access", "current-refresh", 1, 4102444800, null, "passkey", ); const active = try listOAuthGrantsForAccount(allocator, account.did, true, 50); try std.testing.expectEqual(@as(usize, 1), active.len); try std.testing.expect(active[0].active); try std.testing.expectEqualStrings("passkey", active[0].auth_method.?); try revokeOAuthToken("current-refresh"); try std.testing.expectEqual(@as(usize, 0), (try listOAuthGrantsForAccount(allocator, account.did, true, 50)).len); const all = try listOAuthGrantsForAccount(allocator, account.did, false, 50); try std.testing.expectEqual(@as(usize, 1), all.len); try std.testing.expect(!all[0].active); } test "oauth refresh rotation keeps one previous refresh token in grace" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "oauth-rotation.test", "oauth-rotation@test.com", "password", "did:plc:oauthrotation", true, ); try putOAuthToken( account.did, "https://client.example.com/oauth-client-metadata.json", "atproto", "access-one", "refresh-one", 100, 4102444800, "jkt-one", "passkey", ); const access_one = (try getOAuthToken(allocator, "access-one")) orelse return error.MissingRecord; try std.testing.expectEqual(OAuthToken.Kind.access, access_one.kind); try std.testing.expectEqualStrings("access-one", access_one.family_id); try std.testing.expectEqual(@as(i64, 100), access_one.access_expires_at); try std.testing.expectEqual(@as(i64, 4102444800), access_one.refresh_expires_at); try std.testing.expect(try rotateOAuthToken("refresh-one", "access-two", "refresh-two", 200, 4102444801, 300)); try std.testing.expect(try getOAuthToken(allocator, "access-one") == null); const previous = (try getOAuthToken(allocator, "refresh-one")) orelse return error.MissingRecord; try std.testing.expectEqual(OAuthToken.Kind.previous_refresh, previous.kind); try std.testing.expectEqualStrings("access-two", previous.access_token); try std.testing.expectEqualStrings("refresh-two", previous.refresh_token); try std.testing.expectEqual(@as(i64, 300), previous.previous_refresh_expires_at.?); const current = (try getOAuthToken(allocator, "refresh-two")) orelse return error.MissingRecord; try std.testing.expectEqual(OAuthToken.Kind.refresh, current.kind); try std.testing.expectEqual(@as(i64, 200), current.access_expires_at); try std.testing.expectEqual(@as(i64, 4102444801), current.refresh_expires_at); try std.testing.expectEqual(access_one.session_started_at, current.session_started_at); // a second refresh of refresh-one that read the row before the first // rotated loses: nothing it minted is stored, and the winner's pair stands try std.testing.expect(!try rotateOAuthToken("refresh-one", "access-loser", "refresh-loser", 250, 4102444900, 350)); try std.testing.expect(try getOAuthToken(allocator, "access-loser") == null); try std.testing.expect(try getOAuthToken(allocator, "refresh-loser") == null); const after_loss = (try getOAuthToken(allocator, "refresh-one")) orelse return error.MissingRecord; try std.testing.expectEqual(OAuthToken.Kind.previous_refresh, after_loss.kind); try std.testing.expectEqualStrings("access-two", after_loss.access_token); try std.testing.expectEqualStrings("refresh-two", after_loss.refresh_token); try std.testing.expectEqual(@as(i64, 300), after_loss.previous_refresh_expires_at.?); try std.testing.expect(try rotateOAuthToken("refresh-two", "access-three", "refresh-three", 400, 4102444802, 500)); const used = (try getOAuthToken(allocator, "refresh-one")) orelse return error.MissingRecord; try std.testing.expectEqual(OAuthToken.Kind.used_refresh, used.kind); try std.testing.expectEqualStrings("access-one", used.family_id); try std.testing.expectEqualStrings("refresh-three", used.refresh_token); try revokeOAuthToken("refresh-one"); const revoked = (try getOAuthToken(allocator, "refresh-three")) orelse return error.MissingRecord; try std.testing.expect(revoked.revoked); } test "session tokens rotate with a replay grace window and report why a jti is refused" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "session.test", "session@test.com", "password", "did:plc:session", true, ); const session_id = try createSessionTokenRow( allocator, account.did, "access-1", "refresh-1", 4102444800, 4102444800, "password", null, null, ); try std.testing.expect(std.mem.startsWith(u8, session_id, "ses-")); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "access-1", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "refresh-1", "com.atproto.refresh")); try std.testing.expectEqual(SessionTokenStatus.unknown, try sessionTokenStatus(account.did, "refresh-1", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.unknown, try sessionTokenStatus(account.did, "nope", "com.atproto.refresh")); const first = (try refreshSessionByJti(allocator, account.did, "refresh-1")) orelse return error.MissingRecord; try std.testing.expectEqualStrings(session_id, first.id); try std.testing.expectEqualStrings("password", first.auth_method); try std.testing.expect(!first.replayed); try rotateSessionToken(session_id, "refresh-1", "access-2", "refresh-2", 4102444800, 4102444800, 30); try std.testing.expectError(error.InvalidRefreshSession, rotateSessionToken(session_id, "refresh-1", "access-3", "refresh-3", 4102444800, 4102444800, 30)); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "access-2", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "refresh-2", "com.atproto.refresh")); try std.testing.expectEqual(SessionTokenStatus.unknown, try sessionTokenStatus(account.did, "access-1", "com.atproto.access")); // the retired jti stays usable inside the grace window and resolves to the successor try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "refresh-1", "com.atproto.refresh")); const replay = (try refreshSessionByJti(allocator, account.did, "refresh-1")) orelse return error.MissingRecord; try std.testing.expect(replay.replayed); try std.testing.expectEqualStrings("access-2", replay.access_jti); try std.testing.expectEqualStrings("refresh-2", replay.refresh_jti); // a closed grace window refuses the retired jti as expired try rotateSessionToken(session_id, "refresh-2", "access-3", "refresh-3", 4102444800, 4102444800, 0); try std.testing.expectEqual(SessionTokenStatus.expired, try sessionTokenStatus(account.did, "refresh-2", "com.atproto.refresh")); try std.testing.expect((try refreshSessionByJti(allocator, account.did, "refresh-2")) == null); try std.testing.expectEqual(SessionTokenStatus.unknown, try sessionTokenStatus(account.did, "refresh-1", "com.atproto.refresh")); const active_sessions = try listSessionsForAccount(allocator, account.did, true, 50); try std.testing.expectEqual(@as(usize, 1), active_sessions.len); try std.testing.expectEqualStrings(session_id, active_sessions[0].id); try std.testing.expect(active_sessions[0].active); try revokeSessionToken(account.did, "refresh-3"); try std.testing.expectEqual(SessionTokenStatus.revoked, try sessionTokenStatus(account.did, "access-3", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.revoked, try sessionTokenStatus(account.did, "refresh-3", "com.atproto.refresh")); try std.testing.expect((try refreshSessionByJti(allocator, account.did, "refresh-3")) == null); try std.testing.expectEqual(@as(usize, 0), (try listSessionsForAccount(allocator, account.did, true, 50)).len); const inactive_sessions = try listSessionsForAccount(allocator, account.did, false, 50); try std.testing.expectEqual(@as(usize, 1), inactive_sessions.len); try std.testing.expect(!inactive_sessions[0].active); _ = try createSessionTokenRow(allocator, account.did, "stale-access", "stale-refresh", 1, 1, "password", null, null); try std.testing.expectEqual(SessionTokenStatus.expired, try sessionTokenStatus(account.did, "stale-access", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.expired, try sessionTokenStatus(account.did, "stale-refresh", "com.atproto.refresh")); } test "deactivating beneath a takedown keeps the takedown and outlasts it" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "escape.test", "escape@test.com", "password", "did:plc:escape", true); try setAccountTakendown(account.did, true, null); try setAccountActive(account.did, false); try std.testing.expectEqual(AccountStatus.takendown, try accountStatus(account.did)); try std.testing.expectError(Error.InvalidAccountStatus, setAccountActive(account.did, true)); try setAccountTakendown(account.did, false, null); try std.testing.expectEqual(AccountStatus.deactivated, try accountStatus(account.did)); try setAccountActive(account.did, true); try setAccountTakendown(account.did, true, null); try setAccountTakendown(account.did, false, null); try std.testing.expectEqual(AccountStatus.active, try accountStatus(account.did)); } test "account takedown sets precise sync status and ends refresh tokens" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "status.test", "status@test.com", "password", "did:plc:status", true, ); var record_json = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"hello\"}", .{}); defer record_json.deinit(); _ = try create(allocator, account, "app.bsky.feed.post", "3jzfcijpj2z2j", record_json.value); _ = try createSessionTokenRow( allocator, account.did, "status-access", "status-refresh", 4102444800, 4102444800, "password", null, null, ); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "status-access", "com.atproto.access")); try setAccountTakendown(account.did, true, "mod-ref-1"); try std.testing.expectEqual(AccountStatus.takendown, try accountStatus(account.did)); try std.testing.expectEqual(SessionTokenStatus.active, try sessionTokenStatus(account.did, "status-access", "com.atproto.access")); try std.testing.expectEqual(SessionTokenStatus.expired, try sessionTokenStatus(account.did, "status-refresh", "com.atproto.refresh")); try std.testing.expect((try refreshSessionByJti(allocator, account.did, "status-refresh")) == null); const status_json = try writeRepoStatusJson(allocator, account.did); try std.testing.expect(std.mem.indexOf(u8, status_json, "\"active\":false") != null); try std.testing.expect(std.mem.indexOf(u8, status_json, "\"status\":\"takendown\"") != null); try std.testing.expect(std.mem.indexOf(u8, status_json, "\"rev\"") == null); const frame = try accountEventFrame(allocator, 1, account.did, .takendown); try std.testing.expect(std.mem.indexOf(u8, frame, "takendown") != null); try setAccountTakendown(account.did, false, null); try std.testing.expectEqual(AccountStatus.active, try accountStatus(account.did)); } test "app passwords are durable matchable and revoke their sessions" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "app-pass.test", "app-pass@test.com", "password", "did:plc:apppass", true, ); const salt: [16]u8 = @splat(0); const password_hash = try auth.hashPassword(allocator, "aaaa-bbbb-cccc-dddd", salt); const created = try createAppPassword( allocator, account.did, "device one", password_hash, false, "transition:generic", null, ); try std.testing.expectEqualStrings("device one", created.name); try std.testing.expectEqual(false, created.privileged); const listed = try listAppPasswords(allocator, account.did); try std.testing.expectEqual(@as(usize, 1), listed.len); try std.testing.expectEqualStrings("device one", listed[0].name); try std.testing.expectEqualStrings("transition:generic", listed[0].scopes.?); const matched = (try findMatchingAppPassword(allocator, account.did, "aaaa-bbbb-cccc-dddd")) orelse return error.MissingRecord; try std.testing.expectEqualStrings("device one", matched.name); try std.testing.expect(try findMatchingAppPassword(allocator, account.did, "wrong-password") == null); _ = try createSessionTokenRow( allocator, account.did, "app-access-1", "app-refresh-1", 4102444800, 4102444800, "app_password", null, "device one", ); const method = (try sessionAuthMethod(allocator, account.did, "app-access-1")) orelse return error.MissingRecord; try std.testing.expectEqualStrings("app_password", method); try revokeAppPassword(account.did, "device one"); try std.testing.expect(try getAppPasswordByName(allocator, account.did, "device one") == null); try std.testing.expect(try sessionAuthMethod(allocator, account.did, "app-access-1") == null); try std.testing.expectEqual(SessionTokenStatus.revoked, try sessionTokenStatus(account.did, "app-access-1", "com.atproto.access")); } test "account audit log stores subject actor and controller fields" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount( allocator, "audit.test", "audit@test.com", "password", "did:plc:audit", true, ); try recordAuditEvent(account.did, account.did, "did:plc:controller", "repo_write", "{\"collection\":\"app.bsky.feed.post\"}"); const row = try conn.row( \\SELECT subject_did, actor_did, controller_did, action, details_json \\FROM account_audit_log \\WHERE subject_did = ? , .{account.did}); try std.testing.expect(row != null); defer row.?.deinit(); try std.testing.expectEqualStrings(account.did, row.?.text(0)); try std.testing.expectEqualStrings(account.did, row.?.text(1)); try std.testing.expectEqualStrings("did:plc:controller", row.?.text(2)); try std.testing.expectEqualStrings("repo_write", row.?.text(3)); try std.testing.expect(std.mem.indexOf(u8, row.?.text(4), "app.bsky.feed.post") != null); } test "commit event encoding uses Zat firehose builder" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const cid = "bafyreifmpxapiafzeml4ns5nedutwbsnjw5obdganycdy6mhmb2mxth5aq"; const rev = zat.Tid.fromTimestamp(1_700_000_123_456_789, 1).str(); const since = zat.Tid.fromTimestamp(1_700_000_123_456_000, 1).str(); const prev_data_raw = (zat.cbor.Cid.fromString(allocator, cid) catch return Error.InvalidDagCbor).raw; const ops = try allocator.alloc(zat.firehose.CommitEventOp, 3); ops[0] = try firehoseOp(allocator, .create, "sh.tangled.string", "zds-create", cid, null); ops[1] = try firehoseOp(allocator, .update, "sh.tangled.string", "zds-update", cid, cid); ops[2] = try firehoseOp(allocator, .delete, "sh.tangled.string", "zds-delete", null, cid); const frame = try commitEventFrameFromCar( allocator, 1, "did:plc:b64lsctzqnzpv6vd4ry3qktw", cid, &rev, &since, prev_data_raw, "not-a-real-car-for-this-encoding-test", ops, ); const decoded = try zat.firehose.decodeFrame(allocator, frame); try std.testing.expectEqual(@as(i64, 1), decoded.commit.seq); try std.testing.expectEqualStrings("did:plc:b64lsctzqnzpv6vd4ry3qktw", decoded.commit.repo); try std.testing.expectEqualStrings(&rev, decoded.commit.rev); try std.testing.expectEqualStrings(&since, decoded.commit.since.?); try std.testing.expectEqualSlices(u8, prev_data_raw, decoded.commit.prev_data.?.raw); try std.testing.expectEqual(@as(usize, 3), decoded.commit.ops.len); try std.testing.expectEqualStrings("sh.tangled.string/zds-create", decoded.commit.ops[0].path); try std.testing.expectEqualSlices(u8, ops[0].cid.?.raw, decoded.commit.ops[0].cid.?.raw); try std.testing.expectEqualStrings("sh.tangled.string/zds-update", decoded.commit.ops[1].path); try std.testing.expectEqualSlices(u8, ops[1].cid.?.raw, decoded.commit.ops[1].cid.?.raw); try std.testing.expectEqualSlices(u8, ops[1].prev.?.raw, decoded.commit.ops[1].prev.?.raw); try std.testing.expectEqualStrings("sh.tangled.string/zds-delete", decoded.commit.ops[2].path); try std.testing.expect(decoded.commit.ops[2].cid == null); try std.testing.expectEqualSlices(u8, ops[2].prev.?.raw, decoded.commit.ops[2].prev.?.raw); } test "first commit event sends since as null with rebase and tooBig" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const cid = "bafyreifmpxapiafzeml4ns5nedutwbsnjw5obdganycdy6mhmb2mxth5aq"; const rev = zat.Tid.fromTimestamp(1_700_000_123_456_789, 1).str(); const frame = try commitEventFrameFromCar( allocator, 1, "did:plc:b64lsctzqnzpv6vd4ry3qktw", cid, &rev, null, null, "not-a-real-car-for-this-encoding-test", &.{}, ); const header = try zat.cbor.decode(allocator, frame); const payload = try zat.cbor.decodeAll(allocator, frame[header.consumed..]); try std.testing.expect(payload.get("since").? == .null); try std.testing.expectEqual(false, payload.getBool("rebase").?); try std.testing.expectEqual(false, payload.getBool("tooBig").?); } test "Zat firehose builder rejects commit CID since rev" { const allocator = std.testing.allocator; const cid = "bafyreifmpxapiafzeml4ns5nedutwbsnjw5obdganycdy6mhmb2mxth5aq"; const commit_raw = (zat.cbor.Cid.fromString(allocator, cid) catch return Error.InvalidDagCbor).raw; defer allocator.free(commit_raw); const rev = zat.Tid.fromTimestamp(1_700_000_123_456_789, 1).str(); try std.testing.expectError(error.InvalidSince, zat.firehose.encodeCommitEvent(allocator, .{ .seq = 1, .repo_did = "did:plc:b64lsctzqnzpv6vd4ry3qktw", .commit_cid = .{ .raw = commit_raw }, .rev = &rev, .since_rev = cid, .prev_data = null, .blocks = "not-a-real-car-for-this-encoding-test", .ops = &.{}, .time = "2026-06-22T00:00:00.000Z", })); } test "repo rev generation does not move behind current head" { const allocator = std.testing.allocator; const future_tid = zat.Tid.fromTimestamp(nowMicros() + std.time.us_per_s, 0).str(); const rev = try revForSeq(allocator, 42, &future_tid); defer allocator.free(rev); const previous = zat.Tid.parse(&future_tid).?.timestamp(); const next = zat.Tid.parse(rev).?.timestamp(); try std.testing.expect(next > previous); try std.testing.expect(std.mem.lessThan(u8, &future_tid, rev)); } test "a create commit followed by a delete commit leaves no record behind" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "batch.test", "batch@test.com", "password", "did:plc:batchcd", true); const doc = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"io.atcr.manifest\"}", .{}); defer doc.deinit(); const rkey = "1b00000000000000000000000000000000000000000000000000000000000000"; _ = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = doc.value } }, }, .{ .validate = .skip }); _ = try applyWritesWithOptions(allocator, account, &.{ .{ .delete = .{ .collection = "io.atcr.manifest", .rkey = rkey } }, }, .{ .validate = .skip }); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", account.did)); } test "a write past the #commit frame's op or block limits is refused before anything is stored" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "limits.test", "limits@test.com", "password", "did:plc:commitlimits", true); try initializeRepo(allocator, account); const small = try std.json.parseFromSliceLeaky(std.json.Value, allocator, "{\"$type\":\"dev.zat.limit\",\"text\":\"small\"}", .{}); const heavy_text = try allocator.alloc(u8, 700_000); @memset(heavy_text, 'x'); const heavy = try std.json.parseFromSliceLeaky(std.json.Value, allocator, try std.fmt.allocPrint(allocator, "{{\"$type\":\"dev.zat.limit\",\"text\":\"{s}\"}}", .{heavy_text}), .{}); const counts = struct { fn get() ![3]u64 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); return .{ try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", "did:plc:commitlimits"), try scalarCountLocked("SELECT COUNT(*) FROM commits WHERE did = ?", "did:plc:commitlimits"), try scalarCountLocked("SELECT COUNT(*) FROM seq_events WHERE did = ?", "did:plc:commitlimits"), }; } }; const before = try counts.get(); var ops: [max_commit_ops + 1]WriteOp = undefined; for (&ops) |*op| op.* = .{ .create = .{ .collection = "dev.zat.limit", .rkey = null, .value = small } }; try std.testing.expectError(Error.TooManyWrites, applyWritesWithOptions(allocator, account, &ops, .{ .validate = .skip })); try std.testing.expectEqual(before, try counts.get()); const heavy_ops = [_]WriteOp{ .{ .create = .{ .collection = "dev.zat.limit", .rkey = null, .value = heavy } }, .{ .create = .{ .collection = "dev.zat.limit", .rkey = null, .value = heavy } }, .{ .create = .{ .collection = "dev.zat.limit", .rkey = null, .value = heavy } }, }; try std.testing.expectError(Error.CommitTooLarge, applyWritesWithOptions(allocator, account, &heavy_ops, .{ .validate = .skip })); try std.testing.expectEqual(before, try counts.get()); _ = try applyWritesWithOptions(allocator, account, ops[0..max_commit_ops], .{ .validate = .skip }); _ = try applyWritesWithOptions(allocator, account, heavy_ops[0..2], .{ .validate = .skip }); const after = try counts.get(); try std.testing.expectEqual(before[0] + max_commit_ops + 2, after[0]); } test "a same-key create and delete in one batch leaves no record, matching the reference" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "batch2.test", "batch2@test.com", "password", "did:plc:batchone", true); const doc = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"io.atcr.manifest\"}", .{}); defer doc.deinit(); const rkey = "3jzfcijpj2z2b"; // the tree applies these in order, so the key is gone; the index must agree _ = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = doc.value } }, .{ .delete = .{ .collection = "io.atcr.manifest", .rkey = rkey } }, }, .{ .validate = .skip }); const root = blk: { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", account.did)); break :blk (try latestCommitRawLocked(allocator, account.did)).?; }; // and the MST must agree with the index: no such key in the tree var reader = RepoBlockReader{ .allocator = allocator, .did = account.did }; var tree = try zat.mst.Mst.loadLazy(allocator, root.data_cid_raw, reader.reader()); try std.testing.expect(tree.get("io.atcr.manifest/" ++ rkey) == null); } test "a delete then create of the same key in one batch keeps the record" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "batch3.test", "batch3@test.com", "password", "did:plc:batchtwo", true); const doc = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"io.atcr.manifest\"}", .{}); defer doc.deinit(); const rkey = "3jzfcijpj2z2c"; _ = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = doc.value } }, }, .{ .validate = .skip }); // the reverse order: the create wins, and the row must survive the earlier delete _ = try applyWritesWithOptions(allocator, account, &.{ .{ .delete = .{ .collection = "io.atcr.manifest", .rkey = rkey } }, .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = doc.value } }, }, .{ .validate = .skip }); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", account.did)); } test "a create over an existing record is refused and leaves the repo unchanged" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "dupe.test", "dupe@test.com", "password", "did:plc:dupecreate", true); const first = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"io.atcr.manifest\",\"n\":1}", .{}); defer first.deinit(); const second = try std.json.parseFromSlice(std.json.Value, allocator, "{\"$type\":\"io.atcr.manifest\",\"n\":2}", .{}); defer second.deinit(); const rkey = "3jzfcijpj2z2k"; const created = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = first.value } }, }, .{ .validate = .skip }); try std.testing.expectError(Error.RecordAlreadyExists, applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = rkey, .value = second.value } }, }, .{ .validate = .skip })); // two creates of one new key in a batch are refused the same way try std.testing.expectError(Error.RecordAlreadyExists, applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "io.atcr.manifest", .rkey = "3jzfcijpj2z2l", .value = first.value } }, .{ .create = .{ .collection = "io.atcr.manifest", .rkey = "3jzfcijpj2z2l", .value = second.value } }, }, .{ .validate = .skip })); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", account.did)); const root = (try latestCommitRawLocked(allocator, account.did)).?; try std.testing.expectEqualStrings(created.commit.cid, root.commit_cid_text); try std.testing.expectEqualStrings(created.records[0].cid, (try currentRecordCidLocked(allocator, account.did, "io.atcr.manifest", rkey)).?); } test "a record write naming a blob the account does not have is refused" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "blobref.test", "blobref@test.com", "password", "did:plc:blobref", true); const other = try createAccount(allocator, "blobother.test", "blobother@test.com", "password", "did:plc:blobother", true); const owned = try putBlob(allocator, std.Options.debug_io, account, "owned!", "text/plain"); const foreign = try putBlob(allocator, std.Options.debug_io, other, "theirs", "text/plain"); const taken_down = try putBlob(allocator, std.Options.debug_io, account, "gone!!", "text/plain"); try setBlobTakedown(account.did, taken_down, true, "mod-1"); const never_uploaded = "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku"; const doc = struct { fn make(a: std.mem.Allocator, cid: []const u8) !std.json.Value { const text = try std.fmt.allocPrint( a, "{{\"$type\":\"fm.example.blob\",\"media\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"text/plain\",\"size\":6}}}}", .{cid}, ); return std.json.parseFromSliceLeaky(std.json.Value, a, text, .{}); } }; const records = struct { fn count() !u64 { db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); return scalarCountLocked("SELECT COUNT(*) FROM records WHERE did = ?", "did:plc:blobref"); } }; for ([_][]const u8{ foreign, taken_down, never_uploaded }) |cid| { var missing: ?[]const u8 = null; const before = try records.count(); try std.testing.expectError(Error.BlobNotFound, applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "fm.example.blob", .rkey = "ok00000000000", .value = try doc.make(allocator, owned) } }, .{ .create = .{ .collection = "fm.example.blob", .rkey = "bad0000000000", .value = try doc.make(allocator, cid) } }, }, .{ .validate = .skip, .missing_blob = &missing })); try std.testing.expectEqualStrings(cid, missing.?); try std.testing.expectEqual(before, try records.count()); } _ = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "fm.example.blob", .rkey = "ok00000000000", .value = try doc.make(allocator, owned) } }, }, .{ .validate = .skip }); var missing: ?[]const u8 = null; try std.testing.expectError(Error.BlobNotFound, applyWritesWithOptions(allocator, account, &.{ .{ .update = .{ .collection = "fm.example.blob", .rkey = "ok00000000000", .value = try doc.make(allocator, never_uploaded) } }, }, .{ .validate = .skip, .missing_blob = &missing })); try std.testing.expectEqualStrings(never_uploaded, missing.?); try setBlobTakedown(account.did, taken_down, false, null); _ = try applyWritesWithOptions(allocator, account, &.{ .{ .update = .{ .collection = "fm.example.blob", .rkey = "ok00000000000", .value = try doc.make(allocator, taken_down) } }, }, .{ .validate = .skip }); } test "a batch maps each record's blob refs to that record" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "refspan.test", "refspan@test.com", "password", "did:plc:refspan", true); const cid_a = try putBlob(allocator, std.Options.debug_io, account, "alpha!", "text/plain"); const cid_b = try putBlob(allocator, std.Options.debug_io, account, "bravo!", "text/plain"); const body = struct { fn make(a: std.mem.Allocator, cid: []const u8) ![]const u8 { return std.fmt.allocPrint( a, "{{\"$type\":\"fm.example.blob\",\"media\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"text/plain\",\"size\":6}}}}", .{cid}, ); } }; const doc_a = try std.json.parseFromSlice(std.json.Value, allocator, try body.make(allocator, cid_a), .{}); defer doc_a.deinit(); const doc_b = try std.json.parseFromSlice(std.json.Value, allocator, try body.make(allocator, cid_b), .{}); defer doc_b.deinit(); // both records in ONE batch, so their ref spans must not be crossed _ = try applyWritesWithOptions(allocator, account, &.{ .{ .create = .{ .collection = "fm.example.blob", .rkey = "aaaaaaaaaaaaa", .value = doc_a.value } }, .{ .create = .{ .collection = "fm.example.blob", .rkey = "bbbbbbbbbbbbb", .value = doc_b.value } }, }, .{ .validate = .skip }); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); for ([_][2][]const u8{ .{ "aaaaaaaaaaaaa", cid_a }, .{ "bbbbbbbbbbbbb", cid_b } }) |want| { const uri = try std.fmt.allocPrint(allocator, "at://{s}/fm.example.blob/{s}", .{ account.did, want[0] }); const row = try conn.row("SELECT blob_cid FROM expected_blobs WHERE record_uri = ?", .{uri}); try std.testing.expect(row != null); defer row.?.deinit(); try std.testing.expectEqualStrings(want[1], row.?.text(0)); } } test "remembered browser credentials expire and revoke independently of OAuth" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(allocator, "remember.test", "remember@test.com", "password", "did:plc:remember", true); try putBrowserSession("digest", account, 4102444800); try std.testing.expect(try getBrowserSession(allocator, "forged", 1000) == null); try std.testing.expectEqualStrings(account.did, (try getBrowserSession(allocator, "digest", 1000)).?.did); try std.testing.expect(try getBrowserSession(allocator, "digest", 4102444800) == null); try conn.exec("UPDATE accounts SET account_status = 'deactivated' WHERE did = ?", .{account.did}); try std.testing.expect(try getBrowserSession(allocator, "digest", 1000) == null); try conn.exec("UPDATE accounts SET account_status = 'active', password_hash = 'changed' WHERE did = ?", .{account.did}); try std.testing.expect(try getBrowserSession(allocator, "digest", 1000) == null); try conn.exec("UPDATE accounts SET password_hash = ? WHERE did = ?", .{ account.password, account.did }); try deleteBrowserSession("digest"); try std.testing.expect(try getBrowserSession(allocator, "digest", 1000) == null); } test "startup next sequence uses both histories including gaps and empty tables" { try init(std.Options.debug_io, ":memory:"); defer close(); try std.testing.expectEqual(@as(u64, 1), try loadNextSeqLocked()); try conn.execNoArgs("INSERT INTO accounts (did, handle, email, password_hash) VALUES ('did:plc:sequence', 'sequence.test', 'sequence@example.test', 'unused')"); try conn.execNoArgs("INSERT INTO commits (seq, did, cid, rev) VALUES (7, 'did:plc:sequence', 'commit', 'rev')"); try std.testing.expectEqual(@as(u64, 8), try loadNextSeqLocked()); try conn.execNoArgs("INSERT INTO seq_events (seq, did, commit_cid, evt) VALUES (3, 'did:plc:sequence', 'commit', X'A0')"); try std.testing.expectEqual(@as(u64, 8), try loadNextSeqLocked()); try conn.execNoArgs("INSERT INTO seq_events (seq, did, commit_cid, evt) VALUES (19, 'did:plc:sequence', 'commit', X'A0')"); try std.testing.expectEqual(@as(u64, 20), try loadNextSeqLocked()); try conn.execNoArgs("DELETE FROM commits"); try std.testing.expectEqual(@as(u64, 20), try loadNextSeqLocked()); } test "account access groups count beyond pages and isolate detail queries" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(allocator, "access.test", "access@test.com", "password", "did:plc:access", true); const other = try createAccount(allocator, "other.test", "other@test.com", "password", "did:plc:otheraccess", true); const client = "https://api-stg.example/client.json"; for (0..226) |i| { try putOAuthToken(owner.did, client, try std.fmt.allocPrint(allocator, "scope-{d}", .{i}), try std.fmt.allocPrint(allocator, "access-{d}", .{i}), try std.fmt.allocPrint(allocator, "refresh-{d}", .{i}), 100, 4102444800, "jkt", "password"); } try putOAuthToken(other.did, client, "other-scope", "other-access", "other-refresh", 100, 4102444800, "other-jkt", "password"); try putOAuthToken(owner.did, "https://expired.example", "atproto", "old-access", "old-refresh", 1, 2, "jkt", "password"); try putOAuthToken(owner.did, "https://revoked.example", "atproto", "rev-access", "rev-refresh", 100, 4102444800, "jkt", "password"); try revokeOAuthTokenFamily("rev-access"); const summaries = try listAccountAccess(allocator, owner.did, .{ .limit = 1 }); try std.testing.expectEqual(@as(i64, 226), summaries.authorized); try std.testing.expectEqual(@as(i64, 228), summaries.total); try std.testing.expectEqual(@as(usize, 1), summaries.groups.len); try std.testing.expectEqual(@as(i64, 226), summaries.groups[0].count); try std.testing.expect(summaries.next == null); var seen: std.StringHashMap(void) = .init(allocator); var offset: i64 = 0; while (true) { const page = try listAccountAccess(allocator, owner.did, .{ .group = client, .offset = offset }); try std.testing.expect(page.grants.len <= 20); for (page.grants) |grant| { try std.testing.expectEqualStrings(owner.did, grant.did); try std.testing.expect(!seen.contains(grant.scope)); try seen.put(grant.scope, {}); } offset = page.next orelse break; } try std.testing.expectEqual(@as(usize, 226), seen.count()); const past = try listAccountAccess(allocator, owner.did, .{ .state = .past, .limit = 1 }); try std.testing.expectEqual(@as(i64, 1), past.next.?); const past_second = try listAccountAccess(allocator, owner.did, .{ .state = .past, .offset = past.next.? }); try std.testing.expectEqual(@as(usize, 1), past_second.groups.len); try std.testing.expect(!std.mem.eql(u8, past.groups[0].key, past_second.groups[0].key)); const search = try listAccountAccess(allocator, owner.did, .{ .search = "API-STG" }); try std.testing.expectEqual(@as(usize, 1), search.groups.len); const empty = try listAccountAccess(allocator, owner.did, .{ .group = "unknown" }); try std.testing.expectEqual(@as(usize, 0), empty.grants.len); _ = try createSessionTokenRow(allocator, owner.did, "pw-a", "pw-r", 100, 4102444800, "password", null, null); _ = try createSessionTokenRow(allocator, owner.did, "app-a", "app-r", 100, 4102444800, "app_password", null, "phone"); _ = try createSessionTokenRow(allocator, other.did, "other-a", "other-r", 100, 4102444800, "password", null, null); const passwords = try listAccountAccess(allocator, owner.did, .{ .kind = .password }); try std.testing.expectEqual(@as(i64, 2), passwords.authorized); try std.testing.expectEqual(@as(usize, 2), passwords.groups.len); const details = try listAccountAccess(allocator, owner.did, .{ .kind = .password, .group = "password:" }); try std.testing.expectEqual(@as(usize, 1), details.sessions.len); try std.testing.expectEqualStrings(owner.did, details.sessions[0].did); } test "revoking an app ends only that account's live grants for that client" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(allocator, "revoke.test", "revoke@test.com", "password", "did:plc:revokeapp", true); const other = try createAccount(allocator, "bystander.test", "bystander@test.com", "password", "did:plc:bystander", true); const client = "https://app.example/client.json"; try putOAuthToken(owner.did, client, "atproto", "a-1", "r-1", 100, 4102444800, "jkt", "password"); try putOAuthToken(owner.did, client, "atproto", "a-2", "r-2", 100, 4102444800, "jkt", "passkey"); try putOAuthToken(owner.did, "https://keep.example/client.json", "atproto", "a-3", "r-3", 100, 4102444800, "jkt", "password"); try putOAuthToken(other.did, client, "atproto", "a-4", "r-4", 100, 4102444800, "jkt", "password"); try std.testing.expectEqual(@as(usize, 2), try revokeOAuthClient(owner.did, client)); try std.testing.expectEqual(@as(usize, 0), try revokeOAuthClient(owner.did, client)); try std.testing.expectEqual(@as(i64, 1), (try listAccountAccess(allocator, owner.did, .{})).authorized); try std.testing.expectEqual(@as(i64, 1), (try listAccountAccess(allocator, other.did, .{})).authorized); } /// Rebuilds the October 22 schema: per-side policy columns on the space row /// and the member list in simplespace_members. fn restoreSimplespaceSchemaForTest() !void { try conn.execNoArgs("DELETE FROM zds_migrations WHERE name IN ('space-policy-v1', 'service-refs-v1')"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN read_managing_app TEXT"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN read_policy TEXT NOT NULL DEFAULT 'member-list'"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN write_managing_app TEXT"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN write_policy TEXT NOT NULL DEFAULT 'member-list'"); try conn.execNoArgs("ALTER TABLE permissioned_spaces ADD COLUMN app_access_json TEXT NOT NULL DEFAULT '{\"type\":\"open\"}'"); try conn.execNoArgs("ALTER TABLE permissioned_spaces DROP COLUMN policy_json"); try conn.execNoArgs( \\CREATE TABLE simplespace_members ( \\ space TEXT NOT NULL REFERENCES permissioned_spaces(uri) ON DELETE CASCADE, \\ member_did TEXT NOT NULL, \\ read INTEGER NOT NULL DEFAULT 1, \\ write INTEGER NOT NULL DEFAULT 1, \\ created_at INTEGER NOT NULL DEFAULT (unixepoch()), \\ PRIMARY KEY (space, member_did) \\) ); } test "simplespace configurations migrate to space policies" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "migration.test", "migration@test.com", "password", "did:plc:migration", true); const allow = "{\"type\":\"allowList\",\"allowed\":[\"https://client.test\"]}"; const Case = struct { skey: []const u8, read: []const u8, read_app: ?[]const u8, write: []const u8, write_app: ?[]const u8, app_access: []const u8, policy: []const u8, members: usize }; const cases = [_]Case{ .{ .skey = "managed", .read = "managing-app", .read_app = "did:web:manager.test#service", .write = "managing-app", .write_app = "did:web:manager.test#service", .app_access = allow, .policy = "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:manager.test#service\",\"publishPolicy\":false}", .members = 0 }, .{ .skey = "mixed", .read = "public", .read_app = null, .write = "managing-app", .write_app = "did:web:manager.test", .app_access = "{\"type\":\"open\"}", .policy = "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:manager.test#atproto_pds\",\"publishPolicy\":false}", .members = 0 }, .{ .skey = "public", .read = "public", .read_app = null, .write = "member-list", .write_app = null, .app_access = allow, .policy = "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true,\"publicWrite\":false,\"allowedApps\":[\"https://client.test\"],\"publishPolicy\":false,\"publishMembers\":false}", .members = 1 }, .{ .skey = "members", .read = "member-list", .read_app = null, .write = "public", .write_app = null, .app_access = "{\"type\":\"open\"}", .policy = "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":false,\"publicWrite\":true,\"publishPolicy\":false,\"publishMembers\":false}", .members = 1 }, }; for (cases) |case| { _ = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.migration.space", .skey = case.skey, .is_authority = true, .policy = .personal }); } // A writer's host keeps a row for another authority's space, with no policy. _ = try createSpace(a, .{ .actor_did = owner.did, .authority_did = "did:plc:elsewhere", .space_type = "test.migration.space", .skey = "remote", .is_authority = false }); try restoreSimplespaceSchemaForTest(); for (cases) |case| { const uri = try std.fmt.allocPrint(a, "at:space/{s}/test.migration.space/{s}", .{ owner.did, case.skey }); try conn.exec( "UPDATE permissioned_spaces SET read_policy = ?, read_managing_app = ?, write_policy = ?, write_managing_app = ?, app_access_json = ? WHERE uri = ?", .{ case.read, case.read_app, case.write, case.write_app, case.app_access, uri }, ); try conn.exec("INSERT INTO simplespace_members (space, member_did, read, write) VALUES (?, ?, 1, 0)", .{ uri, "did:plc:member" }); try conn.exec("INSERT INTO simplespace_members (space, member_did) VALUES (?, ?)", .{ uri, owner.did }); } try migratePermissionedDataTables(); for (cases) |case| { const uri = try std.fmt.allocPrint(a, "at:space/{s}/test.migration.space/{s}", .{ owner.did, case.skey }); const space = (try getSpace(a, owner.did, uri)).?; try std.testing.expectEqualStrings(case.policy, space.policy_json.?); try std.testing.expectEqual(case.members, try scalarCountLocked("SELECT COUNT(*) FROM memberspace_members WHERE space = ?", uri)); if (case.members > 0) { const members = try listSpaceMembers(a, uri, null, 100); try std.testing.expectEqualStrings("did:plc:member", members[0].did); try std.testing.expect(members[0].read and !members[0].write); } else { try std.testing.expectError(error.PolicyMismatch, listSpaceMembers(a, uri, null, 100)); } } const remote = (try getSpace(a, owner.did, "at:space/did:plc:elsewhere/test.migration.space/remote")).?; try std.testing.expect(remote.policy_json == null); try std.testing.expect(!try tableExistsLocked("simplespace_members")); try std.testing.expect(!try permissionedSpacesColumnExistsLocked("read_policy")); try std.testing.expect(!try permissionedSpacesColumnExistsLocked("app_access_json")); // Restarting leaves the converted policies alone. _ = try updateSpacePolicy(a, "at:space/did:plc:migration/test.migration.space/members", .personal); try migratePermissionedDataTables(); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.personalspace.policy\"}", (try getSpace(a, owner.did, "at:space/did:plc:migration/test.migration.space/members")).?.policy_json.?, ); } test "memberspace member upserts replace access and revoke writers" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "members.test", "members@test.com", "password", "did:plc:members", true); const space = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.members.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); for ([_][2]bool{ .{ true, false }, .{ false, true }, .{ false, false }, .{ true, true } }) |access| { _ = try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = .{ .read = access[0], .write = access[1] } }}); try std.testing.expectEqual(access[0], try spaceMemberAllows(space.uri, "did:plc:member", .read)); try std.testing.expectEqual(access[1], try spaceMemberAllows(space.uri, "did:plc:member", .write)); try std.testing.expectEqual(@as(usize, 1), (try listSpaceMembers(a, space.uri, null, 100)).len); } const rev = "3lx7abcdefg22"; const hash: [zat.space.lthash.digest_bytes]u8 = @splat(0); // Revoking write removes the member from the writer set, which keeps // listing it as removed at the revision of its removal. _ = (try recordSpaceWriter(a, space.uri, "did:plc:member", rev, &hash)).?; const revoked = try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = .{ .read = true, .write = false } }}); try std.testing.expectEqual(@as(usize, 1), revoked.removed_writers.len); try std.testing.expectEqualStrings("did:plc:member", revoked.removed_writers[0].repo_did); var writers = try listSpaceWriters(a, space.uri, null, 50); try std.testing.expect(writers.len == 1 and writers[0].removed); try std.testing.expectEqualStrings(revoked.removed_writers[0].update.space_rev, writers[0].space_rev); // A newer write rejoins it. _ = (try recordSpaceWriter(a, space.uri, "did:plc:member", "3lx7abcdefg23", &hash)).?; writers = try listSpaceWriters(a, space.uri, null, 50); try std.testing.expect(writers.len == 1 and !writers[0].removed); try std.testing.expectEqual(@as(usize, 1), (try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = null }})).removed_writers.len); try std.testing.expect(!try spaceMemberAllows(space.uri, "did:plc:member", .read)); try std.testing.expect(!try spaceMemberAllows(space.uri, "did:plc:member", .write)); try std.testing.expect((try listSpaceWriters(a, space.uri, null, 50))[0].removed); try std.testing.expectEqual(@as(usize, 0), (try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = null }})).removed_writers.len); _ = (try recordSpaceWriter(a, space.uri, "did:plc:writer", rev, &hash)).?; const removed = (try removeSpaceWriter(a, space.uri, "did:plc:writer")).?; try std.testing.expect(removed.prev_space_rev != null); try std.testing.expect(try removeSpaceWriter(a, space.uri, "did:plc:writer") == null); writers = try listSpaceWriters(a, space.uri, null, 50); const last = writers[writers.len - 1]; try std.testing.expect(last.removed and std.mem.eql(u8, last.repo_did, "did:plc:writer") and std.mem.eql(u8, last.space_rev, removed.space_rev)); // The space meta archive carries only the writer set. try std.testing.expectEqual(@as(usize, 0), (try exportSpaceMeta(a, space.uri)).writers.len); _ = (try recordSpaceWriter(a, space.uri, "did:plc:writer", "3lx7abcdefg23", &hash)).?; try std.testing.expectEqual(@as(usize, 1), (try exportSpaceMeta(a, space.uri)).writers.len); try std.testing.expectError(Error.RepoNotFound, removeSpaceWriter(a, "at:space/did:plc:members/test.members.space/absent", "did:plc:writer")); _ = try updateSpacePolicy(a, space.uri, .personal); try std.testing.expectError(error.PolicyMismatch, changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = .{ .read = true, .write = true } }})); try std.testing.expectError(error.PolicyMismatch, changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = null }})); } fn expectSpaceRecordJson(allocator: std.mem.Allocator, space: []const u8, repo: []const u8, collection: []const u8, rkey: []const u8, expected: ?[]const u8) !void { const record = try getSpaceRecord(allocator, space, repo, collection, rkey); if (expected) |json| { try std.testing.expectEqualStrings(json, record.?.value_json); } else { try std.testing.expect(record == null); } } test "published policy and member records follow the configuration" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "publish.test", "publish@test.com", "password", "did:plc:publish", true); const space = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.publish.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{ .publish_policy = true } }, }); const policy_col = space_policy.policy_collection; const member_col = space_policy.member_collection; try expectSpaceRecordJson(a, space.uri, owner.did, policy_col, "self", "{\"$type\":\"com.atproto.space.policy\",\"policy\":{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":false,\"publicWrite\":false,\"publishPolicy\":true,\"publishMembers\":false}}"); try std.testing.expectEqual(@as(u64, 1), try scalarCountLocked("SELECT COUNT(*) FROM permissioned_space_notify_outbox WHERE repo_did = ?", owner.did)); const created_rev = (try getSpaceRepoState(a, space.uri, owner.did)).rev.?; // The write's own request delivers it, so the worker can't claim it yet; // a transient inline failure hands it to the worker. try std.testing.expect(try claimSpaceNotification(a) == null); try releaseSpaceNotification(space.uri, owner.did, created_rev); const claimed = (try claimSpaceNotification(a)).?; try std.testing.expectEqualStrings(created_rev, claimed.rev); try std.testing.expect(try claimSpaceNotification(a) == null); try finishSpaceNotification(space.uri, owner.did, created_rev); // Members are not published until the flag is set. try std.testing.expect(!(try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:web:localhost%3A2583", .access = .{ .read = true, .write = false } }})).changed); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:web:localhost:2583", null); try std.testing.expect(try updateSpacePolicy(a, space.uri, .{ .member = .{ .publish_policy = true, .publish_members = true } })); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:web:localhost:2583", "{\"$type\":\"com.atproto.memberspace.member\",\"read\":true,\"write\":false}"); try std.testing.expect(std.mem.order(u8, (try getSpaceRepoState(a, space.uri, owner.did)).rev.?, created_rev) == .gt); try std.testing.expect((try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = .{ .read = false, .write = true } }})).changed); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:plc:member", "{\"$type\":\"com.atproto.memberspace.member\",\"read\":false,\"write\":true}"); try std.testing.expect((try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:member", .access = null }})).changed); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:plc:member", null); // Clearing the flag withdraws the policy; a $type change withdraws the // member list along with the state it described. try std.testing.expect(try updateSpacePolicy(a, space.uri, .{ .member = .{ .publish_members = true } })); try expectSpaceRecordJson(a, space.uri, owner.did, policy_col, "self", null); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:web:localhost:2583", "{\"$type\":\"com.atproto.memberspace.member\",\"read\":true,\"write\":false}"); try std.testing.expect(try updateSpacePolicy(a, space.uri, .{ .managed = .{ .managing_app = "did:web:forum.test#forum", .publish_policy = true } })); try expectSpaceRecordJson(a, space.uri, owner.did, member_col, "did:web:localhost:2583", null); try expectSpaceRecordJson(a, space.uri, owner.did, policy_col, "self", "{\"$type\":\"com.atproto.space.policy\",\"policy\":{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:forum.test#forum\",\"publishPolicy\":true}}"); try std.testing.expectEqual(@as(u64, 0), try scalarCountLocked("SELECT COUNT(*) FROM memberspace_members WHERE space = (SELECT uri FROM permissioned_spaces WHERE authority_did = ?)", owner.did)); try std.testing.expect(try updateSpacePolicy(a, space.uri, .{ .member = .{} })); try std.testing.expectEqual(@as(usize, 0), (try listSpaceMembers(a, space.uri, null, 100)).len); try expectSpaceRecordJson(a, space.uri, owner.did, policy_col, "self", null); try std.testing.expect(!try updateSpacePolicy(a, space.uri, .personal)); // The published records commit to the authority's repo like any write. const ops = try listSpaceRecordOplog(a, space.uri, owner.did, null, null, 100, false); try std.testing.expect(ops.len >= 6); } test "clearing a space repo keeps the authority's published records" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "clear.test", "clear@test.com", "password", "did:plc:clear", true); const writer = try createAccount(a, "clearwriter.test", "clearwriter@test.com", "password", "did:plc:clearwriter", true); const space = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.clear.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{ .publish_policy = true, .publish_members = true } }, }); _ = try changeSpaceMembers(a, space.uri, &.{.{ .did = writer.did, .access = .{ .read = true, .write = true } }}); _ = try createSpace(a, .{ .actor_did = writer.did, .authority_did = owner.did, .space_type = "test.clear.space", .skey = "self", .is_authority = false }); // A repo with nothing in it clears as a no-op. try std.testing.expect(!try clearSpaceRecords(a, space.uri, writer.did, null)); try std.testing.expect((try getSpaceRepoState(a, space.uri, writer.did)).rev == null); const note = try std.json.parseFromSliceLeaky(std.json.Value, a, "{\"$type\":\"test.clear.note\",\"text\":\"x\",\"file\":{\"$type\":\"blob\",\"ref\":{\"$link\":\"bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu\"},\"mimeType\":\"text/plain\",\"size\":1}}", .{}); const other = try std.json.parseFromSliceLeaky(std.json.Value, a, "{\"$type\":\"test.clear.other\",\"text\":\"y\"}", .{}); for ([_][]const u8{ owner.did, writer.did }) |repo| { _ = try createSpaceRecord(a, space.uri, repo, "test.clear.note", "one", try prepareRecordValue(a, "test.clear.note", "one", note)); _ = try createSpaceRecord(a, space.uri, repo, "test.clear.other", "two", try prepareRecordValue(a, "test.clear.other", "two", other)); } // Every collection being deleted needs the caller's grant. const OnlyNotes = struct { fn allows(_: *const anyopaque, collection: []const u8) bool { return std.mem.eql(u8, collection, "test.clear.note"); } }; const unused: u8 = 0; try std.testing.expectError(error.InsufficientScope, clearSpaceRecords(a, space.uri, writer.did, .{ .context = &unused, .allows = OnlyNotes.allows })); try std.testing.expect((try getSpaceRecord(a, space.uri, writer.did, "test.clear.other", "two")) != null); const before = (try getSpaceRepoState(a, space.uri, writer.did)).rev.?; try std.testing.expect(try clearSpaceRecords(a, space.uri, writer.did, null)); const cleared = try getSpaceRepoState(a, space.uri, writer.did); try std.testing.expect(std.mem.order(u8, cleared.rev.?, before) == .gt); var empty_hash = try zat.space.LtHash.fromBytes(cleared.set_hash.?); const empty_digest = empty_hash.digest(); try std.testing.expectEqualStrings("e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad", &std.fmt.bytesToHex(empty_digest, .lower)); try std.testing.expectEqual(@as(usize, 0), (try listSpaceRecords(a, space.uri, writer.did, null, null, 100, false)).len); try std.testing.expectEqual(@as(usize, 0), (try listSpaceRecordOplog(a, space.uri, writer.did, null, null, 100, false)).len); try std.testing.expect(!try permissionedBlobReferenced(space.uri, writer.did, "bafkreibm6jgipb22ignhvbmf6avdkvuevprpyx6y6722fsxk7wxiofq4wu")); try std.testing.expect(!try clearSpaceRecords(a, space.uri, writer.did, null)); try std.testing.expectEqualStrings(cleared.rev.?, (try getSpaceRepoState(a, space.uri, writer.did)).rev.?); // The authority keeps its published configuration, and the digest covers it. try std.testing.expectError(error.InsufficientScope, clearSpaceRecords(a, space.uri, owner.did, .{ .context = &unused, .allows = OnlyNotes.allows })); try std.testing.expect((try getSpaceRecord(a, space.uri, owner.did, "test.clear.other", "two")) != null); try std.testing.expect(try clearSpaceRecords(a, space.uri, owner.did, null)); const kept = try listSpaceRecords(a, space.uri, owner.did, null, null, 100, false); try std.testing.expectEqual(@as(usize, 2), kept.len); var expected: zat.space.LtHash = .{}; for (kept) |record| try addRecordElement(a, &expected, record.collection, record.rkey, record.cid); try std.testing.expectEqualSlices(u8, &expected.bytes, (try getSpaceRepoState(a, space.uri, owner.did)).set_hash.?); try std.testing.expect(!try clearSpaceRecords(a, space.uri, owner.did, null)); } fn importedSpaceRecordsForTest(allocator: std.mem.Allocator, repo: zat.space.archive.VerifiedRepo) ![]ImportedSpaceRecord { var out: std.ArrayList(ImportedSpaceRecord) = .empty; for (repo.records) |block| { const slash = std.mem.indexOfScalar(u8, block.path, '/').?; const json = try zat.cbor.toJsonAlloc(allocator, try zat.cbor.decodeAll(allocator, block.data)); const value = try std.json.parseFromSliceLeaky(std.json.Value, allocator, json, .{}); const prepared = try prepareRecordValue(allocator, block.path[0..slash], block.path[slash + 1 ..], value); try std.testing.expectEqualStrings(try block.cid.toString(allocator), prepared.cid); try out.append(allocator, .{ .collection = block.path[0..slash], .rkey = block.path[slash + 1 ..], .prepared = prepared }); } return out.toOwnedSlice(allocator); } test "a space and its authority's repo migrate between hosts" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path_len = try tmp.dir.realPath(std.testing.io, &path_buf); blobstore.init(std.Options.debug_io, path_buf[0..path_len]); // the old host try init(std.Options.debug_io, ":memory:"); var alice = try createAccount(a, "mover.test", "mover@test.com", "password", "did:plc:mover", true); const blob_cid = try putBlob(a, std.Options.debug_io, alice, "private bytes", "text/plain"); const space = (try createSpace(a, .{ .actor_did = alice.did, .authority_did = alice.did, .space_type = "test.move.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{ .publish_policy = true, .publish_members = true } }, })).uri; _ = try changeSpaceMembers(a, space, &.{.{ .did = "did:plc:bob", .access = .{ .read = true, .write = false } }}); const note = try std.json.parseFromSliceLeaky(std.json.Value, a, try std.fmt.allocPrint(a, "{{\"$type\":\"test.move.note\",\"file\":{{\"$type\":\"blob\",\"ref\":{{\"$link\":\"{s}\"}},\"mimeType\":\"text/plain\",\"size\":13}}}}", .{blob_cid}), .{}); _ = try createSpaceRecord(a, space, alice.did, "test.move.note", "one", try prepareRecordValue(a, "test.move.note", "one", note)); const hash: [zat.space.lthash.digest_bytes]u8 = @splat(7); _ = (try recordSpaceWriter(a, space, "did:plc:carol", "3mrevcarol222", &hash)).?; const meta = try exportSpaceMeta(a, space); const old_state = try getSpaceRepoState(a, space, alice.did); const commit = try zat.space.archive.commitForState(old_state.set_hash.?, .{ .space = space, .repo = alice.did, .rev = old_state.rev.? }); const star = try zat.space.archive.writeRepoStar(a, commit, try loadSpaceRepoBlocks(a, space, alice.did)); const old_writers = try listSpaceWriters(a, space, null, 100); close(); // the new host try init(std.Options.debug_io, ":memory:"); defer close(); alice = try createAccount(a, "mover.test", "mover@test.com", "password", "did:plc:mover", true); var repo = try zat.space.archive.readRepoStar(std.testing.allocator, star); defer repo.deinit(); const records = try importedSpaceRecordsForTest(a, repo); try std.testing.expectError(Error.RepoNotFound, importSpaceRepo(a, space, alice.did, records, repo.commit.data)); var members: std.ArrayList(ImportedSpaceMember) = .empty; for (meta.members) |m| try members.append(a, .{ .did = m.did, .access = .{ .read = m.read, .write = m.write } }); try std.testing.expectError(error.PolicyMismatch, importSpaceMeta(a, space, .personal, members.items, meta.writers)); try importSpaceMeta(a, space, meta.policy, members.items, meta.writers); for (0..2) |_| try importSpaceRepo(a, space, alice.did, records, repo.commit.data); const state = try getSpaceRepoState(a, space, alice.did); var imported_hash = try zat.space.LtHash.fromBytes(state.set_hash.?); try std.testing.expectEqualSlices(u8, &commit.data, &imported_hash.digest()); try std.testing.expect(!std.mem.eql(u8, state.rev.?, commit.rev)); try std.testing.expectEqual(@as(usize, 3), (try listSpaceRecords(a, space, alice.did, null, null, 100, false)).len); try std.testing.expectEqual(@as(usize, 0), (try listSpaceRecordOplog(a, space, alice.did, null, null, 100, false)).len); const new_members = try listSpaceMembers(a, space, null, 100); try std.testing.expectEqual(@as(usize, 1), new_members.len); try std.testing.expect(new_members[0].read and !new_members[0].write); const new_writers = try listSpaceWriters(a, space, null, 100); try std.testing.expectEqual(old_writers.len, new_writers.len); for (old_writers, new_writers) |old, new| try std.testing.expectEqualStrings(old.space_rev, new.space_rev); const spaces = try listSpaces(a, alice.did, null, null, null, 50); try std.testing.expect(spaces.len == 1 and spaces[0].is_authority); // a later write sorts after the imported writer entries _ = (try recordSpaceWriter(a, space, "did:plc:dave", "3mrevdave2222", &hash)).?; const after = try listSpaceWriters(a, space, old_writers[old_writers.len - 1].space_rev, 100); try std.testing.expectEqualStrings("did:plc:dave", after[0].repo_did); // the blob, uploaded after its record arrived, is not collected try std.testing.expectEqualStrings(blob_cid, try putBlob(a, std.Options.debug_io, alice, "private bytes", "text/plain")); try std.testing.expectEqual(@as(usize, 0), (try collectBlobGarbage(a, 0, 10)).deleted); try std.testing.expect(getBlob(a, alice.did, blob_cid) != null); } test "space sequence, revocations and notification retries survive restart" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const root_len = try tmp.dir.realPath(std.testing.io, &path_buf); const path = try std.fmt.allocPrint(a, "{s}/spaces.sqlite3", .{path_buf[0..root_len]}); try init(std.Options.debug_io, path); defer close(); const did = "did:plc:sequence"; const space = try createSpace(a, .{ .actor_did = did, .authority_did = did, .space_type = "test.sequence.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{} }, }); const first = zat.Tid.fromTimestamp(nowMicros() - 1000, 0).str(); const second = zat.Tid.fromTimestamp(nowMicros(), 0).str(); const hash: [32]u8 = @splat(1); const one = (try recordSpaceWriter(a, space.uri, did, &first, &hash)).?; try std.testing.expect(one.prev_space_rev == null); try std.testing.expect(try recordSpaceWriter(a, space.uri, did, &first, &hash) == null); const two = (try recordSpaceWriter(a, space.uri, "did:plc:another", &second, &hash)).?; try std.testing.expectEqualStrings(one.space_rev, two.prev_space_rev.?); const future = zat.Tid.fromTimestamp(nowMicros() + 301 * std.time.us_per_s, 0).str(); try std.testing.expectError(error.FutureRev, recordSpaceWriter(a, space.uri, did, &future, &hash)); const page = try listSpaceWriters(a, space.uri, one.space_rev, 1); try std.testing.expectEqual(@as(usize, 1), page.len); try std.testing.expectEqualStrings("did:plc:another", page[0].repo_did); try std.testing.expectEqual(@as(usize, 0), (try listSpaceWriters(a, space.uri, two.space_rev, 1)).len); try std.testing.expectEqual(@as(usize, 0), (try listSpaceWriters(a, space.uri, "zzz", 1)).len); try revokeSpaceCredentials(space.uri, &.{ "revoked", "revoked" }); try std.testing.expect(!try spaceCredentialRevoked("other", "revoked")); const value = try std.json.parseFromSlice(std.json.Value, a, "{\"$type\":\"test.sequence.record\",\"text\":\"one\"}", .{}); const prepared = try prepareRecordValue(a, "test.sequence.record", "one", value.value); _ = try putSpaceRecord(a, space.uri, did, "test.sequence.record", "one", prepared); // No request delivers these writes inline here; let the inline lease lapse. try conn.execNoArgs("UPDATE permissioned_space_notify_outbox SET lease_until = 0"); const pending = (try claimSpaceNotification(a)).?; try std.testing.expect(try claimSpaceNotification(a) == null); _ = try putSpaceRecord(a, space.uri, did, "test.sequence.record", "two", prepared); try finishSpaceNotification(pending.space, pending.repo, pending.rev); close(); try init(std.Options.debug_io, path); try std.testing.expect(try spaceCredentialRevoked(space.uri, "revoked")); try std.testing.expect(try claimSpaceNotification(a) == null); try conn.execNoArgs("UPDATE permissioned_space_notify_outbox SET lease_until = 0"); const latest = (try claimSpaceNotification(a)).?; try std.testing.expect(!std.mem.eql(u8, latest.rev, pending.rev)); try retrySpaceNotification(latest); try std.testing.expect(try claimSpaceNotification(a) == null); try conn.execNoArgs("UPDATE permissioned_space_notify_outbox SET retry_at = 0, lease_until = 0"); const retry = (try claimSpaceNotification(a)).?; try std.testing.expectEqual(@as(i64, 2), retry.attempts); try finishSpaceNotification(retry.space, retry.repo, retry.rev); try std.testing.expect(try claimSpaceNotification(a) == null); const three = (try recordSpaceWriter(a, space.uri, did, &second, &hash)).?; try std.testing.expectEqualStrings(two.space_rev, three.prev_space_rev.?); try std.testing.expect(try recordSpaceWriter(a, space.uri, did, &first, &hash) == null); try conn.execNoArgs("DROP INDEX permissioned_space_writer_sequence"); try conn.execNoArgs("ALTER TABLE permissioned_space_writers DROP COLUMN space_rev"); try conn.execNoArgs("DELETE FROM permissioned_space_sequence"); close(); try init(std.Options.debug_io, path); const migrated = try listSpaceWriters(a, space.uri, null, 10); try std.testing.expectEqual(@as(usize, 2), migrated.len); try std.testing.expect(zat.Tid.parse(migrated[0].space_rev) != null); try std.testing.expect(std.mem.lessThan(u8, migrated[0].space_rev, migrated[1].space_rev)); close(); try init(std.Options.debug_io, path); const reopened = try listSpaceWriters(a, space.uri, migrated[0].space_rev, 10); try std.testing.expectEqual(@as(usize, 1), reopened.len); try std.testing.expectEqualStrings(migrated[1].space_rev, reopened[0].space_rev); } test "getRepoBlocks returns held blocks and names missing ones" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(a, "blocks.test", "blocks@test.com", "password", "did:plc:blocks", true); const held = "bafyreih6ce5bdeftdgnbl5owctbxyokmo6t3lsxwrhgpklu4mtjhzt6zlm"; try conn.exec("INSERT INTO repo_blocks (did, cid, data, repo_rev) VALUES (?, ?, ?, NULL)", .{ account.did, held, zqlite.blob("block") }); const missing = "bafyreiabjkuogmtqbmbvwjquklmbtfmbqqsrfmn7t7ogvxlsmwbx46ghzq"; const got = try getRepoBlocks(a, account.did, &.{ held, missing }); try std.testing.expectEqualStrings("block", got.blocks[0].data); try std.testing.expectEqual(@as(usize, 1), got.blocks.len); try std.testing.expectEqual(@as(usize, 1), got.missing.len); try std.testing.expectEqualStrings(missing, got.missing[0]); try std.testing.expectError(error.InvalidCid, getRepoBlocks(a, account.did, &.{"nope"})); } test "record and blob takedowns hide the subject until lifted" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(a, "takedown.test", "takedown@test.com", "password", "did:plc:contenttakedown", true); const parsed = try std.json.parseFromSlice(std.json.Value, a, "{\"$type\":\"app.bsky.feed.post\",\"text\":\"hello\"}", .{}); const record = try create(a, account, "app.bsky.feed.post", "3jzfcijpj2z2a", parsed.value); const uri = "at://did:plc:contenttakedown/app.bsky.feed.post/3jzfcijpj2z2a"; try std.testing.expectError(error.SubjectNotFound, setRecordTakedown("at://did:plc:contenttakedown/app.bsky.feed.post/missing", true, "ref")); try setRecordTakedown(uri, true, "mod-1"); try std.testing.expect(get(a, account.did, "app.bsky.feed.post", "3jzfcijpj2z2a") == null); const status = (try recordTakedownStatus(a, uri)).?; try std.testing.expectEqualStrings(record.cid, status.cid); try std.testing.expect(status.takedown.applied); try std.testing.expectEqualStrings("mod-1", status.takedown.ref.?); try setRecordTakedown(uri, false, null); try std.testing.expect(get(a, account.did, "app.bsky.feed.post", "3jzfcijpj2z2a") != null); try std.testing.expect(!(try recordTakedownStatus(a, uri)).?.takedown.applied); const cid = "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku"; try std.testing.expect(try blobTakedown(a, account.did, cid) == null); try setBlobTakedown(account.did, cid, true, null); try std.testing.expect((try blobTakedown(a, account.did, cid)).?.applied); try std.testing.expectError(error.SubjectNotFound, setBlobTakedown("did:plc:nobody", cid, true, null)); try setBlobTakedown(account.did, cid, false, null); try std.testing.expect(try blobTakedown(a, account.did, cid) == null); try std.testing.expect(!(try accountAdminStatus(a, account.did)).?.takedown.applied); try std.testing.expect(try accountAdminStatus(a, "did:plc:nobody") == null); } test "batched member changes take the last entry and leave non-members alone" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "batch.test", "batch@test.com", "password", "did:plc:batchowner", true); const space = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.batch.space", .skey = "self", .is_authority = true, .policy = .{ .member = .{ .publish_policy = true, .publish_members = true } }, }); const before = (try getSpaceRepoState(a, space.uri, owner.did)).rev.?; try std.testing.expect((try changeSpaceMembers(a, space.uri, &.{ .{ .did = "did:plc:one", .access = .{ .read = true, .write = true } }, .{ .did = "did:plc:two", .access = .{ .read = true, .write = false } }, .{ .did = "did:plc:one", .access = .{ .read = true, .write = false } }, })).changed); try std.testing.expectEqual(@as(usize, 2), (try listSpaceMembers(a, space.uri, null, 100)).len); try std.testing.expect(!try spaceMemberAllows(space.uri, "did:plc:one", .write)); // One commit for the whole batch. const ops = try listSpaceRecordOplog(a, space.uri, owner.did, before, null, 100, false); var revs: usize = 0; for (ops, 0..) |op, idx| { if (idx == 0 or !std.mem.eql(u8, op.rev, ops[idx - 1].rev)) revs += 1; } try std.testing.expectEqual(@as(usize, 1), revs); const hash: [zat.space.lthash.digest_bytes]u8 = @splat(0); _ = (try recordSpaceWriter(a, space.uri, "did:plc:outsider", "3lx7abcdefg22", &hash)).?; try std.testing.expect((try changeSpaceMembers(a, space.uri, &.{ .{ .did = "did:plc:two", .access = null }, .{ .did = "did:plc:outsider", .access = null }, })).changed); try std.testing.expectEqual(@as(usize, 1), (try listSpaceMembers(a, space.uri, null, 100)).len); try std.testing.expectEqual(@as(usize, 1), (try listSpaceWriters(a, space.uri, null, 50)).len); try std.testing.expect(!(try changeSpaceMembers(a, space.uri, &.{.{ .did = "did:plc:nobody", .access = null }})).changed); } test "a seq floor starts sequencing above a previous host's cursor" { app_config.setSeqFloor(73716); defer app_config.setSeqFloor(0); try init(std.Options.debug_io, ":memory:"); defer close(); try std.testing.expectEqual(@as(u64, 73716), latestSequence()); db_mutex.lockUncancelable(store_io); defer db_mutex.unlock(store_io); try std.testing.expectEqual(@as(u64, 73717), try nextSeqLocked()); } test "an email token authorizes only the purpose it was issued for" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(a, "tokens.test", "tokens@test.com", "password", "did:plc:emailtokens", true); try setEmailToken(account.did, .confirm_email, "AAAAA-BBBBB", 2_000); try std.testing.expectEqual(CodeStatus.valid, validateEmailToken(account.did, .confirm_email, "aaaaa-bbbbb", 1_000)); try std.testing.expectEqual(CodeStatus.invalid, validateEmailToken(account.did, .plc_operation, "AAAAA-BBBBB", 1_000)); try std.testing.expectEqual(CodeStatus.invalid, validateEmailToken(account.did, .delete_account, "AAAAA-BBBBB", 1_000)); try std.testing.expectEqual(CodeStatus.expired, validateEmailToken(account.did, .confirm_email, "AAAAA-BBBBB", 2_000)); try std.testing.expectEqual(CodeStatus.invalid, validateEmailToken(account.did, .confirm_email, "CCCCC-DDDDD", 1_000)); try setEmailToken(account.did, .plc_operation, "EEEEE-FFFFF", 2_000); try confirmEmail(account.did); try std.testing.expectEqual(CodeStatus.invalid, validateEmailToken(account.did, .confirm_email, "AAAAA-BBBBB", 1_000)); try std.testing.expectEqual(CodeStatus.valid, validateEmailToken(account.did, .plc_operation, "EEEEE-FFFFF", 1_000)); try clearEmailToken(account.did, .plc_operation); try std.testing.expectEqual(CodeStatus.invalid, validateEmailToken(account.did, .plc_operation, "EEEEE-FFFFF", 1_000)); } test "email changes invalidate recovery codes atomically and invalid reset codes avoid hashing" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(a, "recovery.test", "old@test.com", "password", "did:plc:recovery", true); try std.testing.expectError(error.InvalidToken, changePassword(std.testing.failing_allocator, .{ .reset_token = "unknown" }, "password", "admin")); try setEmailToken(account.did, .reset_password, "AAAAA-BBBBB", nowMs() + 60_000); try conn.execNoArgs("CREATE TRIGGER reject_code_delete BEFORE DELETE ON email_tokens BEGIN SELECT RAISE(ABORT, 'rollback'); END"); if (updateEmail(account.did, "new@test.com")) |_| return error.ExpectedFailure else |_| {} try std.testing.expectEqualStrings("old@test.com", (try findAccount(a, account.did)).?.email); try std.testing.expectEqual(CodeStatus.valid, validateEmailToken(account.did, .reset_password, "AAAAA-BBBBB", nowMs())); try conn.execNoArgs("DROP TRIGGER reject_code_delete"); try updateEmail(account.did, "new@test.com"); try std.testing.expectEqualStrings("new@test.com", (try findAccount(a, account.did)).?.email); try std.testing.expectError(error.InvalidToken, changePassword(a, .{ .reset_token = "AAAAA-BBBBB" }, "stale", "admin")); try std.testing.expect(!try setPasswordResetToken(account.did, "old@test.com", "CCCCC-DDDDD", nowMs() + 60_000)); try std.testing.expect(try setPasswordResetToken(account.did, "NEW@test.com", "EEEEE-FFFFF", nowMs() + 60_000)); } test "the outbox keeps mail newest first, per address, with its code" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); try recordOutboxMail("one@test.com", "one.test", "Confirm email", "first", "AAAAA-BBBBB"); try recordOutboxMail("two@test.com", "two.test", "Welcome", "hello", null); try recordOutboxMail("ONE@test.com", "one.test", "PLC operation", "second", "CCCCC-DDDDD"); const mine = try listOutboxMail(a, "one@test.com", 10); try std.testing.expectEqual(@as(usize, 2), mine.len); try std.testing.expectEqualStrings("CCCCC-DDDDD", mine[0].code.?); try std.testing.expectEqualStrings("first", mine[1].text); try std.testing.expectEqual(@as(usize, 3), (try listOutboxMail(a, null, 10)).len); try std.testing.expect((try listOutboxMail(a, "two@test.com", 10))[0].code == null); } test "bare DIDs in managing apps become did#atproto_pds" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const owner = try createAccount(a, "refs.test", "refs@test.com", "password", "did:plc:servicerefs", true); const space = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.refs.space", .skey = "self", .is_authority = true, .policy = .{ .managed = .{ .managing_app = "did:web:app.test#app" } } }); const kept = try createSpace(a, .{ .actor_did = owner.did, .authority_did = owner.did, .space_type = "test.refs.kept", .skey = "self", .is_authority = true, .policy = .{ .managed = .{ .managing_app = "did:web:app.test#app" } } }); try conn.exec("UPDATE permissioned_spaces SET policy_json = ? WHERE uri = ?", .{ "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:app.test\",\"publishPolicy\":false}", space.uri }); try conn.execNoArgs("DELETE FROM zds_migrations WHERE name = 'service-refs-v1'"); try migrateServiceRefs(); const migrated = (try (try getSpace(a, owner.did, space.uri)).?.policy(a)).?; try std.testing.expectEqualStrings("did:web:app.test#atproto_pds", migrated.managed.managing_app); try std.testing.expectEqualStrings("did:web:app.test#app", (try (try getSpace(a, owner.did, kept.uri)).?.policy(a)).?.managed.managing_app); } test "a #sync frame's CAR is rooted at the commit" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); const account = try createAccount(a, "sync.test", "sync@test.com", "password", "did:plc:sync", true); try sequenceNewAccount(a, account); const row = (try conn.row("SELECT evt FROM seq_events WHERE did = ? ORDER BY seq DESC LIMIT 1", .{account.did})).?; defer row.deinit(); const event = (try zat.firehose.decodeFrame(a, row.blob(0))).sync; const commit = (try latestCommitRawLocked(a, account.did)).?; const commit_cid = try zat.cbor.Cid.fromString(a, commit.commit_cid_text); try std.testing.expectEqualStrings(account.did, event.did); try std.testing.expectEqualStrings(commit.rev, event.rev); const car = try zat.car.read(a, event.blocks); try std.testing.expectEqualSlices(u8, commit_cid.raw, car.roots[0].raw); try std.testing.expectEqualSlices(u8, commit_cid.raw, car.blocks[0].cid_raw); try std.testing.expectEqualSlices(u8, commit.commit_data, car.blocks[0].data); } test "record JSON keeps the data model's float ban" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); for ([_][]const u8{ "{\"n\":1.5}", "{\"a\":[{\"n\":-0.25}]}", "{\"n\":1e300}" }) |text| { const json = try std.json.parseFromSliceLeaky(std.json.Value, a, text, .{}); try std.testing.expectError(Error.InvalidDagCbor, recordValueFromJson(a, json)); } const whole = try recordValueFromJson(a, try std.json.parseFromSliceLeaky(std.json.Value, a, "{\"n\":1.0}", .{})); try std.testing.expectEqual(@as(u64, 1), whole.get("n").?.unsigned); } test "record JSON sentinels are strict" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); for ([_][]const u8{ "{\"$bytes\":\"AQIDBA==\"}", "{\"$bytes\":\"AQIDBA\"}" }) |text| { const value = try recordValueFromJson(a, try std.json.parseFromSliceLeaky(std.json.Value, a, text, .{})); try std.testing.expectEqualSlices(u8, &.{ 1, 2, 3, 4 }, value.bytes); } for ([_][]const u8{ "{\"$bytes\":\"AQIDBA=\"}", "{\"$bytes\":\"AQIDBA===\"}", "{\"$bytes\":4}", "{\"$link\":4}", "{\"$link\":\"bafynotacid\"}", "{\"n\":123456789012345678901234567890}", }) |text| { const json = try std.json.parseFromSliceLeaky(std.json.Value, a, text, .{}); try std.testing.expectError(Error.InvalidDagCbor, recordValueFromJson(a, json)); } } test "#account frames carry a status only for inactive accounts" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); const active = (try zat.firehose.decodeFrame(a, try accountEventFrame(a, 4, "did:plc:status", .active))).account; try std.testing.expect(active.active); try std.testing.expect(active.status == null); for ([_]AccountStatus{ .takendown, .suspended, .deactivated, .deleted }) |status| { const event = (try zat.firehose.decodeFrame(a, try accountEventFrame(a, 5, "did:plc:status", status))).account; try std.testing.expect(!event.active); try std.testing.expectEqualStrings(status.asString(), @tagName(event.status.?)); try std.testing.expectEqual(@as(i64, 5), event.seq); } } test "an authorization request keeps the browser sign-in that authenticated it" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); try init(std.Options.debug_io, ":memory:"); defer close(); try putOAuthRequest("req-signin", "https://app.example/client-metadata.json", "https://app.example/cb", "atproto", "s", "c", "S256", "query", null, null, clock.nowSeconds() + 600); try std.testing.expect((try getOAuthRequest(allocator, "req-signin")).?.signin_hash == null); try setOAuthSignIn("req-signin", "digest", "did:plc:signin", "passkey"); const signed_in = (try getOAuthRequest(allocator, "req-signin")).?; try std.testing.expectEqualStrings("digest", signed_in.signin_hash.?); try std.testing.expectEqualStrings("did:plc:signin", signed_in.signin_did.?); try std.testing.expectEqualStrings("passkey", signed_in.signin_method.?); } test "storage snapshots record sizes by hour" { try init(std.Options.debug_io, ":memory:"); defer close(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); _ = try createAccount(arena.allocator(), "alice.example.com", "a@example.com", "password", "did:plc:storagealice", true); const now = try storageSnapshot(); try std.testing.expect(now.db_bytes > 0); try std.testing.expectEqual(@as(u64, 1), now.accounts); try recordStorageSnapshot(500_000, now); try recordStorageSnapshot(500_000, now); const rows = try listStorageSnapshots(arena.allocator(), 500_000, 500_001); try std.testing.expectEqual(@as(usize, 1), rows.len); try std.testing.expectEqual(now.db_bytes, rows[0].db_bytes); }