Fix form permissions, payment updates and onion validation (#7759)
What changed, and why it matters
This commit fixes three separate issues in BTCPay Server: (1) private forms could be submitted by unauthorized users because permission checks were missing, (2) payment updates could trigger errors or incorrect events when multiple payments belonged to the same invoice, and (3) Tor (.onion) address validation was too simple and could be bypassed or misused. It also fixes two JavaScript error messages so they display as plain text instead of HTML, preventing possible cross-site scripting (XSS) when error content is shown to users.
Review and merge promptly; verify that the new authorization check covers all submission paths (including API and plugin callbacks), test .onion validation against edge cases such as trailing dots and non-DNS hostnames, and confirm the PaymentService caching does not skip necessary invoice reloads on concurrent updates. Consider adding regression tests for each fixed class of issue.
Security signals we found
Missing authorization check added for non-public form submissions
Centralized and stricter .onion hostname validation
Invoice blob deserialization moved out of per-payment loop to avoid duplicate work and potential event inconsistencies
JavaScript error toasts switched from HTML string injection to textContent nodes
Multiple security-relevant areas (forms, payments, onion, XSS) touched in one commit
Evidence from the diff
The patch addresses: (1) an authorization gap in UIFormsController.SubmitForm where non-public forms did not verify the CanCreateInvoice policy before processing submissions; (2) a logic/performance bug in PaymentService.UpdatePayments that deserialized the invoice blob inside every payment loop iteration, now cached per InvoiceDataId; (3) weak .onion host validation replaced by a centralized NetworkExtensions.IsOnionHost helper that uses Uri.CheckHostName and trims trailing dots, applied across HTTP requests, URIs, connection strings, headers middleware, environment security checks, and lightning client factory; (4) two Vue.js toast notifications now construct text nodes instead of concatenating strings into HTML, mitigating XSS from attacker-controlled error messages.
Changed components
BTCPayServer/Plugins/Forms/UIFormsController.csBTCPayServer/Services/Invoices/PaymentService.csBTCPayServer.Abstractions/Extensions/HttpRequestExtensions.csBTCPayServer.Abstractions/Extensions/NetworkExtensions.csBTCPayServer/Configuration/ExternalConnectionString.csBTCPayServer/Extensions.csBTCPayServer/Hosting/HeadersOverrideMiddleware.csBTCPayServer/Services/BTCPayServerEnvironment.csBTCPayServer/Services/LightningClientFactoryService.csBTCPayServer/wwwroot/crowdfund/app.jsBTCPayServer/wwwroot/payment-request/app.jsInspect captured patch +38 / −13
### BTCPayServer.Abstractions/Extensions/HttpRequestExtensions.cs
@@ -6,11 +6,7 @@ namespace BTCPayServer.Abstractions.Extensions;
public static class HttpRequestExtensions
{
public static bool IsOnion(this HttpRequest request)
- {
- if (request?.Host.Host == null)
- return false;
- return request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
- }
+ => NetworkExtensions.IsOnionHost(request?.Host.Host);
public static RequestBaseUrl GetRequestBaseUrl(this HttpRequest request) => new (request);
### BTCPayServer.Abstractions/Extensions/NetworkExtensions.cs
@@ -0,0 +1,12 @@
+#nullable enable
+using System;
+
+namespace BTCPayServer.Abstractions.Extensions;
+
+public static class NetworkExtensions
+{
+ public static bool IsOnionHost(string? host)
+ => host is not null &&
+ Uri.CheckHostName(host) == UriHostNameType.Dns &&
+ host.TrimEnd('.').EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
+}
### BTCPayServer/Configuration/ExternalConnectionString.cs
@@ -38,7 +38,7 @@ public async Task<ExternalConnectionString> Expand(Uri absoluteUrlBase, External
var serviceUri = connectionString.Server.IsAbsoluteUri ? connectionString.Server : ToRelative(absoluteUrlBase, connectionString.Server.ToString());
var isSecure = network != ChainName.Mainnet ||
serviceUri.Scheme == "https" ||
- serviceUri.DnsSafeHost.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
+ serviceUri.IsOnion() ||
Extensions.IsLocalNetwork(serviceUri.DnsSafeHost);
if (!isSecure)
{
### BTCPayServer/Extensions.cs
@@ -763,7 +763,7 @@ public static bool IsOnion(this Uri uri)
{
if (uri == null || !uri.IsAbsoluteUri)
return false;
- return uri.DnsSafeHost.EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
+ return NetworkExtensions.IsOnionHost(uri.DnsSafeHost);
}
public static UserPrefsCookie GetUserPrefsCookie(this HttpContext ctx)
### BTCPayServer/Hosting/HeadersOverrideMiddleware.cs
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
+using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
@@ -21,7 +22,7 @@ public async Task Invoke(HttpContext httpContext)
{
if (!string.IsNullOrEmpty(overrideXForwardedProto))
{
- if (!httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase))
+ if (!httpContext.Request.IsOnion())
{
httpContext.Request.Headers["X-Forwarded-Proto"] = overrideXForwardedProto;
}
### BTCPayServer/Plugins/Forms/UIFormsController.cs
@@ -188,6 +188,12 @@ public async Task<IActionResult> SubmitForm(string formId)
return NotFound();
}
+ if (!formData.Public &&
+ !(await authorizationService.AuthorizeAsync(User, formData.StoreId, Policies.CanCreateInvoice)).Succeeded)
+ {
+ return Forbid();
+ }
+
if (!Request.HasFormContentType)
return await GetFormView(formData);
### BTCPayServer/Services/BTCPayServerEnvironment.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
+using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
@@ -67,7 +68,7 @@ public bool IsSecure(HttpContext httpContext)
{
return NetworkType != ChainName.Mainnet ||
httpContext.Request.Scheme == "https" ||
- httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
+ httpContext.Request.IsOnion() ||
Extensions.IsLocalNetwork(httpContext.Request.Host.Host);
}
### BTCPayServer/Services/Invoices/PaymentService.cs
@@ -83,13 +83,18 @@ public async Task UpdatePayments(List<PaymentEntity> payments)
.Where(data => paymentIds.Contains(data.Id))
.ToDictionaryAsync(data => data.Id);
var eventsToSend = new List<InvoiceEvent>();
+ var invoices = new Dictionary<string, InvoiceEntity>();
foreach (var payment in payments)
{
var dbPayment = dbPayments[payment.Id];
- var invBlob = _invoiceRepository.ToEntity(dbPayment.InvoiceData);
var wasConfirmed = dbPayment.Status is PaymentStatus.Settled;
if (!wasConfirmed && payment.Status is PaymentStatus.Settled)
{
+ if (!invoices.TryGetValue(dbPayment.InvoiceDataId, out var invBlob))
+ {
+ invBlob = _invoiceRepository.ToEntity(dbPayment.InvoiceData);
+ invoices.Add(dbPayment.InvoiceDataId, invBlob);
+ }
eventsToSend.Add(new InvoiceEvent(invBlob, InvoiceEvent.PaymentSettled) { Payment = payment });
}
dbPayment.SetBlob(payment);
### BTCPayServer/Services/LightningClientFactoryService.cs
@@ -45,7 +45,7 @@ public ILightningClient Create(string lightningConnectionString, BTCPayNetwork n
ArgumentNullException.ThrowIfNull(network);
var isOnion = BTCPayServer.Extensions.TryGetLightningServer(lightningConnectionString, out var server) &&
- server.DnsSafeHost.TrimEnd('.').EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
+ server.IsOnion();
var httpClient = (isOnion, allowUnsafe) switch
{
(true, _) => OnionNamedClient,
### BTCPayServer/wwwroot/crowdfund/app.js
@@ -242,7 +242,9 @@ app = new Vue({
msg = JSON.stringify(error);
}
- Vue.toasted.show("Error creating invoice: " + msg, {
+ var message = document.createElement("span");
+ message.textContent = "Error creating invoice: " + msg;
+ Vue.toasted.show(message, {
iconPack: "fontawesome",
icon: "exclamation-triangle",
fullWidth: false,
### BTCPayServer/wwwroot/payment-request/app.js
@@ -157,7 +157,9 @@ document.addEventListener("DOMContentLoaded",function (ev) {
} else {
msg = JSON.stringify(error);
}
- Vue.toasted.error("Error creating invoice: " + msg, Object.assign({}, toastOptions), {
+ var message = document.createElement("span");
+ message.textContent = "Error creating invoice: " + msg;
+ Vue.toasted.error(message, Object.assign({}, toastOptions), {
icon: "exclamation-triangle"
});
});Why this scored 65/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.