Skip to content
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
5 changes: 3 additions & 2 deletions apps/desktop/scripts/neovim-terminal/transport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ export function wslArgs(route) {
export function run(command, args, { input, timeout, cwd, capture = false, env } = {}) {
return new Promise((resolve, reject) => {
// Packaged Electron can inherit a pipe as fd 0 from PowerShell even in a
// terminal window. Open the console explicitly for the interactive child.
// terminal window. Open the console read/write for interactive children:
// WSL 2.2.4 rejects a read-only handle with Wsl/Service/E_ACCESSDENIED.
const consoleInput =
// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone packaged helper owns its platform-specific console handles.
input === undefined && process.platform === "win32"
? NodeFS.openSync("\\\\.\\CONIN$", "r")
? NodeFS.openSync("\\\\.\\CONIN$", "r+")
: undefined;
let child;
try {
Expand Down
70 changes: 70 additions & 0 deletions apps/desktop/scripts/neovim-terminal/transport.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,76 @@ const request = (overrides = {}) => ({
...overrides,
});

// oxlint-disable-next-line t3code/no-global-process-runtime -- Exercises native console handles in the standalone Windows helper.
test.skipIf(process.platform !== "win32")(
"interactive children receive console input with read and write access",
async () => {
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-console-access-"));
const powershell = NodePath.join(
process.env.SystemRoot,
"System32/WindowsPowerShell/v1.0/powershell.exe",
);
const encode = (script) => Buffer.from(script, "utf16le").toString("base64");
const checkAccess = encode(`
$ErrorActionPreference = 'Stop'
Add-Type @'
using System;
using System.Runtime.InteropServices;
public static class InputAccess {
[DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int id);
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool GetConsoleMode(IntPtr handle, out uint mode);
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool FlushConsoleInputBuffer(IntPtr handle);
}
'@
$handle = [InputAccess]::GetStdHandle(-10)
[uint32]$mode = 0
if (-not [InputAccess]::GetConsoleMode($handle, [ref]$mode)) { throw 'Console read access is missing' }
if (-not [InputAccess]::FlushConsoleInputBuffer($handle)) { throw 'Console write access is missing' }
Write-Output 'console-read-write-ok'
`);
// Allocate a private console so the access check never flushes a user's input.
const launch = encode(`
$ErrorActionPreference = 'Stop'
Add-Type @'
using System.Runtime.InteropServices;
public static class PrivateConsole {
[DllImport("kernel32.dll")] public static extern bool FreeConsole();
[DllImport("kernel32.dll")] public static extern bool AllocConsole();
}
'@
[PrivateConsole]::FreeConsole() | Out-Null
if (-not [PrivateConsole]::AllocConsole()) { throw 'Could not allocate test console' }
try {
$child = Start-Process -FilePath $env:T3_TEST_CONSOLE_RUNTIME -ArgumentList ('"' + $env:T3_TEST_CONSOLE_SCRIPT + '"') -NoNewWindow -Wait -PassThru
exit $child.ExitCode
} finally { [PrivateConsole]::FreeConsole() | Out-Null }
`);
try {
const script = NodePath.join(directory, "check.mjs");
const transport = new URL("transport.mjs", import.meta.url).href;
await NodeFSP.writeFile(
script,
`import { run } from ${JSON.stringify(transport)};
console.log(await run(${JSON.stringify(powershell)}, ['-NoProfile', '-EncodedCommand', ${JSON.stringify(checkAccess)}], { capture: true, timeout: 15000 }));
`,
);
const output = await run(powershell, ["-NoProfile", "-EncodedCommand", launch], {
input: "",
capture: true,
timeout: 30000,
env: {
...process.env,
T3_TEST_CONSOLE_RUNTIME: process.execPath,
T3_TEST_CONSOLE_SCRIPT: script,
},
});
NodeAssert.match(output, /console-read-write-ok/u);
} finally {
await NodeFSP.rm(directory, { recursive: true, force: true });
}
},
);

test("target bytes remain data across token and Windows Terminal grammar", () => {
for (const name of [
"spaces here",
Expand Down
Loading