What changed, and why it matters
This commit is a code-quality refactor that moves hard-coded length limits into shared constants so all parts of the app enforce the same limits. It does not add new security checks or remove existing ones, but it fixes places where limits were inconsistent (for example, app names allowed 50 characters in one place and 100 in another). Keeping limits in sync reduces the chance that an attacker could bypass a validation rule by using a different API or UI path.
No immediate action required. Treat as a routine hardening/refactor commit. Reviewers should verify that all call sites now reference the shared constants and that no remaining hard-coded limits drift out of sync in future changes.
Security signals we found
Centralization of validation limits reduces desynchronization bugs
Fixes inconsistent app-name length limits across UI and API (50 vs 100)
No new attack surface introduced
No removal of existing length checks
Evidence from the diff
The change centralizes validation constants: AppData.Min/MaxNameLength, FeatureData.MaxCustomIdLength/MaxDescriptionLength, OfferingData.MaxSuccessRedirectUrlLength, PlanData.MaxDescriptionLength, CurrencyNameTable.MaxCurrencyCodeLength, WebhookBlob.MaxSecretLength, StoreLabelRepository.MaxLabelSize, and FormComponentProviderBase.MaxValueLength. These constants replace duplicated magic numbers in MVC model attributes, GreenField API controllers, and service classes. Notably, the GreenfieldAppsController app-name limit is raised from 50 to 100 to match the UI/Subscriptions API, and the Subscriptions UI view models now enforce the same 100-character app-name limit as the API. The Form input/textarea providers now share a single MaxValueLength constant from their base class. No validation rules are weakened; some are strengthened or aligned.
Changed components
BTCPayServer.Data (AppData, FeatureData, OfferingData, PlanData)BTCPayServer.Rating (CurrencyNameTable)GreenField API controllers (Apps, Lightning Addresses, Subscriptions Offerings, Webhooks)MVC view models (CreateApp, CreateStore, GeneralSettings, Payment, Crowdfund, PointOfSale, Subscriptions, Webhooks, Wallet PSBT)Form component providers (HtmlInput, HtmlTextarea)Services (StoreLabelRepository, WalletRepository)Inspect captured patch +67 / −51
### BTCPayServer.Data/Data/AppData.cs
@@ -7,6 +7,9 @@ namespace BTCPayServer.Data
{
public class AppData
{
+ public const int MinNameLength = 1;
+ public const int MaxNameLength = 100;
+
public string Id { get; set; }
public string Name { get; set; }
public string StoreDataId { get; set; }
### BTCPayServer.Data/Data/Subscriptions/FeatureData.cs
@@ -9,6 +9,9 @@ namespace BTCPayServer.Data.Subscriptions;
[Table("subs_features")]
public class FeatureData
{
+ public const int MaxCustomIdLength = 50;
+ public const int MaxDescriptionLength = 500;
+
/// <summary>
/// The internal ID of the feature, we only really use it in
/// SQL queries. This should not be exposed.
### BTCPayServer.Data/Data/Subscriptions/OfferingData.cs
@@ -10,6 +10,8 @@ namespace BTCPayServer.Data.Subscriptions;
[Table("subs_offerings")]
public class OfferingData : BaseEntityData
{
+ public const int MaxSuccessRedirectUrlLength = 500;
+
[Key]
[Required]
[Column("id")]
### BTCPayServer.Data/Data/Subscriptions/PlanData.cs
@@ -14,6 +14,8 @@ namespace BTCPayServer.Data.Subscriptions;
[Table("subs_plans")]
public class PlanData : BaseEntityData
{
+ public const int MaxDescriptionLength = 1_000;
+
[Key]
[Column("id")]
public string Id { get; set; } = null!;
### BTCPayServer.Rating/CurrencyNameTable.cs
@@ -63,6 +63,8 @@ public Task<CurrencyData[]> LoadCurrencyData(CancellationToken cancellationToken
}
public class CurrencyNameTable
{
+ public const int MaxCurrencyCodeLength = 10;
+
public CurrencyNameTable(IEnumerable<CurrencyDataProvider> currencyDataProviders, ILogger<CurrencyNameTable> logger)
{
_currencyDataProviders = currencyDataProviders;
### BTCPayServer/Controllers/GreenField/GreenfieldAppsController.cs
@@ -593,9 +593,10 @@ private void ValidateAppRequest(IAppRequest? request)
{
ModelState.AddModelError(nameof(request.AppName), "App name is missing");
}
- else if (request.AppName.Length is < 1 or > 50)
+ else if (request.AppName.Length is < AppData.MinNameLength or > AppData.MaxNameLength)
{
- ModelState.AddModelError(nameof(request.AppName), "App name can only be between 1 and 50 characters");
+ ModelState.AddModelError(nameof(request.AppName),
+ $"App name can only be between {AppData.MinNameLength} and {AppData.MaxNameLength} characters");
}
}
}
### BTCPayServer/Controllers/GreenField/GreenfieldStoreLightningAddressesController.cs
@@ -6,6 +6,7 @@
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Data;
+using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
@@ -21,7 +22,6 @@ namespace BTCPayServer.Controllers.Greenfield
public class GreenfieldStoreLightningAddressesController : ControllerBase
{
private const int MaxInvoiceMetadataLength = 64 * 1024;
- private const int MaxCurrencyCodeLength = 10;
private readonly LightningAddressService _lightningAddressService;
public GreenfieldStoreLightningAddressesController(
@@ -92,7 +92,7 @@ public async Task<IActionResult> AddOrUpdateStoreLightningAddress(
if (data.Min is { } min && data.Max is { } max && min > max)
ModelState.AddModelError(nameof(data.Max), "Maximum must be greater than or equal to minimum.");
- if (data.CurrencyCode?.Length > MaxCurrencyCodeLength)
+ if (data.CurrencyCode?.Length > CurrencyNameTable.MaxCurrencyCodeLength)
ModelState.AddModelError(nameof(data.CurrencyCode), "Currency code is too long.");
if (data.InvoiceMetadata is not null &&
### BTCPayServer/Models/AppViewModels/CreateAppViewModel.cs
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
+using BTCPayServer.Data;
using BTCPayServer.Plugins.PointOfSale;
using BTCPayServer.Services.Apps;
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -18,8 +19,8 @@ public CreateAppViewModel(AppService appService)
}
[Required]
- [MaxLength(50)]
- [MinLength(1)]
+ [MaxLength(AppData.MaxNameLength)]
+ [MinLength(AppData.MinNameLength)]
[Display(Name = "App Name")]
public string AppName { get; set; }
### BTCPayServer/Models/StoreViewModels/CreateStoreViewModel.cs
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
+using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace BTCPayServer.Models.StoreViewModels
@@ -14,7 +15,7 @@ public class CreateStoreViewModel
public string Name { get; set; }
[Required]
- [MaxLength(10)]
+ [MaxLength(CurrencyNameTable.MaxCurrencyCodeLength)]
[Display(Name = "Default currency")]
public string DefaultCurrency { get; set; }
### BTCPayServer/Models/StoreViewModels/GeneralSettingsViewModel.cs
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using BTCPayServer.Client.Models;
+using BTCPayServer.Services.Rates;
using BTCPayServer.Validation;
using Microsoft.AspNetCore.Http;
@@ -53,7 +54,7 @@ public class GeneralSettingsViewModel
public double PaymentTolerance { get; set; }
[Display(Name = "Default currency")]
- [MaxLength(10)]
+ [MaxLength(CurrencyNameTable.MaxCurrencyCodeLength)]
public string DefaultCurrency { get; set; }
[Display(Name = "Additional rates to track")]
### BTCPayServer/Models/StoreViewModels/PaymentViewModel.cs
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using BTCPayServer.Client.Models;
+using BTCPayServer.Services.Rates;
namespace BTCPayServer.Models.StoreViewModels
{
@@ -22,7 +23,7 @@ public class PaymentViewModel
public double PaymentTolerance { get; set; }
[Display(Name = "Default currency")]
- [MaxLength(10)]
+ [MaxLength(CurrencyNameTable.MaxCurrencyCodeLength)]
public string DefaultCurrency { get; set; }
[Display(Name = "Minimum acceptable expiration time for BOLT11 for refunds")]
### BTCPayServer/Plugins/Crowdfund/Models/UpdateCrowdfundViewModel.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
+using BTCPayServer.Data;
using BTCPayServer.Services.Apps;
using BTCPayServer.Validation;
using Microsoft.AspNetCore.Http;
@@ -16,8 +17,8 @@ public class UpdateCrowdfundViewModel
public string StoreDefaultCurrency { get; set; }
[Required]
- [MaxLength(50)]
- [MinLength(1)]
+ [MaxLength(AppData.MaxNameLength)]
+ [MinLength(AppData.MinNameLength)]
[Display(Name = "App Name")]
public string AppName { get; set; }
### BTCPayServer/Plugins/Forms/HtmlInputFormProvider.cs
@@ -7,8 +7,6 @@ namespace BTCPayServer.Forms;
public class HtmlInputFormProvider : FormComponentProviderBase
{
- private const int MaxValueLength = 10_000;
-
public override void Register(Dictionary<string, IFormComponentProvider> typeToComponentProvider)
{
foreach (var t in new[] {
### BTCPayServer/Plugins/Forms/HtmlTextareaFormProvider.cs
@@ -6,8 +6,6 @@ namespace BTCPayServer.Forms;
public class HtmlTextareaFormProvider : FormComponentProviderBase
{
- private const int MaxValueLength = 10_000;
-
public override void Register(Dictionary<string, IFormComponentProvider> typeToComponentProvider)
{
typeToComponentProvider.Add("textarea", this);
### BTCPayServer/Plugins/Forms/IFormComponentProvider.cs
@@ -16,6 +16,8 @@ public interface IFormComponentProvider
public abstract class FormComponentProviderBase : IFormComponentProvider
{
+ protected const int MaxValueLength = 10_000;
+
public abstract string View { get; }
public abstract void Register(Dictionary<string, IFormComponentProvider> typeToComponentProvider);
public virtual string GetValue(Form form, Field field)
### BTCPayServer/Plugins/PointOfSale/Models/UpdatePointOfSaleViewModel.cs
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
+using BTCPayServer.Data;
using BTCPayServer.Validation;
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -12,8 +13,8 @@ public class UpdatePointOfSaleViewModel
public string StoreDefaultCurrency { get; set; }
[Required]
- [MaxLength(50)]
- [MinLength(1)]
+ [MaxLength(AppData.MaxNameLength)]
+ [MinLength(AppData.MinNameLength)]
[Display(Name = "App Name")]
public string AppName { get; set; }
### BTCPayServer/Plugins/Subscriptions/Controllers/GreenfieldOfferingController.cs
@@ -33,11 +33,7 @@ public class GreenfieldOfferingController(
AppService appService) : ControllerBase
{
private const int MaxMetadataLength = 64 * 1024;
- private const int MaxAppNameLength = 100;
private const int MaxFeatures = 100;
- private const int MaxFeatureIdLength = 50;
- private const int MaxFeatureDescriptionLength = 500;
- private const int MaxPlanDescriptionLength = 1_000;
private const int MaxCreditDescriptionLength = 500;
[HttpGet("~/api/v1/stores/{storeId}/offerings/{offeringId?}")]
@@ -124,14 +120,14 @@ private void ValidateOffering(OfferingModel? request)
{
if (request?.AppName is null)
ModelState.AddModelError(nameof(request.AppName), "AppName is required");
- else if (request.AppName.Length > MaxAppNameLength)
- ModelState.AddModelError(nameof(request.AppName), $"AppName must be at most {MaxAppNameLength} characters");
+ else if (request.AppName.Length > AppData.MaxNameLength)
+ ModelState.AddModelError(nameof(request.AppName), $"AppName must be at most {AppData.MaxNameLength} characters");
if (!string.IsNullOrEmpty(request?.SuccessRedirectUrl) &&
!PlanCheckoutData.IsSafeRedirectUrl(request.SuccessRedirectUrl))
{
ModelState.AddModelError(nameof(request.SuccessRedirectUrl), "SuccessRedirectUrl must be an HTTP or HTTPS URL");
}
- if (request?.SuccessRedirectUrl?.Length > 500)
+ if (request?.SuccessRedirectUrl?.Length > OfferingData.MaxSuccessRedirectUrlLength)
ModelState.AddModelError(nameof(request.SuccessRedirectUrl), "SuccessRedirectUrl is too long");
ValidateMetadata(nameof(OfferingModel.Metadata), request?.Metadata);
@@ -150,10 +146,10 @@ private void ValidateOffering(OfferingModel? request)
if (string.IsNullOrWhiteSpace(feature.Id))
ModelState.AddModelError($"{nameof(request.Features)}[{i}].{nameof(feature.Id)}", "Feature id is required");
- else if (feature.Id.Length > MaxFeatureIdLength)
- ModelState.AddModelError($"{nameof(request.Features)}[{i}].{nameof(feature.Id)}", $"Feature id must be at most {MaxFeatureIdLength} characters");
- if (feature.Description?.Length > MaxFeatureDescriptionLength)
- ModelState.AddModelError($"{nameof(request.Features)}[{i}].{nameof(feature.Description)}", $"Feature description must be at most {MaxFeatureDescriptionLength} characters");
+ else if (feature.Id.Length > FeatureData.MaxCustomIdLength)
+ ModelState.AddModelError($"{nameof(request.Features)}[{i}].{nameof(feature.Id)}", $"Feature id must be at most {FeatureData.MaxCustomIdLength} characters");
+ if (feature.Description?.Length > FeatureData.MaxDescriptionLength)
+ ModelState.AddModelError($"{nameof(request.Features)}[{i}].{nameof(feature.Description)}", $"Feature description must be at most {FeatureData.MaxDescriptionLength} characters");
}
}
@@ -169,9 +165,9 @@ public async Task<IActionResult> CreateOfferingPlan(string storeId, string offer
ModelState.AddModelError(nameof(request.Price), "Price cannot be negative");
if (request?.Name is null)
ModelState.AddModelError(nameof(request.Name), "Name is required");
- if (request?.Description?.Length > MaxPlanDescriptionLength)
- ModelState.AddModelError(nameof(request.Description), $"Description must be at most {MaxPlanDescriptionLength} characters");
- if (request?.Currency?.Length > 10)
+ if (request?.Description?.Length > PlanData.MaxDescriptionLength)
+ ModelState.AddModelError(nameof(request.Description), $"Description must be at most {PlanData.MaxDescriptionLength} characters");
+ if (request?.Currency?.Length > CurrencyNameTable.MaxCurrencyCodeLength)
ModelState.AddModelError(nameof(request.Currency), "Currency is too long");
ValidateMetadata(nameof(CreatePlanRequest.Metadata), request?.Metadata);
@@ -242,9 +238,9 @@ public async Task<IActionResult> UpdateOfferingPlan(string storeId, string offer
return PlanNotFound();
if (request?.Price < 0m)
ModelState.AddModelError(nameof(request.Price), "Price cannot be negative");
- if (request?.Description?.Length > MaxPlanDescriptionLength)
- ModelState.AddModelError(nameof(request.Description), $"Description must be at most {MaxPlanDescriptionLength} characters");
- if (request?.Currency?.Length > 10)
+ if (request?.Description?.Length > PlanData.MaxDescriptionLength)
+ ModelState.AddModelError(nameof(request.Description), $"Description must be at most {PlanData.MaxDescriptionLength} characters");
+ if (request?.Currency?.Length > CurrencyNameTable.MaxCurrencyCodeLength)
ModelState.AddModelError(nameof(request.Currency), "Currency is too long");
ValidateMetadata(nameof(CreatePlanRequest.Metadata), request?.Metadata);
### BTCPayServer/Plugins/Subscriptions/Views/UIOffering/AddEditPlanViewModel.cs
@@ -39,7 +39,7 @@ public class AddEditPlanViewModel
public int TrialDays { get; set; }
[Display(Name = "Description")]
- [StringLength(1000)]
+ [StringLength(PlanData.MaxDescriptionLength)]
public string Description { get; set; }
[Display(Name = "Optimistic activation")]
### BTCPayServer/Plugins/Subscriptions/Views/UIOffering/ConfigureOfferingViewModel.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
+using BTCPayServer.Data;
using BTCPayServer.Data.Subscriptions;
using BTCPayServer.Validation;
@@ -32,20 +33,20 @@ public ConfigureOfferingViewModel(OfferingData offeringData)
public OfferingData Data { get; set; }
public class FeatureViewModel
{
- [StringLength(50)]
+ [StringLength(FeatureData.MaxCustomIdLength)]
[Required]
public string Id { get; set; }
- [StringLength(500)]
+ [StringLength(FeatureData.MaxDescriptionLength)]
public string ShortDescription { get; set; }
}
public string OriginalName { get; set; }
[Required]
- [StringLength(50)]
+ [StringLength(AppData.MaxNameLength, MinimumLength = AppData.MinNameLength)]
public string Name { get; set; } = null!;
[Uri]
- [StringLength(500)]
+ [StringLength(OfferingData.MaxSuccessRedirectUrlLength)]
[Display(Name = "Success redirect url")]
public string SuccessRedirectUrl { get; set; }
### BTCPayServer/Plugins/Subscriptions/Views/UIOffering/CreateOfferingViewModel.cs
@@ -1,10 +1,11 @@
using System.ComponentModel.DataAnnotations;
+using BTCPayServer.Data;
namespace BTCPayServer.Views.UIStoreMembership;
public class CreateOfferingViewModel
{
[Required]
- [StringLength(50)]
+ [StringLength(AppData.MaxNameLength, MinimumLength = AppData.MinNameLength)]
public string Name { get; set; } = null!;
}
### BTCPayServer/Plugins/Wallets/Views/ViewModels/WalletPSBTCombineViewModel.cs
@@ -38,7 +38,7 @@ public async Task<PSBT> GetPSBT(Network network, ModelStateDictionary modelState
{
if (UploadedPSBTFile != null)
{
- if (UploadedPSBTFile.Length > 500 * 1024)
+ if (UploadedPSBTFile.Length > WalletPSBTViewModel.MaxPSBTLength)
return null;
byte[] bytes = new byte[UploadedPSBTFile.Length];
using (var stream = UploadedPSBTFile.OpenReadStream())
### BTCPayServer/Plugins/Wallets/Views/ViewModels/WalletPSBTViewModel.cs
@@ -11,7 +11,7 @@ namespace BTCPayServer.Plugins.Wallets.Views.ViewModels
{
public class WalletPSBTViewModel : WalletPSBTReadyViewModel
{
- private const int MaxPSBTLength = 500 * 1024;
+ public const int MaxPSBTLength = 500 * 1024;
public string CryptoCode { get; set; }
public string Decoded { get; set; }
### BTCPayServer/Plugins/Webhooks/Controllers/GreenfieldStoreWebhooksController.cs
@@ -20,7 +20,6 @@ namespace BTCPayServer.Plugins.Webhooks.Controllers
[EnableCors(CorsPolicies.All)]
public class GreenfieldStoreWebhooksController : ControllerBase
{
- private const int MaxSecretLength = 64;
private const int MaxSpecificEvents = 100;
private const int MaxSpecificEventLength = 100;
private const int MaxSpecificEventCharacters = 10_000;
@@ -72,8 +71,8 @@ private void ValidateWebhookRequest(StoreWebhookBaseData create)
{
if (!Uri.TryCreate(create?.Url, UriKind.Absolute, out _))
ModelState.AddModelError(nameof(Url), "Invalid Url");
- if (create?.Secret is { Length: > MaxSecretLength })
- ModelState.AddModelError(nameof(create.Secret), $"The secret should be maximum {MaxSecretLength} characters.");
+ if (create?.Secret is { Length: > WebhookBlob.MaxSecretLength })
+ ModelState.AddModelError(nameof(create.Secret), $"The secret should be maximum {WebhookBlob.MaxSecretLength} characters.");
if (create?.AuthorizedEvents?.SpecificEvents is { } specificEvents)
{
if (specificEvents.Length > MaxSpecificEvents ||
### BTCPayServer/Plugins/Webhooks/Views/EditWebhookViewModel.cs
@@ -62,7 +62,7 @@ public EditWebhookViewModel(WebhookBlob blob)
[Uri]
[Required]
public string PayloadUrl { get; set; }
- [MaxLength(64)]
+ [MaxLength(WebhookBlob.MaxSecretLength)]
public string Secret { get; set; }
public List<DeliveryViewModel> Deliveries { get; set; } = new List<DeliveryViewModel>();
### BTCPayServer/Plugins/Webhooks/WebhookDataExtensions.cs
@@ -54,6 +54,8 @@ public bool IsPruned()
}
public class WebhookBlob
{
+ public const int MaxSecretLength = 64;
+
public string Url { get; set; }
public bool Active { get; set; } = true;
public string Secret { get; set; }
### BTCPayServer/Services/Labels/StoreLabelRepository.cs
@@ -13,6 +13,8 @@ namespace BTCPayServer.Services.Labels;
public class StoreLabelRepository
{
+ public const int MaxLabelSize = 50;
+
private readonly ApplicationDbContextFactory _contextFactory;
public StoreLabelRepository(ApplicationDbContextFactory contextFactory)
@@ -357,8 +359,6 @@ private static (string Label, string Color) FormatToLabel(string text, string? c
return !string.IsNullOrEmpty(color) ? (text, color) : (text, ColorPalette.Default.DeterministicColor(text));
}
- private const int MaxLabelSize = 50;
-
private static string NormalizeLabel(string label)
{
label = label.Trim();
### BTCPayServer/Services/WalletRepository.cs
@@ -9,6 +9,7 @@
using BTCPayServer.Data;
using BTCPayServer.Plugins.Wallets.Views.ViewModels;
using BTCPayServer.Services.Invoices;
+using BTCPayServer.Services.Labels;
using BTCPayServer.Services.Wallets;
using Dapper;
using Microsoft.EntityFrameworkCore;
@@ -653,7 +654,6 @@ public async Task ModifyWalletObjectData(WalletObjectId id, Action<JObject> modi
}
}
- const int MaxLabelSize = 50;
public async Task AddWalletObjectLabels(WalletObjectId id, params string[] labels)
{
ArgumentNullException.ThrowIfNull(id);
@@ -667,7 +667,7 @@ public async Task AddWalletObjectLabels((WalletObjectId id, string[] labels)[] r
foreach (var (id, labels) in reqs)
{
objs.TryAdd(id, NewWalletObjectData(id));
- foreach (var l in labels.Select(l => l.Trim().Truncate(MaxLabelSize)).Distinct())
+ foreach (var l in labels.Select(l => l.Trim().Truncate(StoreLabelRepository.MaxLabelSize)).Distinct())
{
var label = CreateLabel(id.WalletId, l);
objs.TryAdd(label.Id, label.ObjectData);
@@ -768,7 +768,7 @@ public async Task<bool> RenameWalletLabel(WalletId id, string oldLabel, string n
{
ArgumentNullException.ThrowIfNull(id);
oldLabel = oldLabel.Trim();
- newLabel = newLabel.Trim().Truncate(MaxLabelSize);
+ newLabel = newLabel.Trim().Truncate(StoreLabelRepository.MaxLabelSize);
if (oldLabel == newLabel)
return true;Why this scored 35/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.