advanced · 28 minute study
Give each browser defense its own job
Security jargon is dangerous when different controls are treated as interchangeable. CORS cannot replace authentication, and CSP cannot repair every unsafe rendering operation.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Authorize the actual resource on every entry path — A protected page can still call an unprotected endpoint. Security must survive a direct request that skips the intended interface.
First, build a mental model
Follow untrusted data and ambient authority. XSS turns data into executable content; CSRF abuses a browser's automatically attached credentials; CORS controls which origins can read eligible cross-origin responses.
- For plain text, use safe text rendering such as textContent. For rich HTML, use a maintained sanitizer suitable for the context; do not invent a regular-expression sanitizer.
- Cookie-authenticated state changes need appropriate CSRF defenses, including verified origin/token strategies and suitable cookie attributes. SameSite is useful defense in depth, not a universal replacement for the full design.
- CORS is a browser response-sharing policy. It does not stop all requests, non-browser clients or unauthorized server operations.
- CSP narrows allowed content/resource execution and is defense in depth. Secrets must stay out of shipped bundles, source maps, logs and fixtures; a variable name containing SECRET does not protect a value.
const message = document.createElement('p');
const untrustedName = '<strong>Alice</strong>';
message.textContent = untrustedName;
// Displays the characters; it does not parse markup.
// Do not replace this with innerHTML for plain text.- Input
A name contains markup-like text that the application does not trust.
- Safe sink
textContent assigns a text value rather than invoking HTML parsing.
- Separate controls
Server authorization, CSRF defenses and CSP still need their own design; one safe text assignment does not prove the whole app secure.
Production thinking
Take this beyond the example.
Use context-specific defenses and negative tests. Validate uploads and structured input, parameterize queries, constrain filesystem/resource access, and keep secrets in trusted execution. This lesson is a foundation, not a complete security audit.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Give each rule one authoritative home →