diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..b54a4c7 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-10-26 - [DoS via Unbounded File Read] +**Vulnerability:** Application read entire file content into memory for regex matching without size check in `CheckHashViewModel`. +**Learning:** `File.ReadAllTextAsync` on user-controlled paths (even "hash files") can lead to OOM/DoS if the file is unexpectedly large. +**Prevention:** Always check `FileInfo.Length` or use streams with limits when reading files, especially when expecting small files like hashes/configs. diff --git a/CheckHash.csproj b/CheckHash.csproj index befb9e9..50df849 100644 --- a/CheckHash.csproj +++ b/CheckHash.csproj @@ -14,6 +14,12 @@ + + + + + + diff --git a/ViewModels/CheckHashViewModel.cs b/ViewModels/CheckHashViewModel.cs index 64e74cb..2f80581 100644 --- a/ViewModels/CheckHashViewModel.cs +++ b/ViewModels/CheckHashViewModel.cs @@ -36,6 +36,8 @@ public partial class CheckHashViewModel : ObservableObject, IDisposable public string TotalFilesText => string.Format(L["Lbl_TotalFiles"], Files.Count); + private const long MaxHashFileSize = 1024 * 1024; // 1MB + public CheckHashViewModel() { // Force cancel event @@ -230,6 +232,13 @@ public partial class CheckHashViewModel : ObservableObject, IDisposable { try { + var info = new FileInfo(path); + if (info.Length > MaxHashFileSize) + { + Logger.Log($"Hash file too large (>{MaxHashFileSize/1024}KB): {path}", LogLevel.Warning); + return ""; + } + var content = await File.ReadAllTextAsync(path); // Find the first hash-like string in the content var match = Regex.Match(content, @"[a-fA-F0-9]{32,128}"); diff --git a/tests/CheckHash.Tests/CheckHash.Tests.csproj b/tests/CheckHash.Tests/CheckHash.Tests.csproj new file mode 100644 index 0000000..2e18766 --- /dev/null +++ b/tests/CheckHash.Tests/CheckHash.Tests.csproj @@ -0,0 +1,26 @@ + + + + net10.0 + enable + enable + false + false + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/CheckHash.Tests/SecurityTests.cs b/tests/CheckHash.Tests/SecurityTests.cs new file mode 100644 index 0000000..7a55c63 --- /dev/null +++ b/tests/CheckHash.Tests/SecurityTests.cs @@ -0,0 +1,50 @@ +using Xunit; +using CheckHash.ViewModels; +using CheckHash.Services; +using System.Reflection; +using System.IO; +using System.Threading.Tasks; +using System; + +namespace CheckHash.Tests; + +public class SecurityTests +{ + public SecurityTests() + { + // Disable UI logging to avoid Dispatcher issues since we are not in an Avalonia App + LoggerService.Instance.IsRecording = false; + } + + [Fact] + public async Task Test_ReadHashFromFile_LargeFile_DoS_Prevention() + { + // Arrange + var tempFile = Path.GetTempFileName(); + try + { + // Write 2MB of data (Hex characters so it might match the regex if read) + var largeContent = new string('A', 2 * 1024 * 1024); + await File.WriteAllTextAsync(tempFile, largeContent); + + var viewModel = new CheckHashViewModel(); + var methodInfo = typeof(CheckHashViewModel).GetMethod("ReadHashFromFile", BindingFlags.NonPublic | BindingFlags.Instance); + + Assert.NotNull(methodInfo); + + // Act + // Invoke the private method + var task = (Task)methodInfo!.Invoke(viewModel, new object[] { tempFile })!; + var result = await task; + + // Assert + // After fix, it should return empty string because file is too large (assuming 1MB limit) + // Before fix, it would read the file and likely return a match (128 chars of 'A') + Assert.Equal("", result); + } + finally + { + if (File.Exists(tempFile)) File.Delete(tempFile); + } + } +}