From 0cf7c8ea2c4a7b7235f0093af8fc9abbcbfeb83a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 16 Nov 2022 04:23:54 +0000 Subject: [PATCH 1/5] chore(deps): bump loader-utils from 2.0.3 to 2.0.4 Bumps [loader-utils](https://github.com/webpack/loader-utils) from 2.0.3 to 2.0.4. - [Release notes](https://github.com/webpack/loader-utils/releases) - [Changelog](https://github.com/webpack/loader-utils/blob/v2.0.4/CHANGELOG.md) - [Commits](https://github.com/webpack/loader-utils/compare/v2.0.3...v2.0.4) --- updated-dependencies: - dependency-name: loader-utils dependency-type: indirect ... Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index ebfb52fd..83c05525 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4835,9 +4835,9 @@ loader-runner@^4.2.0: integrity sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg== loader-utils@^2.0.0: - version "2.0.3" - resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.3.tgz#d4b15b8504c63d1fc3f2ade52d41bc8459d6ede1" - integrity sha512-THWqIsn8QRnvLl0shHYVBN9syumU8pYWEHPTmkiVGd+7K5eFNVSY6AJhRvgGF70gg1Dz+l/k8WicvFCxdEs60A== + version "2.0.4" + resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.4.tgz#8b5cb38b5c34a9a018ee1fc0e6a066d1dfcc528c" + integrity sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw== dependencies: big.js "^5.2.2" emojis-list "^3.0.0" From d2d91f4aa514cebc5735e1e89b4ad9caf0adf802 Mon Sep 17 00:00:00 2001 From: Gerardo Grignoli Date: Wed, 16 Nov 2022 23:49:26 -0300 Subject: [PATCH 2/5] Fix Invoke-gsudo for Pwsh >= 7.3.0 --- src/gsudo.Wrappers/Invoke-gsudo.ps1 | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/gsudo.Wrappers/Invoke-gsudo.ps1 b/src/gsudo.Wrappers/Invoke-gsudo.ps1 index 7fbac2e4..4d934ade 100644 --- a/src/gsudo.Wrappers/Invoke-gsudo.ps1 +++ b/src/gsudo.Wrappers/Invoke-gsudo.ps1 @@ -140,7 +140,7 @@ if ($Debug) { Write-Debug "Full Script to run on the isolated instance: { $remoteCmd }" } -$pwsh = ("""$([System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName)""") # Get same running powershell EXE. +$pwsh = ("$([System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName)") # Get same running powershell EXE. if ($host.Name -notmatch 'consolehost') { # Workaround for PowerShell ISE, or PS hosted inside other process if ($PSVersionTable.PSVersion.Major -le 5) @@ -150,18 +150,12 @@ if ($host.Name -notmatch 'consolehost') { # Workaround for PowerShell ISE, or PS } $windowTitle = $host.ui.RawUI.WindowTitle; - -$dbg = if ($debug) {"--debug "} else {" "} - -if ($LoadProfile -and (-not $NoProfile -or (gsudo.exe --loglevel None config PowerShellLoadProfile).Split(" = ")[1] -like "*true*")) { - $sNoProfile = "" -} else { - $sNoProfile = "-NoProfile " -} +$arguments = "-d", "--LogLevel", "Error" if ($credential) { $currentSid = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value; $user = "-u $($credential.UserName) " + $arguments += "-u", $credential.UserName # At the time of writing this, there is no way (considered secure) to send the password to gsudo. So instead of sending the password, lets start a credentials cache instance. Start-Process "gsudo.exe" -Args "$dbg -u $($credential.UserName) gsudoservice $PID $CurrentSid All 00:05:00" -credential $Credential -LoadUserProfile -WorkingDirectory "$env:windir" *> $null @@ -173,10 +167,20 @@ if ($credential) { $user = ""; } -$arguments = "-d --LogLevel Error $user$dbg$pwsh $sNoProfile -nologo -NonInteractive -OutputFormat Xml -InputFormat Text -encodedCommand IAAoACQAaQBuAHAAdQB0ACAAfAAgAE8AdQB0AC0AUwB0AHIAaQBuAGcAKQAgAHwAIABpAGUAeAAgAA==".Split(" ") +if ($debug) { $arguments += "--debug"} + +$arguments += $pwsh + +if ($LoadProfile -and (-not $NoProfile -or (gsudo.exe --loglevel None config PowerShellLoadProfile).Split(" = ")[1] -like "*true*")) { +} else { + $arguments += "-NoProfile" +} + +$arguments += "-NoLogo", "-NonInteractive", "-OutputFormat", "Xml", "-InputFormat", "Text", "-encodedCommand", "IAAoACQAaQBuAHAAdQB0ACAAfAAgAE8AdQB0AC0AUwB0AHIAaQBuAGcAKQAgAHwAIABpAGUAeAAgAA==" + # Must Read: https://stackoverflow.com/questions/68136128/how-do-i-call-the-powershell-cli-robustly-with-respect-to-character-encoding-i?noredirect=1&lq=1 -$result = $remoteCmd | & gsudo.exe $arguments *>&1 +$result = $remoteCmd | & gsudo.exe @arguments *>&1 $host.ui.RawUI.WindowTitle = $windowTitle; From 74c4a46ffa20d025bd56fd85eac69bc30f313599 Mon Sep 17 00:00:00 2001 From: Gerardo Grignoli Date: Fri, 18 Nov 2022 14:55:19 -0300 Subject: [PATCH 3/5] Fix: On Powershell >= 7.3, when receiving a string literal command, replacing " with \" is no longer needed. --- src/gsudo/Helpers/CommandToRunAdapter.cs | 9 ++- src/gsudo/Helpers/ShellHelper.cs | 70 ++++++++++++------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/gsudo/Helpers/CommandToRunAdapter.cs b/src/gsudo/Helpers/CommandToRunAdapter.cs index 1109cc72..6cb1811e 100644 --- a/src/gsudo/Helpers/CommandToRunAdapter.cs +++ b/src/gsudo/Helpers/CommandToRunAdapter.cs @@ -163,9 +163,12 @@ Running ./gsudo {command} should elevate the powershell command. } // ---- - string pscommand = string.Join(" ", args) - .ReplaceOrdinal("\"", "\\\"") - .Quote(); + string pscommand = string.Join(" ", args); + + if (ShellHelper.GetInvokingShellVersion() < new Version(7, 3, 0)) + pscommand = pscommand.ReplaceOrdinal("\"", "\\\""); + + pscommand = pscommand.Quote(); newArgs.Add(pscommand); } diff --git a/src/gsudo/Helpers/ShellHelper.cs b/src/gsudo/Helpers/ShellHelper.cs index bdb21e80..c7a91296 100644 --- a/src/gsudo/Helpers/ShellHelper.cs +++ b/src/gsudo/Helpers/ShellHelper.cs @@ -28,12 +28,7 @@ public static string InvokingShellFullPath { get { - if (!IsIntialized) - { - _invokingShell = Initialize(out _invokingShellFullPath); - IsIntialized = true; - } - + if (!IsIntialized) Initialize(); return _invokingShellFullPath; } } @@ -42,19 +37,20 @@ public static Shell InvokingShell { get { - if (!IsIntialized) - { - _invokingShell = Initialize(out _invokingShellFullPath); - IsIntialized = true; - } - + if (!IsIntialized) Initialize(); return _invokingShell.Value; } } private static bool IsIntialized { get; set; } - private static Shell Initialize(out string invokingShellFullPath) + private static void Initialize() + { + _invokingShell = InitializeInternal(out _invokingShellFullPath); + IsIntialized = true; + } + + private static Shell InitializeInternal(out string invokingShellFullPath) { var parentProcess = Process.GetCurrentProcess().GetParentProcessExcludingShim(); @@ -63,37 +59,34 @@ private static Shell Initialize(out string invokingShellFullPath) invokingShellFullPath = parentProcess.GetExeName(); var shell = DetectShellByFileName(invokingShellFullPath); - + if (shell.HasValue) return shell.Value; - else + // Depending on how pwsh was installed, Pwsh.exe -calls-> dotnet -calls-> gsudo. + var grandParentProcess = parentProcess.GetParentProcessExcludingShim(); + if (grandParentProcess != null) { - // Depending on how pwsh was installed, Pwsh.exe -calls-> dotnet -calls-> gsudo. - var grandParentProcess = parentProcess.GetParentProcessExcludingShim(); - if (grandParentProcess != null) + var grandParentExeName = Path.GetFileName(grandParentProcess.GetExeName()).ToUpperInvariant(); + if (grandParentExeName == "PWSH.EXE" || grandParentExeName == "PWSH") { - var grandParentExeName = Path.GetFileName(grandParentProcess.GetExeName()).ToUpperInvariant(); - if (grandParentExeName == "PWSH.EXE" || grandParentExeName == "PWSH") - { - invokingShellFullPath = grandParentProcess.GetExeName(); + invokingShellFullPath = grandParentProcess.GetExeName(); - FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(invokingShellFullPath); + Version fileVersion = GetInvokingShellVersion(); - if (Version.Parse(versionInfo.FileVersion) <= Version.Parse("6.2.3.0") && invokingShellFullPath.EndsWith(".dotnet\\tools\\pwsh.exe", StringComparison.OrdinalIgnoreCase)) - { - return Helpers.Shell.PowerShellCore623BuggedGlobalInstall; - } - - return Helpers.Shell.PowerShellCore; + if (fileVersion <= new Version(6, 2, 3) && invokingShellFullPath.EndsWith(".dotnet\\tools\\pwsh.exe", StringComparison.OrdinalIgnoreCase)) + { + return Helpers.Shell.PowerShellCore623BuggedGlobalInstall; } - } - if (ProcessFactory.IsWindowsApp(invokingShellFullPath)) - { - invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC"); - return Helpers.Shell.WindowsApp; // Called from explorer.exe, task mgr, etc. + return Helpers.Shell.PowerShellCore; } } + + if (ProcessFactory.IsWindowsApp(invokingShellFullPath)) + { + invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC"); + return Helpers.Shell.WindowsApp; // Called from explorer.exe, task mgr, etc. + } } // Unknown Shell. @@ -102,6 +95,15 @@ private static Shell Initialize(out string invokingShellFullPath) invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC"); return Helpers.Shell.Cmd; } + + + public static Version GetInvokingShellVersion() + { + if (!IsIntialized) Initialize(); + FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(_invokingShellFullPath); + var fileVersion = new Version(versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart); + return fileVersion; + } public static Shell? DetectShellByFileName(string filename) { From 506efa024af0cef6e4b0cfec42e0c8c5d0b1472c Mon Sep 17 00:00:00 2001 From: Gerardo Grignoli Date: Mon, 21 Nov 2022 12:24:45 -0300 Subject: [PATCH 4/5] Fixed Initialization --- src/gsudo/Helpers/ShellHelper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/gsudo/Helpers/ShellHelper.cs b/src/gsudo/Helpers/ShellHelper.cs index c7a91296..744ba207 100644 --- a/src/gsudo/Helpers/ShellHelper.cs +++ b/src/gsudo/Helpers/ShellHelper.cs @@ -99,7 +99,6 @@ private static Shell InitializeInternal(out string invokingShellFullPath) public static Version GetInvokingShellVersion() { - if (!IsIntialized) Initialize(); FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(_invokingShellFullPath); var fileVersion = new Version(versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart); return fileVersion; From 5b19673ceb9b30ef47eed03774c810411752b88e Mon Sep 17 00:00:00 2001 From: Gerardo Grignoli Date: Mon, 21 Nov 2022 13:23:18 -0300 Subject: [PATCH 5/5] docs: fixed 'Edit this page' link --- docs/docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docusaurus.config.js b/docs/docusaurus.config.js index 8aae489c..899e1620 100644 --- a/docs/docusaurus.config.js +++ b/docs/docusaurus.config.js @@ -31,7 +31,7 @@ const config = { showReadingTime: true, // Please change this to your repo. editUrl: - 'https://github.com/gerardog/gsudo/tree/docs/docs/blog/', + 'https://github.com/gerardog/gsudo/tree/master/docs/blog/', }, theme: { customCss: require.resolve('./src/css/custom.css'),