quart-security 2.0.4__tar.gz → 2.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {quart_security-2.0.4 → quart_security-2.0.6}/PKG-INFO +8 -1
  2. {quart_security-2.0.4 → quart_security-2.0.6}/README.md +7 -0
  3. {quart_security-2.0.4 → quart_security-2.0.6}/pyproject.toml +1 -1
  4. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/__init__.py +2 -0
  5. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/password.py +12 -0
  6. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/signals.py +4 -0
  7. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/views.py +64 -32
  8. {quart_security-2.0.4 → quart_security-2.0.6}/LICENSE +0 -0
  9. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/breach.py +0 -0
  10. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/core.py +0 -0
  11. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/datastore.py +0 -0
  12. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/decorators.py +0 -0
  13. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/email.py +0 -0
  14. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/forms.py +0 -0
  15. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/models.py +0 -0
  16. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/proxies.py +0 -0
  17. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/py.typed +0 -0
  18. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/state.py +0 -0
  19. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/_macros.html +0 -0
  20. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/change_password.html +0 -0
  21. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/login_user.html +0 -0
  22. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/mf_recovery.html +0 -0
  23. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/mf_recovery_codes.html +0 -0
  24. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/register_user.html +0 -0
  25. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/two_factor_select.html +0 -0
  26. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/two_factor_setup.html +0 -0
  27. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/two_factor_verify_code.html +0 -0
  28. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/wan_register.html +0 -0
  29. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/wan_signin.html +0 -0
  30. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/templates/security/wan_verify.html +0 -0
  31. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/totp.py +0 -0
  32. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/utils.py +0 -0
  33. {quart_security-2.0.4 → quart_security-2.0.6}/quart_security/webauthn.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quart-security
3
- Version: 2.0.4
3
+ Version: 2.0.6
4
4
  Summary: Native async auth for Quart
5
5
  Author: quart-security maintainers
6
6
  Requires-Python: >=3.11
@@ -169,6 +169,8 @@ Core:
169
169
 
170
170
  Existing pbkdf2_sha512 and bcrypt hashes continue to verify after the argon2 default change. They are transparently rehashed to argon2id on the user's next successful login.
171
171
 
172
+ Until then, a failed login for an account with a legacy hash takes a different time than one for an unknown email, which is checked against an argon2id dummy, so timing can reveal that the account exists. Apps that import hashes from another system should require a password reset rather than leave them in place.
173
+
172
174
  2FA:
173
175
  - `SECURITY_TWO_FACTOR`
174
176
  - `SECURITY_TOTP_ISSUER`
@@ -196,6 +198,10 @@ Routing:
196
198
  - `SECURITY_POST_LOGIN_VIEW`
197
199
  - `SECURITY_POST_REGISTER_VIEW`
198
200
 
201
+ A visit to `/login?next=/path` sends the user to `/path` after they sign in, through 2FA and passkey sign-in too. Only same-site paths are honored, the next visit to `/login` replaces the target, and a sign-in finishing more than 10 minutes after the form was shown ignores it.
202
+
203
+ Registration answers the same way whether or not the email already has an account, so it cannot be used to find accounts. For an existing email nothing is created and `user_register_conflict` fires with the existing user; send the owner a notice from it in the background, because a receiver that waits on mail slows only that response.
204
+
199
205
  ## Route Map
200
206
 
201
207
  Core:
@@ -245,6 +251,7 @@ from quart_security import (
245
251
  password_changed,
246
252
  tf_profile_changed,
247
253
  user_registered,
254
+ user_register_conflict,
248
255
  url_for_security,
249
256
  )
