This lesson is a review: the shop's small admin panel already works. An admin logs in, changes a product's price and reads the audit page. Your job is to find what an attacker can do with it, then close each hole without breaking the honest requests. The users are admin@example.com (password admin-pass-123, an admin) and ana@example.com (password ana-pass-123, a customer).
The four attacks. 1. A missing guard: the price route has no @admin_only, so any customer can make any product cost 1 and buy it for almost nothing. 2. No audit log: nothing writes down who changed what, so after a bad change nobody can tell who did it. 3. An open redirect: after login the shop goes wherever ?next= says, even to another website, so a link to the real login page can end on a fake one that asks for the password again. 4. A cookie without flags: any script on the page can read the sid cookie, it travels over plain http, and it comes along with forms posted from other websites, so an attacker can steal or borrow a login.
Cookie flags show up in the Set-Cookie header. Here is a cart cookie without them, then with them: HttpOnly hides it from scripts, Secure sends it only over https, and SameSite=Lax leaves it off a form posted from another website.
Set-Cookie: cart=a1b2; Path=/
Set-Cookie: cart=a1b2; Secure; HttpOnly; Path=/; SameSite=Lax
The check plays the attacker: /check/document-cookie shows what a script on the page could read, one request comes from a form on evil.example, and one uses plain http://. When you're done, the requests get these answers:
POST /login?next=https://evil.example/login (Ana) → 302 Location: /
Set-Cookie: sid=<sid>; Secure; HttpOnly; Path=/; SameSite=Lax
GET /check/document-cookie → 200 {"document.cookie": ""}
POST /admin/products/6/price (Ana, price=1) → 403 {"error": "Admins only"}
POST /login?next=//evil.example/login (admin) → 302 Location: /
POST /login?next=/admin/audit (admin) → 302 Location: /admin/audit
POST /admin/products/6/price (admin, price=25) → 200 {"id": 6, "price": 25}
POST /admin/products/6/price (admin's browser, price=1, form on evil.example) → 401 {"error": "Login required"}
GET /admin/audit → 200 [{"action": "set the price of product 6 to 25", "email": "admin@example.com"}]
GET http://localhost/admin/audit (plain http) → 401 {"error": "Login required"}
Your task: close all four holes. Put @admin_only on the price route. Write audit(action), which inserts current_email() and the action into audit_log, and call it in set_price. Send the browser to / when next isn't a page of the shop. Give the sid cookie httponly, secure and samesite="Lax".
Assignment
Follow the Coding Tutorial to close the four holes in the admin panel: the guard, the audit log, the redirect and the cookie flags.
Hint
The // case matters: a browser reads //evil.example/login as another website. audit doesn't commit by itself: set_price commits the new price and its log row together.
Free plan: 30 tutor messages. PRO is unlimited.
