╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
603 604 // a key can be shared, so the push acts as the holder with the highest role 605 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 606 var best: ?struct { id: [evt.event_id_size]u8, role: evt.Repo.Role } = null; 607 for (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, fingerprint)) |holder_id| { 608 const seen = (try evt.readRepoById(io, allocator, arena, users_dir, &owner_id, &repo.event_id, holder_id)) orelse return .not_found; 609 const role = seen.role orelse continue; 610 if (best) |held| if (held.role.atLeast(role)) continue; 611 best = .{ .id = holder_id, .role = role }; 612 } 613 const holder = best orelse return .denied; 614 if (!holder.role.atLeast(min_role)) return .denied; 615 return .{ 616 .allowed = switch (service) { 617 .upload_pack => .fetch, 618 .receive_pack => blk: { 619 const names = try repo.repo.event.protectedBranchNames(arena.allocator()); 620 // members change protected branches only by merging a patch 621 const owner = holder.role == .owner; 622 break :blk .{ .push = .{ 623 .user_id = holder.id, 624 .author = try commitAuthor(moment, arena, &holder.id), 625 .protected_branches = if (owner) names else &.{}, 626 .locked_branches = if (owner) &.{} else names, 627 } }; 628 }, 629 }, 630 }; 631 } 632 633 // an active user as a commit author 634 fn commitAuthor( 635 moment: evt.AdminDB.HashMap(.read_only), 636 arena: *std.heap.ArenaAllocator, 637 user_id: *const [evt.event_id_size]u8, 638 ) !evt.CommitAuthor { 639 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 640 return .{ .name = user.event.name, .email = user.event.email }; 641 } 642 643 // the authorized_keys line of a pubkey blob: its type, then the blob in base64 644 fn authorizedKeyLine(allocator: std.mem.Allocator, blob: []const u8) ![]const u8 { 645 var reader = std.Io.Reader.fixed(blob); 646 const key_type = try ssh.takeString(&reader, 64); 647 const encoder = std.base64.standard.Encoder; 648 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 649 @memcpy(line[0..key_type.len], key_type); 650 line[key_type.len] = ' '; 651 _ = encoder.encode(line[key_type.len + 1 ..], blob); 652 return line; 653 } 654 655 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 656 const body = "ERR haxy ssh: " ++ msg; 657 var packet: [body.len + 4]u8 = undefined; 658 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 659 try sess.writeBytes(line); 660 try sess.exit(1); 661 } 662 663 test "parseGitCommand rejects malformed input" { 664 const allocator = std.testing.allocator; 665 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 666 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 667 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 668 } 669 670 test "parseGitCommand happy paths" { 671 const allocator = std.testing.allocator; 672 673 { 674 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 675 defer parsed.deinit(allocator); 676 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 677 try std.testing.expectEqualStrings("some-repo", parsed.dir); 678 } 679 { 680 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 681 defer parsed.deinit(allocator); 682 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 683 try std.testing.expectEqualStrings("user/proj", parsed.dir); 684 } 685 { 686 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 687 defer parsed.deinit(allocator); 688 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 689 try std.testing.expectEqualStrings("repo", parsed.dir); 690 } 691 } 692