Handle missing API key command permissions
What changed, and why it matters
This commit fixes a bug in BTCPay Server's API key management page. Previously, if a user removed a permission checkbox from the form and then clicked a related command button, the server would crash with an error because it assumed the permission was always present. The fix checks whether the permission is missing and returns a clean 'Bad Request' error instead of crashing.
Apply the patch. The change is low-risk and improves robustness. No immediate incident response is required, but ensure API key forms cannot be manipulated to bypass intended permission checks elsewhere.
Security signals we found
Server-side exception converted to controlled HTTP 400 response
Missing input validation on form/command binding
Client-side form tampering can trigger server error without fix
Test added to prevent regression
Evidence from the diff
In UIManageController.APIKeys.cs, HandleCommands used viewModel.PermissionValues.Single(…) to retrieve the permission matching the submitted command. If the corresponding form field was absent (e.g., a user removed the input via browser dev tools or a malformed request omitted it), Single threw an InvalidOperationException, causing a server error. The patch switches to FirstOrDefault and returns BadRequest() when the permission is null. A Playwright test verifies that submitting a command for a removed permission returns HTTP 400.
Changed components
BTCPayServer/Controllers/UIManageController.APIKeys.csAPI key management UIAdd/Edit API key permission form handlingInspect captured patch +29 / −1
### BTCPayServer.Tests/ApiKeysTests.cs
@@ -273,6 +273,29 @@ await TestApiAgainstAccessToken(accessToken, tester, user,
Assert.Contains("There are no associated permissions to the API key being requested", emptyPageContent);
}
+ [Fact]
+ [Trait("Playwright", "Playwright-2")]
+ public async Task CannotSubmitCommandForMissingApiKeyPermission()
+ {
+ await using var s = CreatePlaywrightTester();
+ await s.StartAsync();
+
+ var user = s.Server.NewAccount();
+ await user.GrantAccessAsync();
+ await s.GoToLogin();
+ await s.LogIn(user.RegisterDetails.Email, user.RegisterDetails.Password);
+ await s.GoToProfile(ManageNavPages.APIKeys);
+ await s.ClickPagePrimary();
+
+ const string permission = Policies.CanModifyStoreSettings;
+ await s.Page.Locator($"input[value='{permission}']").EvaluateAsync("element => element.remove()");
+ var response = await s.Page.RunAndWaitForResponseAsync(
+ () => s.Page.Locator($"button[value='{permission}:change-store-mode']").ClickAsync(),
+ response => response.Request.Method == "POST");
+
+ Assert.Equal(StatusCodes.Status400BadRequest, response.Status);
+ }
+
[Fact]
[Trait("Playwright", "Playwright-2")]
### BTCPayServer/Controllers/UIManageController.APIKeys.cs
@@ -452,7 +452,11 @@ private IActionResult HandleCommands(AddApiKeyViewModel viewModel)
{
return null;
}
- var permissionValueItem = viewModel.PermissionValues.Single(item => item.Permission == permission);
+ var permissionValueItem = viewModel.PermissionValues.FirstOrDefault(item => item.Permission == permission);
+ if (permissionValueItem is null)
+ {
+ return BadRequest();
+ }
var command = parts[1];
var storeIndex = parts.Length == 3 ? parts[2] : null;
### Changelog.md
@@ -31,6 +31,7 @@
* **Generated links**: Use the configured server Base URL for account, password-reset, and store-invitation links, and reject non-HTTP(S) Base URLs (#7590 #7628) @NicolasDorier
* **Store settings**: Accept only absolute HTTP or HTTPS Store Website URLs in the UI and Greenfield API (#7604) @NicolasDorier, reported by @gn00295120
* **API Keys**: Show an error instead of a server error when confirming a removed API key (#7568 #7570) @monasco
+* **API Keys**: Prevent server errors when API key permission forms are incomplete (#7733) @NicolasDorier
* **Access Tokens**: Keep failed token revocations scoped to the authorized store (#7629) @NicolasDorier
* **Server users**: Return Not Found instead of a server error when sending a verification email to a deleted user (#7571 #7572) @monasco
* **Plugins**: Run registered plugin database migrations reliably during startup (#7588) @NicolasDorierWhy this scored 44/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.