Updated November 21, 2026  ·  12 min read  ·  SVGVector

SVG Sandbox Preview: Containing an Untrusted File

An SVG preview is a decision about where somebody else's file gets to run, and most tools make that decision without ever naming it. I built twelve files of 185 to 355 bytes — each a 240×110 drawing with one 60×40 rectangle and one mechanism — and placed every one of them twelve ways: as an image, as a CSS background, inlined into the page, in an object, an embed and an iframe, and in six iframe flavours with the sandbox attribute set or not. 144 cells, 60 of which did something. The image tag and the CSS background ran nothing and fetched nothing. The four unsandboxed document placements ran all nine mechanisms, including one script that read the session cookie and posted it to a second origin and one that replaced the whole page. With sandbox and no flags, none of the nine ran and none could read the cookie — but two of them still fetched.

Those last four words are the reason this article exists. The sandbox attribute is usually described as if it contained the file, and it does contain the code: no script, no parent document, no cookie, no storage, no navigation. It does not contain the network. A cross-origin <image> and a CSS @import reached the second origin from all ten document placements, including the ones where scripts were forbidden. And sandbox="allow-scripts allow-same-origin" gives almost everything back — 8 of the nine mechanisms ran there, including the cookie read — which is why the specification says that combination is no sandbox at all.

Matrix of nine SVG payloads against twelve preview placements. The img tag and CSS background columns are empty. The four unsandboxed document placements are filled for every payload. The sandbox column with no flags is empty again, while the allow-scripts column shows four filled cells and three blocked crosses. A second panel lists what the cookie, the parent document, localStorage, top navigation and the network did in each configuration.
Nine payloads that do something, twelve placements, Chrome 157.0.8103.79. A filled dot ran on its own, a ring needed the one click at the centre of the rectangle, an amber square is a request that reached the second origin, and a cross is the browser refusing the operation.

The twelve files

Every file is the same drawing with a different thing attached, so the only variable is the mechanism. They are small on purpose: 185 bytes is short enough to paste in a comment, and a reviewer who is looking at the picture will not see any of it.

FileSizeWhat is attachedCells that reached something
sb-control195 Bnothing at all (control) 0 of 12
sb-script235 B<script> 9 of 12
sb-onload185 Bonload on the root element 9 of 12
sb-image283 B<image href> at a second origin 0 of 12
sb-cookie340 Bscript reads document.cookie, beacons it out 5 of 12
sb-parent321 Bscript writes into parent.document 5 of 12
sb-storage314 Bscript writes to localStorage 5 of 12
sb-topnav338 Bscript navigates top.location 4 of 12
sb-jshref251 B<a href="javascript:…">, needs a click 5 of 12
sb-fo355 BforeignObject with an XHTML img onerror 9 of 12
sb-extjs300 Bexternal <script href> 9 of 12
sb-cssimport266 B@import inside a <style> 0 of 12

The preview page and the files are served from the same origin, which is what an upload folder usually is: a directory on the same host as the viewer. That matters for the cookie column and nothing else — a file on a separate origin would fail to read the cookie for the boring reason that it is on a separate origin, and the sandbox comparison would not change.

Two placements run nothing

Across the twelve files and two placements — <img> and a CSS background — there were zero executions and zero requests. Not one payload, including the two built specifically to fetch, produced a single log line on the second origin. All 24 cells still painted their rectangle, 2400 pixels each, so this is containment rather than failure.

That is the same result every article in this series has measured, and it is worth restating why: the image loader decodes the file into a picture and never gives it a document. No document means no base URL for the <image href>, no origin to read a cookie from, no parent to write into, and no script engine attached. The cost is that the preview is a photograph: you cannot inspect the tree, select the text or click anything inside it.

Four placements are the page

Inline the markup, or point an <object>, an <embed> or an <iframe> at the file, and the file becomes a document on your origin. All four behave identically here: 8 mechanisms ran, and the ninth replaced the page.

