Skip to content

Auth fixes#640

Open
varmar05 wants to merge 5 commits into
developfrom
auth_fixes
Open

Auth fixes#640
varmar05 wants to merge 5 commits into
developfrom
auth_fixes

Conversation

@varmar05

Copy link
Copy Markdown
Collaborator
  • Session cookie gap fix: load_user now only returns active users; @auth_required checks is_active
  • Bcrypt lazy rehash: configurable BCRYPT_LOG_ROUNDS, passwords rehashed on next login if rounds differ
  • Account lockout: progressive tiers via LOCKOUT_POLICY env var, 423 response with AccountLockedError + added db migration: new failed_login_attempts and locked_until columns added

varmar05 added 3 commits June 17, 2026 15:07
 - `load_user` now returns None for inactive users, so their session cookies are rejected by Flask-Login
 - @auth_required adds `is_active` check
 - anonymize() now explicitly sets active=False for defence-in-depth
Existing passwords will be rehashed organically if needed.
@varmar05 varmar05 requested a review from MarcelGeo June 17, 2026 13:11
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 27823802673

Coverage increased (+0.006%) to 92.162%

Details

  • Coverage increased (+0.006%) from the base build.
  • Patch coverage: 10 uncovered changes across 3 files (132 of 142 lines covered, 92.96%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
server/mergin/auth/controller.py 13 9 69.23%
server/mergin/app.py 3 0 0.0%
server/mergin/auth/models.py 40 37 92.5%
Total (7 files) 142 132 92.96%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
server/mergin/auth/app.py 1 96.77%

Coverage Stats

Coverage Status
Relevant Lines: 10156
Covered Lines: 9360
Line Coverage: 92.16%
Coverage Strength: 0.92 hits per line

💛 - Coveralls

@MarcelGeo MarcelGeo requested a review from harminius June 23, 2026 07:21

@harminius harminius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 🛡️

from ..app import db
from ..sync.models import ProjectUser
from ..sync.utils import get_user_agent, get_ip, get_device_id, is_reserved_word
from .errors import AccountLockedError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used?


def to_dict(self) -> Dict:
data = super().to_dict()
data["locked_until"] = self.locked_until.isoformat()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we make it timezone aware when it's used in client-facing public API?
we already use it when going through schemas

try:
# bcrypt hash format: $2b$<rounds>$<salt+hash>
hash_rounds = int(self.passwd.split("$")[2])
return hash_rounds != rounds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< would be probably safer, but not a big deal

@MarcelGeo MarcelGeo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need something to do in FE/clients/mobile - login attempts logic? @tomasMizera

@varmar05 We didn't implemented ip reputation in this mechanism -> There could be case when somebody can lock another account which he knows by email or accidentally similar emails can block each together. This could be probably handled by infra rate limiting - needs to discuss what values to adjust for current nginx config files in this repository - could be upgraded also nginx proxy.

Flask limiter wouldn't do the same job as database locking - probably issue with redis backend again.

try:
user = authenticate(form.login.data, form.password.data)
except AccountLockedError as e:
abort(423, f"Account temporarily locked until {e.locked_until.isoformat()}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to return exact time (not sure If it's compatible with security guys) - give it to attribute in AccountLockedError new attribute. I think we can show something for user in clients - received count and reset-at time.

Comment thread server/mergin/auth/app.py
if needs_commit:
db.session.commit()
return user
else:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user attempts failing after 5 times -> wait -> is_locked_out will remove locked_until. After 60 seconds another fail will lead to next 60s ... It's like not reseting properly.

now = datetime.datetime.utcnow()
if self.locked_until <= now:
# lockout has expired — clear it so subsequent queries see a clean state
self.locked_until = None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is commited somewhere or it's just related to needsCommit in authenticate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants