╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
601 } 602 603 // the user as a commit author, or null unless they hold the key 604 fn authorWithKey( 605 moment: evt.AdminDB.HashMap(.read_only), 606 arena: *std.heap.ArenaAllocator, 607 user_id: []const u8, 608 fingerprint: *const [ssh.fingerprint_len]u8, 609 ) !?evt.CommitAuthor { 610 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse return null; 611 if (user.removed or !isKeyInAuthorizedKeys(user.event.ssh_keys, fingerprint)) return null; 612 return .{ .name = user.event.name, .email = user.event.email }; 613 } 614 615 fn isKeyInAuthorizedKeys(ssh_keys: []const u8, fingerprint: *const [ssh.fingerprint_len]u8) bool { 616 var it = std.mem.splitScalar(u8, ssh_keys, '\n'); 617 while (it.next()) |line| { 618 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 619 if (std.mem.eql(u8, &fp, fingerprint)) return true; 620 } 621 return false; 622 } 623 624 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 625 // blank or not a parseable "<type> <base64> [comment]" entry 626 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 627 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 628 _ = it.next() orelse return null; // key type 629 const blob_b64 = it.next() orelse return null; 630 631 const decoder = std.base64.standard.Decoder; 632 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 633 var blob_buf: [4096]u8 = undefined; 634 if (blob_len > blob_buf.len) return null; 635 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 636 637 return ssh.formatFingerprint(blob_buf[0..blob_len]); 638 } 639 640 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 641 const body = "ERR haxy ssh: " ++ msg; 642 var packet: [body.len + 4]u8 = undefined; 643 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 644 try sess.writeBytes(line); 645 try sess.exit(1); 646 } 647 648 test "parseGitCommand rejects malformed input" { 649 const allocator = std.testing.allocator; 650 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 651 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 652 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 653 } 654 655 test "parseGitCommand happy paths" { 656 const allocator = std.testing.allocator; 657 658 { 659 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 660 defer parsed.deinit(allocator); 661 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 662 try std.testing.expectEqualStrings("some-repo", parsed.dir); 663 } 664 { 665 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 666 defer parsed.deinit(allocator); 667 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 668 try std.testing.expectEqualStrings("user/proj", parsed.dir); 669 } 670 { 671 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 672 defer parsed.deinit(allocator); 673 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 674 try std.testing.expectEqualStrings("repo", parsed.dir); 675 } 676 } 677