Skip to content

Commit 65bf3d0

Browse files
committed
Error on dubious characters in ending messages
Curly braces and newlines can allow arbitrary messages to be sent to an engine. Do not allow these characters to be sent in end-of-game messages. Add tests to make sure an exception is raised for invalid characters.
1 parent a1f16f8 commit 65bf3d0

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

chess/engine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,9 @@ async def send_opponent_information(self, *, opponent: Optional[Opponent] = None
25082508
async def send_game_result(self, board: chess.Board, winner: Optional[Color] = None, game_ending: Optional[str] = None, game_complete: bool = True) -> None:
25092509
class XBoardGameResultCommand(BaseCommand[XBoardProtocol, None]):
25102510
def start(self, engine: XBoardProtocol) -> None:
2511+
if game_ending and any(c in game_ending for c in "{}\n\r"):
2512+
raise EngineError(f"invalid line break or curly braces in game ending message: {game_ending!r}")
2513+
25112514
engine._new(board, engine.game, {}) # Send final moves to engine.
25122515

25132516
outcome = board.outcome(claim_draw=True)

test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,6 +3752,22 @@ async def main():
37523752
await protocol.send_game_result(timeout_board, chess.BLACK, "Time forfeiture")
37533753
mock.assert_done()
37543754

3755+
error_board = chess.Board()
3756+
mock.expect("new")
3757+
mock.expect("force")
3758+
mock.expect("st 5")
3759+
mock.expect("nopost")
3760+
mock.expect("easy")
3761+
mock.expect("go", ["move e2e4"])
3762+
mock.expect_ping()
3763+
result = await protocol.play(error_board, limit, game="error")
3764+
self.assertEqual(result.move, error_board.parse_uci("e2e4"))
3765+
error_board.push(result.move)
3766+
for c in "\n\r{}":
3767+
with self.assertRaises(chess.engine.EngineError):
3768+
await protocol.send_game_result(error_board, chess.BLACK, f"Time{c}forfeiture")
3769+
mock.assert_done()
3770+
37553771
material_board = chess.Board("k7/8/8/8/8/8/8/K7 b - - 0 1")
37563772
self.assertTrue(material_board.is_insufficient_material())
37573773
mock.expect("new")

0 commit comments

Comments
 (0)