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