Instructor walkthrough: account-recovery design
Preflight
Open exercises/08-insecure-design/student from the repository, or app in a newly exported kit. Confirm node --version is at least 22. Start node server.mjs and open http://127.0.0.1:3000. Stop any other server on port 3000 first.
Alice and Bob are fictional accounts at campus.example. Both initially use Campus-demo-42!. No real email is sent. A reset or server restart clears mailbox/token state and restores those passwords.
Observe, then define the requirement
- In the password-check panel, replace the prefilled proposed new password with
Campus-demo-42!and verify Alice's initial password works. - Without requesting recovery or reading the teaching mailbox, reset Alice's password to
Recovered-demo-42!with the token field empty. - Observe the starter accepting the change. Verify that the new password works and the old one fails.
- Ask what the request proved. It identified the account but did not prove control.
- Have students write the safe sequence and rejection rule before the repair. Missing/wrong/expired/replayed/cross-account proof must not permit a password change.
The core distinction is an omitted security requirement, not only a missing form validator. A form can be functionally correct while allowing an unauthorized state change.
Repair
In recovery-policy.mjs, import consumeRecoveryToken alongside setAccountPassword. Replace the account-existence guard with:
if (!consumeRecoveryToken(state, email, token)) return false;
Keep return setAccountPassword(state, email, password); after that guard. See the full solution. Do not edit token/password helpers or tests.
Verify
Save, stop with Ctrl+C, restart, and refresh. In order:
- Empty or incorrect token is rejected; Alice's initial password remains valid and the proposed replacement fails.
- Request recovery for Alice. The normal response is a generic acknowledgement and does not include the token.
- Explicitly switch to the owner role, open the teaching mailbox and copy Alice's latest token.
- Complete recovery with that token. The new password works and the initial password fails.
- Reuse that token proposing
Second-demo-42!. It is rejected andRecovered-demo-42!still works. - Optionally request two tokens and show that only the latest works. Do not ask students to wait for expiry; use the supplied automated checks.
Stop the server and run node checks.mjs in the app folder. The untouched starter passes five and intentionally fails seven checks. The solution must pass all twelve. Automated checks cover additional token edge cases; students must distinguish those from their manual observations.
Common misconceptions
| Misconception | Teaching response |
|---|---|
| “Email address means identity was verified.” | Addresses can be public; identify the account separately from proving control of its channel. |
| “Any nonempty token is sufficient.” | The server must validate account binding, validity period and single use. |
| “The token is the new password.” | It is temporary proof authorizing one change; the replacement password is a separate value. |
| “Hiding the mailbox repairs the starter.” | The original policy accepts empty proof. In production the channel must be private, but the server must still enforce verification. |
| “Refresh reloads the server code.” | Save, stop and restart the Node process, then refresh. |
| “Passing login proves the design is safe.” | The starter passed normal login checks while permitting unauthorized changes. |
| “A clean dependency scan would prove recovery is secure.” | This flaw is an application-design requirement, not a known package vulnerability. |
Assessment
Use the rubric. Require the edited policy, before/after evidence, check output and design explanation. An accurate limitation statement is part of a good answer, not a failure of the exercise.
Reference answers for the design discussion
- Protected asset/action: control of the account, specifically the ability to replace its password.
- Requester knowledge: an email address, without access to the owner's private recovery channel.
- Required rule: validate a current, unused token bound to the requested account before changing the password; leave credentials unchanged when proof fails.
- Browser versus server: browser fields can guide the user, but the server decides whether the protected action is allowed.
- Expiry and single use: limit the period and number of times a captured token can authorize a change. They do not make a compromised mailbox safe.
- Teaching-mailbox boundary: the learner can see both roles for the demonstration. A real service must not expose the owner's messages to other requesters.
- Production extension: accept a justified example such as protecting delivery, rate limiting requests, notifying the owner, or invalidating existing sessions. Do not require students to implement it.
- CVE Lite boundary: a dependency scan can identify known package vulnerabilities; it does not prove that this business process requires adequate ownership verification.
Accept equivalent explanations in the student's own words. Check that the student connects the missing requirement to the observed unauthorized password change, rather than only naming a helper function.