Cap pre-authentication BitPay request body read
What changed, and why it matters
This change limits how much data BTCPay Server reads from a BitPay-compatible authentication request before the user is fully authenticated. Previously, the server could read an unbounded amount of request data into memory, which could let an attacker send a huge request body and waste server memory or disk space. The patch caps the body at 1 MB and rejects oversized requests.
Deploy this patch promptly; it is a focused hardening fix for a pre-authentication DoS vector. Review whether other pre-authentication handlers buffer request bodies without limits.
Security signals we found
Unbounded request body buffering before authentication
Denial-of-service via large pre-auth request
Buffer size cap introduced
IOException handling added for body read failures
Evidence from the diff
In BitpayAuthenticationHandler.cs, the pre-authentication BitID flow called Request.EnableBuffering() without a bufferLimit, causing ASP.NET Core to buffer the entire request body with no size cap. The patch adds a 1,000,000-byte MaxBitIdBodySize constant, rejects requests whose ContentLength exceeds it, and passes the limit to EnableBuffering(bufferLimit). It also wraps the StreamReader read in a try/catch for IOException and returns null on failure, preventing unbounded memory/disk consumption and likely mitigating a denial-of-service vector.
Changed components
BTCPayServer/Plugins/Bitpay/Security/BitpayAuthenticationHandler.csBitID authentication handlerInspect captured patch +14 / −3
### BTCPayServer/Plugins/Bitpay/Security/BitpayAuthenticationHandler.cs
@@ -25,6 +25,8 @@ public class BitpayAuthenticationHandler(
: AuthenticationHandler<BitpayAuthenticationOptions>(options, logger, encoder)
{
const string BitpayAuthErrorKey = nameof(BitpayAuthErrorKey);
+ // Typed long so EnableBuffering binds the bufferLimit overload, not bufferThreshold(int) which would leave the body unbounded.
+ const long MaxBitIdBodySize = 1_000_000;
protected override Task HandleChallengeAsync(AuthenticationProperties properties)
{
var reason = Context.Items[BitpayAuthErrorKey]?.ToString() ?? "Authentication required";
@@ -69,13 +71,22 @@ private AuthenticateResult Success(string claimType, string claimValue, string a
private async Task<string> CheckBitId(HttpContext httpContext, string sig, string id)
{
- httpContext.Request.EnableBuffering();
+ if (httpContext.Request.ContentLength > MaxBitIdBodySize)
+ return null;
+ httpContext.Request.EnableBuffering(bufferLimit: MaxBitIdBodySize);
string body = string.Empty;
if (httpContext.Request.ContentLength != 0 && httpContext.Request.Body != null)
{
- using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true))
+ try
{
- body = await reader.ReadToEndAsync();
+ using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true))
+ {
+ body = await reader.ReadToEndAsync();
+ }
+ }
+ catch (IOException)
+ {
+ return null;
}
httpContext.Request.Body.Position = 0;
}Why this scored 67/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.