Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

feat: add command escaping. fixes #46

fatDuckeaeb69fa7949
diff --git a/optimizerDuck/Services/UI/BloatwareService.cs b/optimizerDuck/Services/UI/BloatwareService.cs
index 40077f3..d68b640 100644
--- a/optimizerDuck/Services/UI/BloatwareService.cs
+++ b/optimizerDuck/Services/UI/BloatwareService.cs
@@ -112,6 +112,32 @@ public class BloatwareService(
}
}
+ // Validates an AppX package name/identifier to prevent PowerShell injection.
+ // AppX names follow: Publisher.PackageName (alphanumeric, dots, dashes)
+ // PackageFullName follows: Name_Version_Architecture__ResourceId
+ private static readonly Regex AppXNameValidationRegex = new(
+ @"^[a-zA-Z0-9._\-]+$",
+ RegexOptions.Compiled
+ );
+
+ private static readonly Regex AppXPackageFullNameValidationRegex = new(
+ @"^[a-zA-Z0-9._\-]+_[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+_[a-zA-Z0-9_]+__[a-zA-Z0-9_]+$",
+ RegexOptions.Compiled
+ );
+
+ /// <summary>
+ /// Escapes a string value for safe embedding in a PowerShell double-quoted string.
+ /// Replaces <c>"</c> with <c>`"</c> and <c>$</c> with <c>`$</c> to prevent
+ /// string break-out and variable expansion.
+ /// </summary>
+ private static string EscapeForPowerShell(string value)
+ {
+ return value
+ .Replace("`", "``")
+ .Replace("$", "`$")
+ .Replace("\"", "`\"");
+ }
+
/// <summary>
/// Removes an AppX package from the system.
/// </summary>
@@ -130,6 +156,22 @@ public class BloatwareService(
return;
}
+ // Validate AppX identifiers to prevent PowerShell injection
+ var safeName = EscapeForPowerShell(appXPackage.Name ?? string.Empty);
+ var safePackageFullName = EscapeForPowerShell(appXPackage.PackageFullName);
+
+ if (!AppXNameValidationRegex.IsMatch(appXPackage.Name ?? string.Empty))
+ logger.LogWarning(
+ "AppX package name contains unusual characters: {Name}",
+ appXPackage.Name
+ );
+
+ if (!AppXPackageFullNameValidationRegex.IsMatch(appXPackage.PackageFullName))
+ logger.LogWarning(
+ "AppX PackageFullName has unexpected format: {PackageFullName}",
+ appXPackage.PackageFullName
+ );
+
logger.LogInformation(
"Removing AppX package {Name} ({Package})",
appXPackage.Name,
@@ -137,8 +179,8 @@ public class BloatwareService(
);
var script = $$"""
- $pkgFull = "{{appXPackage.PackageFullName}}"
- $name = "{{appXPackage.Name}}"
+ $pkgFull = "{{safePackageFullName}}"
+ $name = "{{safeName}}"
$installed = Get-AppxPackage -PackageTypeFilter Main,Bundle,Resource |
Where-Object { $_.PackageFullName -eq $pkgFull -or $_.Name -eq $name }
diff --git a/optimizerDuck/UI/ViewModels/Pages/StartupManagerViewModel.cs b/optimizerDuck/UI/ViewModels/Pages/StartupManagerViewModel.cs
index 71a68f1..de50c92 100644
--- a/optimizerDuck/UI/ViewModels/Pages/StartupManagerViewModel.cs
+++ b/optimizerDuck/UI/ViewModels/Pages/StartupManagerViewModel.cs
@@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
+using System.IO;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
@@ -123,15 +124,22 @@ public partial class StartupManagerViewModel : ViewModel
[RelayCommand]
private void OpenLocation(StartupApp? app)
{
- if (app?.CanOpenLocation == true)
- try
- {
- Process.Start("explorer.exe", $"/select,\"{app.FilePath}\"");
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Failed to open location for {Name}", app.Name);
- }
+ if (app?.CanOpenLocation != true || app.FilePath == null)
+ return;
+
+ try
+ {
+ // Canonicalize and sanitize the file path to prevent argument injection
+ // into explorer.exe's /select command.
+ var safePath = app.FilePath.Replace("\"", string.Empty);
+ safePath = Path.GetFullPath(safePath);
+
+ Process.Start("explorer.exe", $"/select,\"{safePath}\"");
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Failed to open location for {Name}", app.Name);
+ }
}
[RelayCommand]