╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
537 else 538 return error.UnsupportedService; 539 540 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 541 } 542 543 const RepoAuthorization = union(enum) { allowed: ?evt.CommitAuthor, denied, not_found }; 544 545 fn authorizeRepoKey( 546 io: std.Io, 547 arena: *std.heap.ArenaAllocator, 548 admin_repo_path: []const u8, 549 users_dir: []const u8, 550 owner_name: []const u8, 551 repo_name: []const u8, 552 service: GitService, 553 fingerprint: *const [ssh.fingerprint_len]u8, 554 ) !RepoAuthorization { 555 const allocator = arena.child_allocator; 556 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 557 error.RepoNotFound => return .not_found, 558 else => |e| return e, 559 }; 560 defer admin.deinit(io, allocator); 561 562 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 563 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name)) orelse { 564 if (service == .upload_pack) return .not_found; 565 const owner = (try evt.User.readByName(io, allocator, admin_repo_path, arena, owner_name)) orelse return .not_found; 566 return if (isKeyInAuthorizedKeys(owner.event.ssh_keys, fingerprint)) 567 .{ .allowed = .{ .name = owner.event.name, .email = owner.event.email } } 568 else 569 .denied; 570 }; 571 572 const min_role: evt.Repo.Role = switch (service) { 573 .upload_pack => .read, 574 .receive_pack => .write, 575 }; 576 // skip the key lookups when the base role is enough 577 if (evt.Repo.roleOf(repo.repo, null).atLeast(min_role)) return .{ .allowed = null }; 578 579 const user = (try repoUserWithKey(moment, arena, repo.repo.event, fingerprint)) orelse return .denied; 580 if (!evt.Repo.roleOf(repo.repo, user.id).atLeast(min_role)) return .denied; 581 return .{ .allowed = user.author }; 582 } 583 584 const UserWithKey = struct { 585 id: [evt.event_id_size]u8, 586 author: evt.CommitAuthor, 587 }; 588 589 // the repo's owner or collaborator holding the key, or null for anyone else 590 fn repoUserWithKey( 591 moment: evt.AdminDB.HashMap(.read_only), 592 arena: *std.heap.ArenaAllocator, 593 repo: evt.Repo, 594 fingerprint: *const [ssh.fingerprint_len]u8, 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