Use canonical URL for generated links (#7662)
What changed, and why it matters
This commit changes how BTCPay Server builds links that are shown to users or sent in emails. Instead of using the web address from the current incoming request, it uses a configured canonical server URL. This likely fixes situations where generated links could point to the wrong domain—such as when the server is behind a reverse proxy, accessed via an unexpected hostname, or manipulated by a Host-header attack—preventing users from being redirected to attacker-controlled sites or from email links failing to work.
Review the CallbackGenerator implementation to confirm it only returns the administrator-configured canonical URL and cannot be influenced by request headers. Verify that all other locations generating user-facing or email links also use the canonical helper. Consider whether this change should be backported if previous releases generated request-dependent links in these features.
Security signals we found
Replaces request-derived base URL with canonical configured base URL for generated links
Affects email links (multisig signer invitations) and QR-code checkout links
Mitigates Host-header or reverse-proxy misconfiguration causing incorrect outbound links
Adds/updates tests to assert canonical URL is used in generated content
No explicit CVE, advisory, or security disclosure referenced in commit
Evidence from the diff
The patch replaces direct use of HttpContext.Request.GetRequestBaseUrl() with a new CallbackGenerator.GetRequestBaseUrl() helper in two plugin areas: Multisig signer invitation emails and the Subscriptions plan-checkout QR code. A new RequestBaseUrl property is added to PlanCheckoutViewModel and passed to the Razor view, and Multisig’s pending setup model now stores the canonical base URL. Tests are updated to configure a canonical base URL and assert that generated links/emails contain that canonical URL rather than the request-derived URL. The change is defensive: it centralizes URL generation on a server-configured canonical URL instead of trusting the incoming request’s host/scheme.
Changed components
BTCPayServer.Plugins.Multisig.Controllers.UIMultisigWalletsControllerBTCPayServer.Plugins.Subscriptions.Controllers.UIPlanCheckoutControllerBTCPayServer.Plugins.Subscriptions.Views.UIPlanCheckout.PlanCheckoutBTCPayServer.Plugins.Subscriptions.ViewModels.PlanCheckoutViewModelMultisig signer invitation email generationSubscription plan-checkout QR code generationInspect captured patch +25 / −6
### BTCPayServer.Tests/MonetizationTests.cs
@@ -1,5 +1,6 @@
#nullable enable
using System.Threading.Tasks;
+using BTCPayServer.Abstractions;
using BTCPayServer.Abstractions.Models;
using BTCPayServer.Data;
using BTCPayServer.Events;
@@ -30,12 +31,19 @@ public async Task DoesNotAttachNewSubscriberToExistingUser()
await s.ConfirmModal();
await s.FindAlertMessage(partialText: "Monetization activated");
+ var settingsRepository = s.Server.PayTester.GetService<SettingsRepository>();
+ var serverSettings = await settingsRepository.GetSettingAsync<ServerSettings>() ?? new ServerSettings();
+ serverSettings.UpdateBaseUrl(RequestBaseUrl.FromUrl("https://canonical.example/btcpay"));
+ await settingsRepository.UpdateSetting(serverSettings);
+
SubscriptionEvent.NewSubscriber newSubscriber;
await using (await s.SwitchPage())
{
newSubscriber = await s.Server.WaitForEvent<SubscriptionEvent.NewSubscriber>(async () =>
{
await s.GoToUrl("/monetization/new-user");
+ var qrUrl = await s.Page.Locator(".plan-checkout__qr-modal .qr-code").GetAttributeAsync("alt");
+ Assert.StartsWith("https://canonical.example/btcpay/plan-checkout/", qrUrl);
await s.Page.FillAsync(".plan-checkout__email", "existing-user@gmail.com");
await s.ClickPagePrimary();
});
### BTCPayServer.Tests/MultisigTests.cs
@@ -574,6 +574,10 @@ public async Task CanUseMultisigSetup()
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var storeRepo = scope.ServiceProvider.GetRequiredService<StoreRepository>();
var multisigService = scope.ServiceProvider.GetRequiredService<MultisigService>();
+ var settingsRepository = scope.ServiceProvider.GetRequiredService<SettingsRepository>();
+ var serverSettings = await settingsRepository.GetSettingAsync<ServerSettings>() ?? new ServerSettings();
+ serverSettings.UpdateBaseUrl(RequestBaseUrl.FromUrl("https://canonical.example/btcpay"));
+ await settingsRepository.UpdateSetting(serverSettings);
var signerAUser = await userManager.FindByEmailAsync(signerA);
var signerBUser = await userManager.FindByEmailAsync(signerB);
@@ -613,11 +617,12 @@ async Task<string> CreateRequest(params string[] participantEmails)
Assert.Equal("Multisig signer request for BTC", signerRequestEmail.Subject);
var currentPending = (await multisigService.GetPendingMultisigSetup(storeId))[0];
Assert.NotNull(currentPending);
+ Assert.Equal("https://canonical.example/btcpay", currentPending.RequestBaseUrl.ToString());
Assert.Equal(storeId, currentPending.StoreId);
Assert.Equal("BTC", currentPending.CryptoCode);
Assert.Equal(2, currentPending.Participants.Count);
Assert.All(currentPending.Participants, p => Assert.True(string.IsNullOrEmpty(p.AccountKey)));
- Assert.Contains($"/multisig-setups/{currentPending.RequestId}", signerRequestEmail.Html ?? signerRequestEmail.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase);
+ Assert.Contains($"https://canonical.example/btcpay/multisig-setups/{currentPending.RequestId}", signerRequestEmail.Html ?? signerRequestEmail.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase);
Assert.EndsWith($"/multisig-setups/{currentPending.RequestId}", new Uri(s.Page.Url).AbsolutePath);
await Expect(s.Page.Locator("#MultisigRequiredSigners")).ToHaveCountAsync(0);
await Expect(s.Page.Locator("#MultisigTotalSigners")).ToHaveCountAsync(0);
### BTCPayServer/Plugins/Multisig/Controllers/UIMultisigWalletsController.cs
@@ -12,6 +12,7 @@
using BTCPayServer.Plugins.Multisig.Models;
using BTCPayServer.Plugins.Multisig.Services;
using BTCPayServer.Plugins.Wallets;
+using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
@@ -33,6 +34,7 @@ public class UIMultisigWalletsController(
BTCPayWalletProvider walletProvider,
MultisigService multisigService,
MultisigNotificationService multisigNotificationService,
+ CallbackGenerator callbackGenerator,
PaymentMethodHandlerDictionary paymentMethodHandlerDictionary,
IStringLocalizer stringLocalizer) : Controller
{
@@ -143,7 +145,7 @@ public async Task<IActionResult> SetupMultisig(string storeId, string cryptoCode
UserId = selectedId
})
.ToList(),
- RequestBaseUrl = HttpContext.Request.GetRequestBaseUrl()
+ RequestBaseUrl = callbackGenerator.GetRequestBaseUrl()
};
await multisigService.SavePendingMultisigSetup(pending);
### BTCPayServer/Plugins/Subscriptions/Controllers/UIPlanCheckoutController.cs
@@ -23,7 +23,8 @@ public class UIPlanCheckoutController(
LinkGenerator linkGenerator,
UriResolver uriResolver,
IStringLocalizer stringLocalizer,
- SubscriptionHostedService subsService)
+ SubscriptionHostedService subsService,
+ CallbackGenerator callbackGenerator)
: UISubscriptionControllerBase(dbContextFactory, linkGenerator, stringLocalizer, subsService)
{
[HttpGet]
@@ -42,6 +43,7 @@ public async Task<IActionResult> PlanCheckout(string checkoutId)
var vm = new PlanCheckoutViewModel()
{
CheckoutId = checkoutId,
+ BaseUrl = callbackGenerator.GetRequestBaseUrl(),
StoreBranding = await StoreBrandingViewModel.CreateAsync(Request, uriResolver, plan.Offering.App.StoreData.GetStoreBlob()),
StoreName = plan.Offering.App.StoreData.StoreName,
Title = plan.Name,
### BTCPayServer/Plugins/Subscriptions/Views/UIPlanCheckout/PlanCheckout.cshtml
@@ -265,7 +265,7 @@
</div>
</div>
-<div class="modal fade" id="OpenQrModal" tabindex="-1" aria-labelledby="ConfirmTitle" aria-hidden="true">
+<div class="modal fade plan-checkout__qr-modal" id="OpenQrModal" tabindex="-1" aria-labelledby="ConfirmTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
@@ -276,7 +276,7 @@
</div>
<div class="modal-body pt-0 text-center">
<component type="typeof(BTCPayServer.Blazor.QrCode)"
- param-Data="@LinkGenerator.PlanCheckout(Model.CheckoutId, Context.Request.GetRequestBaseUrl())"
+ param-Data="@LinkGenerator.PlanCheckout(Model.CheckoutId, Model.BaseUrl)"
render-mode="Static"
/>
</div>
### BTCPayServer/Plugins/Subscriptions/Views/UIPlanCheckout/PlanCheckoutViewModel.cs
@@ -1,4 +1,5 @@
-using BTCPayServer.Data.Subscriptions;
+using BTCPayServer.Abstractions;
+using BTCPayServer.Data.Subscriptions;
using BTCPayServer.Models;
namespace BTCPayServer.Views.UIStoreMembership;
@@ -10,6 +11,7 @@ public class PlanCheckoutViewModel
public string StoreName { get; set; }
public string Email { get; set; }
public string CheckoutId { get; set; }
+ public RequestBaseUrl BaseUrl { get; set; }
public PlanData Data { get; set; }
public bool IsPrefilled { get; set; }
public bool IsTrial { get; set; }Why this scored 34/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.