╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
347 348 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 349 // blank or not a parseable "<type> <base64> [comment]" entry 350 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 351 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 352 _ = it.next() orelse return null; // key type 353 const blob_b64 = it.next() orelse return null; 354 355 const decoder = std.base64.standard.Decoder; 356 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 357 var blob_buf: [4096]u8 = undefined; 358 if (blob_len > blob_buf.len) return null; 359 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 360 361 return ssh.formatFingerprint(blob_buf[0..blob_len]); 362 } 363 364 // write an error to the client's stderr and exit non-zero. stderr (not stdout) 365 // because the git exec sessions that call this carry the pack protocol on 366 // stdout, where stray text breaks the client's pkt-line parser. 367 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 368 try sess.writeStderr("haxy ssh: " ++ msg ++ "\n"); 369 try sess.exit(1); 370 } 371 372 test "parseGitCommand rejects malformed input" { 373 const allocator = std.testing.allocator; 374 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 375 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 376 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 377 } 378 379 test "parseGitCommand happy paths" { 380 const allocator = std.testing.allocator; 381 382 { 383 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 384 defer parsed.deinit(allocator); 385 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 386 try std.testing.expectEqualStrings("some-repo", parsed.dir); 387 } 388 { 389 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 390 defer parsed.deinit(allocator); 391 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 392 try std.testing.expectEqualStrings("user/proj", parsed.dir); 393 } 394 { 395 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 396 defer parsed.deinit(allocator); 397 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 398 try std.testing.expectEqualStrings("repo", parsed.dir); 399 } 400 } 401