╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
534 const service_token = tokens.next() orelse return error.InvalidCommand; 535 const dir_token = tokens.next() orelse return error.InvalidCommand; 536 537 const service: GitService = 538 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 539 .upload_pack 540 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 541 .receive_pack 542 else 543 return error.UnsupportedService; 544 545 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 546 } 547 548 const RepoAuthorization = union(enum) { allowed: ?evt.CommitAuthor, create: RepoCreator, denied, not_found }; 549 550 // the owner of a repo a push may create 551 const RepoCreator = struct { 552 owner_id: [evt.event_id_size]u8, 553 owner: evt.CommitAuthor, 554 }; 555 556 fn authorizeRepoKey( 557 io: std.Io, 558 arena: *std.heap.ArenaAllocator, 559 admin_repo_path: []const u8, 560 users_dir: []const u8, 561 owner_name: []const u8, 562 repo_name: []const u8, 563 service: GitService, 564 fingerprint: *const [ssh.fingerprint_len]u8, 565 ) !RepoAuthorization { 566 const allocator = arena.child_allocator; 567 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 568 error.RepoNotFound => return .not_found, 569 else => |e| return e, 570 }; 571 defer admin.deinit(io, allocator); 572 573 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 574 // no viewer yet, so this carries the base role 575 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 576 if (service == .upload_pack) return .not_found; 577 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 578 const owner = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id)) orelse unreachable; 579 return if (isKeyInAuthorizedKeys(owner.event.ssh_keys, fingerprint)) 580 .{ .create = .{ .owner_id = owner_id, .owner = .{ .name = owner.event.name, .email = owner.event.email } } } 581 else 582 .denied; 583 }; 584 585 const min_role: evt.Repo.Role = switch (service) { 586 .upload_pack => .read, 587 .receive_pack => .write, 588 }; 589 // skip the key lookups when the base role is enough 590 if (repo.role) |base| { 591 if (base.atLeast(min_role)) return .{ .allowed = null }; 592 } 593 594 // the creator owns the repo 595 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 596 if (try authorWithKey(moment, arena, &owner_id, fingerprint)) |author| return .{ .allowed = author }; 597 598 // anyone else holding the key needs a grant with the role. a key can be 599 // shared, so a grant that falls short doesn't end the search. 600 for (try evt.readRepoGrants(io, allocator, arena, users_dir, &owner_id, &repo.event_id)) |grant| { 601 if (!grant.role.atLeast(min_role)) continue; 602 if (try authorWithKey(moment, arena, &try evt.parseEventId(&grant.user_id), fingerprint)) |author| return .{ .allowed = author }; 603 } 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