Skip to content

Commit

Permalink
Error on dubious characters in ending messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarkZH committed Jul 9, 2023
1 parent a1f16f8 commit 65bf3d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,9 @@ async def send_opponent_information(self, *, opponent: Optional[Opponent] = None
async def send_game_result(self, board: chess.Board, winner: Optional[Color] = None, game_ending: Optional[str] = None, game_complete: bool = True) -> None:
class XBoardGameResultCommand(BaseCommand[XBoardProtocol, None]):
def start(self, engine: XBoardProtocol) -> None:
if game_ending and any(c in game_ending for c in "{}\n\r"):
raise EngineError(f"invalid line break or curly braces in game ending message: {game_ending!r}")

engine._new(board, engine.game, {}) # Send final moves to engine.

outcome = board.outcome(claim_draw=True)
Expand Down
16 changes: 16 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3752,6 +3752,22 @@ async def main():
await protocol.send_game_result(timeout_board, chess.BLACK, "Time forfeiture")
mock.assert_done()

error_board = chess.Board()
mock.expect("new")
mock.expect("force")
mock.expect("st 5")
mock.expect("nopost")
mock.expect("easy")
mock.expect("go", ["move e2e4"])
mock.expect_ping()
result = await protocol.play(error_board, limit, game="error")
self.assertEqual(result.move, error_board.parse_uci("e2e4"))
error_board.push(result.move)
for c in "\n\r{}":
with self.assertRaises(chess.engine.EngineError):
await protocol.send_game_result(error_board, chess.BLACK, f"Time{c}forfeiture")
mock.assert_done()

material_board = chess.Board("k7/8/8/8/8/8/8/K7 b - - 0 1")
self.assertTrue(material_board.is_insufficient_material())
mock.expect("new")
Expand Down

0 comments on commit 65bf3d0

Please sign in to comment.