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