From c043f6815849b7ab0795118e31fada70f9f93d12 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Fri, 31 Jan 2025 17:05:32 -0800 Subject: [PATCH 1/3] Fix bug when adding solution folder containing .. --- src/Cli/dotnet/commands/dotnet-sln/add/Program.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs index 0c35ac84544b..341c7de35cd4 100644 --- a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs +++ b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs @@ -102,6 +102,12 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum string relativePath = Path.GetRelativePath(Path.GetDirectoryName(solutionFileFullPath), projectPath); // Add fallback solution folder string relativeSolutionFolder = Path.GetDirectoryName(relativePath); + // Don't add solution folder if it contains .. + if (relativeSolutionFolder.Contains("..")) + { + relativeSolutionFolder = string.Empty; + } + if (!_inRoot && solutionFolder is null && !string.IsNullOrEmpty(relativeSolutionFolder)) { if (relativeSolutionFolder.Split(Path.DirectorySeparatorChar).LastOrDefault() == Path.GetFileNameWithoutExtension(relativePath)) From 96168b24df9f89c37eb41a9e25f64edc2ff0497e Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Mon, 3 Feb 2025 14:19:54 -0800 Subject: [PATCH 2/3] Refactor --- src/Cli/dotnet/commands/dotnet-sln/add/Program.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs index 341c7de35cd4..d733944a07b1 100644 --- a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs +++ b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs @@ -100,13 +100,9 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum foreach (var projectPath in projectPaths) { string relativePath = Path.GetRelativePath(Path.GetDirectoryName(solutionFileFullPath), projectPath); - // Add fallback solution folder - string relativeSolutionFolder = Path.GetDirectoryName(relativePath); - // Don't add solution folder if it contains .. - if (relativeSolutionFolder.Contains("..")) - { - relativeSolutionFolder = string.Empty; - } + // Add fallback solution folder if relative path does not contain `..`. + string relativeSolutionFolder = relativePath.Contains("..") + ? string.Empty : Path.GetDirectoryName(relativePath); if (!_inRoot && solutionFolder is null && !string.IsNullOrEmpty(relativeSolutionFolder)) { From 1bb9e25fd5897985a6c1b84d30b67003d5b07f44 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Mon, 3 Feb 2025 16:13:13 -0800 Subject: [PATCH 3/3] Apply pr suggestions --- src/Cli/dotnet/commands/dotnet-sln/add/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs index d733944a07b1..a7b2c0ec687d 100644 --- a/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs +++ b/src/Cli/dotnet/commands/dotnet-sln/add/Program.cs @@ -101,7 +101,7 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum { string relativePath = Path.GetRelativePath(Path.GetDirectoryName(solutionFileFullPath), projectPath); // Add fallback solution folder if relative path does not contain `..`. - string relativeSolutionFolder = relativePath.Contains("..") + string relativeSolutionFolder = relativePath.Split(Path.DirectorySeparatorChar).Any(p => p == "..") ? string.Empty : Path.GetDirectoryName(relativePath); if (!_inRoot && solutionFolder is null && !string.IsNullOrEmpty(relativeSolutionFolder))