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