250
257
  ```
@@ -139,6 +139,8 @@ Core:
139
139
 
140
140
  Existing pbkdf2_sha512 and bcrypt hashes continue to verify after the argon2 default change. They are transparently rehashed to argon2id on the user's next successful login.
141
141
 
142
+ Until then, a failed login for an account with a legacy hash takes a different time than one for an unknown email, which is checked against an argon2id dummy, so timing can reveal that the account exists. Apps that import hashes from another system should require a password reset rather than leave them in place.
143
+
142
144
  2FA:
143
145
  - `SECURITY_TWO_FACTOR`
144
146
  - `SECURITY_TOTP_ISSUER`
@@ -166,6 +168,10 @@ Routing:
166
168
  - `SECURITY_POST_LOGIN_VIEW`
167
169
  - `SECURITY_POST_REGISTER_VIEW`
168
170
 
171
+ A visit to `/login?next=/path` sends the user to `/path` after they sign in, through 2FA and passkey sign-in too. Only same-site paths are honored, the next visit to `/login` replaces the target, and a sign-in finishing more than 10 minutes after the form was shown ignores it.
172
+
173
+ Registration answers the same way whether or not the email already has an account, so it cannot be used to find accounts. For an existing email nothing is created and `user_register_conflict` fires with the existing user; send the owner a notice from it in the background, because a receiver that waits on mail slows only that response.
174
+
169
175
  ## Route Map
170
176
 
171
177
  Core:
@@ -215,6 +221,7 @@ from quart_security import (
215
221
  password_changed,
216
222
  tf_profile_changed,
217
223
  user_registered,
224
+ user_register_conflict,
218
225
  url_for_security,
219
226
  )
220
227
  ```
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
4
4
 
5
5
  [project]
6
6
  name = "quart-security"
7
- version = "2.0.4"
7
+ version = "2.0.6"
8
8
  description = "Native async auth for Quart"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -16,6 +16,7 @@ from .signals import (
16
16
  tf_profile_changed,
17
17
  user_authenticated,
18
18
  user_logged_out,
19
+ user_register_conflict,
19
20
  user_registered,
20
21
  )
21
22
  from .state import SecurityState, SQLAlchemyStateStore
@@ -40,5 +41,6 @@ __all__ = [
40
41
  "password_changed",
41
42
  "tf_profile_changed",
42
43
  "user_registered",
44
+ "user_register_conflict",
43
45
  "url_for_security",
44
46
  ]
@@ -1,6 +1,7 @@
1
1
  """Password hashing and validation helpers."""
2
2
 
3
3
  import asyncio
4
+ import secrets
4
5
 
5
6
  from passlib.context import CryptContext
6
7
  from quart import current_app
@@ -82,6 +83,17 @@ async def verify_password_async(password: str, password_hash: str, *, app=None)
82
83
  return await asyncio.to_thread(verify_password, password, password_hash, app=app)
83
84
 
84
85
 
86
+ async def unknown_user_hash_async(*, app=None) -> str:
87
+ """A hash to verify against when no account matches, so a login for an
88
+ unknown or inactive email costs the same as a wrong password."""
89
+ app = app or current_app._get_current_object()
90
+ cached = app.extensions.get("quart_security_unknown_hash")
91
+ if cached is None:
92
+ cached = await hash_password_async(secrets.token_urlsafe(32), app=app)
93
+ app.extensions["quart_security_unknown_hash"] = cached
94
+ return cached
95
+
96
+
85
97
  def validate_password(password: str, min_length: int = 12) -> list[str]:
86
98
  errors: list[str] = []
87
99
  if len(password) < min_length:
@@ -9,3 +9,7 @@ user_logged_out = _signals.signal("user-logged-out")
9
9
  password_changed = _signals.signal("password-changed")
10
10
  tf_profile_changed = _signals.signal("tf-profile-changed")
11
11
  user_registered = _signals.signal("user-registered")
