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. These are defensive hardening changes rather than fixes for a known active attack, but they close gaps that could let a user accidentally (or via a manipulated client) set a mismatched password or an invalid username during account setup or password change.
Treat as a routine security hardening merge. Review whether other registration/password endpoints also enforce password-repeat matching and username validation consistently, and consider adding server-side validation for password strength if not already present.
Security signals we found
Missing input validation on password-repeat field
Missing username format validation before uniqueness check
New HTTP 400 error responses for mismatched password and invalid username
Test additions confirm the new validation behavior
Evidence from the diff
The patch modifies lnbits/core/views/auth_api.py in two endpoints: update_password and first_install. Previously update_password did not compare data.password to data.password_repeat, and first_install did neither. Both also failed to validate the username format with is_valid_username before checking uniqueness. The change adds explicit ‘Passwords do not match.’ and ‘Invalid username.’ HTTP 400 responses, plus tests covering these cases. It is a partial hardening patch; the commit message gives no indication of a CVE or reported vulnerability.
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.