fix crash in authentication

This commit is contained in:
Lion Kortlepel
2023-12-03 17:43:41 +01:00
parent 47e64a7343
commit 22805af716
+28 -3
View File
@@ -177,6 +177,7 @@ void TNetwork::Identify(TConnection&& RawConnection) {
return; return;
} }
std::shared_ptr<TClient> Client { nullptr }; std::shared_ptr<TClient> Client { nullptr };
try {
if (Code == 'C') { if (Code == 'C') {
Client = Authentication(std::move(RawConnection)); Client = Authentication(std::move(RawConnection));
} else if (Code == 'D') { } else if (Code == 'D') {
@@ -188,6 +189,18 @@ void TNetwork::Identify(TConnection&& RawConnection) {
} else { } else {
beammp_errorf("Invalid code got in Identify: '{}'", Code); beammp_errorf("Invalid code got in Identify: '{}'", Code);
} }
} catch(const std::exception& e) {
beammp_errorf("Error during handling of code {} - client left in invalid state, closing socket", Code);
boost::system::error_code ec;
RawConnection.Socket.shutdown(socket_base::shutdown_both, ec);
if (ec) {
beammp_debugf("Failed to shutdown client socket: {}", ec.message());
}
RawConnection.Socket.close(ec);
if (ec) {
beammp_debugf("Failed to close client socket: {}", ec.message());
}
}
} }
void TNetwork::HandleDownload(TConnection&& Conn) { void TNetwork::HandleDownload(TConnection&& Conn) {
@@ -246,12 +259,24 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
return nullptr; return nullptr;
} }
nlohmann::json AuthReq { std::string key(reinterpret_cast<const char*>(Data.data()), Data.size());
{ "key", std::string(reinterpret_cast<const char*>(Data.data()), Data.size()) }
nlohmann::json AuthReq{};
std::string AuthResStr{};
try {
AuthReq = nlohmann::json {
{ "key", key }
}; };
auto Target = "/pkToUser"; auto Target = "/pkToUser";
unsigned int ResponseCode = 0; unsigned int ResponseCode = 0;
const auto AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode); AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode);
} catch (const std::exception& e) {
beammp_debugf("Invalid json sent by client, kicking: {}", e.what());
ClientKick(*Client, "Invalid Key (invalid UTF8 string)!");
return nullptr;
}
try { try {
nlohmann::json AuthRes = nlohmann::json::parse(AuthResStr); nlohmann::json AuthRes = nlohmann::json::parse(AuthResStr);