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 The conhost command line is not properly escaped #1815

Merged
merged 4 commits into from
Jul 12, 2019
Merged
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
75 changes: 74 additions & 1 deletion src/host/ConsoleArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,79 @@ const std::wstring_view ConsoleArguments::INHERIT_CURSOR_ARG = L"--inheritcursor
const std::wstring_view ConsoleArguments::FEATURE_ARG = L"--feature";
const std::wstring_view ConsoleArguments::FEATURE_PTY_ARG = L"pty";

std::wstring EscapeArgument(std::wstring_view ac)
{
if (ac.empty())
{
return L"\"\"";
}
bool hasSpace = false;
auto n = ac.size();
for (auto c : ac)
{
switch (c)
{
case L'"':
case L'\\':
n++;
break;
case ' ':
case '\t':
hasSpace = true;
break;
default:
break;
}
}
if (hasSpace)
{
n += 2;
}
if (n == ac.size())
{
return std::wstring{ ac };
}
std::wstring buf;
if (hasSpace)
{
buf.push_back(L'"');
}
size_t slashes = 0;
for (auto c : ac)
{
switch (c)
{
case L'\\':
slashes++;
buf.push_back(L'\\');
break;
case L'"':
{
for (; slashes > 0; slashes--)
{
buf.push_back(L'\\');
}
buf.push_back(L'\\');
buf.push_back(c);
}
break;
default:
slashes = 0;
buf.push_back(c);
break;
}
}
if (hasSpace)
{
for (; slashes > 0; slashes--)
{
buf.push_back(L'\\');
}
buf.push_back(L'"');
}
return buf;
}

ConsoleArguments::ConsoleArguments(const std::wstring& commandline,
const HANDLE hStdIn,
const HANDLE hStdOut) :
Expand Down Expand Up @@ -272,7 +345,7 @@ void ConsoleArguments::s_ConsumeArg(_Inout_ std::vector<std::wstring>& args, _In
size_t j = 0;
for (j = index; j < args.size(); j++)
{
_clientCommandline += args[j];
_clientCommandline += EscapeArgument(args[j]); // escape commandline
if (j + 1 < args.size())
{
_clientCommandline += L" ";
Expand Down
24 changes: 22 additions & 2 deletions src/host/ut_host/ConsoleArgumentsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void ConsoleArgumentsTests::ArgSplittingTests()
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
ConsoleArguments(commandline,
L"this is the commandline", // clientCommandLine
L"\"this is the commandline\"", // clientCommandLine
fcharlie marked this conversation as resolved.
Show resolved Hide resolved
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
L"", // vtMode
Expand All @@ -110,7 +110,7 @@ void ConsoleArgumentsTests::ArgSplittingTests()
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
ConsoleArguments(commandline,
L"--vtmode bar this is the commandline", // clientCommandLine
L"\"--vtmode bar this is the commandline\"", // clientCommandLine
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
L"", // vtMode
Expand Down Expand Up @@ -223,6 +223,26 @@ void ConsoleArgumentsTests::ArgSplittingTests()
0, // signalHandle
false), // inheritCursor
true); // successful parse?

commandline = L"conhost.exe this is the commandline";
ArgTestsRunner(L"#9 commandline no quotes",
commandline,
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
ConsoleArguments(commandline,
L"this is the commandline", // clientCommandLine
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
L"", // vtMode
0, // width
0, // height
false, // forceV1
false, // headless
true, // createServerHandle
0, // serverHandle
0, // signalHandle
false), // inheritCursor
true); // successful parse?
}

void ConsoleArgumentsTests::ClientCommandlineTests()
Expand Down