fix: a Kanban user's own password change signs out their other sessions and counts wrong current passwords (security area 3)
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 7m47s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 1m2s

/api/operator/me/password left every other session of the account valid for
up to 8 h, and a wrong current password was never counted, so a session left
open could guess it. Now, as a customer's change already does: the throttle
and login_failed of the sign-in, and the other sessions of the account end.
users_test proves both; the Usuários admin edit already ended sessions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-12-01 14:20:23 -03:00
parent 52fc90cbcd
commit 0f833fa1fa
2 changed files with 30 additions and 4 deletions

View File

@@ -16,8 +16,8 @@ from pydantic import Field, field_validator
from .. import machines, mail, observability from .. import machines, mail, observability
from ..core import db from ..core import db
from ..core.auth import (CAPABILITIES, audit, can, has, machine_bound, machine_question, may_move, may_reorder, from ..core.auth import (CAPABILITIES, audit, can, client_ip, has, login_failed, machine_bound, machine_question,
password_hash, password_matches, roles_of, signed_in) may_move, may_reorder, password_hash, password_matches, roles_of, signed_in, throttle)
from ..core.models import StrictModel from ..core.models import StrictModel
from ..runtime import BACK, ENVIRONMENT, STATES, TRANSITIONS from ..runtime import BACK, ENVIRONMENT, STATES, TRANSITIONS
@@ -149,12 +149,21 @@ def choose_session_machine(body: MachineAnswer, request: Request):
@router.post('/api/operator/me/password') @router.post('/api/operator/me/password')
def change_password(body: PasswordChange, request: Request): def change_password(body: PasswordChange, request: Request):
"""As a customer's (app/api/customer.py): a wrong current password counts
against the sign-in, so a session left open cannot guess it, and the new one
signs out every other session of this account (security area 3, 01/12)."""
account = signed_in(request) account = signed_in(request)
throttle('operator:' + account['email'], request)
with db.connect() as c: with db.connect() as c:
row = c.execute('SELECT password_hash FROM dtf_local.operators WHERE id=%s', (account['id'],)).fetchone() row = c.execute('SELECT password_hash FROM dtf_local.operators WHERE id=%s', (account['id'],)).fetchone()
if not password_matches(body.current, row['password_hash']): if not password_matches(body.current, row['password_hash']):
raise HTTPException(403, 'A senha atual não confere.') login_failed('operator:' + account['email'])
audit('operator_password_change_failed', operator=account['email'], ip=client_ip(request))
raise HTTPException(403, 'A senha atual não confere.')
with db.connect() as c:
c.execute('UPDATE dtf_local.operators SET password_hash=%s WHERE id=%s', (password_hash(body.new), account['id'])) c.execute('UPDATE dtf_local.operators SET password_hash=%s WHERE id=%s', (password_hash(body.new), account['id']))
c.execute('DELETE FROM dtf_local.operator_sessions WHERE username=%s AND token_hash<>%s',
(account['email'], account['session']))
audit('operator_password_changed', operator=account['email']) audit('operator_password_changed', operator=account['email'])
return {'ok': True} return {'ok': True}

View File

@@ -2,6 +2,7 @@
Run inside the API container: python -m tests.users_test Run inside the API container: python -m tests.users_test
""" """
import hashlib
import secrets import secrets
import time import time
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
@@ -318,8 +319,24 @@ def run():
seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'sete-77'}, expected=422) seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'sete-77'}, expected=422)
seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'oito-888'}) seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'oito-888'})
seller = signed_in(emails['vendedor'], 'oito-888') seller = signed_in(emails['vendedor'], 'oito-888')
# The new password signs out the account's other sessions, and keeps
# this one (as a customer's, security area 3, 01/12).
other_device = signed_in(emails['vendedor'], 'oito-888')
seller.call('/operator/me/password', {'current': 'oito-888', 'new': 'outra-senha-de-teste'}) seller.call('/operator/me/password', {'current': 'oito-888', 'new': 'outra-senha-de-teste'})
seller.call('/operator/me')
other_device.call('/operator/me', expected=401)
signed_in(emails['vendedor'], 'outra-senha-de-teste').call('/operator/me') signed_in(emails['vendedor'], 'outra-senha-de-teste').call('/operator/me')
# A session left open cannot guess the current password: wrong ones count
# against the sign-in and stop at 10, as a failed sign-in does. The count
# starts from zero here: the suites before this one share the address.
with db.connect() as c:
c.execute('DELETE FROM dtf_local.login_attempts')
for _ in range(10):
seller.call('/operator/me/password', {'current': 'errada', 'new': 'qualquer-123'}, expected=403)
seller.call('/operator/me/password', {'current': 'errada', 'new': 'qualquer-123'}, expected=429)
with db.connect() as c:
c.execute('DELETE FROM dtf_local.login_attempts WHERE key=%s', (hashlib.sha256(
('auth-account|operator:' + emails['vendedor']).encode()).hexdigest(),))
# A second Têxtil machine. Who is on each machine is who chose it after # A second Têxtil machine. Who is on each machine is who chose it after
# signing in: Usuários only shows it (item 12). # signing in: Usuários only shows it (item 12).