Start learning free

LESSON 41 OF 56 · SECURITY: SECURE THE WEB APP

Reset Links and the Host Header in Python

Practice

A password reset email carries a link with a secret token, and whoever has the token can choose a new password. So the link must point to the real shop. Every request names the site it was sent to in its Host header, and Flask builds request.host_url from it. A browser fills the header in from the address bar, but anyone who sends a request from a script can write any name there.

The attack: say the shop built the link from request.host_url. An attacker asks for a reset of ana@example.com with the header Host: evil.example. The shop emails Ana her real token inside a link to evil.example. The email truly comes from the shop, so Ana clicks it, and her token lands on the attacker's site. The attacker gets Ana's account: they send the token to POST /reset with a password of their own and log in as her.

The fix: build links from a configured address, a setting the server reads when it starts, the way a secret key is read from the environment. No request can change it. For example, a link to a help desk ticket:

HELP_URL = os.environ.get("HELP_URL", "https://help.example.com")

def ticket_link(ticket_id):
    return HELP_URL + "/tickets/" + str(ticket_id)   # never request.host_url

The check reads the shop's emails through /check/last-mail (how many were sent, and the last one's address, link and token) and plays the attacker's site with /check/evil-site-log (the tokens from every emailed link that points to evil.example). Your code must give these answers:

POST /forgot  {"email": "ana@example.com"}
  →  200  {"message": "If that email has an account, a reset link is on its way"}
GET /check/last-mail
  →  200  {"emails": 1, "link": "https://shop.example.com/reset?token=…", "to": "ana@example.com", "token": "…"}
POST /forgot  {"email": "ana@example.com"}   with the header Host: evil.example
  →  200  {"message": "If that email has an account, a reset link is on its way"}
GET /check/last-mail
  →  200  {"emails": 2, "link": "https://shop.example.com/reset?token=…", "to": "ana@example.com", "token": "…"}
GET /check/evil-site-log
  →  200  {"token": null, "visits": 0}
POST /reset  {"token": what evil.example caught, "password": "mallory-pass-1"}
  →  400  {"error": "This reset link is invalid or has expired"}
POST /reset  {"token": the token from the first email, "password": "new-pass-456"}
  →  200  {"message": "Password changed, you can log in"}
POST /login  {"email": "ana@example.com", "password": "new-pass-456"}
  →  200  {"message": "Logged in"}

Your task: the starting code makes a token and stores its hash in RESETS, but sends no email yet. Below app, set SHOP_URL from the environment variable SHOP_URL, with "https://shop.example.com" as the default. In forgot, build link from SHOP_URL, "/reset?token=" and the token, then email it to the user with send_email.


Assignment
Follow the Coding Tutorial to read the shop's address from the environment, then build the reset link from it and send it with send_email.


Hint
Nothing in the link may come from the request: not request.host_url, not any header. send_email stands in for the mail service and keeps each email in OUTBOX, which is where /check/last-mail looks.


STEP 1 OF 3
    Ctrl + Enter to check

    Secx Host Header 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