Skip to content

Commit 2464e93

Browse files
authored
Fix bad pssa settings path crashes PSES (#588)
* Fix PSES crash when pssa settings path has invalid chars * Alway log (verbose) the pssa settings path * Set SettingsPath to null in catch handler * GetFullPath can also throw PathTooLongExc and SecurityExc
1 parent f933b2f commit 2464e93

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs

+37-22
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
using System.IO;
76
using Microsoft.PowerShell.EditorServices.Utility;
87
using System;
9-
using System.Reflection;
108
using System.Collections;
11-
using System.Linq;
12-
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Reflection;
11+
using System.Security;
1312

1413
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
1514
{
@@ -66,31 +65,47 @@ public void Update(
6665

6766
string settingsPath = settings.SettingsPath;
6867

69-
if (string.IsNullOrWhiteSpace(settingsPath))
68+
try
7069
{
71-
settingsPath = null;
72-
}
73-
else if (!Path.IsPathRooted(settingsPath))
74-
{
75-
if (string.IsNullOrEmpty(workspaceRootPath))
70+
if (string.IsNullOrWhiteSpace(settingsPath))
7671
{
77-
// The workspace root path could be an empty string
78-
// when the user has opened a PowerShell script file
79-
// without opening an entire folder (workspace) first.
80-
// In this case we should just log an error and let
81-
// the specified settings path go through even though
82-
// it will fail to load.
83-
logger.Write(
84-
LogLevel.Error,
85-
"Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
72+
settingsPath = null;
8673
}
87-
else
74+
else if (!Path.IsPathRooted(settingsPath))
8875
{
89-
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
76+
if (string.IsNullOrEmpty(workspaceRootPath))
77+
{
78+
// The workspace root path could be an empty string
79+
// when the user has opened a PowerShell script file
80+
// without opening an entire folder (workspace) first.
81+
// In this case we should just log an error and let
82+
// the specified settings path go through even though
83+
// it will fail to load.
84+
logger.Write(
85+
LogLevel.Error,
86+
"Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
87+
}
88+
else
89+
{
90+
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
91+
}
9092
}
93+
94+
this.SettingsPath = settingsPath;
95+
logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'.");
9196
}
97+
catch (Exception ex) when (
98+
ex is NotSupportedException ||
99+
ex is PathTooLongException ||
100+
ex is SecurityException)
101+
{
102+
// Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here
103+
logger.WriteException(
104+
$"Invalid Script Analyzer settings path - '{settingsPath}'.",
105+
ex);
92106

93-
this.SettingsPath = settingsPath;
107+
this.SettingsPath = null;
108+
}
94109
}
95110
}
96111
}

0 commit comments

Comments
 (0)