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