fix[8c7f]: reject SSO for accounts with passwords (#4347)
What changed, and why it matters
This update blocks users from logging into password-protected accounts through single sign-on (SSO), such as 'Login with GitHub'. Before this change, an attacker who knew or guessed a victim's email address could potentially use an SSO provider to access an account that also had a password, possibly bypassing the password (and any two-factor authentication tied to it). The fix makes the system reject SSO logins for any account that has a password set, forcing password-based login instead.
Deploy this patch promptly. Review whether any existing password-protected accounts have previously been linked to SSO identities and consider requiring password reset or re-verification for those users. Ensure monitoring/alerts exist for repeated SSO attempts against password-protected accounts.
Security signals we found
Authentication bypass mitigation: SSO can no longer authenticate accounts that also have local passwords
Account takeover risk reduction: blocks an external identity provider from overriding a password-protected account
State preservation: password hash and email_verified flag remain unchanged on rejection
Test coverage added for both token-flow and direct _handle_sso_login rejection paths
Evidence from the diff
The commit adds a check in _handle_sso_login() in lnbits/core/views/auth_api.py: if the matched account has a password (account.has_password), it raises HTTPException(403, ‘SSO is disabled for accounts with a password…’). This prevents SSO authentication from succeeding for accounts that were originally created with a password. Tests confirm that password-holding accounts are rejected, email_verified is not flipped, the password hash remains intact, and normal password login still works. A two-factor test is updated to clear the password hash before exercising the SSO path.
Changed components
lnbits/core/views/auth_api.py:_handle_sso_loginSSO login flow (/api/v1/auth/{provider}/token and callback)Two-factor authentication test path for SSOInspect captured patch +70 / −2
### lnbits/core/views/auth_api.py
@@ -679,6 +679,12 @@ async def _handle_sso_login(userinfo: OpenID, verified_user_id: str | None = Non
redirect_path = "/account"
if account:
+ if account.has_password:
+ raise HTTPException(
+ HTTPStatus.FORBIDDEN,
+ "SSO is disabled for accounts with a password. "
+ "Sign in with your username and password.",
+ )
account.extra = account.extra or UserExtra()
account.extra.email_verified = True
await update_account(account)
### tests/api/test_auth_api.py
@@ -2,13 +2,15 @@
from uuid import uuid4
import pytest
+from fastapi import HTTPException
from fastapi.responses import RedirectResponse
+from fastapi_sso.sso.base import OpenID
from httpx import AsyncClient
from lnbits.core.crud.users import get_account, update_account
-from lnbits.core.models.users import Account
+from lnbits.core.models.users import Account, UserExtra
from lnbits.core.services.users import create_user_account
-from lnbits.core.views.auth_api import get_account_by_email
+from lnbits.core.views.auth_api import _handle_sso_login, get_account_by_email
from lnbits.settings import Settings
@@ -146,6 +148,63 @@ async def test_auth_api_sso_login_and_callback(http_client: AsyncClient, mocker)
assert account.extra.email_verified is True
+@pytest.mark.anyio
+@pytest.mark.parametrize("has_password", [False, True])
+@pytest.mark.parametrize("email_verified", [False, True])
+async def test_auth_api_sso_rejects_accounts_with_passwords(
+ http_client: AsyncClient, mocker, has_password, email_verified
+):
+ account = Account(
+ id=uuid4().hex,
+ username=f"user_{uuid4().hex[:8]}",
+ email=f"{uuid4().hex}@example.com",
+ extra=UserExtra(email_verified=email_verified),
+ )
+ if has_password:
+ account.hash_password("secret1234")
+ await create_user_account(account)
+ mocker.patch(
+ "lnbits.core.views.auth_api._new_sso",
+ return_value=_FakeSSO(OpenID(id="github-user", email=account.email)),
+ )
+ http_client.cookies.clear()
+
+ response = await http_client.get("/api/v1/auth/github/token")
+
+ if has_password:
+ assert response.status_code == 403
+ assert "Sign in with your username and password" in response.json()["detail"]
+ assert (await http_client.get("/api/v1/auth")).status_code == 401
+ stored = await get_account(account.id)
+ assert stored and stored.extra.email_verified == email_verified
+ assert stored.password_hash == account.password_hash
+ login = await http_client.post(
+ "/api/v1/auth",
+ json={"username": account.username, "password": "secret1234"},
+ )
+ assert login.status_code == 200
+ else:
+ assert response.status_code == 307
+ assert response.headers["location"] == "/wallet"
+
+ assert (await http_client.get("/api/v1/auth")).json()["id"] == account.id
+
+
+@pytest.mark.anyio
+async def test_auth_api_sso_link_cannot_bypass_password_login(http_client):
+ account = Account(id=uuid4().hex)
+ account.hash_password("secret1234")
+ await create_user_account(account)
+ identity = OpenID(id="github-user", email=f"{uuid4().hex}@example.com")
+
+ with pytest.raises(HTTPException, match="Sign in with your username and password"):
+ await _handle_sso_login(identity, verified_user_id=account.id)
+
+ stored = await get_account(account.id)
+ assert stored and stored.password_hash == account.password_hash
+ assert not stored.extra.email_verified
+
+
@pytest.mark.anyio
async def test_auth_api_first_install_success_and_validation(
http_client: AsyncClient, settings: Settings
### tests/api/test_two_factor.py
@@ -569,6 +569,9 @@ async def test_two_factor_sso_does_not_bypass(http_client, factor_account):
factor_account.email = f"{factor_account.id}@example.com"
await update_account(factor_account)
await enroll(http_client, factor_account)
+ # SSO is available only to accounts without a password.
+ factor_account.password_hash = None
+ await update_account(factor_account)
response = await _handle_sso_login(
OpenID.model_validate({"email": factor_account.email})
)Why this scored 63/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.