Track 03 · Lesson 8 of 8Building for everyone
Track 03 · Building for everyone
Never trust the browser
Lesson 8 of 8· 6 min
Browser validation is for convenience. Anyone can skip your form and send whatever they like straight to your server with a tool like curl. Try it as an attacker.
Skip the form
Try “Make myself admin”, then switch the server
Server response
Waiting for a request…
Users table
| id | name | password | role | |
|---|---|---|---|---|
| 1 | Sam Ortiz | sam@example.com | 16 chars, hashed | admin |
| 2 | Ana Lima | ana@example.com | 14 chars, hashed | user |
// server.js
app.post("/api/signup", async (req, res) => {
// "The form already checked everything"
const user = await db.users.create(req.body);
res.status(201).json({ id: user.id, role: user.role });
});Two jobs, two places#
- Client-side validation is a courtesy: instant feedback while someone types, like the helpful form in the last lesson. It makes forms pleasant.
- Server-side validation is the actual protection. Every request reaches the server, whether it came from your form, from curl, or from a script someone wrote at 3 a.m.
You need both, and they should agree. Libraries like Zod let you write one schema and use it in both places.
Allowlist, don't blocklist#
The trusting server passed the whole request body to the database, so an extra field it never expected, role: "admin", went straight in. This is called mass assignment. The fix is to accept only the fields you expect and set sensitive ones, like roles, prices or account IDs, on the server yourself. You'll meet the security side of this again in Attacks and defenses.
Check yourself