Images and security
Which image sources are read, what is blocked, and how to replace the loader.
A spec comes from a model, and a model can be steered by whatever text it read. An Image element holds a src string, so the renderer decides what that string is allowed to reach. The Office writers read image sources themselves and would fetch URLs and read local paths without limits. For that reason the renderers load every image first, with the rules below, and give the writer only the resulting data.
What is allowed
| Source | Result |
|---|---|
data:image/png, jpeg, gif or svg+xml with ;base64, |
Used as is. |
https://... |
Fetched with the default loader. |
Anything else: a local path, file:, http:, ftp:, a relative URL, other data: types |
Blocked. The image is skipped with a blocked-image warning. |
The default loader allows a response only when all of this holds:
- The status is 2xx.
- The content type is
image/png,image/jpeg,image/giforimage/svg+xml. - The body is at most 10 MB.
- It arrives within 10 seconds.
A failure is an image-failed warning and the rest of the spec still renders. The checks are also exported as constants: MAX_IMAGE_BYTES and IMAGE_TIMEOUT_MS from json-render-office, and classifySource(src), which returns 'data', 'https' or 'blocked'.
Limits and cancellation
Pass imageOptions to tune the default loader without replacing it. Every field is optional and the defaults are the ones above.
| Field | Default | Effect |
|---|---|---|
timeoutMs |
10000 |
Time before one remote image is given up. Applies to the default loader. |
signal |
none | An AbortSignal. Aborting it stops pending loads and rejects the render with the abort reason. |
concurrency |
6 |
How many images load at the same time. Changes the timing only, not the output. |
maxBytes |
10485760 |
Byte cap per image. An image over it is skipped with a blocked-image warning. |
allowedHosts |
none | Host names remote images may come from, compared case-insensitively. Other hosts are skipped with blocked-image, and redirects must stay on the list. |
await renderToFile(spec, 'review.pptx', {
imageOptions: { timeoutMs: 5_000, concurrency: 4, allowedHosts: ['images.example.com'] },
})
With allowedHosts set, a browser that hides redirect targets refuses the redirect, because it cannot check where it leads. Use loadImage in that case.
The loadImage hook
Pass loadImage to replace the fetch, for example to add a host allowlist, send credentials, or go through a proxy. It receives the source and returns a data: URI:
import { renderToFile, type ImageLoader } from 'json-render-pptx'
const allowed = new Set(['images.example.com'])
const loadImage: ImageLoader = async (src) => {
if (!allowed.has(new URL(src).hostname)) throw new Error('Host is not allowed')
const response = await fetch(src, { signal: AbortSignal.timeout(5_000) })
const type = response.headers.get('content-type') ?? 'image/png'
const bytes = Buffer.from(await response.arrayBuffer())
return `data:${type};base64,${bytes.toString('base64')}`
}
await renderToFile(spec, 'review.pptx', { loadImage })
Only https sources reach loadImage. A data: image is used directly, and a blocked source never gets that far. If the loader returns anything that is not a data: image, the image fails with a warning.
What this does not cover
- The default loader refuses private addresses written as literals (
10.0.0.5,127.0.0.1,localhost), but it cannot see a public host name that resolves to a private address (DNS rebinding). The renderer runs in browsers and workers, where it cannot resolve DNS. If you render specs from untrusted users on a server, useloadImageto resolve the host yourself, check the result against internal ranges, and fetch the checked address. timeoutMsapplies to the default loader only. A customloadImagemust set its own timeout.signalalways rejects the render, but it stops in-flight requests only in the default loader.- Image content is not scanned. An SVG is embedded as data and is not run, but the application that opens the file decides how to draw it.
- Text in a spec is written to the file as it is. The renderers do not filter what a model says.