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