HTML Security: sandbox, noopener and Escaping

Intermediate · 11 min

Escape untrusted text before it becomes HTML, sandbox iframes you do not control, and know what target=_blank does to window.opener and the Referer header.

Before thisLinks and URLsIframes and Embedded Content

PwnFunction · 11 min

HTML Security: sandbox, noopener and Escaping

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PwnFunctionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Cross-site scripting happens when untrusted text becomes markup. Assigning it with `textContent` kept it inert in a test, while `innerHTML` ran an `onerror` handler from an injected image.
  • Escape for the context: `&`, `<`, `>` and both quote characters when writing into an attribute. An unescaped quote let an extra `onmouseover` attribute appear.
  • Scripts inserted via `innerHTML` do not run, but event-handler attributes on injected elements do. Blocking `<script>` is not a defence.
  • `<iframe sandbox>` with no tokens blocks scripts and gives the page an opaque origin. `allow-scripts allow-same-origin` together on a same-origin page lets it reach the parent, and defeats the point.
  • Modern browsers treat `target="_blank"` as `noopener`: `window.opener` was null in a test. `rel="opener"` brings it back, and `noreferrer` removes the Referer header.

HTML trusts what you give it

A browser can’t tell your markup from a stranger’s. If a username becomes part of your HTML without care, then a username of <script>… or <img onerror=…> is parsed as code. That is cross-site scripting (XSS), and escaping is the habit that stops it. The same trust problem appears in links to other sites and in framed third-party pages, which is why this lesson covers three tools: escaping, rel on links and sandbox on iframes. For the wider picture (CSP, cookies, CSRF) see web security fundamentals.

Escaping

I injected the same hostile string into a page in Chromium, Firefox and a WebKit build, with results that matched:

<img src="/x.png" onerror="pwned.push('img onerror')"><script>pwned.push("script tag")</script><b>bold</b>
  • el.innerHTML = evil: the onerror ran. The <script> element appeared in the DOM but did not execute, since scripts inserted this way do not. Many “sanitisers” only strip script tags and miss the first line.
  • el.textContent = evil: nothing ran, no child elements, and the string showed as text.
  • Writing into a quoted attribute: with title=" + x" onmouseover="… unescaped, the element gained an onmouseover attribute. Escaping &, <, >, " and ' kept it a single title.

The rule is to match the output context. Text content needs < and & escaped, attributes need quotes escaped, URLs need their scheme checked, not just encoded. Best of all, avoid building HTML from strings. Prefer textContent and DOM calls, and use template cloning with text assigned afterwards. When you must accept HTML, use a maintained sanitiser. Element.setHTML() with the Sanitizer API existed in Chromium and Firefox in my tests, but not in the WebKit build, and web-features marks it not yet Baseline, so it is an enhancement for now.

<a target="_blank"> once gave the new page a handle on yours through window.opener, so it could redirect your tab. Browsers now treat _blank as noopener. In the test, pages from a plain target="_blank" link, from rel="noreferrer" and from rel="noopener" all saw window.opener as null, while adding rel="opener" restored it. The Referer header differs by value: a plain link and noopener sent the origin of the sender, and noreferrer sent none. Reverse tabnabbing is largely history in current browsers, but noreferrer is still how you keep the source URL private. Check the links lesson for rel values in general.

Sandboxing iframes

An <iframe> runs another page inside yours. The sandbox attribute takes away powers, then you add back what you need:

  • Bare sandbox: the framed script did not run at all in the test.
  • sandbox="allow-scripts": the script ran, but reading parent.document and localStorage both threw a SecurityError, and document.cookie threw too in Chromium. The frame has an opaque origin.
  • sandbox="allow-scripts allow-same-origin" on a page served from the same origin as the parent: the frame read the parent freely and even removed its own sandbox attribute. The combination cancels the protection when the content is same-origin.
  • The same worked for a srcdoc frame with allow-scripts alone: origin was null.

So sandbox embeds you don’t fully trust, grant the fewest tokens, and never pair allow-scripts with allow-same-origin for same-origin content. See iframes and embedded content for what embeds need.

What the video covers

PwnFunction’s “Cross-Site Scripting (XSS) Explained” (March 2020, about 11 minutes). The description says it explores XSS in under 12 minutes, and links the creator’s XSS game and its source code. It covers the escaping half of this lesson; sandbox and noopener come from my tests and Tony Teaches Tech’s short noopener explainer in further viewing, which is from 2022. I could not read a transcript, and this channel is security-focused rather than HTML-focused, so the HTML-specific advice here is mine. No video I found covered sandbox in clear English.

Where this fits

This opens the production group, building on links and iframes. Language, direction and internationalization is next.

Check yourself

  1. 01A comment is inserted with `el.innerHTML = comment`. The comment contains `<img src=x onerror=...>`. What happens, and how do you fix it?

    The image fails to load and the `onerror` code runs, as in the test. Use `el.textContent = comment`, which treated the same string as inert text, or escape it first. If you need some markup, sanitise with a vetted library or `setHTML()` where supported.

  2. 02Why is `<iframe sandbox="allow-scripts allow-same-origin" src="/same-origin-page">` a poor sandbox?

    A same-origin framed page keeps its origin and can script the parent, and with scripts allowed it can even remove its own `sandbox` attribute. In the test that frame read the parent without an error. Drop `allow-same-origin`, or serve untrusted content from another origin.

  3. 03Do you still need `rel="noopener"` on `target="_blank"` links?

    Mostly no: in a test `window.opener` was null in a page opened from a plain `target="_blank"` link in all three engines, because browsers apply it by default. It is harmless to keep for older browsers. Add `rel="noreferrer"` if you also want no Referer sent.

  4. 04What does an `<iframe sandbox="allow-scripts">` page get when it reads `localStorage`?

    A `SecurityError`, since the sandbox gives it an opaque origin. In the test it could not read the parent or use storage, though its script ran.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark