From 344268b4bed973b10aa14b7cf1b7ccef2e197fd8 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 17 Sep 2015 22:23:07 -0500 Subject: [PATCH 1/2] (GH-349) Ignore PowerShell InitializeDefaultDrives If you have a network drive that is not available or has been disconnected, PowerShell will happily throw an error about it with the following message: "Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed." Since it is not really an error, we should log the message accordingly and move on, but not fail the choco install due to some crazy warning that for some reason is an error message. --- .../infrastructure.app/services/PowershellService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index abaeb51cbd..44d3937686 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -32,6 +32,7 @@ public class PowershellService : IPowershellService private readonly IFileSystem _fileSystem; private readonly string _customImports; private const string OPERATION_COMPLETED_SUCCESSFULLY = "The operation completed successfully."; + private const string INITIALIZE_DEFAULT_DRIVES = "Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed."; public PowershellService(IFileSystem fileSystem) : this(fileSystem, new CustomString(string.Empty)) @@ -275,7 +276,7 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack (s, e) => { if (string.IsNullOrWhiteSpace(e.Data)) return; - if (e.Data.is_equal_to(OPERATION_COMPLETED_SUCCESSFULLY)) + if (e.Data.is_equal_to(OPERATION_COMPLETED_SUCCESSFULLY) || e.Data.is_equal_to(INITIALIZE_DEFAULT_DRIVES)) { this.Log().Info(() => " " + e.Data); } From bb270cb73af709472a7c3e77ff72e2b9ef212163 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 17 Sep 2015 23:06:03 -0500 Subject: [PATCH 2/2] (GH-387) Validate before remove rollback folder As a further enhancement for GH-341 (270ea940ee1bd48747f1968307f75c30023535d1), ensure that package names are not attempting to navigate out of the lib backup directory. A specially crafted package name could cause choco to attempt to delete folders it should not, therefore we need to restrict it to the lib backup folder only. If we find we are no longer in a subdirectory of the backup directory, we should return immediately without attempting to delete anything. --- .../services/ChocolateyPackageService.cs | 3 +++ .../infrastructure.app/services/NugetService.cs | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index a112ac9b36..a59879659c 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -703,7 +703,10 @@ private void rollback_previous_version(ChocolateyConfiguration config, PackageRe } } + rollbackDirectory = _fileSystem.get_full_path(rollbackDirectory); + if (string.IsNullOrWhiteSpace(rollbackDirectory) || !_fileSystem.directory_exists(rollbackDirectory)) return; + if (!rollbackDirectory.StartsWith(ApplicationParameters.PackageBackupLocation) || rollbackDirectory.is_equal_to(ApplicationParameters.PackageBackupLocation)) return; this.Log().Debug("Attempting rollback"); diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index de24741f83..f08f9768ff 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -376,7 +376,7 @@ public ConcurrentDictionary install_run(ChocolateyConfigu public void remove_rollback_directory_if_exists(string packageName) { - var rollbackDirectory = _fileSystem.combine_paths(ApplicationParameters.PackageBackupLocation, packageName); + var rollbackDirectory = _fileSystem.get_full_path(_fileSystem.combine_paths(ApplicationParameters.PackageBackupLocation, packageName)); if (!_fileSystem.directory_exists(rollbackDirectory)) { //search for folder @@ -385,10 +385,13 @@ public void remove_rollback_directory_if_exists(string packageName) { rollbackDirectory = possibleRollbacks.OrderByDescending(p => p).DefaultIfEmpty(string.Empty).FirstOrDefault(); } - } + rollbackDirectory = _fileSystem.get_full_path(rollbackDirectory); + } + if (string.IsNullOrWhiteSpace(rollbackDirectory) || !_fileSystem.directory_exists(rollbackDirectory)) return; - + if (!rollbackDirectory.StartsWith(ApplicationParameters.PackageBackupLocation) || rollbackDirectory.is_equal_to(ApplicationParameters.PackageBackupLocation)) return; + FaultTolerance.try_catch_with_logging_exception( () => _fileSystem.delete_directory_if_exists(rollbackDirectory, recursive: true), "Attempted to remove '{0}' but had an error:".format_with(rollbackDirectory),