╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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