Skip to content

Commit 38daaa7

Browse files
committed
fix: validate registry backup file content after reg.exe export
1 parent c9eb7ce commit 38daaa7

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/DeepPurge.Core/Safety/BackupManager.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ public string BackupRegistryKey(string registryPath)
4848

4949
using var process = Process.Start(psi);
5050
process?.WaitForExit(30000);
51-
return File.Exists(backupFile) ? backupFile : "";
51+
if (!ValidateBackupFile(backupFile))
52+
{
53+
Diagnostics.Log.Warn($"Registry backup failed validation: {backupFile}");
54+
return "";
55+
}
56+
return backupFile;
5257
}
5358
catch { return ""; }
5459
}
@@ -101,6 +106,20 @@ public void CleanOldBackups(int keepDays = 30)
101106
catch { /* best-effort */ }
102107
}
103108

109+
internal static bool ValidateBackupFile(string path)
110+
{
111+
try
112+
{
113+
if (!File.Exists(path)) return false;
114+
var fi = new FileInfo(path);
115+
if (fi.Length == 0) return false;
116+
using var sr = new StreamReader(path, detectEncodingFromByteOrderMarks: true);
117+
var firstLine = sr.ReadLine();
118+
return firstLine != null && firstLine.Contains("Windows Registry Editor", StringComparison.OrdinalIgnoreCase);
119+
}
120+
catch { return false; }
121+
}
122+
104123
private static string NormalizeRegistryPath(string path) => path
105124
.Replace("HKLM\\", "HKEY_LOCAL_MACHINE\\", StringComparison.OrdinalIgnoreCase)
106125
.Replace("HKCU\\", "HKEY_CURRENT_USER\\", StringComparison.OrdinalIgnoreCase)

0 commit comments

Comments
 (0)