╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
625 const blob_b64 = it.next() orelse return null; 626 627 const decoder = std.base64.standard.Decoder; 628 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 629 var blob_buf: [4096]u8 = undefined; 630 if (blob_len > blob_buf.len) return null; 631 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 632 633 return ssh.formatFingerprint(blob_buf[0..blob_len]); 634 } 635 636 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 637 const body = "ERR haxy ssh: " ++ msg; 638 var packet: [body.len + 4]u8 = undefined; 639 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 640 try sess.writeBytes(line); 641 try sess.exit(1); 642 } 643 644 test "parseGitCommand rejects malformed input" { 645 const allocator = std.testing.allocator; 646 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 647 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 648 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 649 } 650 651 test "parseGitCommand happy paths" { 652 const allocator = std.testing.allocator; 653 654 { 655 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 656 defer parsed.deinit(allocator); 657 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 658 try std.testing.expectEqualStrings("some-repo", parsed.dir); 659 } 660 { 661 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 662 defer parsed.deinit(allocator); 663 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 664 try std.testing.expectEqualStrings("user/proj", parsed.dir); 665 } 666 { 667 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 668 defer parsed.deinit(allocator); 669 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 670 try std.testing.expectEqualStrings("repo", parsed.dir); 671 } 672 } 673