╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
625 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 626 if (std.mem.eql(u8, &fp, fingerprint)) return true; 627 } 628 return false; 629 } 630 631 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 632 // blank or not a parseable "<type> <base64> [comment]" entry 633 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 634 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 635 _ = it.next() orelse return null; // key type 636 const blob_b64 = it.next() orelse return null; 637 638 const decoder = std.base64.standard.Decoder; 639 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 640 var blob_buf: [4096]u8 = undefined; 641 if (blob_len > blob_buf.len) return null; 642 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 643 644 return ssh.formatFingerprint(blob_buf[0..blob_len]); 645 } 646 647 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 648 const body = "ERR haxy ssh: " ++ msg; 649 var packet: [body.len + 4]u8 = undefined; 650 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 651 try sess.writeBytes(line); 652 try sess.exit(1); 653 } 654 655 test "parseGitCommand rejects malformed input" { 656 const allocator = std.testing.allocator; 657 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 658 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 659 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 660 } 661 662 test "parseGitCommand happy paths" { 663 const allocator = std.testing.allocator; 664 665 { 666 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 667 defer parsed.deinit(allocator); 668 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 669 try std.testing.expectEqualStrings("some-repo", parsed.dir); 670 } 671 { 672 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 673 defer parsed.deinit(allocator); 674 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 675 try std.testing.expectEqualStrings("user/proj", parsed.dir); 676 } 677 { 678 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 679 defer parsed.deinit(allocator); 680 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 681 try std.testing.expectEqualStrings("repo", parsed.dir); 682 } 683 } 684