gerardog/gsudo

Issue: Backslashes at end of command, in Powershell, cause gsudo to silently fail

PitchBlackNights opened this issue · 0 comments

Issue Description

I was creating a python script that requires elevation, and I just decided to use gsudo to provide the elevation. I noticed that it would succeed when I ran the script directly (python ...) or with gsudo python .... But when I ran it with gsudo !! (after running it directly with python ...), it never actually ran the script.

Steps to Reproduce

  1. Open PowerShell (with gsudoModule imported in your PowerShell profile)
  2. Run a command that accepts a file path as the last argument, and add an extra backslash at the end of your path. (For example: ls .\FOLDER\)
  3. Now run gsudo !! and observe that no command output is show (This is because the command was never actually run)
  4. You can also run the command directly with gsudo (For example: gsudo "ls .\FOLDER\")

Logs

Succesfull Run

gsudo.exe --loglevel All "python .\test.py .\a"
Debug: Invoking Shell: PowerShellCore
Debug: Command Line: --loglevel All "python .\test.py .\a"
Debug: Command to run: "C:\Program Files\PowerShell\7\pwsh.EXE" -NoLogo -NoProfile -Command "python .\test.py .\a"
Debug: Using Console mode TokenSwitch
Debug: Caller PID: 22348
Debug: Connected via Named Pipe ProtectedPrefix\Administrators\gsudo_[REDACTED].
Debug: Creating target process: "C:\Program Files\PowerShell\7\pwsh.EXE" -NoLogo -NoProfile -Command "python .\test.py .\a"
Debug: Process token successfully substituted.
You should see this
['.\\test.py', '.\\a']
Debug: Process exited with code 0

Silently Failed Run

gsudo.exe --loglevel All "python .\test.py .\a\"
Debug: Invoking Shell: PowerShellCore
Debug: Command Line: --loglevel All "python .\test.py .\a\\"
Debug: Command to run: "C:\Program Files\PowerShell\7\pwsh.EXE" -NoLogo -NoProfile -Command "python .\test.py .\a\\\"
Debug: Using Console mode TokenSwitch
Debug: Caller PID: 22348
Debug: Connected via Named Pipe ProtectedPrefix\Administrators\gsudo_[REDACTED].
Debug: Creating target process: "C:\Program Files\PowerShell\7\pwsh.EXE" -NoLogo -NoProfile -Command "python .\test.py .\a\\\"
Debug: Process token successfully substituted.
Debug: Process exited with code 1

Additional Info

Trying to run the actual PowerShell Command that gsudo generates still works.

"C:\Program Files\PowerShell\7\pwsh.EXE" -NoLogo -NoProfile -Command "python .\test.py .\a\\\"
=
C:\\*Program` Files*\\PowerShell\\7\\pwsh.EXE -NoLogo -NoProfile -Command "python .\test.py .\a\\\"

My guess is that when parsing the command, it doesn't correctly escape the ending backslash, and that is causing something to happen internally with backslashes escaping quotation marks that shouldn't be.
It seems that it correctly escapes the backslashes (.\a\ -> .\a\\) when getting the command line arguments (Debug: Command Line: --loglevel All "python .\test.py .\a\\"), but something wrong happens when it converted the command to a PowerShell Command (.\a\\ -> .\a\\\).

Context:

  • Windows version: 23H2 (OS Build: 22631.4249)
  • gsudo version: v2.5.1 (Branch.tags-v2.5.1.Sha.25d89fcac99b25534108804cb843fcbebe05a872)