Note N2 · Peder
LOGIN ENDPOINT
Login endpoints that don't leak accounts: dummy hash, one error message, fail closed
- Sheet
- PEDER
- Topic
- Cloudflare Workers · Authentication
- Source
- src/lib/kimlik.js
- Date
- 2026-11-12
- Reading
- 3 min
Short answer: a login endpoint must not let anyone tell a wrong email from a wrong password, neither by the message nor by the response time. When the user is not found, still run password verification against a fixed dummy hash and return the same message in both cases. If an extra verification step (TOTP, email code) fails, don't leave the login open; close it.
01The timing leak
Passwords are stored with PBKDF2 (100,000 iterations); verification is slow on purpose. Skipping verification when the user doesn't exist looks natural, but the response gets noticeably faster: an attacker can tell which emails are registered from the response time. In a counselling system, "this person has an account" is sensitive on its own.
const SAHTE_SIFRE_HASH = 'pbkdf2$100000$AAAA...==$AAAA...=';
const k = await bir(c, 'SELECT * FROM kullanicilar WHERE email = ? AND aktif = 1 AND silindi = 0', temizEmail);
const sifreDogru = await sifreDogrula(String(sifre || ''), k?.sifre_hash || SAHTE_SIFRE_HASH);
if (!k || !sifreDogru) return { hata: 'E-posta ya da şifre hatalı.' };
If the user doesn't exist, the password is checked against a hash that matches nothing but costs the same. Both paths take the same time and return the same sentence. The password reset endpoint follows the same principle: its cost doesn't depend on whether the email is registered.
02Brute force
Every failed attempt is written to a per-IP counter. When it fills up, login is locked for 15 minutes, and attempts after that point are passed to the IP penalty engine as "brute force". A successful login clears that IP's counter. Every attempt (successful, failed, locked, unverified email) is written to the access log with device, browser and country.
03Right password, but
A correct password doesn't open the door by itself. In order, it checks:
- Is there an active login restriction on the account (an admin ban)?
- Is the email address verified?
- Has the registration been rejected by a supervisor? Trainees get in with supervisor approval.
These messages only appear when the password is correct; someone who doesn't know the password can't learn that an account was rejected or banned.
04Admin TOTP: backwards compatibility that can't be bypassed
TOTP is optional for admin accounts and its setting lives in a separate table. That table was added later by a migration; on an older database without it, login must keep working. But treating "couldn't read the table" as "no MFA" means bypassing MFA with a database error.
try {
mfa = await bir(c, 'SELECT aktif FROM admin_mfa WHERE kullanici_id = ?', k.id);
} catch (hata) {
// Only fall back for a missing migration; on any other error treat
// the login as failed instead of skipping MFA.
if (!/no such table.*admin_mfa/i.test(String(hata?.message || hata))) throw hata;
}
The fallback is granted only for exactly the "no such table" error; any other error stops the login.
05Email code and trusted device
For other roles, signing in from a new device sends a one-time code by email. A device verified with the code is trusted for 7 days (the peder_cihaz
cookie). Admins never get this cookie; they verify on every login.
If the code delivery isn't ready or the check fails, login doesn't open silently:
} catch (hata) {
// If the OTP infrastructure isn't ready, don't open the login silently (fail closed).
return { hata: 'Giriş doğrulaması şu anda yapılamıyor. Lütfen yöneticiyle iletişime geç.' };
}
For a system holding personal health data, the right side of this availability-versus-security trade-off is clear: when the email service is down, users can't sign in for a while, but verification is never skipped.
→More notes from this project
—End of notewritten by MUHAMMEDPYZ