Sub topic 7
Surviving security scanners and link prefetching
This chapter assumes you can read basic Python and SQL, know the difference between an HTTP GET and a POST, and have a signed confirmation token like the one built earlier in this tutorial. The goal is to make sure a machine that opens your links cannot confirm or unsubscribe a person.
The problem: machines click links
Many corporate gateways and some mailbox providers open every link in an incoming message. They do this to check for malware or phishing, or to prefetch a preview. These tools send an ordinary GET request, often within seconds of delivery, and sometimes from several IP addresses.
If your confirmation link changes state on GET, the scanner confirms the subscription before the person has read the email. The same applies to an unsubscribe link, where a scanner can remove a real subscriber who never asked to leave. Both outcomes corrupt your data. The first also undermines the consent evidence that double opt-in exists to create.
HTTP already describes the rule. GET is meant to be safe, so it should not change anything. POST is where state changes happen.
The fix: GET shows, POST changes
The email link opens a page that only displays a button. Pressing the button sends a POST request, which does the work.
from datetime import datetime, timezone
def show_confirmation_page(token: str):
"""Handle GET: only display a page with a Confirm button; change nothing."""
email = verify_token(token, "confirm")
if email is None:
return render("link_invalid_or_expired.html")
return render("confirm_button.html", token=token)
def confirm_subscription(db, token: str):
"""Handle POST: confirm the subscriber. Safe to call more than once."""
email = verify_token(token, "confirm")
if email is None:
return render("link_invalid_or_expired.html")
db.execute(
"UPDATE subscribers SET status = 'confirmed', confirmed_at = %s "
"WHERE email = %s AND status = 'pending'",
(datetime.now(timezone.utc), email),
)
return render("subscription_confirmed.html")Two details matter here:
- The GET handler verifies the token so that bad links show an error early. It does not write to the database.
- The UPDATE has
AND status = 'pending'. A second POST matches no rows, so repeating the request is harmless. This property is called idempotency.
The confirm_button.html page should contain a form with method="post" that sends the token. Do not use JavaScript to submit the form automatically on page load. Some scanners run a headless browser, and an auto-submit would defeat the whole design.
How the flow looks
Unsubscribe links need the same care
Apply the same pattern to the unsubscribe link in the message body: GET shows a page with an Unsubscribe button, and POST removes the person. The exception is the one-click unsubscribe request that mail clients send themselves. It is a POST by design (RFC 8058), so it is safe to act on immediately. The next chapters cover it.
Checking your work
You can test the design without a real scanner. Request the link with a plain HTTP client and check that nothing changed.
import requests
url = "https://example.com/confirm/TOKEN"
requests.get(url)
requests.get(url)
# Query the database now: status must still be 'pending'.If the status has changed after two GET requests, a handler is doing work it should not.
Exercise
A subscriber's corporate gateway opens your confirmation link, then the subscriber also clicks the Confirm button. What is the subscriber's status afterwards, and why is the second step still safe?
Show answer
The status is confirmed. The gateway's GET only displayed the page. The button press sent a POST that changed pending to confirmed. If the POST were repeated, the UPDATE would match no pending row, so nothing would change.
Exercise
Why should the confirm page not submit its form automatically with JavaScript when it loads?
Show answer
Some scanners load pages in a headless browser that runs JavaScript. An automatic submit would send the POST without any human action, so the scanner would confirm the subscription.