fix: `/first_install` new superuser may duplicate an username (#3787)
What changed, and why it matters
This patch fixes a setup-time bug in LNbits where the initial superuser account could be assigned a username that already belongs to another user. Before the fix, the first-install endpoint did not check for duplicate usernames, which could create account confusion or allow the new admin to overlap with an existing user's identity. The patch now rejects the request if the chosen username is already taken.
Treat this as a low-to-moderate hardening fix. Review whether other account-update paths (e.g., profile settings, admin user creation) enforce username uniqueness consistently, and consider adding a unique database constraint on usernames if one does not already exist.
Security signals we found
Missing uniqueness validation on privileged account creation
Potential account identity collision during first-install setup
Fix adds explicit duplicate-username check before superuser update
Evidence from the diff
The /first_install endpoint in lnbits/core/views/auth_api.py now calls get_account_by_username(data.username, False) before assigning the requested username to the superuser account. If a matching account exists, it returns HTTP 400 ‘Username already exists.’ Previously, this validation was missing, so the superuser could be updated to duplicate an existing username, potentially causing authentication/authorization ambiguity or account-linking issues depending on how usernames are used elsewhere.
Changed components
lnbits/core/views/auth_api.py/first_install endpointsuperuser initialization flowInspect captured patch +6 / −0
diff --git a/lnbits/core/views/auth_api.py b/lnbits/core/views/auth_api.py
index ebf2d55..38c8b21 100644
--- a/lnbits/core/views/auth_api.py
+++ b/lnbits/core/views/auth_api.py
@@ -511,15 +511,21 @@ async def update_ui_customization(
async def first_install(data: UpdateSuperuserPassword) -> JSONResponse:
if not settings.first_install:
raise HTTPException(HTTPStatus.FORBIDDEN, "This is not your first install")
+
if settings.first_install_token:
if not data.first_install_token:
raise HTTPException(HTTPStatus.UNAUTHORIZED, "Missing first_install_token.")
if settings.first_install_token != data.first_install_token:
raise HTTPException(HTTPStatus.UNAUTHORIZED, "Invalid first_install_token.")
+ account = await get_account_by_username(data.username, False)
+ if account:
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Username already exists.")
+
account = await get_account(settings.super_user)
if not account:
raise HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR, "Superuser not found.")
+
account.username = data.username
account.extra = account.extra or UserExtra()
account.extra.provider = "lnbits"
Why this scored 46/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.