Skip to content

Commit 11ef420

Browse files
committed
Strip whitespace from game title and enforce minimum length
1 parent 32af084 commit 11ef420

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

server/games/game.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,13 @@ def name(self, value: str):
164164
"""
165165
Verifies that names only contain ascii characters.
166166
"""
167+
value = value.strip()
168+
167169
if not value.isascii():
168-
raise ValueError("Name must be ascii!")
170+
raise ValueError("Game title must be ascii!")
171+
172+
if not value:
173+
raise ValueError("Game title must not be empty!")
169174

170175
self.set_name_unchecked(value)
171176

server/lobbyconnection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ async def command_game_host(self, message):
949949
title = message.get("title") or f"{self.player.login}'s game"
950950
if not title.isascii():
951951
raise ClientError("Title must contain only ascii characters.")
952+
if not title.strip():
953+
raise ClientError("Title must not be empty.")
952954

953955
mod = message.get("mod") or FeaturedModType.FAF
954956
mapname = message.get("mapname") or "scmp_007"

tests/integration_tests/test_server.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,56 @@ async def test_host_missing_fields(lobby_server):
683683
assert msg["featured_mod"] == "faf"
684684

685685

686+
@fast_forward(10)
687+
async def test_host_game_name_only_spaces(lobby_server):
688+
player_id, session, proto = await connect_and_sign_in(
689+
("test", "test_password"),
690+
lobby_server
691+
)
692+
693+
await read_until_command(proto, "game_info")
694+
695+
await proto.send_message({
696+
"command": "game_host",
697+
"mod": "",
698+
"visibility": "public",
699+
"title": " ",
700+
})
701+
702+
msg = await read_until_command(proto, "notice", timeout=10)
703+
704+
assert msg == {
705+
"command": "notice",
706+
"style": "error",
707+
"text": "Title must not be empty.",
708+
}
709+
710+
711+
@fast_forward(10)
712+
async def test_host_game_name_non_ascii(lobby_server):
713+
player_id, session, proto = await connect_and_sign_in(
714+
("test", "test_password"),
715+
lobby_server
716+
)
717+
718+
await read_until_command(proto, "game_info")
719+
720+
await proto.send_message({
721+
"command": "game_host",
722+
"mod": "",
723+
"visibility": "public",
724+
"title": "ÇÒÖL GÃMÊ"
725+
})
726+
727+
msg = await read_until_command(proto, "notice", timeout=10)
728+
729+
assert msg == {
730+
"command": "notice",
731+
"style": "error",
732+
"text": "Title must contain only ascii characters."
733+
}
734+
735+
686736
async def test_play_game_while_queueing(lobby_server):
687737
player_id, session, proto = await connect_and_sign_in(
688738
("test", "test_password"),

tests/unit_tests/test_game.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ async def test_name_sanitization(game, players):
613613
with pytest.raises(ValueError):
614614
game.name = "Hello ⏴⏵⏶⏷⏸⏹⏺⏻♿"
615615

616+
with pytest.raises(ValueError):
617+
game.name = " \n\n\t"
618+
619+
with pytest.raises(ValueError):
620+
game.name = ""
621+
616622
game.name = "A" * 256
617623
assert game.name == "A" * 128
618624

0 commit comments

Comments
 (0)