Skip to content

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

fix: revert shell commands from disk lack integrity check and some hanging problem

fatDuck20d369b58264
diff --git a/optimizerDuck/Services/Revert/RevertManager.cs b/optimizerDuck/Services/Revert/RevertManager.cs
index 6bc0b41..856811d 100644
--- a/optimizerDuck/Services/Revert/RevertManager.cs
+++ b/optimizerDuck/Services/Revert/RevertManager.cs
@@ -182,7 +182,7 @@ public class RevertManager(ILogger<RevertManager> _logger, ILoggerFactory _logge
try
{
var data =
- await LoadAsyncUnlocked(filePath)
+ await LoadAsync(filePath)
?? new RevertData
{
SchemaVersion = SchemaVersion,
@@ -264,17 +264,6 @@ public class RevertManager(ILogger<RevertManager> _logger, ILoggerFactory _logge
}
private static async Task<RevertData?> LoadAsync(string path)
- {
- if (!File.Exists(path))
- return null;
-
- // Static reads are inherently safe with atomic writes (WriteJsonAtomicAsync uses temp+replace),
- // so we avoid allocating a SemaphoreSlim in the global dictionary for read-only operations.
- // This prevents unbounded dictionary growth from one-off reads that never call RemoveRevertData.
- return await LoadAsyncUnlocked(path);
- }
-
- private static async Task<RevertData?> LoadAsyncUnlocked(string path)
{
if (!File.Exists(path))
return null;
@@ -289,7 +278,35 @@ public class RevertManager(ILogger<RevertManager> _logger, ILoggerFactory _logge
if (string.IsNullOrWhiteSpace(json))
return null;
- return JsonConvert.DeserializeObject<RevertData>(json);
+ var data = JsonConvert.DeserializeObject<RevertData>(json);
+ if (data == null)
+ return null;
+
+ // Validate the file is within the expected revert directory (path traversal guard)
+ var resolvedPath = Path.GetFullPath(path);
+ var revertDir = Path.GetFullPath(Shared.RevertDirectory);
+ if (!resolvedPath.StartsWith(revertDir, StringComparison.OrdinalIgnoreCase))
+ {
+ TraceCorruptRevertFile(
+ path,
+ new InvalidOperationException("Revert file path outside expected directory")
+ );
+ return null;
+ }
+
+ // Validate schema version
+ if (data.SchemaVersion != SchemaVersion)
+ {
+ TraceCorruptRevertFile(
+ path,
+ new InvalidOperationException(
+ $"Unsupported schema version {data.SchemaVersion} (expected {SchemaVersion})"
+ )
+ );
+ return null;
+ }
+
+ return data;
}
catch (IOException) when (i < 2)
{
diff --git a/optimizerDuck/Services/System/SystemInfoService.cs b/optimizerDuck/Services/System/SystemInfoService.cs
index 79f8c57..e0a38a8 100644
--- a/optimizerDuck/Services/System/SystemInfoService.cs
+++ b/optimizerDuck/Services/System/SystemInfoService.cs
@@ -496,10 +496,9 @@ public sealed record SystemSnapshot
internal static class WmiHelper
{
- private static readonly Dictionary<string, ManagementScope> ScopeCache = new(
+ private static readonly ConcurrentDictionary<string, ManagementScope> ScopeCache = new(
StringComparer.OrdinalIgnoreCase
);
- private static readonly Lock ScopeLock = new();
private static readonly WmiEnumerationOptions DefaultOptions = new()
{
@@ -526,24 +525,20 @@ internal static class WmiHelper
private static ManagementScope GetScope(string namespacePath = @"root\cimv2")
{
- lock (ScopeLock)
- {
- if (ScopeCache.TryGetValue(namespacePath, out var cached))
- return cached;
-
- var scope = new ManagementScope(namespacePath);
- scope.Connect();
- ScopeCache[namespacePath] = scope;
- return scope;
- }
+ return ScopeCache.GetOrAdd(
+ namespacePath,
+ static ns =>
+ {
+ var scope = new ManagementScope(ns);
+ scope.Connect();
+ return scope;
+ }
+ );
}
public static void DisposeScopes()
{
- lock (ScopeLock)
- {
- ScopeCache.Clear();
- }
+ ScopeCache.Clear();
}
public static IEnumerable<ManagementObject> Query(
@@ -571,8 +566,9 @@ internal static class WmiHelper
/// <summary>
/// Executes a WMI query and passes results to a selector.
/// All ManagementObject items are disposed after the selector completes.
+ /// Returns <c>default(T)</c> if the query fails or throws — callers must null-check for reference types.
/// </summary>
- public static T Query<T>(
+ public static T? Query<T>(
string query,
Func<IEnumerable<ManagementObject>, T> selector,
string namespacePath = @"root\cimv2"
@@ -593,7 +589,7 @@ internal static class WmiHelper
}
catch
{
- return default!;
+ return default;
}
finally
{
@@ -647,6 +643,14 @@ internal static class WmiHelper
}
}
+ /// <summary>
+ /// Escapes a value for safe use in WQL string literals (doubles single quotes).
+ /// </summary>
+ public static string EscapeWql(string value)
+ {
+ return value.Replace("'", "''");
+ }
+
public static ManagementObject? GetFirst(string query, string namespacePath = @"root\cimv2")
{
try
@@ -709,7 +713,7 @@ internal static class CpuProvider
{
private static readonly Lazy<CpuInfo> _cached = new(
Load,
- LazyThreadSafetyMode.ExecutionAndPublication
+ LazyThreadSafetyMode.PublicationOnly
);
/// <summary>
@@ -813,7 +817,7 @@ internal static class RamProvider
{
private static readonly Lazy<List<RamModule>> _cachedModules = new(
LoadPhysicalModules,
- LazyThreadSafetyMode.ExecutionAndPublication
+ LazyThreadSafetyMode.PublicationOnly
);
/// <summary>
@@ -1063,7 +1067,7 @@ public static class DiskHelper
try
{
var partitions = WmiHelper.Query(
- $"ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{driveLetter.TrimEnd('\\')}' }} WHERE AssocClass=Win32_LogicalDiskToPartition"
+ $"ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{WmiHelper.EscapeWql(driveLetter.TrimEnd('\\'))}' }} WHERE AssocClass=Win32_LogicalDiskToPartition"
);
foreach (var partition in partitions)
@@ -1071,7 +1075,7 @@ public static class DiskHelper
try
{
var diskDrives = WmiHelper.Query(
- $"ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
+ $"ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{WmiHelper.EscapeWql(partition["DeviceID"]?.ToString() ?? "")}'}} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
);
foreach (var disk in diskDrives)
@@ -1405,7 +1409,7 @@ internal static class DiskProvider
var letter = driveLetter.TrimEnd('\\');
var classicPartitions = WmiHelper.Query(
- $"ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{letter}'}} WHERE AssocClass=Win32_LogicalDiskToPartition"
+ $"ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{WmiHelper.EscapeWql(letter)}'}} WHERE AssocClass=Win32_LogicalDiskToPartition"
);
foreach (var partition in classicPartitions)
@@ -1413,7 +1417,7 @@ internal static class DiskProvider
try
{
var diskDrives = WmiHelper.Query(
- $"ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
+ $"ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{WmiHelper.EscapeWql(partition["DeviceID"]?.ToString() ?? "")}'}} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
);
foreach (var disk in diskDrives)
@@ -1676,23 +1680,23 @@ internal static class GpuProvider
private static IReadOnlyList<GpuInfo> GetAllViaDxgi()
{
- var factory = DxgiHelper.CreateFactory();
- var wmiLookup = BuildWmiLookup(); // lightweight WMI for DriverVersion + DeviceID
- var gpus = new List<GpuInfo>();
-
+ DxgiHelper.IDXGIFactory1? factory = null;
try
{
+ factory = DxgiHelper.CreateFactory();
+ var wmiLookup = BuildWmiLookup();
+ var gpus = new List<GpuInfo>();
+
for (uint i = 0; ; i++)
{
var hr = factory.EnumAdapters1(i, out var adapter);
if (hr != 0 || adapter == null)
- break; // DXGI_ERROR_NOT_FOUND
+ break;
try
{
adapter.GetDesc1(out var desc);
- // Skip software / virtual adapters
if (DxgiHelper.IsSoftwareAdapter(desc))
continue;
@@ -1703,14 +1707,14 @@ internal static class GpuProvider
var vendor = DetectGpuVendorById(desc.VendorId);
var memoryMB = (int)((long)desc.DedicatedVideoMemory / (1024 * 1024));
- // Match to WMI entry for DriverVersion and DeviceID
var wmiMatch = FindWmiMatch(name, desc.VendorId, desc.DeviceId, wmiLookup);
gpus.Add(
new GpuInfo
{
Name = name,
- DriverVersion = wmiMatch?.DriverVersion ?? Translations.Common_Unknown,
+ DriverVersion =
+ wmiMatch?.DriverVersion ?? Translations.Common_Unknown,
Vendor = vendor,
MemoryMB = memoryMB > 0 ? memoryMB : null,
DeviceId = wmiMatch?.DeviceId,
@@ -1723,14 +1727,14 @@ internal static class GpuProvider
Marshal.ReleaseComObject(adapter);
}
}
+
+ return gpus.Count > 0 ? gpus : [GpuInfo.Unknown];
}
finally
{
if (factory != null)
Marshal.ReleaseComObject(factory);
}
-
- return gpus.Count > 0 ? gpus : [GpuInfo.Unknown];
}
// ── WMI fallback path ──────────────────────────────────────────────────
@@ -2242,6 +2246,10 @@ public sealed class SystemInfoService : IDisposable
return Snapshot;
}
+ catch (OperationCanceledException)
+ {
+ throw;
+ }
catch (Exception ex)
{
_logger.LogError(ex, "Failed to refresh system info");