From 736b1693191e71e84af14ce8d9c2d0e576aa4154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 10 May 2015 19:52:56 +0200 Subject: [PATCH 1/3] sanitize player names --- OpenRA.Game/Network/UnitOrders.cs | 3 ++ OpenRA.Game/Server/Server.cs | 2 +- OpenRA.Game/Settings.cs | 33 +++++++++++++++++++ .../ServerTraits/LobbyCommands.cs | 7 ++-- .../Widgets/Logic/Lobby/LobbyUtils.cs | 2 ++ .../Widgets/Logic/SettingsLogic.cs | 8 +++-- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Network/UnitOrders.cs b/OpenRA.Game/Network/UnitOrders.cs index be3ff8fef2..0355076aed 100644 --- a/OpenRA.Game/Network/UnitOrders.cs +++ b/OpenRA.Game/Network/UnitOrders.cs @@ -152,6 +152,9 @@ namespace OpenRA.Network break; } + Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name); + Game.Settings.Save(); + // Otherwise send the handshake with our current settings and let the server reject us var info = new Session.Client() { diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 1985dbf944..b38718fdc4 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -286,7 +286,7 @@ namespace OpenRA.Server var client = new Session.Client() { - Name = handshake.Client.Name, + Name = OpenRA.Settings.SanitizedPlayerName(handshake.Client.Name), IpAddress = ((IPEndPoint)newConn.Socket.RemoteEndPoint).Address.ToString(), Index = newConn.PlayerIndex, Slot = LobbyInfo.FirstEmptySlot(), diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index c8a5169011..0b3d872749 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -11,7 +11,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using OpenRA.Graphics; +using OpenRA.Traits; namespace OpenRA { @@ -300,6 +302,37 @@ namespace OpenRA root.WriteToFile(settingsFile); } + static string SanitizedName(string dirty) + { + if (string.IsNullOrEmpty(dirty)) + return null; + + var clean = dirty; + + // reserved characters for MiniYAML and JSON + var disallowedChars = new char[] { '#', '@', ':', '\n', '\t', '[', ']', '{', '}', '"', '`' }; + foreach (var disallowedChar in disallowedChars) + clean = clean.Replace(disallowedChar.ToString(), string.Empty); + + // avoid UI glitches + if (clean.Length > 16) + clean = clean.Substring(0, 16); + + return clean; + } + + public static string SanitizedPlayerName(string dirty) + { + var forbiddenNames = new string[] { "Open", "Closed" }; + var botNames = OpenRA.Game.ModData.DefaultRules.Actors["player"].Traits.WithInterface().Select(t => t.Name); + + var clean = SanitizedName(dirty); + if (string.IsNullOrWhiteSpace(clean) || forbiddenNames.Contains(clean) || botNames.Contains(clean)) + clean = new PlayerSettings().Name; + + return clean; + } + static void LoadSectionYaml(MiniYaml yaml, object section) { var defaults = Activator.CreateInstance(section.GetType()); diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index db261a4aed..9bad865b1e 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -717,9 +717,10 @@ namespace OpenRA.Mods.Common.Server { "name", s => { - Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, s); - server.SendMessage("{0} is now known as {1}.".F(client.Name, s)); - client.Name = s; + var sanitizedName = OpenRA.Settings.SanitizedPlayerName(s); + Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, sanitizedName); + server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); + client.Name = sanitizedName; server.SyncLobbyClients(); return true; } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs b/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs index cc61b42482..d91f0f5596 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs @@ -281,6 +281,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic if (name.Text == c.Name) return; + name.Text = Settings.SanitizedPlayerName(name.Text); + orderManager.IssueOrder(Order.Command("name " + name.Text)); Game.Settings.Player.Name = name.Text; Game.Settings.Save(); diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index a5afc008da..524a7ae54b 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -204,9 +204,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic var ps = Game.Settings.Player; var nameTextfield = panel.Get("PLAYERNAME"); - nameTextfield.Text = ps.Name; + nameTextfield.Text = Settings.SanitizedPlayerName(ps.Name); nameTextfield.OnEnterKey = () => { nameTextfield.YieldKeyboardFocus(); return true; }; - nameTextfield.OnLoseFocus = () => { ps.Name = nameTextfield.Text; }; + nameTextfield.OnLoseFocus = () => + { + nameTextfield.Text = Settings.SanitizedPlayerName(nameTextfield.Text); + ps.Name = nameTextfield.Text; + }; var colorPreview = panel.Get("COLOR_MANAGER"); colorPreview.Color = ps.Color; From edca755540c1056d2b85e788d897031da5b58438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 10 May 2015 21:07:22 +0200 Subject: [PATCH 2/3] also sanitize server names --- OpenRA.Game/Server/Server.cs | 3 +++ OpenRA.Game/Settings.cs | 9 +++++++++ .../Widgets/Logic/ServerCreationLogic.cs | 13 +++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index b38718fdc4..2d0f117e81 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -130,6 +130,9 @@ namespace OpenRA.Server Port = localEndpoint.Port; Settings = settings; + + Settings.Name = OpenRA.Settings.SanitizedServerName(Settings.Name); + ModData = modData; randomSeed = (int)DateTime.Now.ToBinary(); diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index 0b3d872749..39ba5debdf 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -321,6 +321,15 @@ namespace OpenRA return clean; } + public static string SanitizedServerName(string dirty) + { + var clean = SanitizedName(dirty); + if (string.IsNullOrWhiteSpace(clean)) + return new ServerSettings().Name; + else + return clean; + } + public static string SanitizedPlayerName(string dirty) { var forbiddenNames = new string[] { "Open", "Closed" }; diff --git a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs index 71f1f67e6d..b61c59b151 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs @@ -55,8 +55,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic panel.Get("MAP_NAME").GetText = () => preview.Title; } - panel.Get("SERVER_NAME").Text = settings.Server.Name ?? ""; + var serverName = panel.Get("SERVER_NAME"); + serverName.Text = Settings.SanitizedServerName(settings.Server.Name); + serverName.OnEnterKey = () => { serverName.YieldKeyboardFocus(); return true; }; + serverName.OnLoseFocus = () => + { + serverName.Text = Settings.SanitizedServerName(serverName.Text); + settings.Server.Name = serverName.Text; + }; + panel.Get("LISTEN_PORT").Text = settings.Server.ListenPort.ToString(); + advertiseOnline = Game.Settings.Server.AdvertiseOnline; var externalPort = panel.Get("EXTERNAL_PORT"); @@ -80,7 +89,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic void CreateAndJoin() { - var name = panel.Get("SERVER_NAME").Text; + var name = Settings.SanitizedServerName(panel.Get("SERVER_NAME").Text); int listenPort, externalPort; if (!Exts.TryParseIntegerInvariant(panel.Get("LISTEN_PORT").Text, out listenPort)) listenPort = 1234; From fefad25d9d6416a47a26a206cb87f82539a5182b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 30 May 2015 09:20:44 +0200 Subject: [PATCH 3/3] don't change player names without necessity --- OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index 9bad865b1e..e7eff5123f 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -718,6 +718,9 @@ namespace OpenRA.Mods.Common.Server s => { var sanitizedName = OpenRA.Settings.SanitizedPlayerName(s); + if (sanitizedName == client.Name) + return true; + Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, sanitizedName); server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); client.Name = sanitizedName;