╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
595 ) !?UserWithKey { 596 if (try userWithKey(moment, arena, repo.user_id, fingerprint)) |user| return user; 597 598 for ([_][]const u8{ repo.owner_user_ids, repo.write_user_ids, repo.read_user_ids }) |user_ids| { 599 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 600 while (lines.next()) |id| { 601 const user_id = try evt.parseEventId(id); 602 if (try userWithKey(moment, arena, &user_id, fingerprint)) |user| return user; 603 } 604 } 605 return null; 606 } 607 608 fn userWithKey( 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 ) !?UserWithKey { 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 .{ 617 .id = user_id[0..evt.event_id_size].*, 618 .author = .{ .name = user.event.name, .email = user.event.email }, 619 }; 620 } 621 622 fn isKeyInAuthorizedKeys(ssh_keys: []const u8, fingerprint: *const [ssh.fingerprint_len]u8) bool { 623 var it = std.mem.splitScalar(u8, ssh_keys, '\n'); 624 while (it.next()) |line| { 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