Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(updater): restrict stale workspace sweep to Linux and Filee-owned names

Alenbosh8fd4bc55f417
diff --git a/src/Filee.App/App.axaml.cs b/src/Filee.App/App.axaml.cs
index d17609f2..69e94801 100644
--- a/src/Filee.App/App.axaml.cs
+++ b/src/Filee.App/App.axaml.cs
@@ -137,8 +137,9 @@ public partial class App : Application
_ = Task.Run(() => Filee.Core.Web.WebDrop.DeleteStale(Filee.Core.Web.WebDrop.DefaultWorkRoot, TimeSpan.FromDays(1)));
// Likewise the work folders of conversions that never got to clean up after themselves.
_ = Task.Run(() => JobQueue.DeleteStaleWorkFolders(JobQueue.DefaultWorkRoot, TimeSpan.FromDays(1)));
- // Leftover update workspaces from cancelled or failed in-app updates.
- _ = Task.Run(() => UpdateService.DeleteStaleWorkspaces());
+ // Leftover update workspaces from cancelled or failed in-app updates (Linux only).
+ if (OperatingSystem.IsLinux())
+ _ = Task.Run(() => UpdateService.DeleteStaleWorkspaces());
// Once LibreOffice is installed: prepare its profiles while the user isn't converting yet.
AppHost.Get<EngineDownloadService>().WarmUpInBackground(TimeSpan.FromSeconds(10));
diff --git a/src/Filee.App/Services/UpdateService.cs b/src/Filee.App/Services/UpdateService.cs
index 174a9241..1c750490 100644
--- a/src/Filee.App/Services/UpdateService.cs
+++ b/src/Filee.App/Services/UpdateService.cs
@@ -1044,10 +1044,34 @@ public sealed partial class UpdateService : ObservableObject
}
}
+ /// <summary>
+ /// Checks whether <paramref name="name"/> matches the Filee-owned workspace pattern
+ /// <c>.update-&lt;pid&gt;-&lt;32-hex-characters&gt;</c> and extracts the valid positive process ID.
+ /// </summary>
+ internal static bool TryParseWorkspaceName(string name, out int pid)
+ {
+ pid = 0;
+ var parts = name.Split('-');
+ if (parts.Length != 3 || parts[0] != ".update")
+ return false;
+
+ // The final component must contain exactly 32 hexadecimal characters (0-9, a-f, or A-F)
+ if (parts[2].Length != 32 || !parts[2].All(char.IsAsciiHexDigit))
+ return false;
+
+ // <pid> must be a valid positive process ID in decimal (digits only, no signs)
+ if (parts[1].Length == 0 || !parts[1].All(char.IsAsciiDigit) || !int.TryParse(parts[1], out var parsedPid) || parsedPid <= 0)
+ return false;
+
+ pid = parsedPid;
+ return true;
+ }
+
/// <summary>
/// Deletes stale update workspaces left behind by crashes, forced kills, or aborted updates.
/// Protects active workspaces by checking both an age threshold (default 1 day) and process liveness
- /// (never deleting a workspace belonging to a running PID).
+ /// (never deleting a workspace belonging to a running PID). Restricts cleanup strictly to Filee-owned
+ /// workspace directories matching <c>.update-&lt;pid&gt;-&lt;32-hex-characters&gt;</c>.
/// </summary>
public static void DeleteStaleWorkspaces(string? root = null, TimeSpan? age = null, ILogger? log = null)
{
@@ -1064,14 +1088,15 @@ public sealed partial class UpdateService : ObservableObject
if (folder.LinkTarget is not null || folder.Attributes.HasFlag(FileAttributes.ReparsePoint))
continue;
+ // Restrict cleanup strictly to Filee-owned workspace names: .update-<pid>-<32-hex-chars>
+ // Foreign directories (e.g. .update-something or .update-cache) are rejected immediately.
+ if (!TryParseWorkspaceName(folder.Name, out var pid))
+ continue;
+
try
{
- var parts = folder.Name.Split('-');
- if (parts.Length >= 2 && int.TryParse(parts[1], out var pid))
- {
- if (IsProcessAlive(pid))
- continue;
- }
+ if (IsProcessAlive(pid))
+ continue;
if (DateTime.UtcNow - folder.LastWriteTimeUtc > threshold)
{
diff --git a/tests/Filee.App.Tests/UpdateServiceTests.cs b/tests/Filee.App.Tests/UpdateServiceTests.cs
index c21e64b8..7f0ee7d9 100644
--- a/tests/Filee.App.Tests/UpdateServiceTests.cs
+++ b/tests/Filee.App.Tests/UpdateServiceTests.cs
@@ -768,13 +768,16 @@ public class UpdateServiceTests
public void DeleteStaleWorkspaces_preserves_active_pid_and_deletes_stale_dead_pid()
{
var root = TempFolder();
- var activeDir = Path.Combine(root, $".update-{Environment.ProcessId}-active123");
+ var token1 = "0123456789abcdef0123456789abcdef";
+ var token2 = "abcdef0123456789abcdef0123456789";
+ var token3 = "11223344556677889900aabbccddeeff";
+ var activeDir = Path.Combine(root, $".update-{Environment.ProcessId}-{token1}");
var deadPid = 999999;
while (UpdateService.IsProcessAlive(deadPid))
deadPid++;
- var deadStaleDir = Path.Combine(root, $".update-{deadPid}-stale456");
- var deadFreshDir = Path.Combine(root, $".update-{deadPid}-fresh789");
+ var deadStaleDir = Path.Combine(root, $".update-{deadPid}-{token2}");
+ var deadFreshDir = Path.Combine(root, $".update-{deadPid}-{token3}");
Directory.CreateDirectory(activeDir);
Directory.CreateDirectory(deadStaleDir);
@@ -789,6 +792,69 @@ public class UpdateServiceTests
Assert.True(Directory.Exists(deadFreshDir), "Fresh dead PID workspace must be kept");
}
+ [Fact]
+ public void DeleteStaleWorkspaces_ignores_foreign_and_malformed_directories()
+ {
+ var root = TempFolder();
+ var token32 = "0123456789abcdef0123456789abcdef";
+
+ // Foreign directories and malformed names
+ var foreign1 = Path.Combine(root, ".update-something");
+ var foreign2 = Path.Combine(root, ".update-cache");
+ var malformedZeroPid = Path.Combine(root, $".update-0-{token32}");
+ var malformedNegativePid = Path.Combine(root, $".update--123-{token32}");
+ var malformedNonDecimalPid = Path.Combine(root, $".update-abc-{token32}");
+ var malformedShortToken = Path.Combine(root, ".update-12345-0123456789abcdef");
+ var malformedLongToken = Path.Combine(root, $".update-12345-{token32}extra");
+ var malformedNonHexToken = Path.Combine(root, ".update-12345-0123456789abcdef0123456789abcdeg");
+ var malformedExtraSegment = Path.Combine(root, $".update-12345-{token32}-extra");
+
+ var allFolders = new[]
+ {
+ foreign1, foreign2, malformedZeroPid, malformedNegativePid,
+ malformedNonDecimalPid, malformedShortToken, malformedLongToken,
+ malformedNonHexToken, malformedExtraSegment
+ };
+
+ foreach (var dir in allFolders)
+ {
+ Directory.CreateDirectory(dir);
+ File.WriteAllText(Path.Combine(dir, "content.txt"), "preserve me");
+ Directory.SetLastWriteTimeUtc(dir, DateTime.UtcNow.AddDays(-2));
+ }
+
+ UpdateService.DeleteStaleWorkspaces(root, TimeSpan.FromDays(1));
+
+ foreach (var dir in allFolders)
+ {
+ Assert.True(Directory.Exists(dir), $"Directory {Path.GetFileName(dir)} must remain untouched");
+ Assert.True(File.Exists(Path.Combine(dir, "content.txt")), $"File inside {Path.GetFileName(dir)} must survive");
+ }
+ }
+
+ [Theory]
+ [InlineData(".update-123-0123456789abcdef0123456789abcdef", true, 123)]
+ [InlineData(".update-1-0123456789ABCDEF0123456789ABCDEF", true, 1)]
+ [InlineData(".update-999999-abcdef0123456789abcdef0123456789", true, 999999)]
+ [InlineData(".update-something", false, 0)]
+ [InlineData(".update-cache", false, 0)]
+ [InlineData(".update-", false, 0)]
+ [InlineData(".update-0-0123456789abcdef0123456789abcdef", false, 0)]
+ [InlineData(".update--123-0123456789abcdef0123456789abcdef", false, 0)]
+ [InlineData(".update-+123-0123456789abcdef0123456789abcdef", false, 0)]
+ [InlineData(".update-abc-0123456789abcdef0123456789abcdef", false, 0)]
+ [InlineData(".update-123-short", false, 0)]
+ [InlineData(".update-123-0123456789abcdef0123456789abcdefextra", false, 0)]
+ [InlineData(".update-123-0123456789abcdef0123456789abcdeg", false, 0)]
+ [InlineData(".update-123-0123456789abcdef-0123456789abcdef", false, 0)]
+ [InlineData("other-123-0123456789abcdef0123456789abcdef", false, 0)]
+ public void TryParseWorkspaceName_validates_pattern(string name, bool expectedResult, int expectedPid)
+ {
+ var result = UpdateService.TryParseWorkspaceName(name, out var pid);
+ Assert.Equal(expectedResult, result);
+ Assert.Equal(expectedPid, pid);
+ }
+
[Fact]
public void Helper_script_restarts_filee_on_failure_and_writes_marker()
{