fix: accounts confirmed more than a day ago also bring their orders
Some checks failed
Build and deploy / Validate source (push) Successful in 9s
Build and deploy / Integration suite on a real stack (push) Failing after 4m2s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Has been skipped

The signups that marked an account as confirmed are deleted after a day, so
an older account was left unconfirmed and brought nothing. The code e-mail
sent before the account existed stays in the queue and now counts too. A
password reset, whose link came by e-mail, also confirms the address and
brings the orders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-16 15:51:25 -03:00
parent 1a7736dc8b
commit 4f49ace867
3 changed files with 14 additions and 3 deletions

View File

@@ -394,13 +394,16 @@ def reset_password(body: PasswordReset, request: Request, response: Response):
raise HTTPException(400, 'Este link venceu ou já foi usado. Peça outro em “Esqueci minha senha”.') raise HTTPException(400, 'Este link venceu ou já foi usado. Peça outro em “Esqueci minha senha”.')
identity = reset['account_id'] identity = reset['account_id']
account = account_row(c, identity, lock=True) account = account_row(c, identity, lock=True)
c.execute('UPDATE dtf_local.accounts SET password_hash=%s WHERE id=%s', (password_hash(body.password), identity)) # The link came by e-mail, so the address is confirmed too.
c.execute('UPDATE dtf_local.accounts SET password_hash=%s, email_verified=true WHERE id=%s',
(password_hash(body.password), identity))
c.execute('UPDATE dtf_local.password_resets SET used_at=now() WHERE token_hash=%s', (digest,)) c.execute('UPDATE dtf_local.password_resets SET used_at=now() WHERE token_hash=%s', (digest,))
# Every other link to this account stops working, and so does every # Every other link to this account stops working, and so does every
# session signed in with the old password. # session signed in with the old password.
c.execute('DELETE FROM dtf_local.password_resets WHERE account_id=%s AND used_at IS NULL', (identity,)) c.execute('DELETE FROM dtf_local.password_resets WHERE account_id=%s AND used_at IS NULL', (identity,))
if previous: if previous:
transfer_guest(c, previous, identity) transfer_guest(c, previous, identity)
claim_orders(c, identity, account['profile'])
c.execute('DELETE FROM dtf_local.sessions WHERE owner=%s', (identity,)) c.execute('DELETE FROM dtf_local.sessions WHERE owner=%s', (identity,))
if previous: if previous:
c.execute('DELETE FROM dtf_local.sessions WHERE id=%s', (previous['id'],)) c.execute('DELETE FROM dtf_local.sessions WHERE id=%s', (previous['id'],))

View File

@@ -304,10 +304,14 @@ CREATE TABLE IF NOT EXISTS dtf_local.email_changes (
sent_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz NOT NULL, sent_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz NOT NULL,
sends int NOT NULL DEFAULT 1, attempts int NOT NULL DEFAULT 0, done_at timestamptz sends int NOT NULL DEFAULT 1, attempts int NOT NULL DEFAULT 0, done_at timestamptz
); );
-- Accounts confirmed by a code before email_verified existed. -- Accounts confirmed by a code before email_verified existed. The signups
-- are deleted after a day; the code e-mail sent before the account existed
-- stays in the queue.
UPDATE dtf_local.accounts a SET email_verified=true WHERE NOT a.email_verified AND ( UPDATE dtf_local.accounts a SET email_verified=true WHERE NOT a.email_verified AND (
EXISTS (SELECT 1 FROM dtf_local.account_signups s WHERE s.email=a.email AND s.done_at IS NOT NULL) EXISTS (SELECT 1 FROM dtf_local.account_signups s WHERE s.email=a.email AND s.done_at IS NOT NULL)
OR EXISTS (SELECT 1 FROM dtf_local.email_changes e WHERE e.account_id=a.id AND e.new_email=a.email AND e.done_at IS NOT NULL)); OR EXISTS (SELECT 1 FROM dtf_local.email_changes e WHERE e.account_id=a.id AND e.new_email=a.email AND e.done_at IS NOT NULL)
OR EXISTS (SELECT 1 FROM dtf_local.mail_queue m WHERE m.kind='code' AND lower(m.to_addr)=a.email
AND m.created_at<=a.created_at));
-- E-mail kinds: a password-recovery link, a code, the notice that an account -- E-mail kinds: a password-recovery link, a code, the notice that an account
-- already exists, the notice of a changed e-mail, the team's alerts and a test. -- already exists, the notice of a changed e-mail, the team's alerts and a test.
-- Defined once, here: the schema runs on every start, and an earlier, narrower -- Defined once, here: the schema runs on every start, and an earlier, narrower

View File

@@ -91,11 +91,15 @@ def recovery(tag):
with db.connect() as c: with db.connect() as c:
# Only the token's hash is kept. # Only the token's hash is kept.
assert not c.execute('SELECT 1 FROM dtf_local.password_resets WHERE token_hash=%s', (token,)).fetchone() assert not c.execute('SELECT 1 FROM dtf_local.password_resets WHERE token_hash=%s', (token,)).fetchone()
# An account never confirmed: the reset link, sent to the address, confirms it.
c.execute('UPDATE dtf_local.accounts SET email_verified=false WHERE email=%s', (address,))
stranger.call('/account/reset', {'token': 'x' * 43, 'password': NEW_PASSWORD}, expected=400) stranger.call('/account/reset', {'token': 'x' * 43, 'password': NEW_PASSWORD}, expected=400)
stranger.call('/account/reset', {'token': token, 'password': 'curta'}, expected=422) stranger.call('/account/reset', {'token': token, 'password': 'curta'}, expected=422)
assert stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD})['customer']['mail'] == address assert stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD})['customer']['mail'] == address
assert stranger.call('/account/me')['customer']['mail'] == address assert stranger.call('/account/me')['customer']['mail'] == address
with db.connect() as c:
assert c.execute('SELECT email_verified FROM dtf_local.accounts WHERE email=%s', (address,)).fetchone()['email_verified']
# Works once; the old password and the old sessions are gone. # Works once; the old password and the old sessions are gone.
stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD}, expected=400) stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD}, expected=400)
elsewhere.call('/account/me', expected=401) elsewhere.call('/account/me', expected=401)