🛡️ Sentinel: [MEDIUM] Fix DoS vulnerability in hash file reading

- 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`.
This commit is contained in:
google-labs-jules[bot] committed 2026-01-25 13:27:10 +00:00
1 parent d1cec02a35
commit d6c6a862b8
5 files changed
+95

No files matched your search

+4
View File
@@ -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.
+6
View File
@@ -14,6 +14,12 @@
<AvaloniaResource Include="Assets\**"/>
</ItemGroup>
<ItemGroup>
<Compile Remove="tests\**" />
<EmbeddedResource Remove="tests\**" />
<None Remove="tests\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.11"/>
<PackageReference Include="Avalonia.Controls.ColorPicker" Version="11.3.11"/>
+9
View File
@@ -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}");
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CheckHash.csproj" />
</ItemGroup>
</Project>
+50
View File
@@ -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<string>)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);
}
}
}