Skip to content

Commit 1b95e15

Browse files
authored
Merge pull request #64969 from dotnet/copilot/sub-pr-64966
Fix command injection vulnerability in WSL certificate trust
2 parents 6efd7e6 + bcbf3ac commit 1b95e15

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Shared/CertificateGeneration/UnixCertificateManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ private static bool TryTrustCertificateInWindowsStore(string certificatePath)
617617
// PowerShell command to import the certificate into the CurrentUser Root store.
618618
// We use Import-Certificate which can handle PEM files on modern Windows.
619619
// The -CertStoreLocation parameter specifies the store location.
620+
// Using -EncodedCommand with Base64 encoding to avoid command shell escaping issues.
621+
// We still need to escape single quotes within the PowerShell script itself to prevent
622+
// PowerShell injection vulnerabilities.
620623
var escapedPath = certificatePath.Replace("'", "''");
621624
var escapedFriendlyName = WslFriendlyName.Replace("'", "''");
622625
var powershellScript = $@"
@@ -628,7 +631,10 @@ private static bool TryTrustCertificateInWindowsStore(string certificatePath)
628631
$store.Close()
629632
";
630633

631-
var startInfo = new ProcessStartInfo(PowerShellCommand, $"-NoProfile -NonInteractive -Command \"{powershellScript}\"")
634+
// Encode the PowerShell script to Base64 (UTF-16LE as required by PowerShell)
635+
var encodedCommand = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(powershellScript));
636+
637+
var startInfo = new ProcessStartInfo(PowerShellCommand, $"-NoProfile -NonInteractive -EncodedCommand {encodedCommand}")
632638
{
633639
RedirectStandardOutput = true,
634640
RedirectStandardError = true,

0 commit comments

Comments
 (0)