Bug Report: The conhost command line is not properly escaped #1454

Open
opened 2026-01-30 22:27:39 +00:00 by claunia · 0 comments
Owner

Originally created by @fcharlie on GitHub (Jun 1, 2019).

Environment

Windows build number: [run "ver" at a command prompt]
Windows Terminal version (if applicable):

Any other software?

Today I modified the Windows Terminal configuration file and wanted to create a Pwsh with the emoji title. However, I encountered some confusion in this process. I checked the task manager and found that conhost did not escape when the process was started, causing the command line to parse the exception.

Steps to reproduce

Expected behavior

I need to start pwsh by the following command line (note that the following command line is in cmd, or you can start pwsh normally using CreateProcessW.)

"C:\Program Files\PowerShell\7-preview\pwsh.exe" -NoExit -Command "$Host.UI.RawUI.WindowTitle=\"Windows Pwsh 💙 (7 Preview)\""

Here is the Windows Terminal configuration file (the command line has been properly escaped):

        {
            "startingDirectory": "C:\\Users\\CharlieInc",
            "guid": "{08a0be98-ff68-4e3a-a054-0fbd3969d3bb}",
            "name": "Windows Pwsh 💙 (7 Preview)",
            "colorscheme": "Campbell",
            "historySize": 9001,
            "snapOnInput": true,
            "cursorColor": "#FFFFFF",
            "cursorShape": "bar",
            "commandline": "\"C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe\" -NoExit -Command \"$Host.UI.RawUI.WindowTitle=\\\"Windows Pwsh 💙 (7 Preview)\\\"\"",
            "fontFace": "Consolas",
            "fontSize": 12,
            "acrylicOpacity": 0.75,
            "useAcrylic": true,
            "closeOnExit": false,
            "padding": "0, 0, 0, 0",
            "icon": "ms-appdata:///roaming/pwsh-32.png"
        }

Actual behavior

As expected, Pwsh should set the title correctly, but pwsh reported the error:
image

Let's take a look at the command line:

image

image

When you see the red line, the command line that pwsh starts is incorrect, and when you start conhost, the command line is still correct!

Currently I have found a suspicious code, most likely this code caused an error:

71e19cd825/src/host/ConsoleArguments.cpp (L263-L288)

In this code, command line composition is simply a simple connection rather than a concatenated string. If the string contains spaces, this will result in an incorrect command line.

Below is a code for the command line escaping, which may be useful:

inline std::wstring escape_argument(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.data(), ac.size());
  }
  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;
}
Originally created by @fcharlie on GitHub (Jun 1, 2019). <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: [run "ver" at a command prompt] Windows Terminal version (if applicable): Any other software? ``` Today I modified the Windows Terminal configuration file and wanted to create a Pwsh with the emoji title. However, I encountered some confusion in this process. I checked the task manager and found that conhost did not escape when the process was started, causing the command line to parse the exception. # Steps to reproduce <!-- A description of how to trigger this bug. --> # Expected behavior <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> I need to start pwsh by the following command line (note that the following command line is in cmd, or you can start pwsh normally using CreateProcessW.) ``` "C:\Program Files\PowerShell\7-preview\pwsh.exe" -NoExit -Command "$Host.UI.RawUI.WindowTitle=\"Windows Pwsh 💙 (7 Preview)\"" ``` Here is the Windows Terminal configuration file (the command line has been properly escaped): ```json { "startingDirectory": "C:\\Users\\CharlieInc", "guid": "{08a0be98-ff68-4e3a-a054-0fbd3969d3bb}", "name": "Windows Pwsh 💙 (7 Preview)", "colorscheme": "Campbell", "historySize": 9001, "snapOnInput": true, "cursorColor": "#FFFFFF", "cursorShape": "bar", "commandline": "\"C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe\" -NoExit -Command \"$Host.UI.RawUI.WindowTitle=\\\"Windows Pwsh 💙 (7 Preview)\\\"\"", "fontFace": "Consolas", "fontSize": 12, "acrylicOpacity": 0.75, "useAcrylic": true, "closeOnExit": false, "padding": "0, 0, 0, 0", "icon": "ms-appdata:///roaming/pwsh-32.png" } ``` # Actual behavior As expected, Pwsh should set the title correctly, but pwsh reported the error: ![image](https://user-images.githubusercontent.com/6904176/58741634-e7558300-844d-11e9-9b7e-dcdff905ffa3.png) Let's take a look at the command line: ![image](https://user-images.githubusercontent.com/6904176/58741674-49ae8380-844e-11e9-9ca4-43ce9bf4b1ab.png) ![image](https://user-images.githubusercontent.com/6904176/58741666-38657700-844e-11e9-8711-e813c22bc137.png) When you see the red line, the command line that pwsh starts is incorrect, and when you start conhost, the command line is still correct! <!-- What's actually happening? --> Currently I have found a suspicious code, most likely this code caused an error: https://github.com/microsoft/terminal/blob/71e19cd82528d66a0a7867cbed85990cfc1685f1/src/host/ConsoleArguments.cpp#L263-L288 In this code, command line composition is simply a simple connection rather than a concatenated string. If the string contains spaces, this will result in an incorrect command line. Below is a code for the command line escaping, which may be useful: ```cpp inline std::wstring escape_argument(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.data(), ac.size()); } 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; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#1454