@@ -129,12 +129,10 @@ public async Task<OperationResult> InstallAsync(ProgressWindow progressWindow)
129129 public Task < OperationResult > RemoveAsync ( ProgressWindow progressWindow )
130130 {
131131 var rootPath = PathManager . RiivolutionWhWzFolderPath ;
132- var rootFullPath = _fileSystem . Path . GetFullPath ( rootPath + Path . AltDirectorySeparatorChar ) ;
133132
134133 foreach ( var entry in LoadManifest ( ) )
135134 {
136- var fullPath = _fileSystem . Path . GetFullPath ( _fileSystem . Path . Combine ( rootPath , entry ) ) ;
137- if ( ! fullPath . StartsWith ( rootFullPath , StringComparison . Ordinal ) )
135+ if ( ! PathSafetyHelper . TryGetPathWithinDirectory ( rootPath , entry , out var fullPath ) )
138136 continue ;
139137
140138 if ( _fileSystem . File . Exists ( fullPath ) )
@@ -195,7 +193,7 @@ out bool badPassword
195193 badPassword = false ;
196194 try
197195 {
198- using var archive = ArchiveFactory . Open ( zipPath , new ReaderOptions { Password = password } ) ;
196+ using var archive = ArchiveFactory . OpenArchive ( zipPath , new ReaderOptions { Password = password } ) ;
199197 var entries = archive . Entries . Where ( entry => ! entry . IsDirectory ) . ToList ( ) ;
200198 if ( entries . Count == 0 )
201199 return Ok ( ) ;
@@ -205,20 +203,16 @@ out bool badPassword
205203 progressWindow . SetExtraText ( Common . State_Extracting ) . SetGoal ( $ "Extracting { entries . Count } files") ;
206204 } ) ;
207205
208- var absoluteDestinationPath = _fileSystem . Path . GetFullPath ( destinationDirectory + Path . AltDirectorySeparatorChar ) ;
209-
210206 for ( var i = 0 ; i < entries . Count ; i ++ )
211207 {
212208 var entry = entries [ i ] ;
213- var normalized = NormalizeEntryPath ( entry . Key ?? string . Empty ) ;
214- if ( string . IsNullOrWhiteSpace ( normalized ) )
209+ if ( ! PathSafetyHelper . TryNormalizeRelativePath ( entry . Key ?? string . Empty , out var normalized ) )
215210 continue ;
216211
217212 if ( ! TryGetRelativeExtractionPath ( normalized , out var relativePath ) )
218213 return Fail ( "Unexpected file in the test archive. Please contact the developers." ) ;
219214
220- var destinationPath = _fileSystem . Path . GetFullPath ( _fileSystem . Path . Combine ( destinationDirectory , relativePath ) ) ;
221- if ( ! destinationPath . StartsWith ( absoluteDestinationPath , StringComparison . Ordinal ) )
215+ if ( ! PathSafetyHelper . TryGetPathWithinDirectory ( destinationDirectory , relativePath , out var destinationPath ) )
222216 return Fail ( "The file path is outside the destination directory. Please contact the developers." ) ;
223217
224218 var destinationDir = _fileSystem . Path . GetDirectoryName ( destinationPath ) ;
@@ -256,8 +250,6 @@ private static bool IsBadPasswordException(Exception ex)
256250 return ex . InnerException != null && IsBadPasswordException ( ex . InnerException ) ;
257251 }
258252
259- private static string NormalizeEntryPath ( string path ) => path . Replace ( '\\ ' , '/' ) . TrimStart ( '/' ) ;
260-
261253 private bool TryGetRelativeExtractionPath ( string normalizedPath , out string relativePath )
262254 {
263255 relativePath = string . Empty ;
@@ -308,8 +300,6 @@ private OperationResult<List<string>> MoveExtractedFiles(string tempExtractionPa
308300 . Directory . EnumerateFiles ( betaFolderSource , "*" , SearchOption . AllDirectories )
309301 . Concat ( _fileSystem . Directory . EnumerateFiles ( xmlFolderSource , "*" , SearchOption . AllDirectories ) ) ;
310302
311- var absoluteDestinationRoot = _fileSystem . Path . GetFullPath ( destinationRoot + Path . AltDirectorySeparatorChar ) ;
312-
313303 foreach ( var file in sourceFiles )
314304 {
315305 var relativePath = _fileSystem . Path . GetRelativePath ( tempExtractionPath , file ) ;
@@ -319,9 +309,7 @@ private OperationResult<List<string>> MoveExtractedFiles(string tempExtractionPa
319309 continue ;
320310 }
321311
322- var destinationPath = _fileSystem . Path . Combine ( destinationRoot , relativePath ) ;
323- var fullDestinationPath = _fileSystem . Path . GetFullPath ( destinationPath ) ;
324- if ( ! fullDestinationPath . StartsWith ( absoluteDestinationRoot , StringComparison . Ordinal ) )
312+ if ( ! PathSafetyHelper . TryGetPathWithinDirectory ( destinationRoot , relativePath , out var destinationPath ) )
325313 return Fail ( "The file path is outside the destination directory. Please contact the developers." ) ;
326314
327315 var destinationDirectory = _fileSystem . Path . GetDirectoryName ( destinationPath ) ;
0 commit comments