PayloadWhat the preview did
sb-cookieRead document.cookie, got sid=secret123, and requested /c.png?d=sid%3Dsecret123 from the second origin — one hit per placement, five placements.
sb-parentWrote data-pwned onto the parent <body>. After the load the top page carried the attribute.
sb-storageWrote to localStorage on your origin. It survives the preview being closed.
sb-topnavNavigated the top frame to another URL. In four cells the harness came back holding a different page than the one it opened.
sb-jshrefRan on one click in the middle of the rectangle — the payload the previous article's marker variable kept reporting as clean.
sb-extjsFetched a script from the second origin and executed it: /e.js, 1 hit per placement.
sb-foAn XHTML <img onerror> inside foreignObject fired, and drew a broken-image glyph — the 2605 pixels instead of 2,400 that show up in every document placement.

In these four placements Chrome says nothing at all about it, because nothing is being blocked: the console shows only the harmless 404 from the foreignObject image. A preview that runs a file this way is not misconfigured; it is doing exactly what an iframe is for.

What the sandbox attribute changes

Same twelve files, same origin, three configurations. This is the whole article in one table.

Mechanismsandbox+ allow-scripts+ allow-scripts allow-same-origin
<script>, onloadblockedrunsruns
read document.cookieblockedopaque origin, cookie is the empty stringreads sid=secret123
write into parent.documentblockedblocked, SecurityErrorworks
localStorageblockedblocked, SecurityErrorworks
navigate top.locationblockedblockedblocked
javascript: link on clickblockedblocked, silentlyruns
external <script href>not even fetchedfetched and executedfetched and executed
cross-origin <image>fetchedfetchedfetched
CSS @importfetchedfetchedfetched
the drawing itself2400 px2400 px2400 px

Two rows deserve their own paragraph. The cookie row is the difference between contained and contained-looking: with allow-scripts alone the script runs, and every attempt to read the cookie throws — not because the attribute is a filter but because the document is in an opaque origin, so there is no cookie jar to read. Add allow-same-origin and the script is back on your origin, with your cookie, your storage and your parent document.

The top-navigation row is the one thing the attribute keeps even in the permissive case, because allow-top-navigation is a separate flag. Chrome says so directly: Unsafe attempt to initiate navigation for frame with URL … The frame attempting navigation of the top-level window is sandboxed, but the flag of 'allow-top-navigation' or 'allow-top-navigation-by-user-activation' is not set. If your preview needs a link inside the file to work, think about whether you want it to work.

srcdoc, blob and data: bytes you already have in JS

Three of the twelve placements never send the file back to the server. srcdoc puts the markup in an attribute, blob: wraps it in a Blob and hands the frame a URL, and data: encodes it into the URL itself. All three are common in editor previews, because they avoid a round trip and let you preview unsaved changes.

With sandbox="allow-scripts" all three land on the same numbers as the plain sandboxed iframe: 4 mechanisms run, 3 blocked, the cookie unreadable. The origin of the URL does not decide the origin of the frame — the sandbox does. A blob: URL inherits the creator's origin, and it is still opaque inside a sandboxed frame, which is exactly the property you want if the editor has the bytes in memory and no server route to preview them.

The one measurable difference in the whole set is cosmetic and comes from the missing base URL. The foreignObject payload points a relative src="/missing.png" at a file that does not exist; in seven placements the browser draws a broken-image glyph (2605 painted pixels instead of 2,400), and in the blob: and data: cells the same payload paints 2400 — the glyph never appears, because a relative reference has nothing to resolve against. Same code, same origin rules, different pixels. If your preview pipeline compares renders, that is a difference to expect, not a bug to chase.

A sandbox does not stop the network

This is the part that gets left out of most advice. Four payloads can reach out, and the sandbox stops only the one that needs script permission to start.

PayloadReached the second origin inCells
sb-image — <image href>every document placement, sandboxed or not10
sb-cssimport — @importevery document placement, sandboxed or not10
sb-extjs — external scriptevery placement except the one where scripts are forbidden9
sb-cookie — beacon after reading the cookieonly where the cookie was readable5

The @import row is the interesting one. It produces a request in the no-flags sandbox, where the console is busy reporting blocked script execution and every visible sign says the file is inert. CSS is not script: the sandbox does not gate stylesheet loading, so a file can still announce that it was opened, from which address, and when. That is one bit of exfiltration without a single line of JavaScript, and it is the same finding the CSP article ran into from the other direction.

If the network matters — and for user content it usually does — the control that works is a Content-Security-Policy on the preview response that forbids every origin but your own, or serving previews from a host that has nothing else on it. The external reference article lists the nine ways a file can leave; the sandbox is orthogonal to all of them.

The four states, and a function that assigns them

