What changed, and why it matters
This commit tightens the Bitpay-compatible authentication path in BTCPay Server. Previously, a request authenticated with a valid cryptographic SIN could act as the first token linked to that account, regardless of which token the caller actually supplied. The patch now reads the token from the request and only authorizes the matching token, preventing one token's privileges from being used in place of another's. The commit title says 'Harden' and no explicit security disclosure is provided, so the exact vulnerability is inferred from the code change rather than stated by the vendor.
Treat this as a security hardening fix with likely security relevance. Review whether the previous behavior was reachable by an attacker who knows or can obtain any valid SIN, and confirm the fix is deployed. Consider requesting a security advisory or CVE from the maintainers if the issue was exploitable across tenants or stores.
Security signals we found
Authorization now binds to a specific token rather than the first token for a SIN
Token value extracted from both query string and POST JSON body
Commit title explicitly frames change as 'Harden'
No explicit CVE or security advisory referenced in commit or supplied materials
Evidence from the diff
The Bitpay compatibility layer authenticates callers via a BitID-style signature over the request URL and body. After authentication, authorization resolves a store by looking up tokens associated with the authenticated SIN. Before the patch, the code used tokens.FirstOrDefault(), meaning any valid SIN-bearing request would be authorized as the first arbitrary token for that SIN. The patch extracts a token from the query string or POST JSON body, adds it as a BitpayClaims.Token claim, and then selects the token whose Value equals the supplied token. For GET requests without a token, it still falls back to the first token. This is a hardening fix that likely closes a privilege-confusion or token-misuse issue in the Bitpay API compatibility layer.
Changed components
BTCPayServer.Plugins.Bitpay.Security.BitpayAuthenticationHandlerBTCPayServer.Plugins.Bitpay.Security.BitpayAuthorizationHandlerBTCPayServer.Plugins.Bitpay.Security.BitpayClaimsBitpay compatibility API authentication/authorization flowInspect captured patch +33 / −11
### BTCPayServer/Plugins/Bitpay/Security/BitpayAuthenticationHandler.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
@@ -14,6 +15,7 @@
using NBitcoin;
using NBitpayClient.Extensions;
using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace BTCPayServer.Plugins.Bitpay.Security
@@ -44,14 +46,14 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
Context.Request.HttpContext.TryGetBitpayAuth(out var bitpayAuth);
if (!string.IsNullOrEmpty(bitpayAuth.Signature) && !string.IsNullOrEmpty(bitpayAuth.Id))
{
- var sin = await CheckBitId(Context.Request.HttpContext, bitpayAuth.Signature, bitpayAuth.Id);
- if (sin == null)
+ var claims = await CheckBitId(Context.Request.HttpContext, bitpayAuth.Signature, bitpayAuth.Id);
+ if (claims == null)
return Fail("BitId authentication failed");
- return Success(BitpayClaims.SIN, sin, BitpayAuthenticationTypes.SinAuthentication);
+ return Success(claims, BitpayAuthenticationTypes.SinAuthentication);
}
else
{
- return Success(null, null, BitpayAuthenticationTypes.Anonymous);
+ return Success(new List<Claim>(), BitpayAuthenticationTypes.Anonymous);
}
}
@@ -61,15 +63,12 @@ private AuthenticateResult Fail(string reason)
return AuthenticateResult.Fail(reason);
}
- private AuthenticateResult Success(string claimType, string claimValue, string authenticationType)
+ private AuthenticateResult Success(List<Claim> claims, string authenticationType)
{
- List<Claim> claims = new List<Claim>();
- if (claimType != null)
- claims.Add(new Claim(claimType, claimValue));
return AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType)), authenticationType));
}
- private async Task<string> CheckBitId(HttpContext httpContext, string sig, string id)
+ private async Task<List<Claim>> CheckBitId(HttpContext httpContext, string sig, string id)
{
if (httpContext.Request.ContentLength > MaxBitIdBodySize)
return null;
@@ -97,11 +96,29 @@ private async Task<string> CheckBitId(HttpContext httpContext, string sig, strin
var key = new PubKey(id);
if (BitIdExtensions.CheckBitIDSignature(key, sig, url, body))
{
- return key.GetBitIDSIN();
+ List<Claim> claims = new List<Claim>();
+ claims.Add(new Claim(BitpayClaims.SIN, key.GetBitIDSIN()));
+ if (GetToken(httpContext.Request, body) is { } token)
+ claims.Add(new Claim(BitpayClaims.Token, token));
+ return claims;
}
}
catch { }
return null;
}
+
+ private static string GetToken(HttpRequest request, string body)
+ {
+ var token = request.Query["token"].FirstOrDefault();
+ if (string.IsNullOrEmpty(token) && HttpMethods.IsPost(request.Method) && !string.IsNullOrEmpty(body))
+ {
+ try
+ {
+ token = JObject.Parse(body).GetValue("token", StringComparison.OrdinalIgnoreCase)?.Value<string>();
+ }
+ catch { }
+ }
+ return string.IsNullOrEmpty(token) ? null : token;
+ }
}
}
### BTCPayServer/Plugins/Bitpay/Security/BitpayAuthorizationHandler.cs
@@ -25,7 +25,11 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
if (context.User.Identity is { AuthenticationType: BitpayAuthenticationTypes.SinAuthentication })
{
var sin = context.User.Claims.Where(c => c.Type == BitpayClaims.SIN).Select(c => c.Value).First();
- var bitToken = (await tokenRepository.GetTokens(sin)).FirstOrDefault();
+ var token = context.User.Claims.Where(c => c.Type == BitpayClaims.Token).Select(c => c.Value).FirstOrDefault();
+ var tokens = await tokenRepository.GetTokens(sin);
+ var bitToken = token is null && HttpMethods.IsGet(httpContext.Request.Method)
+ ? tokens.FirstOrDefault()
+ : tokens.FirstOrDefault(t => t.Value == token);
storeId = bitToken?.StoreId;
}
else if (context.User.Identity is { AuthenticationType: BitpayAuthenticationTypes.Anonymous })
### BTCPayServer/Plugins/Bitpay/Security/BitpayClaims.cs
@@ -3,5 +3,6 @@ namespace BTCPayServer.Plugins.Bitpay.Security
public class BitpayClaims
{
public const string SIN = "Bitpay.SIN";
+ public const string Token = "Bitpay.Token";
}
}Why this scored 61/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.