From 6855fda8b22bd9de0bb0e5551dd18f8a1140be1b Mon Sep 17 00:00:00 2001 From: James Holderness Date: Thu, 20 Jan 2022 17:33:04 +0000 Subject: [PATCH 1/2] Make sure the title is always sanitized. --- src/host/consoleInformation.cpp | 14 ++++++++++++++ src/host/getset.cpp | 21 +-------------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/host/consoleInformation.cpp b/src/host/consoleInformation.cpp index 9489ff59094..7313132b73b 100644 --- a/src/host/consoleInformation.cpp +++ b/src/host/consoleInformation.cpp @@ -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; diff --git a/src/host/getset.cpp b/src/host/getset.cpp index 82241ea09e1..0e18bddebc2 100644 --- a/src/host/getset.cpp +++ b/src/host/getset.cpp @@ -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; } From aff6264323a6b34b42389849e36efd8d7eb8f4d7 Mon Sep 17 00:00:00 2001 From: James Holderness Date: Thu, 20 Jan 2022 20:39:37 +0000 Subject: [PATCH 2/2] Make sure we have vt handles when testing conpty. --- src/host/ConsoleArguments.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/host/ConsoleArguments.cpp b/src/host/ConsoleArguments.cpp index 148a6d321d2..333f9e1d859 100644 --- a/src/host/ConsoleArguments.cpp +++ b/src/host/ConsoleArguments.cpp @@ -667,7 +667,7 @@ 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: // - // Return Value: @@ -675,5 +675,7 @@ bool ConsoleArguments::IsWin32InputModeEnabled() const void ConsoleArguments::EnableConptyModeForTests() { _headless = true; + _vtInHandle = GetStdHandle(STD_INPUT_HANDLE); + _vtOutHandle = GetStdHandle(STD_OUTPUT_HANDLE); } #endif