const std = @import("std"); const hegel = @import("hegel"); const zds = @import("zds"); const permissions = zds.atproto.permission_sets; const store = zds.storage.store; test "campaign MIME signatures override headers and arbitrary prefixes remain bounded" { try hegel.run(.{ .key = "zds-mime-prefix" }, {}, mimeProperty); } fn mimeProperty(_: void, case: *hegel.TestCase) !void { var buffer: [4100]u8 = undefined; const arbitrary = try case.bytes(&buffer, 0, buffer.len); const fallback = if (try case.boolean()) "text/plain" else "image/png"; const result = zds.internal.blob_mime.detect(arbitrary, fallback); if (result.len == 0) return case.fail("empty MIME result"); const signatures = [_][]const u8{ "\x89PNG\r\n\x1a\n", "\xff\xd8\xff", "GIF89a", "fLaC", "\x00\x00\x00\x18ftypisom", "RIFF\x00\x00\x00\x00WEBP" }; const expected = [_][]const u8{ "image/png", "image/jpeg", "image/gif", "audio/flac", "video/mp4", "image/webp" }; const index = try case.index(signatures.len); const signature = signatures[index]; const length = try case.int(usize, signature.len, buffer.len); @memset(&buffer, 0xa5); @memcpy(buffer[0..signature.len], signature); if (!std.mem.eql(u8, expected[index], zds.internal.blob_mime.detect(buffer[0..length], fallback))) return case.fail("signature did not override claimed MIME"); case.event("recognized signature with untrusted header"); const truncated = try case.int(usize, 0, signature.len - 1); _ = zds.internal.blob_mime.detect(buffer[0..truncated], fallback); } const ScopeRule = enum { declare, check, refresh, malformed }; var scope_tally: hegel.Tally(ScopeRule) = .{}; test "campaign dynamic grants track declarations while explicit grants remain fixed" { defer scope_tally.print(); try hegel.run(.{ .key = "zds-dynamic-collections" }, {}, scopeProperty); } fn scopeProperty(_: void, case: *hegel.TestCase) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); defer permissions.testing.clearCache(); scope_tally.case(); const did = "did:plc:fuzz"; const space_type = "test.fuzz.space"; const collections = [_][]const u8{ "test.fuzz.first", "test.fuzz.second" }; var declared = [_]bool{ try case.boolean(), try case.boolean() }; var grant = try permissions.prepareScopes(a, "atproto space:test.fuzz.space", did); const explicit = try permissions.prepareScopes(a, "space:test.fuzz.space?collection=test.fuzz.first", did); var machine = try case.machine(ScopeRule, null, 24); defer machine.deinit(); while (try machine.next()) |rule| { scope_tally.step(rule); switch (rule) { .declare => { const index = try case.index(2); declared[index] = !declared[index]; case.event("declaration changed for existing grant"); }, .refresh => { grant = try permissions.prepareScopes(a, grant, did); if (std.mem.indexOf(u8, grant, "collection=") != null) return case.fail("refresh froze defaults"); }, .malformed => { try permissions.testing.cacheDeclaration(space_type, "{}"); if (permissions.resolveSpaceWriteScopes(a, grant, space_type)) |_| return case.fail("invalid declaration accepted") else |err| if (err != error.InvalidSpaceTypeDeclaration) return err; const fixed = try permissions.resolveSpaceWriteScopes(a, explicit, space_type); if (!zds.internal.scopes.spaceAllows(fixed, .create, space_type, did, "self", collections[0])) return case.fail("explicit grant depended on declaration"); }, .check => {}, } const body = try std.fmt.allocPrint(a, "{{\"value\":{{\"defs\":{{\"main\":{{\"type\":\"space\",\"collections\":[{s}{s}{s}]}}}}}}}}", .{ if (declared[0]) "\"test.fuzz.first\"" else "", if (declared[0] and declared[1]) "," else "", if (declared[1]) "\"test.fuzz.second\"" else "", }); try permissions.testing.cacheDeclaration(space_type, body); const dynamic = try permissions.resolveSpaceWriteScopes(a, grant, space_type); const fixed = try permissions.resolveSpaceWriteScopes(a, explicit, space_type); for (collections, 0..) |collection, i| { inline for (.{ .create, .update, .delete }) |action| { if (zds.internal.scopes.spaceAllows(dynamic, action, space_type, did, "self", collection) != declared[i]) return case.fail("dynamic write differs from declaration model"); if (zds.internal.scopes.spaceAllows(fixed, action, space_type, did, "self", collection) != (i == 0)) return case.fail("explicit grant changed"); if (zds.internal.scopes.spaceAllows(dynamic, action, space_type, "did:plc:other", "self", collection)) return case.fail("foreign authority accepted"); } } if (!zds.internal.scopes.spaceAllows(dynamic, .read_self, space_type, did, "self", null)) return case.fail("read depended on collections"); } scope_tally.finish(@intFromBool(declared[0]) + @as(u64, @intFromBool(declared[1]))); } const PasswordRule = enum { issue, reset, email, expire }; var password_tally: hegel.Tally(PasswordRule) = .{}; test "campaign password recovery histories consume codes and bind them to the current inbox" { defer password_tally.print(); try hegel.run(.{ .key = "zds-password-recovery" }, {}, passwordProperty); } fn passwordProperty(_: void, case: *hegel.TestCase) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try store.init(std.Options.debug_io, ":memory:"); defer store.close(); const did = "did:plc:fuzzpassword"; const emails = [_][]const u8{ "first@test.com", "second@test.com" }; const codes = [_][]const u8{ "AAAAA-BBBBB", "CCCCC-DDDDD", "EEEEE-FFFFF" }; var email: usize = try case.index(2); var password: []const u8 = "initial"; _ = try store.createAccount(a, "fuzz.test", emails[email], password, did, true); var pending: ?usize = null; var expired = false; password_tally.case(); const initial_code = try case.index(2); try std.testing.expect(try store.setPasswordResetToken(did, emails[email], codes[initial_code], store.nowMs() + 60_000)); password_tally.step(.issue); email = 1 - email; try store.updateEmail(did, emails[email]); password_tally.step(.email); if (store.changePassword(a, .{ .reset_token = codes[initial_code] }, "stale-inbox", did)) |_| return case.fail("old inbox code survived email change") else |err| if (err != error.InvalidToken) return err; password_tally.step(.reset); case.event("stale inbox code refused"); var machine = try case.machine(PasswordRule, null, 12); defer machine.deinit(); while (try machine.next()) |rule| { password_tally.step(rule); switch (rule) { .issue => { const index = try case.index(2); const stale = try case.boolean(); const issued = try store.setPasswordResetToken(did, emails[if (stale) 1 - email else email], codes[index], store.nowMs() + 60_000); if (issued == stale) return case.fail("code issuance did not bind current inbox"); if (issued) { pending = index; expired = false; } }, .email => { email = 1 - email; try store.updateEmail(did, emails[email]); if (pending != null) case.event("email changed with pending reset"); pending = null; }, .expire => { if (pending) |index| { try store.setEmailToken(did, .reset_password, codes[index], 1); expired = true; } }, .reset => { const index = try case.index(codes.len); const expected: ?anyerror = if (pending != index) error.InvalidToken else if (expired) error.ExpiredToken else null; if (store.changePassword(a, .{ .reset_token = codes[index] }, codes[index], did)) |_| { if (expected != null) return case.fail("invalid or expired reset changed password"); password = codes[index]; pending = null; case.event("valid reset consumed"); } else |err| { if (expected == null or err != expected.?) return err; } }, } } const account = (try store.findAccount(a, did)).?; if (!zds.auth.tokens.passwordMatches(account, password)) return case.fail("password differs from recovery model"); password_tally.finish(@intFromBool(pending != null)); }