Web Security Fundamentals for Frontend
The frontend-relevant OWASP basics — XSS, CSRF, CSP, same-origin policy and CORS, and why you sanitize input and escape output.
Web Security Fundamentals for Frontend
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Cross-site scripting (XSS) happens when attacker-controlled input gets rendered as code instead of text, which is why output needs to be escaped, not just input validated.
- Cross-site request forgery (CSRF) tricks a logged-in user's browser into sending a request they didn't intend, exploiting the fact that cookies get attached automatically.
- A Content Security Policy (CSP) is a server-sent rule telling the browser which sources of scripts, styles, and other resources are allowed to load, which limits the damage even if an XSS injection succeeds.
- The same-origin policy is the browser's default rule that one site's JavaScript cannot read another site's data, and CORS is the controlled exception a server can grant to specific other origins.
- Sanitizing input and escaping output are different jobs: sanitizing removes or neutralizes dangerous content before it's stored, escaping makes stored content safe to display no matter what it contains.
Why frontend security is its own topic
It’s tempting to think of security as a backend concern — validate input, guard the database, done. But a huge share of real-world vulnerabilities live specifically in how the frontend renders content, handles cookies, and talks to other origins. These issues share a theme: the browser is a powerful, trusting execution environment, and a lot of security work is about constraining what it’s willing to do, and for whom.
Cross-site scripting: when data becomes code
XSS happens when content that should be inert text gets interpreted and executed as code. The classic example: a comment form that stores <script>stealCookies()</script> as a comment, and a page that renders comments by dropping their raw content into the HTML. Every visitor who views that comment now runs the attacker’s script, in their own browser, with their own cookies and session.
The fix is escaping output — converting characters like < and > into their harmless text equivalents (<, >) at the moment content is rendered, so a stored <script> tag displays as literal text instead of executing. This is distinct from sanitizing input, which strips or neutralizes dangerous content before it’s ever stored. Both matter, but escaping output is the layer that actually prevents execution, because it protects you regardless of how questionable content made it into storage in the first place — through your own form, a bulk import, or another system sharing the database. Modern frameworks escape by default when you use their templating (React’s JSX, for instance, escapes any value inserted via {}), which is why manually injecting raw HTML — dangerouslySetInnerHTML and equivalents — is the dangerous exception worth specifically watching for.
Cross-site request forgery: the browser’s trust, weaponized
CSRF takes a different angle. It doesn’t need to inject anything into your page — it exploits the fact that browsers automatically attach cookies to any request sent to a matching domain, no matter which page triggered that request. If you’re logged into your bank in one tab, and a malicious page in another tab silently submits a form to yourbank.com/transfer, your browser attaches your valid session cookie, and the bank’s server sees what looks like a legitimate request from you.
The standard defense is a CSRF token: a random value the server embeds in legitimate forms and checks on submission. An attacker’s forged request can get your cookie attached automatically, but it has no way to know or guess that token, so the server rejects it. Modern cookies also support a SameSite attribute, which can tell the browser not to attach the cookie at all on requests originating from other sites — a simpler, broader defense that’s now common by default.
Same-origin policy and CORS: the browser’s default lock, and the key
The same-origin policy is the browser’s baseline rule: JavaScript running on siteA.com cannot read responses from siteB.com. This is what stops a malicious site from just fetching your bank’s API directly and reading the result. It’s a default, not a suggestion — it applies everywhere unless a server explicitly opts out of it for specific origins.
CORS (Cross-Origin Resource Sharing) is that opt-out mechanism. A server sends headers like Access-Control-Allow-Origin naming exactly which other origins are permitted to read its responses. This is why a frontend calling its own backend on a different subdomain, or a legitimate third-party API, needs that API to explicitly allow it — CORS failures that show up as a confusing browser console error are the same-origin policy doing exactly its job, just blocking a request the server never approved.
Content Security Policy: limiting the blast radius
Even careful teams occasionally miss an escaping bug, so a second layer of defense helps: CSP. It’s a header the server sends that tells the browser which sources are allowed to supply scripts, styles, images, and other resources — for example, “only run scripts from this domain, never inline ones.” A CSP doesn’t stop malicious content from being injected, but it limits what that content can actually do once it’s there. An injected <script> tag pointing at an attacker’s server to exfiltrate data simply won’t execute if the CSP doesn’t allow scripts from that source, which turns a successful injection into a mostly harmless, inert piece of text instead of a working exploit.
The common thread
XSS, CSRF, CORS, and CSP all come back to the same underlying question: what is the browser willing to trust, and who gets to say so. XSS is about making sure injected content isn’t trusted as code. CSRF is about making sure a request’s origin is actually trustworthy before cookies get attached to it. CORS and CSP are both about a server explicitly declaring, rather than silently assuming, what it trusts — which origins can read its data, and which sources can supply code and resources to its pages. Treating these as one family of related guardrails, instead of four separate facts to memorize, is what makes them stick.
Check yourself
Why does escaping output matter even if an app already validates input on the way in?
Validation can be bypassed, incomplete, or simply not anticipate every way data enters a system — through an API, an import, or another app sharing the same database. Escaping output guarantees that whatever ends up in the database is rendered as inert text rather than executable code, regardless of how it got there, which is why it's the layer that actually stops XSS.
How does a CSRF attack succeed even though the attacker's site can't read the victim's cookies?
The attacker doesn't need to read the cookie — the browser attaches it automatically to any request sent to the cookie's domain, regardless of which page triggered that request. A malicious page can submit a form or trigger a request to a banking site, and the victim's browser helpfully attaches their valid session cookie, making the forged request look legitimate to the server.
What's the difference between what the same-origin policy blocks by default and what CORS allows?
The same-origin policy is the browser's default: JavaScript running on one origin cannot read responses from a different origin. CORS is a mechanism for a server to explicitly opt back into allowing specific other origins to read its responses, by sending headers that name which origins, methods, and headers are permitted.
What does a Content Security Policy actually protect against, given that it doesn't stop an XSS injection from happening?
A CSP doesn't prevent malicious content from being injected into the page, but it restricts what that injected content is allowed to do — for example, blocking inline scripts or scripts loaded from untrusted domains. Even if an attacker successfully injects a script tag, a correctly configured CSP can stop the browser from actually executing it or from loading resources from attacker-controlled servers.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.