File tree Expand file tree Collapse file tree
src/DeepPurge.Core/Safety Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments