Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix windows bat execution #1692

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Tests/SonarScanner.MSBuild.Common.Test/ProcessRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;
Expand Down Expand Up @@ -342,11 +343,8 @@ REM The sonar-scanner.bat uses %* to pass the argument to javac.exe
logger.InfoMessages.Should().BeEquivalentTo(
@"""-Dsonar.scanAllFiles=true"" ""-Dproject.settings=D:\DevLibTest\ClassLibraryTest.sonarqube\out\sonar-project.properties"" ""--from=ScannerMSBuild/5.13.1"" ""--debug""",
@"""-Dsonar.scanAllFiles=true""",
string.Empty,
@"""-Dproject.settings=D:\DevLibTest\ClassLibraryTest.sonarqube\out\sonar-project.properties""",
string.Empty,
@"""--from=ScannerMSBuild/5.13.1""",
string.Empty,
@"""--debug""");
}

Expand Down Expand Up @@ -398,6 +396,31 @@ public void ProcRunner_DoNotLogSensitiveData()
AssertExpectedLogContents(testDir, allArgs);
}

[TestMethod]
public void Test_ShellEscape_NoSpecialCharacters()
{
// Arrange
string input = "-Dsonar.scanAllFiles=true -Dproject.settings=D:\\DevLibTest\\ClassLibraryTest.sonarqube\\out\\sonar-project.properties --from=ScannerMSBuild/5.13.1";

// Act
string result = InvokeShellEscape(input);

// Assert
Assert.AreEqual(input, result);
}

[TestMethod]
public void Test_ShellEscape_WithSpecialCharacters()
{
// Arrange
string input = "-Dsonar.scanAllFiles=true| -Dproject.settings=D:\\DevLibTest\\ClassLibraryTest.sonarqube\\out\\sonar-project.properties^ --from=ScannerMSBuild/5.13.1";

// Act
string result = InvokeShellEscape(input);

// Assert
Assert.AreEqual("-Dsonar.scanAllFiles=true^^^| -Dproject.settings=D:\\DevLibTest\\ClassLibraryTest.sonarqube\\out\\sonar-project.properties^^^^ --from=ScannerMSBuild/5.13.1", result);
}
#endregion Tests

#region Private methods
Expand Down Expand Up @@ -441,6 +464,11 @@ private static void AssertTextDoesNotAppearInLog(string text, IList<string> logE
"Specified text should not appear anywhere in the log file: {0}", text);
}

private string InvokeShellEscape(string input)
{
MethodInfo methodInfo = typeof(ProcessRunnerArguments).GetMethod("ShellEscape", BindingFlags.NonPublic | BindingFlags.Static);
return (string)methodInfo.Invoke(null, new object[] { input });
}
#endregion Private methods
}
}
15 changes: 8 additions & 7 deletions src/SonarScanner.MSBuild.Common/ProcessRunnerArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public ProcessRunnerArguments(string exeName, bool isBatchScript)
/// </summary>
public IDictionary<string, string> EnvironmentVariables { get; set; }

private static readonly HashSet<char> SpecialChars = new HashSet<char> { '^', '>', '<', '|', '&', '"' };

public string GetEscapedArguments()
{
if (CmdLineArgs == null)
Expand Down Expand Up @@ -216,17 +218,16 @@ private static string ShellEscape(string argLine)
var sb = new StringBuilder();
foreach (var c in argLine)
faulycoelho marked this conversation as resolved.
Show resolved Hide resolved
{
// This escape is required after %* is expanded to prevent command injections
sb.Append('^');
sb.Append('^');

// This escape is required only to pass the argument line to the batch script
sb.Append('^');
if (SpecialChars.Contains(c))
{
sb.Append('^');
sb.Append('^');
sb.Append('^');
}
sb.Append(c);
}
return sb.ToString();
}

#endregion Public properties
}
}