@@ -76,15 +76,26 @@ def _browse(auth_uri, browser_name=None): # throws ImportError, webbrowser.Erro
7676
7777 # In WSL which doesn't have www-browser, try launching browser with PowerShell
7878 if not browser_opened and is_wsl ():
79- try :
80- import subprocess
81- # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
82- # Ampersand (&) should be quoted
83- exit_code = subprocess .call (
84- ['powershell.exe' , '-NoProfile' , '-Command' , 'Start-Process "{}"' .format (auth_uri )])
79+ import subprocess
80+ try : # Try wslview first, which is the recommended way on WSL
81+ # https://github.com/wslutilities/wslu
82+ exit_code = subprocess .call (['wslview' , auth_uri ])
8583 browser_opened = exit_code == 0
86- except FileNotFoundError : # WSL might be too old
84+ except FileNotFoundError : # wslview might not be installed
8785 pass
86+ if not browser_opened :
87+ try :
88+ # Fallback to powershell.exe, using -EncodedCommand to prevent injection.
89+ # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
90+ import base64
91+ # PowerShell expects UTF-16LE for EncodedCommand
92+ cmd = u'Start-Process "{}"' .format (auth_uri .replace ('"' , '`"' ))
93+ encoded_cmd = base64 .b64encode (cmd .encode ('utf-16-le' )).decode ('ascii' )
94+ exit_code = subprocess .call (
95+ ['powershell.exe' , '-NoProfile' , '-NonInteractive' , '-EncodedCommand' , encoded_cmd ])
96+ browser_opened = exit_code == 0
97+ except (FileNotFoundError , ImportError ): # WSL might be too old
98+ pass
8899 return browser_opened
89100
90101
0 commit comments