mxc icon indicating copy to clipboard operation
mxc copied to clipboard

mxc-sdk: diagnostic.js runs a PATH-resolved `whoami` at import time, even with diagnostics off

Open Ninozzz95 opened this issue 2 months ago • 2 comments

Package: @microsoft/mxc-sdk 0.8.0, dist/diagnostic.js.

What happens At module load, diagnostic.js computes PIPE_NAME by calling getDiagnosticPipeName(). On Windows this runs execSync('whoami /user /fo csv /nh', …). It runs on every import, whether or not the diagnostic console is enabled. execSync goes through the shell, so whoami is resolved from PATH.

Why it matters

  1. On machines where another whoami comes before System32 in PATH, that program runs instead. Git for Windows' usr/bin is a common case. It prints whoami: extra operand '/user' on the host process's stderr. In a full-screen terminal application that text lands on top of the UI.
  2. Any executable named whoami found earlier in PATH runs at import time with the host's rights, even though the caller never enabled diagnostics.

Suggested fix (any of these)

  • Compute the pipe name lazily, only when the diagnostic console is enabled.
  • Run %SystemRoot%\System32\whoami.exe by absolute path with execFileSync (no shell).
  • Obtain the SID without spawning a process.

Repro On Windows, put Git for Windows' usr\bin before System32 in PATH, then import the package, for example with node --input-type=module -e "await import('@microsoft/mxc-sdk')". The whoami error appears on stderr.

Ninozzz95 avatar Sep 24 '26 18:09 Ninozzz95

Also seen with 0.9.0 and 1.0.0 on Windows 11 25H2 when the host process runs from a Git Bash session: the PATH-resolved whoami is MSYS's, which prints whoami: extra operand '/user' to stderr on every import (the run continues). Resolving %SystemRoot%\System32\whoami.exe explicitly, or using the token APIs instead, would avoid both the wrong binary and the PATH lookup.

PierrunoYT avatar Oct 09 '26 11:10 PierrunoYT

Still present in 1.1.0 on Windows (dist/diagnostic.js:33, PIPE_NAME computed at module load, line 49); 1.1.0 skips it off Windows. Two more points from embedding it:

  • execSync goes through cmd.exe, which looks in the current directory before PATH (unless NoDefaultCurrentDirectoryInExePath is set). Measured: a whoami.cmd in the importing process's cwd ran at import. For an app whose cwd can be a folder a sandboxed workload writes to, that is code from inside the sandbox running outside it.
  • getPlatformSupport() on Windows runs execSync('dism /online …') (dist/v1/platform.js:108) the same way, so the same applies to its first call.

What we do meanwhile: process.chdir() to the app's own folder around the import() and around every synchronous SDK call. execFileSync with %SystemRoot%\System32\whoami.exe / dism.exe (no shell) would remove the need.

mrestrepo95 avatar Nov 19 '26 13:11 mrestrepo95