Login and Sessions
You'll give your app accounts, with passwords hashed properly, a session in a secure cookie, a reset by email and a login that bots can't hammer.
About
Once a site can tell who you are, it can keep your things, and that's where a page turns into an app. It's also where mistakes stop being cosmetic. A password stored in plain text, a session cookie that can be guessed or a form that another site can submit for you all hurt real people, and all three are common in first apps. You'll add accounts to the guestbook or to any app of your own, using the hashing and session tools your language already ships with rather than inventing your own, and then you'll try to break it before anyone else does. A framework is welcome here, as long as you can explain what each piece is doing.
At a glance
- Per requirement
- 35 XP
- Completion bonus
- 300 XP
- Requirements
- 6
- Takes
- Three weeks
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Store passwords with bcrypt, scrypt or argon2 and nothing else. Use the hashing function in your language's standard library or its best-known package, with a work factor that takes about a quarter of a second on your machine. Never MD5, never a plain SHA-256, never a scheme of your own. Open the users table and check that no password is readable. Take a screenshot of one user row with the hash showing. Photo
-
2
Sign up, log in, and stay logged in across a server restart. Make the session id from a cryptographically random source, store it in a cookie with HttpOnly, Secure and SameSite set, and keep the session itself on the server. Log in, restart the server, reload, and you should still be you. Film the login and the reload after the restart. Video
-
3
Add a CSRF token to every form that changes something. Any form that posts while you're logged in, such as delete my entry, can be fired by another site unless you check a token the other site can't know. Keep a token in the session, print it in each form as a hidden field, and reject posts without it. Then write a one-line HTML page on your own disk that tries to post to your app, open it, and watch the request fail. Take a screenshot of the rejected request. Photo
-
4
Rate-limit the login without locking anyone out. Allow about five failed attempts per account per fifteen minutes, then make the user wait, with a message that says so in plain words. Don't lock the account outright, because then a stranger can lock you out by guessing wrong on purpose. Write a tiny script that tries twenty wrong passwords and film the sixth attempt being told to wait. Video
-
5
Send a password reset link that expires. Email a link with a random single-use token that dies after an hour. The free tiers of Resend, Postmark or Mailgun cover a hobby app, and printing the link to the terminal is fine while you build. Don't say whether the email address exists on the request page, because that tells a stranger who has an account. Take a screenshot of the email with the link and, an hour later, the link saying it has expired. Photo
-
6
Write errors in plain words and keep what people typed. When a signup fails, show the form again with the values still in place (never the password), the error next to the field it belongs to and linked with aria-describedby. Enter an email address with an @ in it beats invalid input. Take a screenshot of a failed signup with the message beside the field and the name still filled in. Photo