Skip to content

Commit 6c3370d

Browse files
fix: validate MaxBodyCaptureSizeKb rejects negative values
MaxBodyCaptureSizeKb could previously be set to negative values, which caused incorrect body size calculations and unexpected capture behavior during request/response truncation.
2 parents 1db7a04 + 9045a18 commit 6c3370d

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

DebugProbe.AspNetCore.Tests/Configuration/DebugProbeOptionsTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,21 @@ public void MaxEntries_negative_throws_InvalidOperationException()
8787

8888
Assert.Contains("MaxEntries", exception.Message);
8989
}
90+
91+
[Fact]
92+
public void MaxBodyCaptureSizeKb_negative_throws_ArgumentOutOfRangeException()
93+
{
94+
var options = new DebugProbeOptions();
95+
96+
Assert.Throws<ArgumentOutOfRangeException>(() => options.MaxBodyCaptureSizeKb = -1);
97+
}
98+
99+
[Fact]
100+
public void MaxBodyCaptureSizeKb_zero_is_valid()
101+
{
102+
var options = new DebugProbeOptions();
103+
options.MaxBodyCaptureSizeKb = 0;
104+
105+
Assert.Equal(0, options.MaxBodyCaptureSizeKb);
106+
}
90107
}

DebugProbe.AspNetCore/Options/DebugProbeOptions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DebugProbe.AspNetCore.Options;
1+
namespace DebugProbe.AspNetCore.Options;
22

33
/// <summary>
44
/// Configuration options for DebugProbe.
@@ -13,7 +13,18 @@ public class DebugProbeOptions
1313
/// <summary>
1414
/// Maximum captured request or response body size in kilobytes.
1515
/// </summary>
16-
public int MaxBodyCaptureSizeKb { get; set; } = 32;
16+
private int _maxBodyCaptureSizeKb = 32;
17+
public int MaxBodyCaptureSizeKb
18+
{
19+
get => _maxBodyCaptureSizeKb;
20+
set
21+
{
22+
if (value < 0)
23+
throw new ArgumentOutOfRangeException(nameof(MaxBodyCaptureSizeKb),
24+
"MaxBodyCaptureSizeKb must be 0 or greater. Use 0 to disable body capture.");
25+
_maxBodyCaptureSizeKb = value;
26+
}
27+
}
1728

1829
internal int MaxBodyCaptureSizeBytes => MaxBodyCaptureSizeKb * 1024;
1930

0 commit comments

Comments
 (0)