From d845be376ad2b1a2a0a6063787b8c801baa09f9a Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 21 Aug 2020 12:55:11 +0500 Subject: [PATCH 1/3] Write better error message if config file is broken --- .../engine/PSConfiguration.cs | 4 + .../resources/PSConfigurationStrings.resx | 123 ++++++++++++++++++ test/xUnit/csharp/test_PSConfiguration.cs | 26 ++++ 3 files changed, 153 insertions(+) create mode 100644 src/System.Management.Automation/resources/PSConfigurationStrings.resx diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index aacf5c50e32..c3a1343ba74 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -402,6 +402,10 @@ private T ReadValueFromFile(ConfigScope scope, string key, T defaultValue = d configData = serializer.Deserialize(jsonReader) ?? emptyConfig; } + catch (Exception exc) + { + PSTraceSource.NewInvalidOperationException(exc, PSConfigurationStrings.CanNotConfigurationFile, args: fileName); + } finally { fileLock.ExitReadLock(); diff --git a/src/System.Management.Automation/resources/PSConfigurationStrings.resx b/src/System.Management.Automation/resources/PSConfigurationStrings.resx new file mode 100644 index 00000000000..59a7af4fe6c --- /dev/null +++ b/src/System.Management.Automation/resources/PSConfigurationStrings.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Can not read the configuration file: {0} + + diff --git a/test/xUnit/csharp/test_PSConfiguration.cs b/test/xUnit/csharp/test_PSConfiguration.cs index 45e057bea75..558d8b6e3ca 100644 --- a/test/xUnit/csharp/test_PSConfiguration.cs +++ b/test/xUnit/csharp/test_PSConfiguration.cs @@ -361,6 +361,22 @@ private void CreateEmptyFile(string fileName) File.Create(fileName).Dispose(); } + public void SetupConfigFile5() + { + CleanupConfigFiles(); + + // System wide config file is broken + CreateBrokenConfigFile(systemWideConfigFile); + + // Current user config file is broken + CreateBrokenConfigFile(currentUserConfigFile); + } + + private void CreateBrokenConfigFile(string fileName) + { + File.WriteAllText(fileName, "[abbra"); + } + internal void ForceReadingFromFile() { // Reset the cached roots. @@ -951,5 +967,15 @@ public void Utils_GetPolicySetting_BothConfigFilesNotExist() consoleSessionConfiguration = Utils.GetPolicySetting(Utils.CurrentUserThenSystemWideConfig); fixture.CompareConsoleSessionConfiguration(consoleSessionConfiguration, null); } + + [Fact, TestPriority(11)] + public void PowerShellConfig_GetPowerShellPolicies_BrokenSystemConfig() + { + fixture.SetupConfigFile5(); + fixture.ForceReadingFromFile(); + + Assert.Throws(() => PowerShellConfig.Instance.GetPowerShellPolicies(ConfigScope.AllUsers)); + Assert.Throws(() => PowerShellConfig.Instance.GetPowerShellPolicies(ConfigScope.CurrentUser)); + } } } From 6ac1547f648e7d093f4e80c6634eb76467f5af12 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 21 Aug 2020 13:10:17 +0500 Subject: [PATCH 2/3] Fix typo. --- src/System.Management.Automation/engine/PSConfiguration.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index c3a1343ba74..2423a87aeb6 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -404,7 +404,7 @@ private T ReadValueFromFile(ConfigScope scope, string key, T defaultValue = d } catch (Exception exc) { - PSTraceSource.NewInvalidOperationException(exc, PSConfigurationStrings.CanNotConfigurationFile, args: fileName); + throw PSTraceSource.NewInvalidOperationException(exc, PSConfigurationStrings.CanNotConfigurationFile, args: fileName); } finally { From 54d3ae20c56b845d12414780cfcfcbc4e4684337 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 25 Aug 2020 08:29:28 +0500 Subject: [PATCH 3/3] Update src/System.Management.Automation/resources/PSConfigurationStrings.resx Co-authored-by: Travis Plunk --- .../resources/PSConfigurationStrings.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/resources/PSConfigurationStrings.resx b/src/System.Management.Automation/resources/PSConfigurationStrings.resx index 59a7af4fe6c..a1671d7f9e8 100644 --- a/src/System.Management.Automation/resources/PSConfigurationStrings.resx +++ b/src/System.Management.Automation/resources/PSConfigurationStrings.resx @@ -118,6 +118,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Can not read the configuration file: {0} + PowerShell has stopped working because of a security issue: Cannot read the configuration file: {0}