Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Security

Browser Security Best Practices

Opens with credits

A practical guide to browser security for web developers. It explains how cross-site scripting happens in modern frameworks and how Trusted Types and a strict Content Security Policy contain it, how to configure CORS without opening your API, which cookie attributes and prefixes to use, how to stop CSRF and clickjacking, which security headers matter, and how to load third-party scripts and talk between windows safely.

Written by Tauseef Fayyaz7 chapters, 22 practicesAbout 22 min24 quiz questionsReviewed 13 October 2026

What you will learn

7 chapters · about 22 min

  1. 1Cross-site scripting and Trusted TypesHow XSS gets into apps built with modern frameworks, the code patterns that prevent it, and how Trusted Types lets the browser enforce those patterns for you.4 practices · 4 min
  2. 2Content Security PolicyA strict Content Security Policy tells the browser which scripts may run, so an injected one does not. These practices get you to a strict policy without breaking the site.3 practices · 3 min
  3. CORS done rightCORS does not protect your API; it relaxes the same-origin policy for origins you name. These practices keep that relaxation narrow.3 practices · 3 minOpens with credits
  4. CookiesCookie attributes decide which requests carry your session and which code can read it. Small settings here close entire classes of attack.3 practices · 3 minOpens with credits
  5. Cross-site request forgeryCSRF tricks a signed-in user's browser into sending a request they did not mean to. These are the defenses OWASP recommends, in the order to reach for them.3 practices · 3 minOpens with credits
  6. Framing, HSTS and security headersA handful of response headers switch on protections the browser already has: no framing, HTTPS only, no content sniffing, fewer referrer leaks and isolated windows.3 practices · 3 minOpens with credits
  7. Third-party scripts and cross-window messagingEvery script you include runs with your page's full power, and every window you talk to could be someone else's. Load less, pin what you load, and check who is talking.3 practices · 3 minOpens with credits

About this guide

Browser security is about the rules the browser enforces on your behalf, and the headers and code that tell it what to enforce. The browser already isolates sites from each other with the same-origin policy. Most front-end vulnerabilities come from weakening that isolation by accident: a CORS header that trusts every origin, a cookie sent on cross-site requests, a page that any site can frame, or a string that ends up in innerHTML and runs as script.

Cross-site scripting is still the biggest single issue. OWASP's Top 10:2025 counts it inside Injection, where cross-site scripting alone accounts for more than 30,000 CVEs. Modern frameworks escape output by default, so today's XSS mostly comes through escape hatches, DOM APIs and URLs. The good news is that browsers now offer strong defenses in depth: a nonce-based Content Security Policy, and Trusted Types, which MDN lists as Baseline 2026, available across current major browsers since February 2026.

This guide works through the browser's security model layer by layer. It starts with XSS and Trusted Types, then Content Security Policy, CORS, cookies, CSRF, framing and the rest of the security headers, and ends with third-party scripts and cross-window messaging. Each practice explains why it matters and shows the exact header or code to use. The advice follows the OWASP Cheat Sheet Series and MDN's web security documentation. Server-side injection, authorization and sign-in each have their own guides.

Common questions

What is the most important browser security header?

A strict Content Security Policy does the most, because it stops injected scripts from running. Pair it with HSTS for HTTPS, frame-ancestors against clickjacking, X-Content-Type-Options: nosniff and a Referrer-Policy. Cookie attributes matter just as much for sessions, even though they are not a separate header.

Does CORS protect my API from attackers?

No. CORS is a way to let other origins read your responses in a browser. It does not stop requests from being sent and does not apply to non-browser clients. Your API still needs authentication, authorization and CSRF protection; CORS should only name the exact origins that need access.

Is SameSite=Lax enough to prevent CSRF?

OWASP treats SameSite as defense in depth. Lax still sends cookies on top-level GET navigations, it works at the registrable domain rather than the exact origin, and it does not stop client-side CSRF. Use your framework's CSRF tokens or a Fetch Metadata check as the main control.

What are Trusted Types?

Trusted Types is a browser API that blocks plain strings from reaching dangerous DOM sinks like innerHTML and script URLs. With the CSP directive require-trusted-types-for 'script', only values created by policies you define are accepted. MDN lists it as Baseline 2026, available across current major browsers since February 2026.

Should I use a nonce or a hash for my Content Security Policy?

Use a nonce when pages are rendered per request, generating a new one for every response. Use hashes when pages are static or cached, since a nonce cannot change on cached HTML. Both work with strict-dynamic so trusted scripts can load your bundles.

Is X-Frame-Options still needed?

The CSP frame-ancestors directive replaces it, and browsers ignore X-Frame-Options when frame-ancestors is set. Sending X-Frame-Options: DENY as well is harmless and covers older browsers, which is why OWASP still lists it.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Cross Site Scripting Prevention Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
  2. Trusted Types API, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
  3. Content Security Policy Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html
  4. Cross-Origin Resource Sharing (CORS), MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS
  5. Set-Cookie header, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie
  6. Cross-Site Request Forgery Prevention Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html
  7. HTTP Strict Transport Security Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/HTTP_Strict_Transport_Security_Cheat_Sheet.html
  8. HTTP Security Response Headers Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com