Skip to content

Commit

Permalink
Limit the length of game names to 256 characters
Browse files Browse the repository at this point in the history
This limits the maximum length of game names to 256 characters. Longer
game names will be simply cut to the first 256 characters. While
Pyrogenesis already contains a check to limit the game name to 256
characters, this is to counter modified clients trying to circumvent
this limitation.

Fixes https://gitea.wildfiregames.com/0ad/0ad/issues/3651
  • Loading branch information
Dunedan committed Oct 8, 2024
1 parent 64d6cf2 commit 4e56dc0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
40 changes: 37 additions & 3 deletions tests/test_xpartamupp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from unittest.mock import Mock, call, patch

from cachetools import FIFOCache
from hypothesis import example, given
from hypothesis import strategies as st
from parameterized import parameterized
from slixmpp.jid import JID

Expand All @@ -35,7 +37,12 @@ def test_add(self):
"""Test successfully adding a game."""
games = Games()
jid = JID(jid="[email protected]")
game_data = {"players": ["player1", "player2"], "nbp": "foo", "state": "init"}
game_data = {
"players": ["player1", "player2"],
"name": "game",
"nbp": "foo",
"state": "init",
}
self.assertTrue(games.add_game(jid, game_data))
all_games = games.get_all_games()
game_data.update(
Expand All @@ -61,14 +68,41 @@ def test_add_invalid(self, jid, game_data):
games = Games()
self.assertFalse(games.add_game(jid, game_data))

@given(game_name=st.text())
@example(game_name="a" * 300)
def test_add_long_game_name(self, game_name):
"""Test adding a game with a long name cuts the name."""
games = Games()
jid = JID(jid="[email protected]")
game_data = {
"players": ["player1", "player2"],
"name": game_name,
"nbp": "foo",
"state": "init",
}
self.assertTrue(games.add_game(jid, game_data))
all_games = games.get_all_games()
self.assertEqual(len(all_games), 1)
self.assertEqual(all_games["[email protected]"]["name"], game_name[:256])

def test_remove(self):
"""Test removal of games."""
games = Games()
jid1 = JID(jid="[email protected]")
jid2 = JID(jid="[email protected]")
game_data1 = {"players": ["player1", "player2"], "nbp": "foo", "state": "init"}
game_data1 = {
"players": ["player1", "player2"],
"name": "game1",
"nbp": "foo",
"state": "init",
}
games.add_game(jid1, game_data1)
game_data2 = {"players": ["player3", "player4"], "nbp": "bar", "state": "init"}
game_data2 = {
"players": ["player3", "player4"],
"name": "game2",
"nbp": "bar",
"state": "init",
}
games.add_game(jid2, game_data2)
game_data1.update(
{
Expand Down
1 change: 1 addition & 0 deletions xpartamupp/xpartamupp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def add_game(self, jid, data):
data["players-init"] = data["players"]
data["nbp-init"] = data["nbp"]
data["state"] = "init"
data["name"] = data["name"][:256]
except (KeyError, TypeError, ValueError):
logger.warning("Received invalid data for add game from %s: %s", jid, data)
return False
Expand Down

0 comments on commit 4e56dc0

Please sign in to comment.