Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

srcdoc

Gives an iframe an inline HTML document instead of a URL. Without sandbox the document runs with your page's origin, so untrusted HTML always needs sandbox.

Overview

The srcdoc attribute gives an <iframe> its document as a string of HTML, with no separate URL. The frame loads it as about:srcdoc. If both srcdoc and src are present, srcdoc wins.

The most important fact about srcdoc: on its own it isolates nothing. An unsandboxed srcdoc document inherits your page's origin (and its Content Security Policy). Its scripts can read your cookies and storage and reach into your page through parent. Isolation comes only from adding sandbox without allow-same-origin.

Applies to: <iframe>

The page saves a value in localStorage. The unsandboxed frame reports the page's own origin and reads the value; the sandboxed frame has origin null and cannot.

Values

ValueResult
An HTML stringParsed as a complete document in the frame. A missing doctype is fine: srcdoc documents never enter quirks mode.
"" (empty)An empty document. src is still ignored while the attribute is present.
Attribute absentThe frame loads src (or about:blank).

Setting the srcdoc property from script (frame.srcdoc = html) reloads the frame with the new document.

Examples

Live preview of user-written HTML

The preview frame has an empty sandbox, so the <script> in the sample never runs, and anything it did run could not reach the page.

Writing srcdoc in markup

Inside a double-quoted attribute you must escape " as &quot; and & as &amp;. Literal < and > are allowed in attribute values, so escaping them is optional. Single quotes inside the HTML avoid most of the escaping.

Rendering an HTML email

An empty sandbox stops scripts, forms and popups in the message. It does not stop remote images, which are how emails track opens. To block them, put a CSP <meta> at the start of the string you build, or rewrite image URLs on the server.

In Vanilla Breeze

  • VB treats srcdoc as a script sink. The sanitiser (src/lib/sanitize-html.js) drops srcdoc in every profile, and <card-list> refuses to write it from data through data-field-attr (src/web-components/card-list/logic.js, alongside style and is).
  • data-mock fills an empty iframe[data-mock] (no src, no srcdoc) with a placeholder srcdoc showing the frame's title (src/utils/mock-init.js).
  • Style the frame with iframe.bordered, iframe.rounded or data-ratio from src/native-elements/iframe/styles.css. VB's CSS does not reach inside the srcdoc document; link a stylesheet from the string if you need it.

Accessibility

  • Give the iframe a title that names its content.
  • The srcdoc document has its own accessibility tree: include a lang attribute, headings and real structure in the string, as you would for any page.
  • Set a fixed height or use data-ratio; a frame that clips its content hides it from sighted keyboard users.

Security

  • Unsandboxed srcdoc is your origin. Treat its HTML exactly as if you inserted it into your page with innerHTML.
  • For untrusted HTML, use sandbox with no tokens, or with allow-scripts only. Never add allow-same-origin as well: the frame would get your origin back and could remove its own sandbox.
  • Escape when you build the attribute in a template. An unescaped " in user HTML ends the attribute and injects markup into your page. Setting the srcdoc property from script avoids this.
  • Sanitise as well as sandbox when the content can link out: a sandboxed frame can still show phishing links and load tracking images.
  • The srcdoc document inherits your page's CSP. A policy that blocks inline scripts or styles blocks them inside the frame too.

Gotchas

  • Relative URLs resolve against your page. The srcdoc document's base URL is the parent's, so href="next.html" points next to your page, not anywhere special.
  • No URL of its own. The content cannot be bookmarked, opened in a new tab or linked to from outside.
  • Size. Long strings bloat the page HTML and are parsed even if the frame is never seen; loading="lazy" defers rendering, not the bytes.

Related

  • sandbox — required for isolation; explains the allow-scripts allow-same-origin escape.
  • allow — grant powerful APIs to the frame.
  • loading — lazy-load frames below the fold.
  • data-mock — placeholder content for empty frames.
  • <iframe> — the element and its VB styles.
  • <markdown-viewer> — renders untrusted markdown inline through VB's sanitiser.

Which to choose

  • Your own trusted HTML snippet — plain markup in the page, or srcdoc without sandbox if you need style isolation.
  • Untrusted HTML that must keep its own styles (emails, previews) — srcdoc with an empty sandbox.
  • Untrusted text or markdown shown in the page — <markdown-viewer> or the sanitiser; no frame needed.
↑