What changed, and why it matters
This commit adds missing safety checks to two password-related features in LNbits. It now verifies that the new password and its confirmation match, and that chosen usernames follow the allowed format. Previously, these checks were either absent or inconsistent, which could let a user accidentally set a mismatched password or use an invalid username during account setup or password change.
Review whether any other registration, password-reset, or superuser-setup endpoints also lack `password_repeat` and `is_valid_username` checks, and apply the same validation consistently. Consider adding server-side validation for password strength if not already present.
Security signals we found
Missing input validation on password confirmation field
Missing username format validation in password-update and first-install flows
Potential for account state inconsistency if mismatched password values are processed
Tests added specifically for negative/error cases, indicating defensive hardening
Evidence from the diff
The patch hardens update_password() and first_install() in lnbits/core/views/auth_api.py. In update_password(), it adds a password != password_repeat check and applies is_valid_username() to the supplied username before checking for duplicates. In first_install(), it adds the same password-match and username-format validations. The change also includes tests confirming that mismatched passwords and invalid usernames are rejected with HTTP 400.
Changed components
lnbits/core/views/auth_api.pytests/api/test_auth.pyInspect captured patch +87 / −6
diff --git a/lnbits/core/views/auth_api.py b/lnbits/core/views/auth_api.py
index 504bfc5..8e11453 100644
--- a/lnbits/core/views/auth_api.py
+++ b/lnbits/core/views/auth_api.py
@@ -503,12 +503,13 @@ async def update_password(
_validate_auth_timeout(payload.auth_time)
if data.user_id != account.id:
raise ValueError("Invalid user ID.")
- if (
- data.username
- and account.username != data.username
- and await get_account_by_username(data.username)
- ):
- raise HTTPException(HTTPStatus.BAD_REQUEST, "Username already exists.")
+ if data.password != data.password_repeat:
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Passwords do not match.")
+ if account.username != data.username:
+ if not is_valid_username(data.username):
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Invalid username.")
+ if await get_account_by_username(data.username):
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Username already exists.")
# old accounts do not have a password
if account.password_hash:
@@ -609,6 +610,11 @@ async def first_install(data: UpdateSuperuserPassword) -> JSONResponse:
if settings.first_install_token != data.first_install_token:
raise HTTPException(HTTPStatus.UNAUTHORIZED, "Invalid first_install_token.")
+ if data.password != data.password_repeat:
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Passwords do not match.")
+ if not is_valid_username(data.username):
+ raise HTTPException(HTTPStatus.BAD_REQUEST, "Invalid username.")
+
account = await get_account_by_username(data.username, False)
if account:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Username already exists.")
diff --git a/tests/api/test_auth.py b/tests/api/test_auth.py
index 4aca157..41201ea 100644
--- a/tests/api/test_auth.py
+++ b/tests/api/test_auth.py
@@ -689,6 +689,81 @@ async def test_change_password_ok(http_client: AsyncClient, settings: Settings):
assert response.json().get("access_token") is not None, "Access token created."
+async def _register_and_get_token(http_client: AsyncClient, settings: Settings):
+ tiny_id = shortuuid.uuid()[:8]
+ response = await http_client.post(
+ "/api/v1/auth/register",
+ json={
+ "username": f"u21.{tiny_id}",
+ "password": "secret1234",
+ "password_repeat": "secret1234",
+ "email": f"u21.{tiny_id}@lnbits.com",
+ },
+ )
+ assert response.status_code == 200, "User created."
+ access_token = response.json().get("access_token")
+ payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
+ return f"u21.{tiny_id}", access_token, AccessTokenPayload(**payload).usr
+
+
+@pytest.mark.anyio
+async def test_change_password_passwords_do_not_match(
+ http_client: AsyncClient, settings: Settings
+):
+ username, access_token, user_id = await _register_and_get_token(
+ http_client, settings
+ )
+
+ response = await http_client.put(
+ "/api/v1/auth/password",
+ headers={"Authorization": f"Bearer {access_token}"},
+ json={
+ "username": username,
+ "user_id": user_id,
+ "password_old": "secret1234",
+ "password": "secret0000",
+ "password_repeat": "secret9999",
+ },
+ )
+
+ assert response.status_code == 400
+ assert response.json().get("detail") == "Passwords do not match."
+
+ response = await http_client.post(
+ "/api/v1/auth", json={"username": username, "password": "secret1234"}
+ )
+ assert response.status_code == 200, "Old password still works."
+
+
+@pytest.mark.anyio
+async def test_change_password_invalid_username(
+ http_client: AsyncClient, settings: Settings
+):
+ username, access_token, user_id = await _register_and_get_token(
+ http_client, settings
+ )
+
+ response = await http_client.put(
+ "/api/v1/auth/password",
+ headers={"Authorization": f"Bearer {access_token}"},
+ json={
+ "username": "someone@lnbits.com",
+ "user_id": user_id,
+ "password_old": "secret1234",
+ "password": "secret0000",
+ "password_repeat": "secret0000",
+ },
+ )
+
+ assert response.status_code == 400
+ assert response.json().get("detail") == "Invalid username."
+
+ response = await http_client.post(
+ "/api/v1/auth", json={"username": username, "password": "secret1234"}
+ )
+ assert response.status_code == 200, "Username and password did not change."
+
+
@pytest.mark.anyio
async def test_change_password_not_authenticated(http_client: AsyncClient):
tiny_id = shortuuid.uuid()[:8]
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.