Validate API key authorization redirects (#7721)
What changed, and why it matters
This commit tightens how BTCPay Server validates the web address users are sent back to after authorizing an API key. Previously, the code only rejected non-absolute redirect URLs in one place and did not re-check the redirect after form submission. The new helper requires the redirect to be an absolute http:// or https:// URL and applies the same check in both the GET and POST authorization handlers. This prevents an attacker from tricking the server into redirecting a user to a malicious address or local service after authorization.
Review whether any other redirect parameters in the API key or OAuth-like flows bypass this helper, and confirm the `IsSafeRedirect` logic is reused consistently. Consider adding automated tests covering scheme, absolute URI, and null cases. No immediate emergency action is indicated, but deploy with the next maintenance window.
Security signals we found
Open redirect / unsafe redirect validation added
Missing server-side validation in POST handler remediated
Scheme whitelist introduced (http/https only)
Absolute URI requirement enforced
Evidence from the diff
The patch replaces a single redirect?.IsAbsoluteUri is false check with a new IsSafeRedirect helper that requires the URI to be absolute and use the http or https scheme. It also adds the same validation to the POST AuthorizeAPIKey action, which had been missing. The change closes a likely open redirect / SSRF-adjacent issue in the API key authorization flow where a crafted redirect/RedirectUrl parameter could send the browser or authorization code to an arbitrary URI (including file://, data://, or internal addresses).
Changed components
BTCPayServer/Controllers/UIManageController.APIKeys.csAPI key authorization flow (GET and POST ~/api-keys/authorize)Inspect captured patch +9 / −1
### BTCPayServer/Controllers/UIManageController.APIKeys.cs
@@ -146,7 +146,7 @@ public async Task<IActionResult> AuthorizeAPIKey(string[] permissions, string ap
.Where(permission => _permissionService.IsValidPolicy(permission.Policy))
.ToList();
- if (redirect?.IsAbsoluteUri is false)
+ if (!IsSafeRedirect(redirect))
{
redirect = null;
}
@@ -178,6 +178,11 @@ public async Task<IActionResult> AuthorizeAPIKey(string[] permissions, string ap
[HttpPost("~/api-keys/authorize")]
public async Task<IActionResult> AuthorizeAPIKey([FromForm] AuthorizeApiKeysViewModel viewModel)
{
+ if (!IsSafeRedirect(viewModel.RedirectUrl))
+ {
+ viewModel.RedirectUrl = null;
+ }
+
viewModel = await SetViewModelValues(viewModel);
AdjustVMForAuthorization(viewModel);
@@ -305,6 +310,8 @@ public async Task<IActionResult> AddApiKey(AddApiKeyViewModel viewModel)
return RedirectToAction("APIKeys");
}
+ private static bool IsSafeRedirect(Uri redirect) => redirect is { IsAbsoluteUri: true } && redirect.Scheme is "http" or "https";
+
private async Task<APIKeyData> CheckForMatchingApiKey(IEnumerable<Permission> requestedPermissions, AuthorizeApiKeysViewModel vm)
{
if (string.IsNullOrEmpty(vm.ApplicationIdentifier) || vm.RedirectUrl == null)
### docs/maintainers/coding-conventions.md
@@ -4,6 +4,7 @@
- Follow the repository `.editorconfig`; it is the source of truth for
formatting and C# style.
+- Prefer expression-bodied methods when the implementation fits on one line.
- Prefer `Newtonsoft.Json` over `System.Text.Json` when adding or changing JSON
serialization.
- Do not use `ConfigureAwait(false)`; BTCPay Server does not rely on aWhy this scored 67/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.