╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
546 } 547 548 fn keyBelongsToUser( 549 moment: evt.AdminDB.HashMap(.read_only), 550 arena: *std.heap.ArenaAllocator, 551 user_id: []const u8, 552 fingerprint: *const [ssh.fingerprint_len]u8, 553 ) !bool { 554 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse return false; 555 return !user.removed and isKeyInAuthorizedKeys(user.event.ssh_keys, fingerprint); 556 } 557 558 fn isKeyInAuthorizedKeys(ssh_keys: []const u8, fingerprint: *const [ssh.fingerprint_len]u8) bool { 559 var it = std.mem.splitScalar(u8, ssh_keys, '\n'); 560 while (it.next()) |line| { 561 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 562 if (std.mem.eql(u8, &fp, fingerprint)) return true; 563 } 564 return false; 565 } 566 567 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 568 // blank or not a parseable "<type> <base64> [comment]" entry 569 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 570 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 571 _ = it.next() orelse return null; // key type 572 const blob_b64 = it.next() orelse return null; 573 574 const decoder = std.base64.standard.Decoder; 575 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 576 var blob_buf: [4096]u8 = undefined; 577 if (blob_len > blob_buf.len) return null; 578 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 579 580 return ssh.formatFingerprint(blob_buf[0..blob_len]); 581 } 582 583 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 584 const body = "ERR haxy ssh: " ++ msg; 585 var packet: [body.len + 4]u8 = undefined; 586 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 587 try sess.writeBytes(line); 588 try sess.exit(1); 589 } 590 591 test "parseGitCommand rejects malformed input" { 592 const allocator = std.testing.allocator; 593 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 594 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 595 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 596 } 597 598 test "parseGitCommand happy paths" { 599 const allocator = std.testing.allocator; 600 601 { 602 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 603 defer parsed.deinit(allocator); 604 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 605 try std.testing.expectEqualStrings("some-repo", parsed.dir); 606 } 607 { 608 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 609 defer parsed.deinit(allocator); 610 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 611 try std.testing.expectEqualStrings("user/proj", parsed.dir); 612 } 613 { 614 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 615 defer parsed.deinit(allocator); 616 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 617 try std.testing.expectEqualStrings("repo", parsed.dir); 618 } 619 } 620