Frontend security
XSS and why frameworks mostly save you (until dangerouslySetInnerHTML), CSRF, CSP, cookie flags. One planted XSS question appears in most senior loops.
The mental model
Frontend security is defending the one place you do not control: **the user's browser, running your code next to code you never wrote, on a network you cannot trust.** The server can validate and authenticate all it likes, but the browser is where a stolen token becomes a stolen account and where a <script> you did not intend becomes an attacker acting as your user.
The analogy: your web page is a house party where the guest list is public and anyone can slip a note under the door. XSS is a note that, if you read it aloud without checking, makes you do whatever it says — because in a browser, "reading aloud" means executing. CSRF is a forged letter on your letterhead: the bank obeys because it arrived with your seal (the cookie) even though you did not write it. Clickjacking is someone putting a transparent sheet over your doorbell so you buzz in a burglar thinking you opened your own door. Each attack abuses a default the browser was designed to be helpful about: HTML executes, cookies attach automatically, pages can be framed.
Why this exists — what breaks without it. The browser's foundational rules are permissive by design. HTML that contains a script runs that script. Cookies attach to every request to their origin, whoever caused the request. Any page can embed any other in an iframe. JavaScript on a page can read everything on that page — every token, every input, the whole DOM. These behaviours make the web work, and every one of them is an attack surface. Frontend security is the set of controls — output encoding, CSP, cookie flags, framing headers, careful token storage — that keep the helpful defaults from becoming exploits. Get one wrong and the consequence is not a bug report; it is account takeover, data theft, or your users' browsers mining crypto.
The organising rule: the browser trusts your origin completely, so any code that runs in your origin is you. Everything below is about controlling what runs in your origin and what your origin's credentials can be tricked into doing.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision