Loading...

HTML essentialsAdvanced

HTML security: CSP, sandbox & rel=noopener

7 min lesson

Next
  • HTML security means stopping attackers who try to inject scripts or hijack links in your page.
  • CSP (Content Security Policy) is a header that tells the browser which scripts and styles it may run.
  • The sandbox attribute on an iframe restricts what the embedded page can do.
  • rel="noopener" on target="_blank" links stops the new page from controlling yours.
  • Think of CSP as a list of allowed guests, and sandbox as a leash on the iframe: it can move but not run away.
  • This builds on Modern HTML: new features must always be combined with safety.
  • The example shows a CSP meta tag, an iframe with sandbox and a safe external link; you will see the page load but untrusted scripts blocked.
  • Next, Performance & loading strategies makes the page fast without sacrificing safety.
Keep in mind
  • Prefer a real CSP response header; the meta version is a fallback.
  • Add sandbox to every iframe you do not fully control.
  • Always pair target="_blank" with rel="noopener".
  • Never trust user input; escape it before putting it in HTML.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Security</title>
  <!-- CSP: allow scripts only from this page -->
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
</head>
<body>
  <!-- A sandboxed iframe with no scripts allowed -->
  <iframe src="https://example.com" title="Example" sandbox="allow-same-origin" width="300" height="200"></iframe>

  <!-- A safe external link: noopener prevents tab hijacking -->
  <a href="https://example.com" target="_blank" rel="noopener noreferrer">Open example</a>
</body>
</html>

What happens: A page with an embedded example.com iframe whose scripts are blocked by sandbox, and the link "Open example" that opens a new tab safely. Inline scripts on the page would also be blocked by the CSP.

Breaking it down

CSP meta

<meta http-equiv="Content-Security-Policy" content="...">

Tells the browser which sources are allowed. A real response header is stronger than the meta version.

script-src

script-src 'self'

Allows scripts only from your own site. Inline scripts and third-party scripts are blocked.

sandbox

<iframe sandbox="allow-same-origin">

Limits the iframe. Without any allow- values, scripts, forms and popups are all disabled.

rel=noopener

<a target="_blank" rel="noopener noreferrer">

Stops the new page from accessing window.opener. noreferrer also hides the Referer header.

Security tools

ToolProtects against
CSPInjected and untrusted scripts
sandboxIframes running unwanted actions
rel=noopenerNew tabs controlling your page
rel=noreferrerLeaking your page URL to the target
HTTPSEavesdropping on the connection

A helpful analogy

CSP is a guest list at the door: only approved scripts get in. Sandbox is a leash on the iframe so it cannot open doors on its own. rel=noopener is a locked window on the new tab, so the visitor cannot climb back into your room.

Nesting rules

  • 1.Send CSP as a real HTTP header on production sites; the meta version cannot do everything.
  • 2.Add sandbox to every iframe unless you fully trust the source.

Common mistakes

Using target=_blank without rel=noopener

Avoid

<a href="..." target="_blank">

Use instead

<a href="..." target="_blank" rel="noopener">

Without noopener, the new page can access window.opener and redirect your original tab to a malicious site.

Trusting an external iframe without sandbox

Avoid

<iframe src="https://untrusted.example">

Use instead

<iframe src="..." sandbox="allow-same-origin">

An unsandboxed iframe can run scripts, show popups and steal data. sandbox limits those powers.

Quick tips

  • •Test your CSP in report-only mode first; it logs what would be blocked without breaking the page.
  • •Check the Network and Console panels; blocked scripts and CSP violations show up there.
Previous
Next