Security

Web & Frontend Security

Intermediate

The browser runs our code on a stranger's machine, next to other tabs, extensions, and scripts that may be hostile. Web security is about making sure the page cannot be tricked into running an attacker's code, leaking a user's session, or acting on a forged request. Most of it comes from a few well-understood defences applied consistently.

The classic web attacks have names and known fixes. Cross-site scripting (XSS) injects script into a page. Cross-site request forgery (CSRF) makes the user's browser send an action they did not intend. Clickjacking tricks clicks through an invisible frame. And some attacks leak tokens or data the page should not expose. The frontend is never the security boundary. The server must re-check everything (see Authentication & Authorization).

If you are newer to this, the short version is: let the framework escape output, never build HTML from untrusted strings, keep tokens out of places scripts can read, and set the standard security headers. Do those, and you have closed the doors most attacks use.

Stop injection and code execution

Protect sessions, requests, and the page

Token where scripts can read it localStorage.setItem('jwt', token); // any XSS can steal this

If a single XSS bug ever lands, the attacker reads the token straight out of localStorage and becomes the user. Use an HttpOnly cookie the script cannot read instead.

HttpOnly cookie plus CSP // server sets: Set-Cookie: session=...; HttpOnly; Secure; SameSite=Lax
// page sends: Content-Security-Policy: default-src 'self'

The token is invisible to JavaScript, and CSP stops injected scripts from running even if some untrusted content slips in.

Self-review checklist

Why it matters: The frontend is the part of our system most exposed to hostile input. Web attacks like XSS and CSRF can hijack a real user's session, including a customer's access to their financial data. The defences are well known and cheap to apply consistently. Skipping them hands attackers the easiest way in.