diff --git a/src/c#/GeneralUpdate.Core/Configuration/Environments.cs b/src/c#/GeneralUpdate.Core/Configuration/Environments.cs index f946af7d..0cf22bfb 100644 --- a/src/c#/GeneralUpdate.Core/Configuration/Environments.cs +++ b/src/c#/GeneralUpdate.Core/Configuration/Environments.cs @@ -1,24 +1,63 @@ +using System; using System.IO; +using System.Security.Cryptography; +using System.Text; namespace GeneralUpdate.Core.Configuration; +/// +/// Secure IPC environment variable provider. +/// AES-encrypted temp files in a dedicated subdirectory, auto-deleted after read. +/// public static class Environments { + // Fixed key/IV derived from a constant — not crypto-grade, but sufficient for + // ephemeral IPC where the file lives < 1 second and is in a per-user directory. + private static readonly byte[] _aesKey = SHA256.Create() + .ComputeHash(Encoding.UTF8.GetBytes("GeneralUpdate.IPC.EnvironmentProvider.v1")); + private static readonly byte[] _aesIV = new byte[16] { 0x47, 0x55, 0x50, 0x44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + private static string IpcDir + { + get + { + var dir = Path.Combine(Path.GetTempPath(), "GeneralUpdate", "ipc"); + Directory.CreateDirectory(dir); + return dir; + } + } + public static void SetEnvironmentVariable(string key, string value) { - var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt"); - File.WriteAllText(filePath, value); + var filePath = Path.Combine(IpcDir, $"{key}.enc"); + var plainBytes = Encoding.UTF8.GetBytes(value); + using var aes = Aes.Create(); + aes.Key = _aesKey; + aes.IV = _aesIV; + using var encryptor = aes.CreateEncryptor(); + var encrypted = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); + File.WriteAllBytes(filePath, encrypted); } public static string GetEnvironmentVariable(string key) { - var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt"); + var filePath = Path.Combine(Path.GetTempPath(), "GeneralUpdate", "ipc", $"{key}.enc"); if (!File.Exists(filePath)) return string.Empty; - var content = File.ReadAllText(filePath); - File.SetAttributes(filePath, FileAttributes.Normal); - File.Delete(filePath); - return content; + try + { + var encrypted = File.ReadAllBytes(filePath); + using var aes = Aes.Create(); + aes.Key = _aesKey; + aes.IV = _aesIV; + using var decryptor = aes.CreateDecryptor(); + var plainBytes = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length); + return Encoding.UTF8.GetString(plainBytes); + } + finally + { + try { File.Delete(filePath); } catch { /* best-effort cleanup */ } + } } -} \ No newline at end of file +} diff --git a/src/c#/GeneralUpdate.Core/Event/EventManager.cs b/src/c#/GeneralUpdate.Core/Event/EventManager.cs index c67c5a31..6267b209 100644 --- a/src/c#/GeneralUpdate.Core/Event/EventManager.cs +++ b/src/c#/GeneralUpdate.Core/Event/EventManager.cs @@ -7,28 +7,13 @@ namespace GeneralUpdate.Core.Event { public class EventManager : IDisposable { - private static readonly object _lockObj = new(); - private static EventManager _instance; + private static readonly Lazy _lazy = new(() => new EventManager()); private Dictionary _dicDelegates = new(); private bool _disposed = false; private EventManager() { } - public static EventManager Instance - { - get - { - if (_instance == null) - { - lock (_lockObj) - { - if (_instance == null) - _instance = new EventManager(); - } - } - return _instance; - } - } + public static EventManager Instance => _lazy.Value; public void AddListener(Action listener) where TEventArgs : EventArgs {