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

Make sure titles always sanitized before passing over conpty #12211

Merged
2 commits merged into from
Jan 24, 2022
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
4 changes: 3 additions & 1 deletion src/host/ConsoleArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,15 @@ bool ConsoleArguments::IsWin32InputModeEnabled() const
#ifdef UNIT_TESTING
// Method Description:
// - This is a test helper method. It can be used to trick us into thinking
// we're headless (in conpty mode), even without parsing any arguments.
// we're in conpty mode, even without parsing any arguments.
// Arguments:
// - <none>
// Return Value:
// - <none>
void ConsoleArguments::EnableConptyModeForTests()
{
_headless = true;
_vtInHandle = GetStdHandle(STD_INPUT_HANDLE);
_vtOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}
#endif
14 changes: 14 additions & 0 deletions src/host/consoleInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ InputBuffer* const CONSOLE_INFORMATION::GetActiveInputBuffer() const
void CONSOLE_INFORMATION::SetTitle(const std::wstring_view newTitle)
{
_Title = std::wstring{ newTitle.begin(), newTitle.end() };

// Sanitize the input if we're in pty mode. No control chars - this string
// will get emitted back to the TTY in a VT sequence, and we don't want
// to embed control characters in that string. Note that we can't use
// IsInVtIoMode for this test, because the VT I/O thread won't have
// been created when the title is first set during startup.
if (ServiceLocator::LocateGlobals().launchArgs.InConptyMode())
{
_Title.erase(std::remove_if(_Title.begin(), _Title.end(), [](auto ch) {
return ch < UNICODE_SPACE || (ch > UNICODE_DEL && ch < UNICODE_NBSP);
}),
_Title.end());
}

_TitleAndPrefix = _Prefix + _Title;

auto* const pRender = ServiceLocator::LocateGlobals().pRender;
Expand Down
21 changes: 1 addition & 20 deletions src/host/getset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1798,26 +1798,7 @@ void DoSrvPrivateRefreshWindow(_In_ const SCREEN_INFORMATION& screenInfo)
[[nodiscard]] HRESULT DoSrvSetConsoleTitleW(const std::wstring_view title) noexcept
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

// Sanitize the input if we're in pty mode. No control chars - this string
// will get emitted back to the TTY in a VT sequence, and we don't want
// to embed control characters in that string.
if (gci.IsInVtIoMode())
{
std::wstring sanitized{ title };
sanitized.erase(std::remove_if(sanitized.begin(), sanitized.end(), [](auto ch) {
return ch < UNICODE_SPACE || (ch > UNICODE_DEL && ch < UNICODE_NBSP);
}),
sanitized.end());

gci.SetTitle({ sanitized });
}
else
{
// SetTitle will trigger the renderer to update the titlebar for us.
gci.SetTitle(title);
}

gci.SetTitle(title);
return S_OK;
}

Expand Down