Data URIs and Content-Security-Policy
Published 2026-11-10 · what the data: source expression really allows
Embed your first data URI on a site that ships a Content-Security-Policy and there is a good chance it will not render. CSP evaluates every load against the policy's directives, and data: is just another source expression — allowed only where a directive lists it. This page maps out where data: belongs, where it must never go, the SVG-specific wrinkles, and how to test a policy without breaking your pages.
One framing note before the details: a data URI is not a security feature. Base64 obscures content; it does not protect it. Anything in the string is trivially decodable — the Base64 to SVG decoder will turn an opaque SVG string back into readable markup in one paste. CSP is the layer that decides what the browser may load; the encoding is just a transport.
Source expressions are per-directive
A CSP is a list of directives, each with its own source list: img-src, script-src, style-src, font-src, frame-src, object-src, connect-src, and others. Adding data: to one directive changes nothing for the others. A directive that is absent falls back to default-src, so writing default-src 'self' data: quietly permits data URIs in every context that has no explicit directive of its own — usually not what the author intended.
The source expressions themselves are simple: scheme sources like data: or https:, host sources like cdn.example.com, the keyword 'self', and — for scripts and styles — nonces and hashes that whitelist exact inline content. Most data-URI policy mistakes are not clever attacks; they are someone widening default-src to make one image render.
When a data URI is refused, the console message names the directive that blocked it and the start of the refused URL. That name is the directive to change — seeing img-src there means the fix belongs in img-src, not in default-src.
img-src data: — the directive you probably need
Any page that renders images as data URIs needs data: in img-src. That includes inline icons, favicon links, self-contained HTML exports, and the output of converter tools — including this site's, where the product of the tool is a data URI. Without the source expression, every such image is blocked and the console fills with violations.
This is one of the low-risk places for data:. Content loaded in image context is treated as image data — it cannot execute script there. The main drawback is not malicious but operational: data URI images skip HTTP caching, bypass server-side validation, and carry any embedded metadata with them (more on that below). A minimal policy that allows your own origin plus inline images looks like:
Content-Security-Policy: default-src 'self'; img-src 'self' data:
Note the scope: data: in img-src does not bless remote images. An https: host you load photos from is a separate source expression and needs its own entry. Also note that img-src covers more than <img> tags — CSS background-image and favicon loads are image loads too, so an icon shipped as a data URI inside a stylesheet still needs the data: entry here.
script-src data: — the directive you must not add
Scripts are the opposite case. If script-src includes data:, then any injection point that lets an attacker write markup — a comment field, a URL parameter reflected into the DOM, a CMS text field — can carry a script tag pointing at a data URI. The exact exploitation details vary by framework, but the structural point is simple: allowing data: scripts converts "attacker can insert markup" into "attacker can execute code". It removes an important layer of defense, and injection bugs are common enough that you want every layer you can keep.
There is no legitimate need that compensates. Your own scripts load from your origin or a CDN you name; inline scripts are covered by nonces or hashes, which whitelist exactly the content you shipped. So script-src data: is a red flag in any policy review: nothing good needs it.
The same reasoning applies, with lower stakes, to style-src data: — CSS cannot run code, but attacker-controlled styles can deface a page and have historically been used to probe page content. font-src data: is somewhere in between: small self-contained builds sometimes embed fonts as data URIs, and that is a defensible choice, but hosting fonts under 'self' is the cleaner default.
The SVG nuance — image or document?
SVG is where the directive boundaries matter most, because the same markup behaves completely differently depending on how it loads:
- As an image — via
<img>, a CSS background, or a data URI in image context — SVG cannot execute its scripts. Browsers treat it as static image data. This is why an SVG data URI in an<img>is generally safe from script execution, and why the choice covered in inline SVG vs a data URI has a trust dimension: inline SVG markup is document content, and a<script>inside it runs. - As a document — via
<object>,<iframe>, or navigation — SVG is a full document, and its scripts execute. That is whydata:has no business inframe-srcorobject-src: it would let an embedded data URI run code inside a frame on your page.
Separately from CSP, modern browsers block top-level navigation to data: URIs — closing the old phishing trick of linking directly to a data:text/html page that renders a fake login form. Do not treat that browser-side block as your defense, though: it covers top-level navigation, not embedded contexts, and its exact behavior varies at the edges between browsers. Keep frame-src and object-src explicitly tight in the policy itself.
A quick way to tell which context your SVG is in: if the <svg> element is a child of your page's DOM, it is a document. If it arrives through a URL or a string dropped into an image slot, the browser renders it as an image.
A directive-by-directive guide
| Directive | data: reasonable here? | Typical safer policy |
|---|---|---|
img-src | Yes, if the page embeds images as data URIs | img-src 'self' data: |
script-src | No — never | 'self' plus nonces or hashes for inline scripts |
style-src | Rarely | 'self'; hashes for the few inline styles you really need |
font-src | Occasionally, in self-contained builds | Host fonts under 'self' when you can |
frame-src / object-src | No — data URIs here are full documents | Allow specific https: origins only |
connect-src | No | Explicit API origins; data URIs are not endpoints |
default-src | Careful — it inherits into every directive without its own list | Keep it strict; widen specific directives instead |
Testing your policy
Nothing about CSP is hypothetical in your console: every blocked load produces a report naming the directive involved and the source that was refused. The practical loop:
- Read the console on every page type. A converter page that renders data URI previews behaves differently from an article page. Test each template, not just the homepage.
- Ship Report-Only first. The
Content-Security-Policy-Report-Onlyheader evaluates the policy and logs what would be blocked without blocking anything. Let it run, sweep the console and any report endpoint, then switch to the enforcing header once the noise is gone. - Watch the fallback. If a data URI image loads but a font or a worker doesn't, check whether the missing piece falls back to
default-srcwhileimg-srcgot its own list. - Collect violations somewhere durable. Console reports vanish with the tab. Even a simple logging endpoint turns them into a checklist you can actually burn down before flipping the enforcing header on.
One non-CSP hygiene point that pairs with embedding: a data URI carries the image's metadata with it. EXIF blocks, GPS tags, and camera fields ride along inside the bytes whether the image lives in a file or in a string. Strip them first with the photo metadata remover before an image becomes part of a deliverable you hand to someone else.
As a starting template, the policy behind most converter and docs pages that embed images looks like: default-src 'self'; img-src 'self' data:, widening only what the console proves you need. For the embedding-versus-hosting trade-off behind these choices — when a data URI is worth its payload and its cache behavior — see when not to inline Base64.
Summary
- Allow
data:onimg-srcfor pages that genuinely render data URI images — and leave it off everything else by default. - Never add
data:toscript-src,frame-src,object-src, orconnect-src, and think twice before putting it indefault-src. - Test with Report-Only before enforcing, and treat every console violation as a real page problem, not a warning to dismiss.