//! Space policies: an open union discriminated by `$type`. A policy is stored //! normalized, with defaults filled and unknown fields dropped, so what is //! stored is exactly what is enforced. const std = @import("std"); const zat = @import("zat"); pub const personalspace_type = "com.atproto.personalspace.policy"; pub const memberspace_type = "com.atproto.memberspace.policy"; pub const managedspace_type = "com.atproto.managedspace.policy"; pub const policy_collection = "com.atproto.space.policy"; pub const policy_rkey = "self"; pub const member_collection = "com.atproto.memberspace.member"; /// The collections the host writes from a space's configuration. pub const published_collections = [_][]const u8{ policy_collection, member_collection }; pub const Policy = union(enum) { personal, member: Memberspace, managed: Managedspace, }; pub const Memberspace = struct { public_read: bool = false, public_write: bool = false, allowed_apps: ?[]const []const u8 = null, publish_policy: bool = false, publish_members: bool = false, }; pub const Managedspace = struct { managing_app: []const u8, publish_policy: bool = false, }; pub const Error = error{ UnsupportedPolicy, InvalidPolicy, OutOfMemory }; pub fn fromJson(allocator: std.mem.Allocator, value: std.json.Value) Error!Policy { const object = switch (value) { .object => |object| object, else => return error.InvalidPolicy, }; const kind = switch (object.get("$type") orelse return error.InvalidPolicy) { .string => |text| text, else => return error.InvalidPolicy, }; if (std.mem.eql(u8, kind, personalspace_type)) return .personal; if (std.mem.eql(u8, kind, memberspace_type)) return .{ .member = .{ .public_read = try optionalBool(object, "publicRead"), .public_write = try optionalBool(object, "publicWrite"), .allowed_apps = try optionalStrings(allocator, object, "allowedApps"), .publish_policy = try optionalBool(object, "publishPolicy"), .publish_members = try optionalBool(object, "publishMembers"), } }; if (std.mem.eql(u8, kind, managedspace_type)) { const managing_app = switch (object.get("managingApp") orelse return error.InvalidPolicy) { .string => |text| text, else => return error.InvalidPolicy, }; if (!validServiceRef(managing_app)) return error.InvalidPolicy; return .{ .managed = .{ .managing_app = try allocator.dupe(u8, managing_app), .publish_policy = try optionalBool(object, "publishPolicy"), } }; } return error.UnsupportedPolicy; } /// A policy from a request or an imported archive. Beyond fromJson, a /// memberspace's `allowedApps` must be URIs, as the lexicon types the OAuth /// client IDs it holds. Stored policies are read with parseStored, which does /// not re-check them. pub fn fromInput(allocator: std.mem.Allocator, value: std.json.Value) Error!Policy { const policy = try fromJson(allocator, value); switch (policy) { .member => |member| for (member.allowed_apps orelse &.{}) |app| { _ = std.Uri.parse(app) catch return error.InvalidPolicy; }, else => {}, } return policy; } pub fn parseStored(allocator: std.mem.Allocator, text: []const u8) Error!Policy { const parsed = std.json.parseFromSlice(std.json.Value, allocator, text, .{}) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => return error.InvalidPolicy, }; defer parsed.deinit(); return fromJson(allocator, parsed.value); } pub fn toJson(allocator: std.mem.Allocator, policy: Policy) error{OutOfMemory}![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); writeJson(&out.writer, policy) catch return error.OutOfMemory; return out.toOwnedSlice(); } fn writeJson(writer: *std.Io.Writer, policy: Policy) std.Io.Writer.Error!void { switch (policy) { .personal => try writer.writeAll("{\"$type\":\"" ++ personalspace_type ++ "\"}"), .member => |member| { try writer.print("{{\"$type\":\"" ++ memberspace_type ++ "\",\"publicRead\":{},\"publicWrite\":{}", .{ member.public_read, member.public_write }); if (member.allowed_apps) |apps| { try writer.writeAll(",\"allowedApps\":["); for (apps, 0..) |app, idx| { if (idx != 0) try writer.writeByte(','); try writer.print("{f}", .{std.json.fmt(app, .{})}); } try writer.writeByte(']'); } try writer.print(",\"publishPolicy\":{},\"publishMembers\":{}}}", .{ member.publish_policy, member.publish_members }); }, .managed => |managed| try writer.print( "{{\"$type\":\"" ++ managedspace_type ++ "\",\"managingApp\":{f},\"publishPolicy\":{}}}", .{ std.json.fmt(managed.managing_app, .{}), managed.publish_policy }, ), } } pub fn policyRecordJson(allocator: std.mem.Allocator, policy: Policy) error{OutOfMemory}![]const u8 { return std.fmt.allocPrint(allocator, "{{\"$type\":\"" ++ policy_collection ++ "\",\"policy\":{s}}}", .{try toJson(allocator, policy)}); } pub fn memberRecordJson(allocator: std.mem.Allocator, read: bool, write: bool) error{OutOfMemory}![]const u8 { return std.fmt.allocPrint(allocator, "{{\"$type\":\"" ++ member_collection ++ "\",\"read\":{},\"write\":{}}}", .{ read, write }); } pub fn sameType(a: Policy, b: Policy) bool { return std.meta.activeTag(a) == std.meta.activeTag(b); } /// Only a public-read memberspace can issue a credential without a delegation /// token saying who the user is. pub fn isPublicRead(policy: Policy) bool { return policy == .member and policy.member.public_read; } pub fn isPolicyPublished(policy: Policy) bool { return switch (policy) { .personal => false, .member => |member| member.publish_policy, .managed => |managed| managed.publish_policy, }; } pub fn areMembersPublished(policy: Policy) bool { return policy == .member and policy.member.publish_members; } /// Only memberspace restricts apps, judged by the attested client_id, so a /// request without a client attestation is never one of the allowed apps. pub fn isAppAuthorized(policy: Policy, client_id: ?[]const u8) bool { if (policy != .member) return true; const apps = policy.member.allowed_apps orelse return true; const id = client_id orelse return false; for (apps) |app| { if (std.mem.eql(u8, app, id)) return true; } return false; } pub fn isPublishedCollection(collection: []const u8) bool { for (published_collections) |published| { if (std.mem.eql(u8, collection, published)) return true; } return false; } /// A record key can't contain `%`, so a did:web's percent-encoded port is /// keyed unencoded (did:web:localhost%3A2583 -> did:web:localhost:2583), as /// the reference PDS does until Lexicon has a DID record-key format. pub fn didToRecordKey(allocator: std.mem.Allocator, did: []const u8) error{OutOfMemory}![]const u8 { const idx = std.mem.indexOf(u8, did, "%3A") orelse return allocator.dupe(u8, did); return std.fmt.allocPrint(allocator, "{s}:{s}", .{ did[0..idx], did[idx + 3 ..] }); } /// The inverse of didToRecordKey: `did:web:localhost:2583` -> `did:web:localhost%3A2583`. pub fn recordKeyToDid(allocator: std.mem.Allocator, rkey: []const u8) error{OutOfMemory}![]const u8 { const prefix = "did:web:"; if (std.mem.startsWith(u8, rkey, prefix)) { const rest = rkey[prefix.len..]; if (std.mem.indexOfScalar(u8, rest, ':')) |colon| { const port = rest[colon + 1 ..]; const digits = port.len > 0 and for (port) |c| { if (!std.ascii.isDigit(c)) break false; } else true; if (colon > 0 and digits) return std.fmt.allocPrint(allocator, "{s}{s}%3A{s}", .{ prefix, rest[0..colon], port }); } } return allocator.dupe(u8, rkey); } /// A `did#serviceId` reference, e.g. `did:web:example.com#forum`. pub fn validServiceRef(value: []const u8) bool { const hash = std.mem.indexOfScalar(u8, value, '#') orelse return false; const did = value[0..hash]; if (zat.Did.parse(did) == null) return false; const atproto_did = std.mem.startsWith(u8, did, "did:plc:") or (std.mem.startsWith(u8, did, "did:web:") and std.mem.indexOfScalar(u8, did["did:web:".len..], ':') == null); if (!atproto_did) return false; const fragment = value[hash + 1 ..]; return fragment.len > 0 and std.mem.indexOfScalar(u8, fragment, '#') == null; } /// The October 22 simplespace configuration, as zds stored it. pub const Legacy = struct { read_policy: []const u8, read_managing_app: ?[]const u8, write_policy: []const u8, write_managing_app: ?[]const u8, app_access_json: []const u8, }; /// A managing app on either side becomes a managedspace, dropping any app /// allow-list; anything else becomes a memberspace. Matches the reference /// PDS's `006-space-policy` migration. pub fn fromLegacy(allocator: std.mem.Allocator, legacy: Legacy) Error!Policy { const read_app = if (std.mem.eql(u8, legacy.read_policy, "managing-app")) legacy.read_managing_app else null; const write_app = if (std.mem.eql(u8, legacy.write_policy, "managing-app")) legacy.write_managing_app else null; if (read_app orelse write_app) |managing_app| { return .{ .managed = .{ .managing_app = try allocator.dupe(u8, managing_app) } }; } return .{ .member = .{ .public_read = std.mem.eql(u8, legacy.read_policy, "public"), .public_write = std.mem.eql(u8, legacy.write_policy, "public"), .allowed_apps = try legacyAllowedApps(allocator, legacy.app_access_json), } }; } fn legacyAllowedApps(allocator: std.mem.Allocator, raw: []const u8) Error!?[]const []const u8 { const parsed = std.json.parseFromSlice(std.json.Value, allocator, raw, .{}) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => return error.InvalidPolicy, }; defer parsed.deinit(); const object = switch (parsed.value) { .object => |object| object, else => return error.InvalidPolicy, }; const kind = switch (object.get("type") orelse return error.InvalidPolicy) { .string => |text| text, else => return error.InvalidPolicy, }; if (std.mem.eql(u8, kind, "open")) return null; if (!std.mem.eql(u8, kind, "allowList")) return error.InvalidPolicy; return (try optionalStrings(allocator, object, "allowed")) orelse error.InvalidPolicy; } fn optionalBool(object: std.json.ObjectMap, key: []const u8) Error!bool { return switch (object.get(key) orelse return false) { .bool => |value| value, else => error.InvalidPolicy, }; } fn optionalStrings(allocator: std.mem.Allocator, object: std.json.ObjectMap, key: []const u8) Error!?[]const []const u8 { const items = switch (object.get(key) orelse return null) { .array => |array| array.items, else => return error.InvalidPolicy, }; const out = try allocator.alloc([]const u8, items.len); for (items, 0..) |item, idx| { out[idx] = switch (item) { .string => |text| try allocator.dupe(u8, text), else => return error.InvalidPolicy, }; } return out; } fn parseValue(allocator: std.mem.Allocator, text: []const u8) !std.json.Value { return (try std.json.parseFromSliceLeaky(std.json.Value, allocator, text, .{})); } test "policies normalize with defaults filled and unknown fields dropped" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); const member = try fromJson(a, try parseValue(a, "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true,\"extra\":1}")); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true,\"publicWrite\":false,\"publishPolicy\":false,\"publishMembers\":false}", try toJson(a, member), ); const apps = try fromJson(a, try parseValue(a, "{\"$type\":\"com.atproto.memberspace.policy\",\"allowedApps\":[\"https://a.test/c.json\"]}")); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":false,\"publicWrite\":false,\"allowedApps\":[\"https://a.test/c.json\"],\"publishPolicy\":false,\"publishMembers\":false}", try toJson(a, apps), ); const personal = try fromJson(a, try parseValue(a, "{\"$type\":\"com.atproto.personalspace.policy\",\"publicRead\":true}")); try std.testing.expectEqualStrings("{\"$type\":\"com.atproto.personalspace.policy\"}", try toJson(a, personal)); const managed = try fromJson(a, try parseValue(a, "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:forum.test#forum\",\"publicRead\":true}")); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:forum.test#forum\",\"publishPolicy\":false}", try toJson(a, managed), ); for ([_][]const u8{ "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true}", "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:forum.test#forum\",\"publishPolicy\":true}", "{\"$type\":\"com.atproto.personalspace.policy\"}", }) |text| { const stored = try toJson(a, try fromJson(a, try parseValue(a, text))); try std.testing.expectEqualStrings(stored, try toJson(a, try parseStored(a, stored))); } } test "input policies name allowed apps by URI" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); const bad = try parseValue(a, "{\"$type\":\"com.atproto.memberspace.policy\",\"allowedApps\":[\"not a uri\"]}"); try std.testing.expectError(error.InvalidPolicy, fromInput(a, bad)); _ = try fromJson(a, bad); _ = try fromInput(a, try parseValue(a, "{\"$type\":\"com.atproto.memberspace.policy\",\"allowedApps\":[\"https://a.test/c.json\",\"http://localhost\"]}")); } test "policies reject unknown types and malformed fields" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try std.testing.expectError(error.UnsupportedPolicy, fromJson(a, try parseValue(a, "{\"$type\":\"com.example.policy\"}"))); try std.testing.expectError(error.UnsupportedPolicy, fromJson(a, try parseValue(a, "{\"$type\":\"com.atproto.simplespace.defs#publicPolicy\"}"))); for ([_][]const u8{ "{}", "\"com.atproto.personalspace.policy\"", "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":\"yes\"}", "{\"$type\":\"com.atproto.memberspace.policy\",\"allowedApps\":[1]}", "{\"$type\":\"com.atproto.managedspace.policy\"}", "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"https://forum.test\"}", "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:forum.test#\"}", }) |text| { try std.testing.expectError(error.InvalidPolicy, fromJson(a, try parseValue(a, text))); } } test "policy predicates follow the baseline policies" { const personal: Policy = .personal; const open: Policy = .{ .member = .{ .public_read = true, .publish_members = true } }; const gated: Policy = .{ .member = .{ .allowed_apps = &.{"https://a.test/c.json"}, .publish_policy = true } }; const managed: Policy = .{ .managed = .{ .managing_app = "did:web:forum.test#forum", .publish_policy = true } }; try std.testing.expect(isPublicRead(open) and !isPublicRead(gated) and !isPublicRead(personal) and !isPublicRead(managed)); try std.testing.expect(!isPolicyPublished(personal) and !isPolicyPublished(open) and isPolicyPublished(gated) and isPolicyPublished(managed)); try std.testing.expect(areMembersPublished(open) and !areMembersPublished(gated) and !areMembersPublished(managed)); try std.testing.expect(isAppAuthorized(open, null) and isAppAuthorized(managed, null) and isAppAuthorized(personal, null)); try std.testing.expect(!isAppAuthorized(gated, null) and !isAppAuthorized(gated, "https://b.test/c.json")); try std.testing.expect(isAppAuthorized(gated, "https://a.test/c.json")); try std.testing.expect(sameType(open, gated) and !sameType(open, managed)); } test "legacy simplespace configurations map like the reference migration" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); const allow = "{\"type\":\"allowList\",\"allowed\":[\"https://a.test/c.json\"]}"; const cases = [_]struct { legacy: Legacy, expected: []const u8 }{ .{ .legacy = .{ .read_policy = "managing-app", .read_managing_app = "did:web:m.test#app", .write_policy = "member-list", .write_managing_app = null, .app_access_json = allow }, .expected = "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:m.test#app\",\"publishPolicy\":false}" }, .{ .legacy = .{ .read_policy = "public", .read_managing_app = null, .write_policy = "managing-app", .write_managing_app = "did:web:m.test", .app_access_json = "{\"type\":\"open\"}" }, .expected = "{\"$type\":\"com.atproto.managedspace.policy\",\"managingApp\":\"did:web:m.test\",\"publishPolicy\":false}" }, .{ .legacy = .{ .read_policy = "public", .read_managing_app = null, .write_policy = "member-list", .write_managing_app = null, .app_access_json = "{\"type\":\"open\"}" }, .expected = "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":true,\"publicWrite\":false,\"publishPolicy\":false,\"publishMembers\":false}" }, .{ .legacy = .{ .read_policy = "member-list", .read_managing_app = null, .write_policy = "public", .write_managing_app = null, .app_access_json = allow }, .expected = "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":false,\"publicWrite\":true,\"allowedApps\":[\"https://a.test/c.json\"],\"publishPolicy\":false,\"publishMembers\":false}" }, .{ .legacy = .{ .read_policy = "member-list", .read_managing_app = "did:web:stale.test", .write_policy = "member-list", .write_managing_app = null, .app_access_json = "{\"type\":\"allowList\",\"allowed\":[]}" }, .expected = "{\"$type\":\"com.atproto.memberspace.policy\",\"publicRead\":false,\"publicWrite\":false,\"allowedApps\":[],\"publishPolicy\":false,\"publishMembers\":false}" }, }; for (cases) |case| { try std.testing.expectEqualStrings(case.expected, try toJson(a, try fromLegacy(a, case.legacy))); } } test "published record shapes and did record keys" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); try std.testing.expectEqualStrings( "{\"$type\":\"com.atproto.space.policy\",\"policy\":{\"$type\":\"com.atproto.personalspace.policy\"}}", try policyRecordJson(a, .personal), ); try std.testing.expectEqualStrings("{\"$type\":\"com.atproto.memberspace.member\",\"read\":true,\"write\":false}", try memberRecordJson(a, true, false)); try std.testing.expectEqualStrings("did:web:localhost:2583", try didToRecordKey(a, "did:web:localhost%3A2583")); try std.testing.expectEqualStrings("did:plc:abc", try didToRecordKey(a, "did:plc:abc")); try std.testing.expectEqualStrings("did:web:localhost%3A2583", try recordKeyToDid(a, "did:web:localhost:2583")); try std.testing.expectEqualStrings("did:web:example.com", try recordKeyToDid(a, "did:web:example.com")); try std.testing.expectEqualStrings("did:plc:abc", try recordKeyToDid(a, "did:plc:abc")); try std.testing.expect(zat.Rkey.parse(try didToRecordKey(a, "did:web:localhost%3A2583")) != null); try std.testing.expect(isPublishedCollection(policy_collection) and isPublishedCollection(member_collection)); try std.testing.expect(!isPublishedCollection("com.example.note")); } test "service references name a service on an atproto DID" { try std.testing.expect(validServiceRef("did:plc:abc123#atproto_pds")); try std.testing.expect(validServiceRef("did:web:app.example#forum")); try std.testing.expect(validServiceRef("did:web:localhost%3A8080#forum")); try std.testing.expect(!validServiceRef("did:web:app.example")); try std.testing.expect(!validServiceRef("did:web:app.example:path#forum")); try std.testing.expect(!validServiceRef("did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme#svc")); try std.testing.expect(!validServiceRef("did:plc:abc123#")); }