summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-22 08:17:36 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 08:02:54 +0200
commite1cebe0ea0719ad408a0a7494efe1d8581b702ce (patch)
treea9891283253ab252620e621145e5de94c4a29bc0
parent2e0f3799a809086054f6d5ac4120283b5bb6fa3b (diff)
downloadopenttd-e1cebe0ea0719ad408a0a7494efe1d8581b702ce.tar.xz
Add: [Network] Validate the client name server side, so no clients with invalid names can actually join
-rw-r--r--src/lang/english.txt2
-rw-r--r--src/network/network.cpp1
-rw-r--r--src/network/network_client.cpp41
-rw-r--r--src/network/network_server.cpp15
-rw-r--r--src/network/network_type.h1
5 files changed, 38 insertions, 22 deletions
diff --git a/src/lang/english.txt b/src/lang/english.txt
index 7ba1768e2..ff12f122a 100644
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -2188,6 +2188,7 @@ STR_NETWORK_ERROR_TIMEOUT_PASSWORD :{WHITE}You took
STR_NETWORK_ERROR_TIMEOUT_COMPUTER :{WHITE}Your computer is too slow to keep up with the server
STR_NETWORK_ERROR_TIMEOUT_MAP :{WHITE}Your computer took too long to download the map
STR_NETWORK_ERROR_TIMEOUT_JOIN :{WHITE}Your computer took too long to join the server
+STR_NETWORK_ERROR_INVALID_CLIENT_NAME :{WHITE}Your player name is not valid
############ Leave those lines in this order!!
STR_NETWORK_ERROR_CLIENT_GENERAL :general error
@@ -2210,6 +2211,7 @@ STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD :received no pas
STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER :general timeout
STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP :downloading map took too long
STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN :processing map took too long
+STR_NETWORK_ERROR_CLIENT_INVALID_CLIENT_NAME :invalid client name
############ End of leave-in-this-order
STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION :{WHITE}Possible connection loss
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 7c3a9ef72..97eadb976 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -314,6 +314,7 @@ StringID GetNetworkErrorMsg(NetworkErrorCode err)
STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN,
+ STR_NETWORK_ERROR_CLIENT_INVALID_CLIENT_NAME,
};
static_assert(lengthof(network_error_strings) == NETWORK_ERROR_END);
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index a80931c33..b234880d7 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -668,26 +668,27 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(Packet *p)
{
static const StringID network_error_strings[] = {
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_GENERAL
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_DESYNC
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_SAVEGAME_FAILED
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_CONNECTION_LOST
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_ILLEGAL_PACKET
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_NEWGRF_MISMATCH
- STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_NOT_AUTHORIZED
- STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_NOT_EXPECTED
- STR_NETWORK_ERROR_WRONG_REVISION, // NETWORK_ERROR_WRONG_REVISION
- STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_NAME_IN_USE
- STR_NETWORK_ERROR_WRONG_PASSWORD, // NETWORK_ERROR_WRONG_PASSWORD
- STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_COMPANY_MISMATCH
- STR_NETWORK_ERROR_KICKED, // NETWORK_ERROR_KICKED
- STR_NETWORK_ERROR_CHEATER, // NETWORK_ERROR_CHEATER
- STR_NETWORK_ERROR_SERVER_FULL, // NETWORK_ERROR_FULL
- STR_NETWORK_ERROR_TOO_MANY_COMMANDS, // NETWORK_ERROR_TOO_MANY_COMMANDS
- STR_NETWORK_ERROR_TIMEOUT_PASSWORD, // NETWORK_ERROR_TIMEOUT_PASSWORD
- STR_NETWORK_ERROR_TIMEOUT_COMPUTER, // NETWORK_ERROR_TIMEOUT_COMPUTER
- STR_NETWORK_ERROR_TIMEOUT_MAP, // NETWORK_ERROR_TIMEOUT_MAP
- STR_NETWORK_ERROR_TIMEOUT_JOIN, // NETWORK_ERROR_TIMEOUT_JOIN
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_GENERAL
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_DESYNC
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_SAVEGAME_FAILED
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_CONNECTION_LOST
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_ILLEGAL_PACKET
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_NEWGRF_MISMATCH
+ STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_NOT_AUTHORIZED
+ STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_NOT_EXPECTED
+ STR_NETWORK_ERROR_WRONG_REVISION, // NETWORK_ERROR_WRONG_REVISION
+ STR_NETWORK_ERROR_LOSTCONNECTION, // NETWORK_ERROR_NAME_IN_USE
+ STR_NETWORK_ERROR_WRONG_PASSWORD, // NETWORK_ERROR_WRONG_PASSWORD
+ STR_NETWORK_ERROR_SERVER_ERROR, // NETWORK_ERROR_COMPANY_MISMATCH
+ STR_NETWORK_ERROR_KICKED, // NETWORK_ERROR_KICKED
+ STR_NETWORK_ERROR_CHEATER, // NETWORK_ERROR_CHEATER
+ STR_NETWORK_ERROR_SERVER_FULL, // NETWORK_ERROR_FULL
+ STR_NETWORK_ERROR_TOO_MANY_COMMANDS, // NETWORK_ERROR_TOO_MANY_COMMANDS
+ STR_NETWORK_ERROR_TIMEOUT_PASSWORD, // NETWORK_ERROR_TIMEOUT_PASSWORD
+ STR_NETWORK_ERROR_TIMEOUT_COMPUTER, // NETWORK_ERROR_TIMEOUT_COMPUTER
+ STR_NETWORK_ERROR_TIMEOUT_MAP, // NETWORK_ERROR_TIMEOUT_MAP
+ STR_NETWORK_ERROR_TIMEOUT_JOIN, // NETWORK_ERROR_TIMEOUT_JOIN
+ STR_NETWORK_ERROR_INVALID_CLIENT_NAME, // NETWORK_ERROR_INVALID_CLIENT_NAME
};
static_assert(lengthof(network_error_strings) == NETWORK_ERROR_END);
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 321c8aa09..fbde713eb 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -945,8 +945,12 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
break;
}
- /* We need a valid name.. make it Player */
- if (!NetworkIsValidClientName(name)) strecpy(name, "Player", lastof(name));
+ if (!NetworkIsValidClientName(name)) {
+ /* An invalid client name was given. However, the client ensures the name
+ * is valid before it is sent over the network, so something went horribly
+ * wrong. This is probably someone trying to troll us. */
+ return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
+ }
if (!NetworkFindName(name, lastof(name))) { // Change name if duplicate
/* We could not create a name for this client */
@@ -1441,6 +1445,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME(Packet
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
if (ci != nullptr) {
+ if (!NetworkIsValidClientName(client_name)) {
+ /* An invalid client name was given. However, the client ensures the name
+ * is valid before it is sent over the network, so something went horribly
+ * wrong. This is probably someone trying to troll us. */
+ return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
+ }
+
/* Display change */
if (NetworkFindName(client_name, lastof(client_name))) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
diff --git a/src/network/network_type.h b/src/network/network_type.h
index 5a7705346..4c75346da 100644
--- a/src/network/network_type.h
+++ b/src/network/network_type.h
@@ -132,6 +132,7 @@ enum NetworkErrorCode {
NETWORK_ERROR_TIMEOUT_COMPUTER,
NETWORK_ERROR_TIMEOUT_MAP,
NETWORK_ERROR_TIMEOUT_JOIN,
+ NETWORK_ERROR_INVALID_CLIENT_NAME,
NETWORK_ERROR_END,
};