From d6c6a862b8120a91ea1e90695704bac5579f36f5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 25 Jan 2026 13:27:10 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[MEDIUM]=20?= =?UTF-8?q?Fix=20DoS=20vulnerability=20in=20hash=20file=20reading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added 1MB size limit to `ReadHashFromFile` in `CheckHashViewModel`. - Added warning log if file is too large. - Added `tests/CheckHash.Tests` project with regression test. - Excluded `tests/**` from `CheckHash.csproj` to prevent duplicate compilation. - Added Sentinel journal entry in `.jules/sentinel.md`. --- .jules/sentinel.md | 4 ++ CheckHash.csproj | 6 +++ ViewModels/CheckHashViewModel.cs | 9 ++++ tests/CheckHash.Tests/CheckHash.Tests.csproj | 26 ++++++++++ tests/CheckHash.Tests/SecurityTests.cs | 50 ++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 .jules/sentinel.md create mode 100644 tests/CheckHash.Tests/CheckHash.Tests.csproj create mode 100644 tests/CheckHash.Tests/SecurityTests.cs 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); + } + } +}