╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
579 return .{ .create = .{ .owner_id = owner_id, .owner = try commitAuthor(moment, arena, &owner_id) } }; 580 }; 581 582 const min_role: evt.Repo.Role = switch (service) { 583 .upload_pack => .read, 584 .receive_pack => .write, 585 }; 586 // skip the key lookups when the base role is enough 587 if (repo.role) |base| { 588 if (base.atLeast(min_role)) return .{ .allowed = null }; 589 } 590 591 // a key can be shared, so a holder whose role falls short doesn't end the search 592 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 593 for (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, fingerprint)) |holder_id| { 594 const seen = (try evt.readRepoById(io, allocator, arena, users_dir, &owner_id, &repo.event_id, holder_id)) orelse return .not_found; 595 const role = seen.role orelse continue; 596 if (role.atLeast(min_role)) return .{ .allowed = try commitAuthor(moment, arena, &holder_id) }; 597 } 598 return .denied; 599 } 600 601 // an active user as a commit author 602 fn commitAuthor( 603 moment: evt.AdminDB.HashMap(.read_only), 604 arena: *std.heap.ArenaAllocator, 605 user_id: *const [evt.event_id_size]u8, 606 ) !evt.CommitAuthor { 607 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 608 return .{ .name = user.event.name, .email = user.event.email }; 609 } 610 611 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 612 const body = "ERR haxy ssh: " ++ msg; 613 var packet: [body.len + 4]u8 = undefined; 614 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 615 try sess.writeBytes(line); 616 try sess.exit(1); 617 } 618 619 test "parseGitCommand rejects malformed input" { 620 const allocator = std.testing.allocator; 621 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 622 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 623 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 624 } 625 626 test "parseGitCommand happy paths" { 627 const allocator = std.testing.allocator; 628 629 { 630 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 631 defer parsed.deinit(allocator); 632 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 633 try std.testing.expectEqualStrings("some-repo", parsed.dir); 634 } 635 { 636 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 637 defer parsed.deinit(allocator); 638 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 639 try std.testing.expectEqualStrings("user/proj", parsed.dir); 640 } 641 { 642 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 643 defer parsed.deinit(allocator); 644 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 645 try std.testing.expectEqualStrings("repo", parsed.dir); 646 } 647 } 648