What changed, and why it matters
This commit adds a new configuration option that lets LNbits administrators decide whether authentication cookies must only be sent over HTTPS and whether single-sign-on (SSO) should reject plain HTTP connections. By default the setting is turned on, which improves security for production deployments. The change also fixes a related bug where SSO login responses did not include the user's account ID in the access token. Test and CI configurations explicitly disable the HTTPS-only requirement so automated tests can run without TLS.
No immediate action required; this is a hardening/feature commit. Operators should ensure `AUTH_HTTPS_ONLY` remains enabled in production and that LNbits is served behind TLS. Reviewers may want to verify that no other auth cookies are still missing the secure flag and that the SSO `allow_insecure_http` logic cannot be bypassed by reverse-proxy misconfiguration.
Security signals we found
Adds secure cookie flag controlled by HTTPS-only setting
Adds SameSite=Lax to auth cookies
SSO provider now respects HTTPS-only setting for insecure HTTP allowance
Fixes missing user ID in SSO redirect access token payload
Default is secure (True), with explicit opt-out for local/dev/test use
Evidence from the diff
The patch introduces auth_https_only (default True) in lnbits/settings.py. When enabled, all auth-related cookies (cookie_access_token, admin_access_token, is_lnbits_user_authorized, is_lnbits_user_impersonated) are set with secure=True and samesite="lax". It also wires the same flag into SSO provider initialization so allow_insecure_http becomes False when HTTPS-only is enabled. Additionally, _auth_redirect_response now receives and stores user_id in the token payload, and _handle_sso_login passes account.id to it. Tests and the JMeter CI workflow set AUTH_HTTPS_ONLY=false to keep non-TLS test environments working.
Changed components
lnbits/core/views/auth_api.pylnbits/settings.pytests/conftest.py.github/workflows/jmeter.ymlInspect captured patch +59 / −11
diff --git a/.github/workflows/jmeter.yml b/.github/workflows/jmeter.yml
index 19fe000..a2e29d5 100644
--- a/.github/workflows/jmeter.yml
+++ b/.github/workflows/jmeter.yml
@@ -22,6 +22,7 @@ jobs:
- name: run LNbits
env:
LNBITS_ADMIN_UI: true
+ AUTH_HTTPS_ONLY: false
LNBITS_EXTENSIONS_DEFAULT_INSTALL: "watchonly, satspay, tipjar, tpos, lnurlp, withdraw"
LNBITS_BACKEND_WALLET_CLASS: FakeWallet
run: |
diff --git a/lnbits/core/views/auth_api.py b/lnbits/core/views/auth_api.py
index 38c8b21..9fd0d8a 100644
--- a/lnbits/core/views/auth_api.py
+++ b/lnbits/core/views/auth_api.py
@@ -154,9 +154,20 @@ async def impersonate_user(
max_age = settings.auth_token_expire_minutes * 60
response.set_cookie(
- "admin_access_token", cookie_access_token, httponly=True, max_age=max_age
+ "admin_access_token",
+ cookie_access_token,
+ httponly=True,
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
+ )
+ response.set_cookie(
+ "is_lnbits_user_impersonated",
+ "true",
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
)
- response.set_cookie("is_lnbits_user_impersonated", "true", max_age=max_age)
return response
@@ -177,7 +188,12 @@ async def stop_impersonate_user(
)
max_age = settings.auth_token_expire_minutes * 60
response.set_cookie(
- "cookie_access_token", admin_access_token, httponly=True, max_age=max_age
+ "cookie_access_token",
+ admin_access_token,
+ httponly=True,
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
)
response.delete_cookie("admin_access_token")
response.delete_cookie("is_access_token_expired")
@@ -560,7 +576,7 @@ async def _handle_sso_login(userinfo: OpenID, verified_user_id: str | None = Non
id=uuid4().hex, email=email, extra=UserExtra(email_verified=True)
)
await create_user_account(account)
- return _auth_redirect_response(redirect_path, email)
+ return _auth_redirect_response(redirect_path, account.id, email)
def _auth_success_response(
@@ -575,9 +591,20 @@ def _auth_success_response(
max_age = settings.auth_token_expire_minutes * 60
response = JSONResponse({"access_token": access_token, "token_type": "bearer"})
response.set_cookie(
- "cookie_access_token", access_token, httponly=True, max_age=max_age
+ "cookie_access_token",
+ access_token,
+ httponly=True,
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
+ )
+ response.set_cookie(
+ "is_lnbits_user_authorized",
+ "true",
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
)
- response.set_cookie("is_lnbits_user_authorized", "true", max_age=max_age)
response.delete_cookie("is_access_token_expired")
return response
@@ -594,15 +621,28 @@ def _auth_api_token_response(
)
-def _auth_redirect_response(path: str, email: str) -> RedirectResponse:
- payload = AccessTokenPayload(sub="" or "", email=email, auth_time=int(time()))
+def _auth_redirect_response(path: str, user_id: str, email: str) -> RedirectResponse:
+ payload = AccessTokenPayload(
+ usr=user_id, sub="", email=email, auth_time=int(time())
+ )
access_token = create_access_token(data=payload.dict())
max_age = settings.auth_token_expire_minutes * 60
response = RedirectResponse(path)
response.set_cookie(
- "cookie_access_token", access_token, httponly=True, max_age=max_age
+ "cookie_access_token",
+ access_token,
+ httponly=True,
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
+ )
+ response.set_cookie(
+ "is_lnbits_user_authorized",
+ "true",
+ secure=settings.auth_https_only,
+ samesite="lax",
+ max_age=max_age,
)
- response.set_cookie("is_lnbits_user_authorized", "true", max_age=max_age)
response.delete_cookie("is_access_token_expired")
return response
@@ -622,7 +662,10 @@ def _new_sso(provider: str) -> SSOBase | None:
sso_provider_class = _find_auth_provider_class(provider)
sso_provider = sso_provider_class(
- client_id, client_secret, None, allow_insecure_http=True
+ client_id,
+ client_secret,
+ None,
+ allow_insecure_http=not settings.auth_https_only,
)
if (
discovery_url
diff --git a/lnbits/settings.py b/lnbits/settings.py
index 9fa049c..2fa18b7 100644
--- a/lnbits/settings.py
+++ b/lnbits/settings.py
@@ -1005,6 +1005,9 @@ class EnvSettings(LNbitsSettings):
debug: bool = Field(default=False)
debug_database: bool = Field(default=False)
bundle_assets: bool = Field(default=True)
+ # When enabled, auth cookies require HTTPS and SSO will reject insecure HTTP.
+ # Set to false for local/dev environments that run without TLS.
+ auth_https_only: bool = Field(default=True)
host: str = Field(default="127.0.0.1")
port: int = Field(default=5000, gt=0)
forwarded_allow_ips: str = Field(default="*")
diff --git a/tests/conftest.py b/tests/conftest.py
index 95ab104..9dd3d90 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -43,6 +43,7 @@ def anyio_backend():
@pytest.fixture(scope="session")
def settings():
# override settings for tests
+ lnbits_settings.auth_https_only = False
lnbits_settings.lnbits_admin_extensions = []
lnbits_settings.lnbits_data_folder = "./tests/data"
lnbits_settings.lnbits_admin_ui = True
Why this scored 36/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.