diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index aacf5c50e32..2423a87aeb6 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) + { + throw 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..a1671d7f9e8 --- /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 + + + PowerShell has stopped working because of a security issue: Cannot 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)); + } } }