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