╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
470 // the route names the forker and the target repo 471 const forker_repo = evt.parseOwnerRepoPath(route.identity) orelse return writeError(sess, "repo path must be <forker>:<repo>"); 472 473 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = handler.admin_repo_path }); 474 defer admin.deinit(io, allocator); 475 var admin_arena = std.heap.ArenaAllocator.init(allocator); 476 defer admin_arena.deinit(); 477 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 478 const fork_id = try evt.parseEventId(&route.id); 479 const forker_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, forker_repo.owner)) orelse 480 return writeError(sess, "patch draft not found"); 481 const fork_record = (try evt.readForkById(io, allocator, &admin_arena, handler.users_dir, &forker_id, &fork_id)) orelse 482 return writeError(sess, "patch draft not found"); 483 if (fork_record.removed) return writeError(sess, "patch draft not found"); 484 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &admin_arena, &forker_id)) orelse 485 return writeError(sess, "invalid patch draft"); 486 if (user.removed) return writeError(sess, "invalid patch draft"); 487 if (service == .receive_pack and !isKeyInAuthorizedKeys(user.event.ssh_keys, &sess.fingerprint)) 488 return writeError(sess, "unauthorized: this SSH key is not registered to the patch author"); 489 490 const target = (try evt.readRepoById(io, allocator, &admin_arena, handler.users_dir, fork_record.event.repo_user_id[0..evt.event_id_size], fork_record.event.repo_id[0..evt.event_id_size], forker_id)) orelse 491 return writeError(sess, "repo not found"); 492 // patches that are off take their drafts with them 493 const patch_role = target.repo.event.patch_role orelse return writeError(sess, "patch draft not found"); 494 if (service == .receive_pack) { 495 const role = target.role orelse return writeError(sess, "repo not found"); 496 if (!role.atLeast(patch_role)) return writeError(sess, "unauthorized: your role in this repo may not write patches"); 497 } 498 499 const draft_path = try fork.forkPath(allocator, handler.users_dir, &forker_id, &fork_id); 500 defer allocator.free(draft_path); 501 var draft = rp.AnyRepo(.xit, any_repo_opts).open(io, allocator, .{ .path = draft_path }) catch 502 return writeError(sess, "patch draft not found"); 503 defer draft.deinit(io, allocator); 504 if (service == .upload_pack) { 505 var sideband = progress.Sideband{ .writer = writer, .sess = sess }; 506 switch (draft) { 507 inline else => |*repo| try repo.uploadPack(io, allocator, reader, writer, .{ .protocol_version = protocol_version }, &sideband), 508 } 509 } else { 510 if (!std.mem.eql(u8, target.repo.event.name, forker_repo.name)) return writeError(sess, "patch draft belongs to another repo"); 511 const target_path = try evt.repoPath(allocator, handler.users_dir, fork_record.event.repo_user_id, fork_record.event.repo_id); 512 defer allocator.free(target_path); 513 const author: evt.CommitAuthor = .{ .name = user.event.name, .email = user.event.email }; 514 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 515 switch (draft) { 516 inline else => |*repo| { 517 var target_repo = rp.Repo(.xit, repo.self_repo_opts).open(io, allocator, .{ .path = target_path }) catch 518 return writeError(sess, "repo not found or has the wrong hash"); 519 defer target_repo.deinit(io, allocator); 520 try push.receiveFork(repo.self_repo_opts, io, allocator, repo, &target_repo, handler.users_dir, &route.id, author, timestamp, reader, writer, handler.err, sess); 521 }, 522 } 523 } 524 } 525 526 fn parseGitCommand(allocator: std.mem.Allocator, command: []const u8) !ParsedGitCommand { 527 var tokens = try std.process.Args.IteratorGeneral(.{ .single_quotes = true }).init(allocator, command); 528 defer tokens.deinit(); 529 530 const service_token = tokens.next() orelse return error.InvalidCommand; 531 const dir_token = tokens.next() orelse return error.InvalidCommand; 532 533 const service: GitService = 534 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 535 .upload_pack 536 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 537 .receive_pack 538 else 539 return error.UnsupportedService; 540 541 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 542 } 543 544 const RepoAuthorization = union(enum) { allowed: ?evt.CommitAuthor, create: RepoCreator, denied, not_found }; 545 546 // the owner of a repo a push may create 547 const RepoCreator = struct { 548 owner_id: [evt.event_id_size]u8, 549 owner: evt.CommitAuthor, 550 }; 551 552 fn authorizeRepoKey( 553 io: std.Io, 554 arena: *std.heap.ArenaAllocator, 555 admin_repo_path: []const u8, 556 users_dir: []const u8, 557 owner_name: []const u8, 558 repo_name: []const u8, 559 service: GitService, 560 fingerprint: *const [ssh.fingerprint_len]u8, 561 ) !RepoAuthorization { 562 const allocator = arena.child_allocator; 563 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 564 error.RepoNotFound => return .not_found, 565 else => |e| return e, 566 }; 567 defer admin.deinit(io, allocator); 568 569 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 570 // no viewer yet, so this carries the base role 571 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 572 if (service == .upload_pack) return .not_found; 573 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 574 const owner = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id)) orelse unreachable; 575 return if (isKeyInAuthorizedKeys(owner.event.ssh_keys, fingerprint)) 576 .{ .create = .{ .owner_id = owner_id, .owner = .{ .name = owner.event.name, .email = owner.event.email } } } 577 else 578 .denied; 579 }; 580 581 const min_role: evt.Repo.Role = switch (service) { 582 .upload_pack => .read, 583 .receive_pack => .write, 584 }; 585 // skip the key lookups when the base role is enough 586 if (repo.role) |base| { 587 if (base.atLeast(min_role)) return .{ .allowed = null }; 588 } 589 590 // the creator owns the repo 591 const owner_id = repo.repo.event.user_id[0..evt.event_id_size]; 592 if (try authorWithKey(moment, arena, owner_id, fingerprint)) |author| return .{ .allowed = author }; 593 594 // anyone else holding the key needs a grant with the role. a key can be 595 // shared, so a grant that falls short doesn't end the search. 596 for (try evt.readRepoGrants(io, allocator, arena, users_dir, owner_id, &repo.event_id)) |grant| { 597 if (!grant.role.atLeast(min_role)) continue; 598 if (try authorWithKey(moment, arena, grant.user_id, fingerprint)) |author| return .{ .allowed = author }; 599 } 600 return .denied; 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