Fix API error responses and bound wallet address previews (#7853)
What changed, and why it matters
This commit fixes two separate issues in BTCPay Server. First, it corrects a localization helper so that error messages actually include their placeholder values instead of showing raw format strings. Second, it limits how many wallet addresses can be previewed at once and prevents negative offsets, which could otherwise be abused to request huge numbers of addresses or trigger excessive computation. It also changes one anonymous BitPay API access denial from a generic 'Forbid' response to a proper BitPay-style error.
Review whether the MaxPreviewAddresses limit of 1000 is appropriate for your deployment and consider additional rate limiting on the preview endpoints. Verify that the localization fix does not expose any sensitive data through improved error message rendering.
Security signals we found
Input validation added to limit address preview count (potential DoS/resource exhaustion mitigation)
Negative offset validation added
API error response formatting corrected
Authentication failure response normalized to expected API error format
Evidence from the diff
The patch modifies three files. NullLocalizers.cs fixes string formatting by passing ‘arguments’ into string.Format and LocalizedHtmlString, resolving missing argument substitution. GreenfieldStoreOnChainPaymentMethods.cs adds MaxPreviewAddresses=1000 and ValidatePreviewRange to bound offset/count parameters for wallet address preview endpoints, returning validation errors for out-of-range requests. BitpayInvoiceController.cs replaces Forbid(…) with a BitpayHttpException(403, ‘Forbidden’) for anonymous BitPay authentication, likely to produce a correctly-shaped API error response.
Changed components
BTCPayServer/NullLocalizers.csBTCPayServer/Plugins/Wallets/Controllers/GreenfieldStoreOnChainPaymentMethodsController.csBTCPayServer/Plugins/Bitpay/Controllers/BitpayInvoiceController.csInspect captured patch +19 / −4
### BTCPayServer/NullLocalizers.cs
@@ -13,19 +13,19 @@ public class NullStringLocalizer : IStringLocalizer
public LocalizedString this[string name] => new(name, name);
- public LocalizedString this[string name, params object[] arguments] => new(name, string.Format(name));
+ public LocalizedString this[string name, params object[] arguments] => new(name, string.Format(name, arguments));
}
public class NullViewLocalizer : IViewLocalizer
{
public static readonly NullViewLocalizer Instance = new();
public LocalizedString GetString(string name) => new(name, name);
- public LocalizedString GetString(string name, params object[] arguments) => new(name, string.Format(name));
+ public LocalizedString GetString(string name, params object[] arguments) => new(name, string.Format(name, arguments));
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) => Array.Empty<LocalizedString>();
public LocalizedHtmlString this[string name] => new(name, name);
- public LocalizedHtmlString this[string name, params object[] arguments] => new(name, string.Format(name));
+ public LocalizedHtmlString this[string name, params object[] arguments] => new(name, name, false, arguments);
}
### BTCPayServer/Plugins/Bitpay/Controllers/BitpayInvoiceController.cs
@@ -96,7 +96,7 @@ public async Task<IActionResult> GetInvoices(
int? offset = null)
{
if (User.Identity?.AuthenticationType == Security.BitpayAuthenticationTypes.Anonymous)
- return Forbid(Security.BitpayAuthenticationTypes.Anonymous);
+ throw new BitpayHttpException(403, "Forbidden");
if (dateEnd != null)
dateEnd = dateEnd.Value + TimeSpan.FromDays(1); //Should include the end day
### BTCPayServer/Plugins/Wallets/Controllers/GreenfieldStoreOnChainPaymentMethodsController.cs
@@ -36,6 +36,7 @@ public partial class GreenfieldStoreOnChainPaymentMethodsController(
EventAggregator eventAggregator)
: ControllerBase
{
+ private const int MaxPreviewAddresses = 1000;
private StoreData Store => HttpContext.GetStoreData();
public PoliciesSettings PoliciesSettings { get; } = policiesSettings;
@@ -53,6 +54,9 @@ public IActionResult GetOnChainPaymentMethodPreview(
PaymentMethodId paymentMethodId,
int offset = 0, int count = 10)
{
+ ValidatePreviewRange(offset, count);
+ if (!ModelState.IsValid)
+ return this.CreateValidationError(ModelState);
AssertCryptoCodeWallet(paymentMethodId, out var network, out _);
if (!IsConfigured(paymentMethodId, out var settings))
{
@@ -71,6 +75,9 @@ public async Task<IActionResult> GetProposedOnChainPaymentMethodPreview(
[FromBody] UpdatePaymentMethodRequest request = null,
int offset = 0, int count = 10)
{
+ ValidatePreviewRange(offset, count);
+ if (!ModelState.IsValid)
+ return this.CreateValidationError(ModelState);
if (request is null)
{
ModelState.AddModelError(nameof(request), "Missing body");
@@ -98,6 +105,14 @@ public async Task<IActionResult> GetProposedOnChainPaymentMethodPreview(
return Ok(result);
}
+ private void ValidatePreviewRange(int offset, int count)
+ {
+ if (offset < 0)
+ ModelState.AddModelError(nameof(offset), "Offset must not be negative");
+ if (count < offset || (long)count - offset > MaxPreviewAddresses)
+ ModelState.AddModelError(nameof(count), $"The exclusive end index must be between offset and offset + {MaxPreviewAddresses}");
+ }
+
internal static OnChainPaymentMethodPreviewResultData GetPreviewResultData(int offset, int count, BTCPayNetwork network, DerivationStrategyBase strategy)
{
var line = strategy.GetLineFor(DerivationFeature.Deposit);Why this scored 38/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.