From 2f186d560b848cc99c16e77d203d82ab9688ba44 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 27 Dec 2017 15:39:13 -0700 Subject: [PATCH 1/4] Fix PSES crash when pssa settings path has invalid chars --- .../Server/LanguageServerSettings.cs | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs index cea4c8351..0bf7251cc 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs @@ -66,29 +66,39 @@ public void Update( string settingsPath = settings.SettingsPath; - if (string.IsNullOrWhiteSpace(settingsPath)) + try { - settingsPath = null; - } - else if (!Path.IsPathRooted(settingsPath)) - { - if (string.IsNullOrEmpty(workspaceRootPath)) + if (string.IsNullOrWhiteSpace(settingsPath)) { - // The workspace root path could be an empty string - // when the user has opened a PowerShell script file - // without opening an entire folder (workspace) first. - // In this case we should just log an error and let - // the specified settings path go through even though - // it will fail to load. - logger.Write( - LogLevel.Error, - "Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath."); + settingsPath = null; } - else + else if (!Path.IsPathRooted(settingsPath)) { - settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath)); + if (string.IsNullOrEmpty(workspaceRootPath)) + { + // The workspace root path could be an empty string + // when the user has opened a PowerShell script file + // without opening an entire folder (workspace) first. + // In this case we should just log an error and let + // the specified settings path go through even though + // it will fail to load. + logger.Write( + LogLevel.Error, + "Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath."); + } + else + { + settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath)); + } } } + catch (Exception ex) when (ex is NotSupportedException) + { + // Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here + logger.WriteException( + $"Invalid Script Analyzer settings path - '{settingsPath}'.", + ex); + } this.SettingsPath = settingsPath; } From 8d05da31875db9db91a326b5fe3c5db8de8e3f58 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 27 Dec 2017 16:18:37 -0700 Subject: [PATCH 2/4] Alway log (verbose) the pssa settings path --- .../Server/LanguageServerSettings.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs index 0bf7251cc..cfc1b1fce 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs @@ -101,6 +101,7 @@ public void Update( } this.SettingsPath = settingsPath; + logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'."); } } } From 55fae77554be1cf0347b2f840a523631e072c96a Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 28 Dec 2017 19:16:35 -0700 Subject: [PATCH 3/4] Set SettingsPath to null in catch handler --- .../Server/LanguageServerSettings.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs index cfc1b1fce..958fc536d 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs @@ -91,6 +91,9 @@ public void Update( settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath)); } } + + this.SettingsPath = settingsPath; + logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'."); } catch (Exception ex) when (ex is NotSupportedException) { @@ -98,10 +101,9 @@ public void Update( logger.WriteException( $"Invalid Script Analyzer settings path - '{settingsPath}'.", ex); - } - this.SettingsPath = settingsPath; - logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'."); + this.SettingsPath = null; + } } } } From 6f46cd95002953926b7b196c479799393532a194 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sat, 30 Dec 2017 18:55:21 -0700 Subject: [PATCH 4/4] GetFullPath can also throw PathTooLongExc and SecurityExc --- .../Server/LanguageServerSettings.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs index 958fc536d..1a2aed48a 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs @@ -3,13 +3,12 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -using System.IO; using Microsoft.PowerShell.EditorServices.Utility; using System; -using System.Reflection; using System.Collections; -using System.Linq; -using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Security; namespace Microsoft.PowerShell.EditorServices.Protocol.Server { @@ -95,7 +94,10 @@ public void Update( this.SettingsPath = settingsPath; logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'."); } - catch (Exception ex) when (ex is NotSupportedException) + catch (Exception ex) when ( + ex is NotSupportedException || + ex is PathTooLongException || + ex is SecurityException) { // Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here logger.WriteException(