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