12
+ # Registration with an email that already has an account. The form answers as if
13
+ # it succeeded, so tell the owner out of band; receivers run inside the request,
14
+ # so send mail in the background or the delay reveals the account.
15
+ user_register_conflict = _signals.signal("user-register-conflict")
@@ -37,11 +37,17 @@ from .forms import (
37
37
  from .password import (
38
38
  hash_password_async,
39
39
  password_needs_rehash,
40
+ unknown_user_hash_async,
40
41
  validate_password,
41
42
  verify_password_async,
42
43
  )
43
44
  from .proxies import _security, current_user
44
- from .signals import password_changed, tf_profile_changed, user_registered
45
+ from .signals import (
46
+ password_changed,
47
+ tf_profile_changed,
48
+ user_register_conflict,
49
+ user_registered,
50
+ )
45
51
  from .utils import maybe_await, naive_utcnow, url_for_security
46
52
 
47
53
  security_bp = Blueprint("security", __name__, template_folder="templates")
@@ -146,6 +152,22 @@ def _safe_redirect_target(candidate: str | None, fallback: str = "/") -> str:
146
152
  return candidate
147
153
 
148
154
 
155
+ # A login that finishes later than this after the form was shown ignores its
156
+ # `next`, so an abandoned target cannot redirect an unrelated later sign-in.
157
+ NEXT_TTL_SECONDS = 600
158
+
159
+
160
+ async def _complete_login(user):
161
+ """Log the user in and send them where `/login?next=` asked, if it was safe."""
162
+ # login_user clears the session, so read the target first.
163
+ target, stored_at = session.pop("security_next", None) or (None, 0)
164
+ if time.time() - stored_at > NEXT_TTL_SECONDS:
165
+ target = None
166
+ await _security.login_user(user)
167
+ fallback = _resolve_redirect("SECURITY_POST_LOGIN_VIEW", "login")
168
+ return redirect(_safe_redirect_target(target, fallback=fallback))
169
+
170
+
149
171
  def _webauthn_rp_id() -> str:
150
172
  configured = current_app.config.get("SECURITY_WAN_RP_ID")
151
173
  if configured:
@@ -348,6 +370,14 @@ def _format_webauthn_credential(credential) -> dict:
348
370
  async def login():
349
371
  form = await _security.login_form_cls.from_formdata()
350
372
  await _enforce_csrf(getattr(form, "_submitted_csrf", None))
373
+ if not _is_post():
374
+ # Every visit to the form replaces the target, so an abandoned one never
375
+ # redirects a later login. The passkey and 2FA paths finish with it too.
376
+ next_url = _safe_redirect_target(request.args.get("next"), fallback="")
377
+ if next_url:
378
+ session["security_next"] = [next_url, int(time.time())]
379
+ else:
380
+ session.pop("security_next", None)
351
381
 
352
382
  if _is_post() and form.validate():
353
383
  user = await _find_user(email=_normalize_email(form.email.data))
@@ -364,11 +394,14 @@ async def login():
364
394
  user.locked_until = None
365
395
  await _commit()
366
396
 
367
- if (
368
- user
369
- and getattr(user, "active", True)
370
- and await verify_password_async(form.password.data, user.password)
371
- ):
397
+ # Always run one verify, so response time does not reveal which emails
398
+ # have an active account.
399
+ known = user is not None and getattr(user, "active", True)
400
+ password_ok = await verify_password_async(
401
+ form.password.data,
402
+ user.password if known else await unknown_user_hash_async(),
403
+ )
404
+ if known and password_ok:
372
405
  # Transparent rehash: upgrade weak/legacy hash on successful login
373
406
  if password_needs_rehash(user.password):
374
407
  user.password = await hash_password_async(form.password.data)
@@ -383,8 +416,7 @@ async def login():
383
416
  return redirect(url_for_security("two_factor_token_validation"))
384
417
 
385
418
  await _reset_auth_failures(user)
386
- await _security.login_user(user)
387
- return redirect(_resolve_redirect("SECURITY_POST_LOGIN_VIEW", "login"))
419
+ return await _complete_login(user)
388
420
 
389
421
  # Track failed login
390
422
  if user:
@@ -405,12 +437,9 @@ async def register():
405
437
 
406
438
  if _is_post() and form.validate():
407
439
  email = _normalize_email(form.email.data)
408
- if await _find_user(email=email):
409
- await flash("A user with this email already exists", "error")
410
- return await render_template(
411
- "security/register_user.html", register_user_form=form
412
- )
413
-
440
+ # Password checks come first and an existing email gets the same answer as
441
+ # a new one, after the same hashing work, so the form never reveals which
442
+ # emails have an account.
414
443
  errors = validate_password(
415
444
  form.password.data,
416
445
  min_length=current_app.config.get("SECURITY_PASSWORD_LENGTH_MIN", 12),
@@ -437,20 +466,22 @@ async def register():
437
466
  "security/register_user.html", register_user_form=form
438
467
  )
439
468
 
440
- user_kwargs = {
441
- "email": email,
442
- "password": await hash_password_async(form.password.data),
443
- "active": True,
444
- }
445
- if hasattr(form, "name") and form.name.data:
446
- user_kwargs["name"] = form.name.data.strip()
469
+ password = await hash_password_async(form.password.data)
470
+ app = current_app._get_current_object()
471
+ existing = await _find_user(email=email)
472
+ if existing:
473
+ await user_register_conflict.send_async(app, user=existing)
474
+ else:
475
+ user_kwargs = {"email": email, "password": password, "active": True}
476
+ if hasattr(form, "name") and form.name.data:
477
+ user_kwargs["name"] = form.name.data.strip()
447
478
 
448
- user = await _create_user(**user_kwargs)
449
- if hasattr(user, "confirmed_at"):
450
- user.confirmed_at = naive_utcnow()
479
+ user = await _create_user(**user_kwargs)
480
+ if hasattr(user, "confirmed_at"):
481
+ user.confirmed_at = naive_utcnow()
451
482
 
452
- await _commit()
453
- await user_registered.send_async(current_app._get_current_object(), user=user)
483
+ await _commit()
484
+ await user_registered.send_async(app, user=user)
454
485
 
455
486
  await flash("Registration successful. Please log in.", "success")
456
487
  return redirect(_resolve_redirect("SECURITY_POST_REGISTER_VIEW", "login"))
@@ -716,8 +747,7 @@ async def two_factor_token_validation():
716
747
  return redirect(url_for_security("login"))
717
748
  session.pop("tf_user_id", None)
718
749
  await _reset_auth_failures(user)
719
- await _security.login_user(user)
720
- return redirect(_resolve_redirect("SECURITY_POST_LOGIN_VIEW", "login"))
750
+ return await _complete_login(user)
721
751
 
722
752
  await _record_auth_failure(user)
723
753
  await flash("Invalid code", "error")
@@ -804,8 +834,7 @@ async def mf_recovery():
804
834
  return redirect(url_for_security("login"))
805
835
  session.pop("tf_user_id", None)
806
836
  await _reset_auth_failures(user)
807
- await _security.login_user(user)
808
- return redirect(_resolve_redirect("SECURITY_POST_LOGIN_VIEW", "login"))
837
+ return await _complete_login(user)
809
838
 
810
839
  await _record_auth_failure(user)
811
840
  await flash("Invalid recovery code", "error")
@@ -929,7 +958,11 @@ async def wan_register_response():
929
958
  await flash("Passkey registration failed.", "error")
930
959
  return redirect(url_for_security("wan_register"))
931
960
 
961
+ # Look across all users too: a software authenticator can pick any credential
962
+ # ID, and sign-in resolves the owner by ID alone.
932
963
  existing = await _find_webauthn_credential(
964
+ verification["credential_id"], user=None
965
+ ) or await _find_webauthn_credential(
933
966
  verification["credential_id"], user=current_user
934
967
  )
935
968
  if existing:
@@ -1054,8 +1087,7 @@ async def wan_signin_response():
1054
1087
  if hasattr(stored_credential, "lastuse_datetime"):
1055
1088
  stored_credential.lastuse_datetime = naive_utcnow()
1056
1089
 
1057
- await _security.login_user(user)
1058
- return redirect(_resolve_redirect("SECURITY_POST_LOGIN_VIEW", "login"))
1090
+ return await _complete_login(user)
1059
1091
 
1060
1092
 
1061
1093
  @security_bp.route("/wan-verify", methods=["GET", "POST"])
File without changes