What you'll learn
Harden the API where untrusted data and browser behavior enter the system. Apply allowlist validation, safe query construction, security headers, precise CORS, and CSRF protection appropriate to the credential model.
By the end of this lesson, you'll be able to:
- Validate every request location
- Prevent injection and unsafe output
- Configure headers and CORS narrowly
- Defend cookie-authenticated mutations from CSRF
Core mental model
Node.js becomes easier when you separate the JavaScript language from the runtime and the operating-system capabilities it exposes. Use this table as a decision guide.
| Concept | What it means | Decision rule |
|---|---|---|
| Allowlist | A closed set of accepted shapes and values | Reject unknown fields and normalize only after validation |
| CORS | A browser read-permission policy | Treat it as browser isolation, never authentication |
| CSRF | Cross-site use of ambient credentials | Protect state changes when cookies are sent automatically |
Professional workflow
Build and verify Node.js programs from the terminal in small, observable steps.
- Define the HTTP security boundary boundary: inputs, outputs, invariants, ownership, and expected failures.
- Design the data or message contract before choosing implementation details.
- Implement the smallest correct path with dependencies passed explicitly.
- Add validation, failure translation, cleanup, and concurrency behavior.
- Verify the boundary with realistic data and at least one adversarial case.
- Measure or observe the behavior before optimizing or extracting abstractions.
Keep the feedback loop short
Guided code lab
Compose a hardened browser boundary
Security headers, a fixed origin allowlist, bounded JSON, and explicit CSRF verification each solve a different risk.
app.disable('x-powered-by');
app.use(helmet());
app.use(express.json({ limit: '32kb', type: 'application/json' }));
app.use(cors({
origin: ['https://app.example.test'],
credentials: true,
methods: ['GET', 'POST', 'PATCH', 'DELETE'],
}));
app.use('/api', verifyCsrfForUnsafeMethods);Production practice
Contract
Each route defines body, parameter, query, content-type, size, authentication, authorization, and response schemas.
Verification
Fuzz unknown fields, oversized bodies, SQL/meta operators, hostile origins, preflight behavior, missing CSRF tokens, and unsafe redirects.
Operations
Log validation categories rather than secrets, monitor rejection spikes, and review browser policies as clients change.
Common failure mode
Independent workshop
Create a reusable security pipeline for all task API routes.
Your finished workshop must include:
- Request schemas
- Size/content limits
- Parameterized data access
- Helmet policy
- Origin allowlist
- CSRF tests
Definition of done
Recap & quick check
Key takeaways
- Validate at boundaries
- CORS is not auth
- Cookies create CSRF risk
- Headers reduce browser attack surface
- Output needs a contract
Quick check
1. What does CORS authorize?
2. When is CSRF central?
3. How should dynamic sort fields work?
Next: Rate Limits, Secrets & Supply-Chain Security