From 7631c33ff5c5542863d596b22a9324e8b00345eb Mon Sep 17 00:00:00 2001 From: jimmy wu Date: Wed, 12 Apr 2023 12:06:51 -0400 Subject: [PATCH] encode player name before sending to game (#138) --- services/client/src/hooks/useGame.ts | 2 +- .../spacerover/game/websocket/server/GameServer.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/services/client/src/hooks/useGame.ts b/services/client/src/hooks/useGame.ts index db45556..9510780 100644 --- a/services/client/src/hooks/useGame.ts +++ b/services/client/src/hooks/useGame.ts @@ -174,7 +174,7 @@ const useGame = (gameSocketURL: string, durationInSeconds: number) => { if (gameState === GameState.NotStarted) { setPlayerName(playerName); setGameMode(gameMode); - sendMessage(Event.Start, [playerName, gameMode].join(",")); + sendMessage(Event.Start, [encodeURIComponent(playerName), gameMode].join(",")); setGameState(GameState.InGame); startTimer(); } diff --git a/services/game/src/main/java/io/openliberty/spacerover/game/websocket/server/GameServer.java b/services/game/src/main/java/io/openliberty/spacerover/game/websocket/server/GameServer.java index 57d0c34..f2a8282 100644 --- a/services/game/src/main/java/io/openliberty/spacerover/game/websocket/server/GameServer.java +++ b/services/game/src/main/java/io/openliberty/spacerover/game/websocket/server/GameServer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. + * Copyright (c) 2022, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,6 +11,9 @@ package io.openliberty.spacerover.game.websocket.server; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -143,6 +146,12 @@ private String getErrorMessage(final String errorText) { private void startGame(final String[] properties) { String playerId = properties[0]; + try { + playerId = URLDecoder.decode(playerId, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + // utf-8 always supported + } + int gameMode = Integer.parseInt(properties[1]); LOGGER.log(Level.INFO, "Start Game received for player ID: {0}, GameMode: {1}", new Object[] { playerId, gameMode });