After the table the useful abstraction is small: every cell is inert, escaped, contained, or contained-with-scripts. Written out, it is the thing to put in a review tool rather than in a document nobody reads.

const INERT = new Set(['img', 'cssbg']);
const SANDBOXED = new Set(['sbnone', 'sbscripts', 'sbboth', 'sbsrcdoc', 'sbblob', 'sbdata']);

// The attribute is a list of permissions, so build it from what the preview
// actually needs instead of copying a string from a blog post.
function sandboxFor({ scripts = false, sameOrigin = false } = {}) {
  const flags = [];
  if (scripts) flags.push('allow-scripts');
  if (sameOrigin) flags.push('allow-same-origin');   // cancels the containment
  return flags.length ? `sandbox="${flags.join(' ')}"` : 'sandbox';
}

// One measured cell in, one of four states out. "escaped" means the file
// reached something that belongs to the page, not that it looks dangerous.
function classify({ mode, executed, cookie, navigated }) {
  if (INERT.has(mode)) return 'inert';
  if (navigated || cookie === 'secret') return 'escaped';
  if (!SANDBOXED.has(mode)) return 'escaped';
  return executed ? 'contained-with-scripts' : 'contained';
}

Run over the 144 measured cells it returns inert for the 24 image and background cells, escaped for the 36 unsandboxed document cells and for the one cookie cell under allow-same-origin, contained for the twelve no-flags sandbox cells, and contained-with-scripts for the 16 cells where an opaque origin still ran code.

The two states that matter for a decision are the last two. If a preview only needs to show the drawing, contained is available: no script runs, and the picture is identical to the unsandboxed one. If the preview needs interaction — hover states, an animated icon, a clickable prototype — then contained-with-scripts is the honest ceiling, and the network is still open.

The checklist

  1. Never inline, never object, never embed. Those four placements ran all nine mechanisms on the viewer's origin.
  2. Use <img> for thumbnails. It is the only placement with zero executions and zero requests, and it paints the file correctly.
  3. If you need a document, use an iframe with sandbox and no flags. The drawing survives; nothing else does.
  4. Add allow-scripts only when the feature needs it, and never add allow-same-origin alongside. Together they undo the containment, and every guide that recommends the pair is describing a way to make the attribute decorative.
  5. Do not rely on the sandbox for the network. Add a CSP that allows only your own origin, or serve the preview from a host with nothing else on it.
  6. Keep the file off the origin that holds the session. The sandbox is a second line; a separate origin is the first.
  7. Re-test after changing the preview. The difference between a thumbnail and a detail view is one attribute, and the table above changes with it.

What this does not cover

It does not cover cost. A sandboxed iframe still renders the file, and a file with 400 filters costs the same milliseconds whether or not its scripts are allowed to run — the filter article is the one that measured that. It does not cover the download path either: a file served with Content-Type: image/svg+xml and opened directly is a document on your origin, and no preview attribute applies. And it does not cover allow-popups, allow-forms or allow-modals, each of which adds a way out that this harness did not test.

It also does not cover the browser you are not using. Everything here is Chrome 157.0.8103.79 on macOS. The sandbox specification is older and more uniform than most of the SVG surface, so I would expect the containment rows to hold and the network rows to be identical, but "would expect" is not a measurement.

For the pipeline around the preview: the upload checklist covers what to check before you store a file, the SVG Validator shows the tree before anything renders, and the SVG Editor opens a file you have already decided to trust. One sentence: preview in an image tag when you only need a picture, in a no-flags sandbox when you need a document, and treat the network as a separate problem because the sandbox does not touch it.

Related SVG Workflows

Containment is one layer of a pipeline that has several. These pages cover the layers on either side of it.

Try These Free Online Tools

No download required — convert, compress & optimize SVGs right in your browser

Validate

SVG Validator

See the elements and references before anything renders.

Open validator
Inspect

SVG Editor

Open a file you have already decided to trust.

Open editor
Flatten

SVG to PNG

Preview as pixels: the one placement that runs nothing.

Convert to PNG
Optimise

SVG Optimiser

Strip scripts, references and metadata before storing.

Open optimiser
Encode

SVG to Data URI

Inline a trusted icon instead of serving a file.

Encode it
Inspect

SVG to Code

Read the markup as JSX before it becomes a component.

Convert to code