Skip to content

Commit 835d30c

Browse files
committed
IsValidSolutionFolderPath -> IsSolutionFolderPathInDirectoryScope
1 parent 5eb45b8 commit 835d30c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/Cli/dotnet/commands/dotnet-sln/add/Program.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ private static string GetSolutionFolderPathWithForwardSlashes(string path)
3232
return "/" + string.Join("/", PathUtility.GetPathWithDirectorySeparator(path).Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)) + "/";
3333
}
3434

35-
private static bool IsValidSolutionFolderPath(string pathFragment)
35+
private static bool IsSolutionFolderPathInDirectoryScope(string relativePath)
3636
{
37-
return !string.IsNullOrEmpty(pathFragment) && !pathFragment
38-
.Split(Path.DirectorySeparatorChar)
39-
.Any(pathFragment => pathFragment == ".." || pathFragment == "." || Path.GetInvalidFileNameChars().Any(c => pathFragment.Contains(c)));
37+
return !string.IsNullOrEmpty(relativePath)
38+
&& !Path.IsPathRooted(relativePath) // This means path is in a different volume
39+
&& !relativePath.StartsWith(".."); // This means path is outside the solution directory
4040
}
4141

4242
public AddProjectToSolutionCommand(ParseResult parseResult) : base(parseResult)
@@ -137,8 +137,8 @@ private async Task AddProjectsToSolutionAsync(IEnumerable<string> projectPaths,
137137
relativeSolutionFolderPath = Path.Combine([.. relativeSolutionFolderPath.Split(Path.DirectorySeparatorChar).SkipLast(1)]);
138138
}
139139

140-
// If the generated path is invalid, make it empty
141-
if (!IsValidSolutionFolderPath(relativeSolutionFolderPath))
140+
// If the generated path is outside the solution directory, we need to set it to empty
141+
if (!IsSolutionFolderPathInDirectoryScope(relativeSolutionFolderPath))
142142
{
143143
relativeSolutionFolderPath = string.Empty;
144144
}

0 commit comments

Comments
 (0)