╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
552 arena: *std.heap.ArenaAllocator, 553 admin_repo_path: []const u8, 554 users_dir: []const u8, 555 owner_name: []const u8, 556 repo_name: []const u8, 557 service: GitService, 558 fingerprint: *const [ssh.fingerprint_len]u8, 559 ) !RepoAuthorization { 560 const allocator = arena.child_allocator; 561 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 562 error.RepoNotFound => return .not_found, 563 else => |e| return e, 564 }; 565 defer admin.deinit(io, allocator); 566 567 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 568 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name)) orelse { 569 if (service == .upload_pack) return .not_found; 570 const owner = (try evt.User.readByName(io, allocator, admin_repo_path, arena, owner_name)) orelse return .not_found; 571 return if (isKeyInAuthorizedKeys(owner.event.ssh_keys, fingerprint)) 572 .{ .allowed = .{ .name = owner.event.name, .email = owner.event.email } } 573 else 574 .denied; 575 }; 576 577 const min_role: evt.Repo.Role = switch (service) { 578 .upload_pack => .read, 579 .receive_pack => .write, 580 }; 581 // skip the key lookups when the base role is enough 582 if (evt.Repo.roleOf(repo.repo, null).atLeast(min_role)) return .{ .allowed = null }; 583 584 const user = (try repoUserWithKey(moment, arena, repo.repo.event, fingerprint)) orelse return .denied; 585 if (!evt.Repo.roleOf(repo.repo, user.id).atLeast(min_role)) return .denied; 586 return .{ .allowed = user.author }; 587 } 588 589 const UserWithKey = struct { 590 id: [evt.event_id_size]u8, 591 author: evt.CommitAuthor, 592 }; 593 594 // the repo's owner or collaborator holding the key, or null for anyone else 595 fn repoUserWithKey( 596 moment: evt.AdminDB.HashMap(.read_only), 597 arena: *std.heap.ArenaAllocator, 598 repo: evt.Repo, 599 fingerprint: *const [ssh.fingerprint_len]u8, 600 ) !?UserWithKey { 601 if (try userWithKey(moment, arena, repo.user_id, fingerprint)) |user| return user; 602 603 for ([_][]const u8{ repo.owner_user_ids, repo.write_user_ids, repo.read_user_ids }) |user_ids| { 604 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 605 while (lines.next()) |id| { 606 const user_id = try evt.parseEventId(id); 607 if (try userWithKey(moment, arena, &user_id, fingerprint)) |user| return user; 608 } 609 } 610 return null; 611 } 612 613 fn userWithKey( 614 moment: evt.AdminDB.HashMap(.read_only), 615 arena: *std.heap.ArenaAllocator, 616 user_id: []const u8, 617 fingerprint: *const [ssh.fingerprint_len]u8, 618 ) !?UserWithKey { 619 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse return null; 620 if (user.removed or !isKeyInAuthorizedKeys(user.event.ssh_keys, fingerprint)) return null; 621 return .{ 622 .id = user_id[0..evt.event_id_size].*, 623 .author = .{ .name = user.event.name, .email = user.event.email }, 624 }; 625 } 626 627 fn isKeyInAuthorizedKeys(ssh_keys: []const u8, fingerprint: *const [ssh.fingerprint_len]u8) bool { 628 var it = std.mem.splitScalar(u8, ssh_keys, '\n'); 629 while (it.next()) |line| { 630 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 631 if (std.mem.eql(u8, &fp, fingerprint)) return true; 632 } 633 return false; 634 } 635 636 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 637 // blank or not a parseable "<type> <base64> [comment]" entry 638 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 639 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 640 _ = it.next() orelse return null; // key type 641 const blob_b64 = it.next() orelse return null; 642 643 const decoder = std.base64.standard.Decoder; 644 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 645 var blob_buf: [4096]u8 = undefined; 646 if (blob_len > blob_buf.len) return null; 647 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 648 649 return ssh.formatFingerprint(blob_buf[0..blob_len]); 650 } 651 652 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 653 const body = "ERR haxy ssh: " ++ msg; 654 var packet: [body.len + 4]u8 = undefined; 655 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 656 try sess.writeBytes(line); 657 try sess.exit(1); 658 } 659 660 test "parseGitCommand rejects malformed input" { 661 const allocator = std.testing.allocator; 662 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 663 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 664 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 665 } 666 667 test "parseGitCommand happy paths" { 668 const allocator = std.testing.allocator; 669 670 { 671 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 672 defer parsed.deinit(allocator); 673 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 674 try std.testing.expectEqualStrings("some-repo", parsed.dir); 675 } 676 { 677 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 678 defer parsed.deinit(allocator); 679 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 680 try std.testing.expectEqualStrings("user/proj", parsed.dir); 681 } 682 { 683 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 684 defer parsed.deinit(allocator); 685 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 686 try std.testing.expectEqualStrings("repo", parsed.dir); 687 } 688 } 689