╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
413 owner_name: []const u8, 414 fingerprint: *const [ssh.fingerprint_len]u8, 415 ) !bool { 416 var arena = std.heap.ArenaAllocator.init(allocator); 417 defer arena.deinit(); 418 419 const user = (try evt.User.readByName(io, allocator, admin_repo_path, &arena, owner_name)) orelse return false; 420 421 var it = std.mem.splitScalar(u8, user.ssh_keys, '\n'); 422 while (it.next()) |line| { 423 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 424 if (std.mem.eql(u8, &fp, fingerprint)) return true; 425 } 426 return false; 427 } 428 429 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 430 // blank or not a parseable "<type> <base64> [comment]" entry 431 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 432 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 433 _ = it.next() orelse return null; // key type 434 const blob_b64 = it.next() orelse return null; 435 436 const decoder = std.base64.standard.Decoder; 437 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 438 var blob_buf: [4096]u8 = undefined; 439 if (blob_len > blob_buf.len) return null; 440 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 441 442 return ssh.formatFingerprint(blob_buf[0..blob_len]); 443 } 444 445 // write an error to the client's stderr and exit non-zero. stderr (not stdout) 446 // because the git exec sessions that call this carry the pack protocol on 447 // stdout, where stray text breaks the client's pkt-line parser. 448 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 449 try sess.writeStderr("haxy ssh: " ++ msg ++ "\n"); 450 try sess.exit(1); 451 } 452 453 test "parseGitCommand rejects malformed input" { 454 const allocator = std.testing.allocator; 455 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 456 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 457 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 458 } 459 460 test "parseGitCommand happy paths" { 461 const allocator = std.testing.allocator; 462 463 { 464 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 465 defer parsed.deinit(allocator); 466 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 467 try std.testing.expectEqualStrings("some-repo", parsed.dir); 468 } 469 { 470 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 471 defer parsed.deinit(allocator); 472 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 473 try std.testing.expectEqualStrings("user/proj", parsed.dir); 474 } 475 { 476 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 477 defer parsed.deinit(allocator); 478 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 479 try std.testing.expectEqualStrings("repo", parsed.dir); 480 } 481 } 482