╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 // no viewer yet, so this carries the base role 564 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 565 if (service == .upload_pack) return .not_found; 566 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 567 const owner = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id)) orelse unreachable; 568 return if (isKeyInAuthorizedKeys(owner.event.ssh_keys, fingerprint)) 569 .{ .create = .{ .owner_id = owner_id, .owner = .{ .name = owner.event.name, .email = owner.event.email } } } 570 else 571 .denied; 572 }; 573 574 const min_role: evt.Repo.Role = switch (service) { 575 .upload_pack => .read, 576 .receive_pack => .write, 577 }; 578 // skip the key lookups when the base role is enough 579 if (repo.role.atLeast(min_role)) return .{ .allowed = null }; 580 581 // the creator owns the repo 582 const owner_id = repo.repo.event.user_id[0..evt.event_id_size]; 583 if (try authorWithKey(moment, arena, owner_id, fingerprint)) |author| return .{ .allowed = author }; 584 585 // anyone else holding the key needs a grant with the role. a key can be 586 // shared, so a grant that falls short doesn't end the search. 587 for (try evt.readRepoGrants(io, allocator, arena, users_dir, owner_id, &repo.event_id)) |grant| { 588 if (!grant.role.atLeast(min_role)) continue; 589 if (try authorWithKey(moment, arena, grant.user_id, fingerprint)) |author| return .{ .allowed = author }; 590 } 591 return .denied; 592 } 593 594 // the user as a commit author, or null unless they hold the key 595 fn authorWithKey( 596 moment: evt.AdminDB.HashMap(.read_only), 597 arena: *std.heap.ArenaAllocator, 598 user_id: []const u8, 599 fingerprint: *const [ssh.fingerprint_len]u8, 600 ) !?evt.CommitAuthor { 601 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse return null; 602 if (user.removed or !isKeyInAuthorizedKeys(user.event.ssh_keys, fingerprint)) return null; 603 return .{ .name = user.event.name, .email = user.event.email }; 604 } 605 606 fn isKeyInAuthorizedKeys(ssh_keys: []const u8, fingerprint: *const [ssh.fingerprint_len]u8) bool { 607 var it = std.mem.splitScalar(u8, ssh_keys, '\n'); 608 while (it.next()) |line| { 609 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 610 if (std.mem.eql(u8, &fp, fingerprint)) return true; 611 } 612 return false; 613 } 614 615 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 616 // blank or not a parseable "<type> <base64> [comment]" entry 617 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 618 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 619 _ = it.next() orelse return null; // key type 620 const blob_b64 = it.next() orelse return null; 621 622 const decoder = std.base64.standard.Decoder; 623 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 624 var blob_buf: [4096]u8 = undefined; 625 if (blob_len > blob_buf.len) return null; 626 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 627 628 return ssh.formatFingerprint(blob_buf[0..blob_len]); 629 } 630 631 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 632 const body = "ERR haxy ssh: " ++ msg; 633 var packet: [body.len + 4]u8 = undefined; 634 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 635 try sess.writeBytes(line); 636 try sess.exit(1); 637 } 638 639 test "parseGitCommand rejects malformed input" { 640 const allocator = std.testing.allocator; 641 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 642 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 643 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 644 } 645 646 test "parseGitCommand happy paths" { 647 const allocator = std.testing.allocator; 648 649 { 650 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 651 defer parsed.deinit(allocator); 652 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 653 try std.testing.expectEqualStrings("some-repo", parsed.dir); 654 } 655 { 656 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 657 defer parsed.deinit(allocator); 658 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 659 try std.testing.expectEqualStrings("user/proj", parsed.dir); 660 } 661 { 662 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 663 defer parsed.deinit(allocator); 664 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 665 try std.testing.expectEqualStrings("repo", parsed.dir); 666 } 667 } 668