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