Start learning free

LESSON 42 OF 56 · SECURITY: FIND AND FIX

Project: Review a Login Flow in Python

Practice

This is a review project. The shop's login flow works: visitors sign up, log in, ask for a reset link and set a new password. A security review found four holes in it, and each one is an attack any visitor can run:

  • Unsalted hash: hash_password never uses the user's salt, so sunshine always gives the same hash. A thief who steals the users table looks the hashes up in a list of hashed common passwords and reads Ana's password.
  • A signup that tells: signing up again with a taken email breaks the UNIQUE email column and answers 500, while a new email gets 202. A stranger learns which emails have an account at the shop.
  • A guessable reset token: the token is a hash of the email and the time. The attacker asks for Ana's reset link, works out the same token without ever seeing Ana's mail, and sets Ana's password to their own.
  • No lockout: nothing limits wrong passwords, so trying common passwords one after another logs the attacker in as Ana.

Same answer either way: an answer that differs for known and unknown emails tells a stranger which is which. This newsletter route answers the same whether or not the email is already on the list:

@app.post("/newsletter")
def newsletter():
    email = request.get_json()["email"]
    if email not in SUBSCRIBERS:
        SUBSCRIBERS.append(email)
    return jsonify({"message": "Thanks, check your inbox"}), 202

The check plays the attacker with helper routes: /check/stolen cracks the stored hashes like a thief, /check/guess-password tries five common passwords, /check/guess-token works out a token from the email and the time, and /check/mail lists the mails the shop sent. Some requests carry the check's own clock time, so a 15-minute lock is quick to test. Once fixed, the shop must answer:

POST /signup  {"email": "ana@example.com", "password": "sunshine"}  →  202  {"message": "Check your inbox to finish signing up"}
POST /signup  {"email": "ana@example.com", "password": "anything-1"}  →  202  {"message": "Check your inbox to finish signing up"}
GET  /check/stolen  →  200  [{"cracked": null, "email": "ana@example.com"}]
GET  /check/guess-password?email=ana@example.com  →  200  {"found": null, "tries": ["123456: 401", "password: 401", "qwerty: 401", "letmein: 429", "sunshine: 429"]}
POST /forgot  {"email": "ana@example.com"}  (time 1000)  →  200  {"message": "If that email has an account, a reset link is on its way"}
GET  /check/guess-token?email=ana@example.com&clock=1000  →  200  {"token": "…"}
POST /reset  {"token": the guessed token, "password": "attacker-pass-1"}  →  400  {"error": "This reset link is invalid or has expired"}
GET  /check/mail  →  200  {"mails": ["ana@example.com: Welcome to the shop", "ana@example.com: You already have an account", "ana@example.com: Reset your password"], "token": "…"}
POST /reset  {"token": the token from Ana's mail, "password": "new-pass-456"}  →  200  {"message": "Password changed, you can log in"}
POST /login  {"email": "ana@example.com", "password": "new-pass-456"}  (time 2900)  →  200  {"email": "ana@example.com", "message": "Logged in"}

Your task: close the four holes without breaking honest users. Feed each user's salt into hash_password. At signup, answer a taken email with the same 202 and mail its owner instead. Feed 32 random bytes from secrets into make_token. And after 3 failed logins, lock the email for 15 minutes, answering 429.


Assignment
Follow the Coding Tutorial to fix the four holes in the shop's login flow.


Hint
Put the lock check at the very top of login, before the password is checked, so even a right guess during the lock gets 429. LOCKED_UNTIL.get(email, 0) gives 0 for an email that was never locked.


STEP 1 OF 11
    Ctrl + Enter to check

    Secx Review Login in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free