Skip to content

Commit bf6f710

Browse files
fix: validate MaxEntries to prevent infinite loop in DebugEntryStore
Added DebugProbeOptionsValidator that validates MaxEntries >= 1 during startup. If the value is invalid, an InvalidOperationException is thrown immediately with a clear error message.
2 parents 4f09fd5 + db3fe9b commit bf6f710

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

DebugProbe.AspNetCore.Tests/Configuration/DebugProbeOptionsTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,31 @@ public void Custom_options_are_registered_and_used()
6060
Assert.Equal("***", options.RedactionText);
6161
Assert.NotNull(store.Environment);
6262
}
63+
[Fact]
64+
public void MaxEntries_zero_throws_InvalidOperationException()
65+
{
66+
var services = new ServiceCollection();
67+
68+
var exception = Assert.Throws<InvalidOperationException>(() =>
69+
services.AddDebugProbe(options =>
70+
{
71+
options.MaxEntries = 0;
72+
}));
73+
74+
Assert.Contains("MaxEntries", exception.Message);
75+
}
76+
77+
[Fact]
78+
public void MaxEntries_negative_throws_InvalidOperationException()
79+
{
80+
var services = new ServiceCollection();
81+
82+
var exception = Assert.Throws<InvalidOperationException>(() =>
83+
services.AddDebugProbe(options =>
84+
{
85+
options.MaxEntries = -1;
86+
}));
87+
88+
Assert.Contains("MaxEntries", exception.Message);
89+
}
6390
}

DebugProbe.AspNetCore/Extensions/DebugProbeExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static IServiceCollection AddDebugProbe(this IServiceCollection services,
3636

3737
configure?.Invoke(options);
3838

39+
var validator = new DebugProbeOptionsValidator();
40+
var result = validator.Validate(null, options);
41+
if (result.Failed)
42+
throw new InvalidOperationException(result.FailureMessage);
43+
3944
services.AddSingleton(options);
4045

4146
services.AddSingleton<DebugEntryStore>();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace DebugProbe.AspNetCore.Options;
4+
5+
internal sealed class DebugProbeOptionsValidator
6+
: IValidateOptions<DebugProbeOptions>
7+
{
8+
public ValidateOptionsResult Validate(
9+
string? name,
10+
DebugProbeOptions options)
11+
{
12+
if (options.MaxEntries < 1)
13+
{
14+
return ValidateOptionsResult.Fail(
15+
$"DebugProbe configuration is invalid. " +
16+
$"MaxEntries must be greater than or equal to 1. " +
17+
$"Provided value: {options.MaxEntries}.");
18+
}
19+
20+
return ValidateOptionsResult.Success;
21+
}
22+
}

0 commit comments

Comments
 (0)