Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): -control refuses online world commands, web Origin

Olcay Seygan86e79295cba2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 627651861..078ef55b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -949,6 +949,7 @@ found and fixed, EA's own, not port damage.**
- LAN refuses to start between two machines whose game files differ.
- A player whose name holds a comma, a colon or a control character is turned away at the door instead of scrambling the lobby's idea of who is in the room.
- A Quick Match can no longer crash your game as it starts. A server that sent colour 999, or an army number past the end of the list, took the game down on the loading screen; now that seat reads as Random.
+- The scripting socket our test tools drive the game through is no cheat online. In an online match it refuses to spawn units or count the enemy's army through the fog, and a web page open in your browser cannot reach it at all.
- An open seat can be taken. The map's number of starting positions used to overrule the host: a four position map turned away the fifth player even with a seat left open for him, and that seat stayed open on everyone's screen.
- A map sent to you over the network has to be a map. The other machine used to name any file it liked and fill it with anything at all, and it was written where the name pointed; now the name cannot leave the map folder, the kind has to be one a map transfer carries, and the contents have to match the kind.
- The processor's rounding mode is reset from the right register every frame.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h b/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
index 5eb88693b..fc612b450 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
@@ -76,4 +76,8 @@ extern void ControlServer_shutdown( void );
and RFC 6455 ships a worked example to test it against. */
extern Bool ControlServer_computeAcceptKey( const char *clientKey, char *out, Int outSize );
+/** Whether an upgrade request may have the socket: no Origin (a script), "null", or a page on this
+ machine. Anything else is a web page reaching for ws://127.0.0.1 from the player's browser. */
+extern Bool ControlServer_originAllowed( const char *request );
+
#endif // __CONTROLSERVER_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
index 9eeb7d77c..ff2768734 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
@@ -51,6 +51,7 @@
#include "GameLogic/ScenarioDrill.h"
#include "GameLogic/TerrainLogic.h"
#include "GameNetwork/GameInfo.h" // MAX_SLOTS, for the skirmish command's player count
+#include "GameNetwork/Online/OnlineQueues.h"
#include <vector>
@@ -480,35 +481,71 @@ static Bool takeFrame( unsigned char *opcodeOut, AsciiString *payloadOut )
// the http upgrade
// ------------------------------------------------------------------------------------------------
-/** Read the client key out of the request headers. Case-insensitive on the header name, because
- RFC 7230 says header names are, and browsers and Python libraries disagree about the casing. */
-static Bool findClientKey( const char *request, char *keyOut, Int keyOutSize )
+/** Read one header's value out of the request; FALSE when the header is absent. `name` is lower case
+ with its colon. Case-insensitive on the name, because RFC 7230 says header names are, and browsers
+ and Python libraries disagree about the casing; matched only at the start of a line, so the path in
+ the request line, which a web page chooses, cannot pass for a header. */
+static Bool findHeader( const char *request, const char *name, char *valueOut, Int valueOutSize )
{
- static const char *HEADER = "sec-websocket-key:";
- const Int headerLength = (Int)strlen( HEADER );
+ const Int nameLength = (Int)strlen( name );
for( const char *at = request; *at; ++at )
{
+ if (at != request && at[ -1 ] != '\n')
+ continue;
+
Int i = 0;
- while (i < headerLength && at[ i ] && tolower( (unsigned char)at[ i ] ) == HEADER[ i ])
+ while (i < nameLength && at[ i ] && tolower( (unsigned char)at[ i ] ) == name[ i ])
++i;
- if (i < headerLength)
+ if (i < nameLength)
continue;
- const char *value = at + headerLength;
+ const char *value = at + nameLength;
while (*value == ' ' || *value == '\t')
++value;
Int written = 0;
- while (*value && *value != '\r' && *value != '\n' && written < keyOutSize - 1)
- keyOut[ written++ ] = *value++;
- keyOut[ written ] = 0;
- return written > 0;
+ while (*value && *value != '\r' && *value != '\n' && written < valueOutSize - 1)
+ valueOut[ written++ ] = *value++;
+ while (written > 0 && (valueOut[ written - 1 ] == ' ' || valueOut[ written - 1 ] == '\t'))
+ --written;
+ valueOut[ written ] = 0;
+ return TRUE;
}
return FALSE;
}
+/* A browser names the page that opened the socket in Origin, and any web page can open ws://127.0.0.1.
+ So a request with an Origin is let in only from this machine's own pages (or "null", a file:// page);
+ the project's tools (control_client.py, Node's WebSocket) send no Origin at all. */
+Bool ControlServer_originAllowed( const char *request )
+{
+ char origin[ 256 ];
+ if (!findHeader( request, "origin:", origin, sizeof( origin ) ))
+ return TRUE;
+ if (strcmp( origin, "null" ) == 0)
+ return TRUE;
+
+ static const char *const LOCAL[] = { "http://localhost", "https://localhost", "http://127.0.0.1", "https://127.0.0.1", "http://[::1]", "https://[::1]" };
+ for( Int k = 0; k < (Int)ARRAY_SIZE( LOCAL ); ++k )
+ {
+ const size_t length = strlen( LOCAL[ k ] );
+ if (strncmp( origin, LOCAL[ k ], length ) != 0)
+ continue;
+ const char *port = origin + length;
+ if (*port == 0)
+ return TRUE;
+ if (*port++ != ':' || *port == 0)
+ continue;
+ while (*port >= '0' && *port <= '9')
+ ++port;
+ if (*port == 0)
+ return TRUE;
+ }
+ return FALSE;
+}
+
static Bool tryHandshake( void )
{
// the request ends at the blank line; until then there is nothing to answer
@@ -520,9 +557,16 @@ static Bool tryHandshake( void )
if (end == NULL)
return FALSE;
+ if (!ControlServer_originAllowed( request ))
+ {
+ DEBUG_LOG(("CONTROL: a web page's request (Origin not this machine), closing\n"));
+ closeClient();
+ return FALSE;
+ }
+
char clientKey[ 256 ];
char acceptKey[ 64 ];
- if (!findClientKey( request, clientKey, sizeof( clientKey ) )
+ if (!findHeader( request, "sec-websocket-key:", clientKey, sizeof( clientKey ) ) || clientKey[ 0 ] == 0
|| !ControlServer_computeAcceptKey( clientKey, acceptKey, sizeof( acceptKey ) ))
{
DEBUG_LOG(("CONTROL: not a websocket request, closing\n"));
@@ -593,9 +637,19 @@ static Int revealedPercent( const Player *player )
/** frame number, whether a match is running, and what each player owns, with enough of each
player's state that every console cheat can be seen to have landed: tools/cheat_check.py. */
+/* A network match a lobby started, LAN or online. This machine plays one seat of it, so a world command
+ would split the match, and the census, tally and spectator views would read through the fog: any
+ player can pass -control. A -netgame match is the harnesses' own, and so is a match on an online
+ server started for a test (OnlineIsTestServer), where online_check.py forces its desync. */
+static Bool isGuardedMatch( void )
+{
+ return TheGameLogic && TheGameLogic->isInMultiplayerGame() && !TheGlobalData->m_netGameStarted && !OnlineIsTestServer();
+}
+
static void replyStatus( void )
{
const Bool inGame = TheGameLogic && TheGameLogic->isInGame() && !TheGameLogic->isInShellGame();
+ const Bool ownSeatOnly = isGuardedMatch();
std::vector<char> reply;
char piece[ 512 ];
@@ -617,7 +671,7 @@ static void replyStatus( void )
for( Int i = 0; i < ThePlayerList->getPlayerCount(); ++i )
{
Player *player = ThePlayerList->getNthPlayer( i );
- if (player == NULL)
+ if (player == NULL || (ownSeatOnly && player != ThePlayerList->getLocalPlayer()))
continue;
Int units = 0;
@@ -850,6 +904,12 @@ static void handleCommand( const AsciiString &command )
rest that InGameUI::runSpectatorAction knows. */
if (strncmp( command.str(), "spectator ", 10 ) == 0)
{
+ const Player *local = ThePlayerList ? ThePlayerList->getLocalPlayer() : NULL;
+ if (isGuardedMatch() && (local == NULL || !local->isPlayerObserver()))
+ {
+ replyError( "a player in a network match cannot use the spectator's views" );
+ return;
+ }
TheInGameUI->runSpectatorAction( command.str() + 10 );
replyOk( "\"done\":true" );
return;
@@ -913,15 +973,14 @@ static void handleCommand( const AsciiString &command )
return;
}
- /* A world command changes this machine's simulation and nobody else's. A network match the
- lobby started would split on it, and a replay would stop matching its recording; a -netgame
- match is allowed, on the same terms -scenario is (GameLogic::startNewGame). So is an online match a
- copy logged into with the -onlineuser test switch, where online_check.py forces its desync: a split
- there ends the match unrated, which is what the check proves. */
- const Bool scriptedOnline = TheGlobalData->m_onlineUser.isNotEmpty() && TheGlobalData->m_onlinePort != 0;
- if ((TheGameLogic->isInMultiplayerGame() && !TheGlobalData->m_netGameStarted && !scriptedOnline) || TheGameLogic->isInReplayGame())
+ /* A world command changes this machine's simulation and nobody else's, and tally reads through the
+ fog. A network match the lobby started would split on it (isGuardedMatch), and a replay would stop
+ matching its recording; a -netgame match is allowed, on the same terms -scenario is
+ (GameLogic::startNewGame), and so is a match on an online test server, where a split ends the match
+ unrated, which is what online_check.py proves. */
+ if (isGuardedMatch() || TheGameLogic->isInReplayGame())
{
- replyError( "world commands only run in a skirmish or a -netgame match" );
+ replyError( "world commands only run in a skirmish, a -netgame match or on an online test server" );
return;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index e40f6a9fc..ad1528b52 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -21800,6 +21800,41 @@ TEST(control_server_answers_the_rfc_handshake_example)
CHECK( !ControlServer_computeAcceptKey( "dGhlIHNhbXBsZSBub25jZQ==", tooSmall, sizeof( tooSmall ) ) );
}
+/* Any web page the player opens can try ws://127.0.0.1:<port> while -control is on, and its browser
+ * names the page in Origin. The project's own clients send none. */
+TEST(control_server_refuses_a_web_pages_origin)
+{
+ const char *head = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8931\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n";
+ std::string request;
+
+ // control_client.py and Node's WebSocket: no Origin
+ request = std::string( head ) + "\r\n";
+ CHECK( ControlServer_originAllowed( request.c_str() ) );
+
+ const char *allowed[] = { "null", "http://localhost", "http://localhost:5173", "https://127.0.0.1:443", "http://[::1]:8080", "null " };
+ for( Int i = 0; i < (Int)ARRAY_SIZE( allowed ); ++i )
+ {
+ request = std::string( head ) + "Origin: " + allowed[ i ] + "\r\n\r\n";
+ CHECK( ControlServer_originAllowed( request.c_str() ) );
+ }
+ request = std::string( head ) + "oRiGiN:http://localhost:1\r\n\r\n";
+ CHECK( ControlServer_originAllowed( request.c_str() ) );
+
+ const char *refused[] = { "https://evil.example", "http://localhost.evil.example", "http://localhost:80.evil.example",
+ "http://127.0.0.1.nip.io", "http://localhost:", "", "file://", "http://LOCALHOSTX" };
+ for( Int i = 0; i < (Int)ARRAY_SIZE( refused ); ++i )
+ {
+ request = std::string( head ) + "Origin: " + refused[ i ] + "\r\n\r\n";
+ CHECK( !ControlServer_originAllowed( request.c_str() ) );
+ }
+
+ // the path is the page's to choose, and must not read as a header
+ request = "GET /x\r\norigin: null HTTP/1.1\r\nOrigin: https://evil.example\r\n\r\n";
+ CHECK( !ControlServer_originAllowed( request.c_str() ) );
+ request = "GET /?origin:null HTTP/1.1\r\nOrigin: https://evil.example\r\n\r\n";
+ CHECK( !ControlServer_originAllowed( request.c_str() ) );
+}
+
/* Telling a click from a drag.
*
* Three terms, and each of them has been wrong at some point. The screen distance was compared one