Skip to content

Commit 1afa5c3

Browse files
committed
sln-add: Don't add solution folders with invalid chars
1 parent d79ec02 commit 1afa5c3

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

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

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

34+
private static bool DoesSolutionFolderPathContainInvalidChars(string pathFragment)
35+
{
36+
return pathFragment
37+
.Split(Path.DirectorySeparatorChar)
38+
.Any(pathFragment => pathFragment == ".." || pathFragment == "." || Path.GetInvalidFileNameChars().Any(c => pathFragment.Contains(c)));
39+
}
40+
3441
public AddProjectToSolutionCommand(ParseResult parseResult) : base(parseResult)
3542
{
3643
_fileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument);
@@ -82,15 +89,10 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
8289
{
8390
Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)
8491
});
92+
8593
// Set default configurations and platforms for sln file
86-
foreach (var platform in _defaultPlatforms)
87-
{
88-
solution.AddPlatform(platform);
89-
}
90-
foreach (var buildType in _defaultBuildTypes)
91-
{
92-
solution.AddBuildType(buildType);
93-
}
94+
_defaultPlatforms.ToList().ForEach(solution.AddPlatform);
95+
_defaultBuildTypes.ToList().ForEach(solution.AddBuildType);
9496
}
9597

9698
SolutionFolderModel? solutionFolder = (!_inRoot && !string.IsNullOrEmpty(_solutionFolderPath))
@@ -100,9 +102,9 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
100102
foreach (var projectPath in projectPaths)
101103
{
102104
string relativePath = Path.GetRelativePath(Path.GetDirectoryName(solutionFileFullPath), projectPath);
103-
// Add fallback solution folder if relative path does not contain `..`.
104-
string relativeSolutionFolder = relativePath.Split(Path.DirectorySeparatorChar).Any(p => p == "..")
105-
? string.Empty : Path.GetDirectoryName(relativePath);
105+
106+
// Add fallback solution folder if relative path does not contain invalid characters
107+
string relativeSolutionFolder = DoesSolutionFolderPathContainInvalidChars(projectPath) ? string.Empty : Path.GetDirectoryName(relativePath);
106108

107109
if (!_inRoot && solutionFolder is null && !string.IsNullOrEmpty(relativeSolutionFolder))
108110
{

0 commit comments

Comments
 (0)