From 847f21e53150713d4f73ab03bb1c7f97e373e8b8 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 15:49:29 -0400 Subject: [PATCH 1/6] Add RawJson field to CommCommand --- .../Components/Interop/CommCommand.cs | 7 ++++++- .../Components/Interop/CommContext.cs | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs b/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs index cc9ab8571a..7145d35aae 100644 --- a/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs +++ b/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs @@ -8,8 +8,13 @@ namespace Tgstation.Server.Host.Components.Interop sealed class CommCommand { /// - /// The raw JSON decond of the + /// The dictionary of the /// public IReadOnlyDictionary Parameters { get; set; } + + /// + /// The raw JSON of the + /// + public string RawJson { get; set; } } } \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/Interop/CommContext.cs b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs index 839086a3bb..1b3c95a6c7 100644 --- a/src/Tgstation.Server.Host/Components/Interop/CommContext.cs +++ b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs @@ -109,7 +109,8 @@ namespace Tgstation.Server.Host.Components.Interop { command = new CommCommand { - Parameters = JsonConvert.DeserializeObject>(file) + Parameters = JsonConvert.DeserializeObject>(file), + RawJson = file }; } catch (JsonSerializationException ex) From bf292ef560fd503dc5d51da1f09e35de333700ff Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 15:50:01 -0400 Subject: [PATCH 2/6] Fix CommContext not catching the right JsonException --- src/Tgstation.Server.Host/Components/Interop/CommContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/Components/Interop/CommContext.cs b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs index 1b3c95a6c7..f9c5208d88 100644 --- a/src/Tgstation.Server.Host/Components/Interop/CommContext.cs +++ b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs @@ -113,7 +113,7 @@ namespace Tgstation.Server.Host.Components.Interop RawJson = file }; } - catch (JsonSerializationException ex) + catch (JsonException ex) { //file not fully written yet logger.LogDebug("Suppressing json convert exception for command file write: {0}", ex); From 64d225e98f38060e5c303dda5420387ad7617a92 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 16:09:29 -0400 Subject: [PATCH 3/6] Implement tgs_chat_send --- .../Components/Watchdog/SessionController.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs b/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs index dd5d2b7ebb..8cd90b5b72 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs @@ -1,6 +1,7 @@ using Byond.TopicSender; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Globalization; @@ -286,6 +287,25 @@ namespace Tgstation.Server.Host.Components.Watchdog content = new object(); switch (method) { + case Constants.DMCommandChat: + try + { + var message = JsonConvert.DeserializeObject(command.RawJson, new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + if (message.ChannelIds == null) + throw new InvalidOperationException("Missing ChannelIds field!"); + if (message.Message == null) + throw new InvalidOperationException("Missing Message field!"); + await chat.SendMessage(message.Message, message.ChannelIds, cancellationToken).ConfigureAwait(false); + } + catch (Exception e) + { + logger.LogDebug("Exception while decoding chat message! Exception: {0}", e); + goto default; + } + break; case Constants.DMCommandServerPrimed: //currently unused, maybe in the future break; From ee62423d1ef090c8e3a537004424eb9af228d9cb Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 16:22:17 -0400 Subject: [PATCH 4/6] Remove unused using --- src/Tgstation.Server.Host/Controllers/InstanceController.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tgstation.Server.Host/Controllers/InstanceController.cs b/src/Tgstation.Server.Host/Controllers/InstanceController.cs index 5db9cd5897..8a4cb807e1 100644 --- a/src/Tgstation.Server.Host/Controllers/InstanceController.cs +++ b/src/Tgstation.Server.Host/Controllers/InstanceController.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Linq.Expressions; using System.Net; using System.Reflection; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Api; From 9dea3ba0887017aaf37a5da45f56fdefe6c27379 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 16:25:21 -0400 Subject: [PATCH 5/6] Prevent instances from being created in the installation directory --- .../Controllers/InstanceController.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Tgstation.Server.Host/Controllers/InstanceController.cs b/src/Tgstation.Server.Host/Controllers/InstanceController.cs index 8a4cb807e1..e220a74e39 100644 --- a/src/Tgstation.Server.Host/Controllers/InstanceController.cs +++ b/src/Tgstation.Server.Host/Controllers/InstanceController.cs @@ -120,6 +120,17 @@ namespace Tgstation.Server.Host.Controllers return BadRequest(new ErrorMessage { Message = "path must not be empty!" }); NormalizeModelPath(model, out var rawPath); + + var localPath = ioManager.ResolvePath("."); + NormalizeModelPath(new Api.Models.Instance + { + Path = localPath + }, out var normalizedLocalPath); + + if (rawPath.StartsWith(normalizedLocalPath, StringComparison.Ordinal)) + return Conflict("Instances cannot be created in the installation directory!"); + + var dirExistsTask = ioManager.DirectoryExists(model.Path, cancellationToken); bool attached = false; if (await ioManager.FileExists(model.Path, cancellationToken).ConfigureAwait(false) || await dirExistsTask.ConfigureAwait(false)) From 0c563d014cdf68eaecbcb739a7ccd883bfdfb3a3 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 23 Oct 2018 16:29:21 -0400 Subject: [PATCH 6/6] Add a test for creating instances in the installation directory --- tests/Tgstation.Server.Tests/InstanceManagerTest.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Tgstation.Server.Tests/InstanceManagerTest.cs b/tests/Tgstation.Server.Tests/InstanceManagerTest.cs index 0c9efe68b9..6a09d73d63 100644 --- a/tests/Tgstation.Server.Tests/InstanceManagerTest.cs +++ b/tests/Tgstation.Server.Tests/InstanceManagerTest.cs @@ -51,6 +51,13 @@ namespace Tgstation.Server.Tests }, cancellationToken)).ConfigureAwait(false); await Assert.ThrowsExceptionAsync(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false); + //can't create instances in installation directory + await Assert.ThrowsExceptionAsync(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance + { + Path = "./A/Local/Path", + Name = "NoInstallDirTest" + }, cancellationToken)).ConfigureAwait(false); + //can't move to existent directories await Assert.ThrowsExceptionAsync(() => instanceManagerClient.Update(new Api.Models.Instance {