Skip to content

Commit 22b9e77

Browse files
committed
[FlashpointManager] Improve security of I/O operations
1 parent 86fe126 commit 22b9e77

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

FlashpointManager/src/Common.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,24 @@ public static void SyncManager(bool init = false)
449449
// Deletes a file as well as any directories made empty by its deletion
450450
public static void DeleteFileAndDirectories(string file)
451451
{
452-
try { File.Delete(file); } catch { }
452+
file = Path.GetFullPath(file);
453+
454+
if (!Path.GetFullPath(file).StartsWith(Path.GetFullPath(SourcePath))) return;
455+
456+
try
457+
{
458+
File.Delete(file);
459+
}
460+
catch (Exception e) when (!(e is DirectoryNotFoundException))
461+
{
462+
MessageBox.Show(
463+
"Failed to delete the following file:\n" + file + "\n\n" +
464+
"Make sure it is not open or being used by another program.",
465+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
466+
);
467+
468+
return;
469+
}
453470

454471
string folder = Path.GetDirectoryName(file);
455472

FlashpointManager/src/Forms/Operate.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ private async void Operation_Load(object sender, EventArgs e)
130130
await Task.Run(ApplyComponents);
131131
}
132132

133-
await Task.Run(DeleteTempDirectory);
134-
135133
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress, FPM.Main.Handle);
136134

137135
if (FPM.AutoDownload.Count == 0) FPM.SyncManager();
@@ -292,6 +290,23 @@ private void RemoveComponents()
292290

293291
private void ApplyComponents()
294292
{
293+
void MoveDelete(string source, string dest)
294+
{
295+
try
296+
{
297+
File.Move(source, dest);
298+
FPM.DeleteFileAndDirectories(source);
299+
}
300+
catch
301+
{
302+
MessageBox.Show(
303+
"Failed to move the following file:\n" + source + "\n\n" +
304+
"You will have to move it manually from the Temp folder.",
305+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
306+
);
307+
}
308+
}
309+
295310
string tempPath = Path.Combine(FPM.SourcePath, "Temp");
296311
string tempInfoFile = Path.Combine(tempPath, "Components", workingComponent.ID);
297312
string[] infoText = File.ReadLines(tempInfoFile).Skip(1).ToArray();
@@ -302,25 +317,11 @@ private void ApplyComponents()
302317
string destFile = Path.Combine(FPM.SourcePath, file);
303318

304319
Directory.CreateDirectory(Path.GetDirectoryName(destFile));
305-
try { File.Move(tempFile, destFile); } catch { }
306-
}
307-
308-
try { File.Move(tempInfoFile, workingComponent.InfoFile); } catch { }
309-
}
310-
311-
public static void DeleteTempDirectory()
312-
{
313-
string tempPath = Path.Combine(FPM.SourcePath, "Temp");
314320

315-
if (Directory.Exists(tempPath))
316-
{
317-
foreach (string tempFile in Directory.EnumerateFiles(tempPath))
318-
{
319-
try { File.Delete(tempFile); } catch { }
320-
}
321-
322-
try { Directory.Delete(tempPath, true); } catch { }
321+
MoveDelete(tempFile, destFile);
323322
}
323+
324+
MoveDelete(tempInfoFile, workingComponent.InfoFile);
324325
}
325326

326327
private async void CancelButton_Click(object sender, EventArgs e)
@@ -337,7 +338,17 @@ await Task.Run(() =>
337338
{
338339
while (cancelStatus != 2) { }
339340

340-
DeleteTempDirectory();
341+
string tempPath = Path.Combine(FPM.SourcePath, "Temp");
342+
343+
if (Directory.Exists(tempPath))
344+
{
345+
foreach (string tempFile in Directory.EnumerateFiles(tempPath))
346+
{
347+
try { File.Delete(tempFile); } catch { }
348+
}
349+
350+
try { Directory.Delete(tempPath, true); } catch { }
351+
}
341352
});
342353

343354
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress, FPM.Main.Handle);

0 commit comments

Comments
 (0)