Skip to content

Commit 568a279

Browse files
committed
decode a bitmap glyph once a render by its place in the font, at most 2048x2048, and split svg-dos-paint.js
1 parent b87145c commit 568a279

8 files changed

Lines changed: 127 additions & 45 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
137137
### Fixes
138138

139139
- `renderSVG()` bounds the points paths are made of: an arc between far-apart ends is drawn with at most 64 curves a turn (a 139-byte path, `M 1e25 100 A 40 30 20 1 1 160 100`, stroked, ran the process out of memory), and the curves of arcs and the points of all paths, also of each copy drawn through `<use>`, count against 2^25 points plus 4 per pixel of the output in one render and 2^21 in one path. Reading path data and placing text along a `<textPath>` count, too, and SVGs embedded as images share the budget of the render they are in. More throw a `LimitError` (which a text's layout no longer takes for a damaged font). A path drawn through `<use>` is read once a render instead of once a copy.
140-
- SVG text: the glyph outlines of all texts count against the render's points before they are made, 16 points a command (20 texts of 1,000 characters of a complex font ran the process out of memory), so very long texts need a larger output; many `<tspan>`s in one `<text>` take linear time (10,000 took 20 s); a `<textPath>` on a long path renders at a normal size; bitmap emoji (CBDT, sbix) are decoded once a render instead of once a draw (copies through `<use>` took minutes).
140+
- SVG text: the glyph outlines of all texts count against the render's points before they are made, 16 points a command (20 texts of 1,000 characters of a complex font ran the process out of memory), so very long texts need a larger output; many `<tspan>`s in one `<text>` take linear time (10,000 took 20 s); a `<textPath>` on a long path renders at a normal size; bitmap emoji (CBDT, sbix) are decoded once a render instead of once a draw (copies through `<use>` took minutes), at most 2048x2048 pixels each (larger ones aren't drawn, also in `renderText()`: one that said 16383x16383 ran the process out of memory).
141141
- `renderText()` takes linear time for many combining marks and lines (400,000 lines took minutes).
142142
- Very long text (50,000 characters in SVG, 200,000 combining marks or lines in `renderText()`) no longer overflows the call stack or says the font is damaged.
143143
- Binary data and dates of other realms (iframes, `node:vm`, Jest) are accepted; objects that only claim to be one by `Symbol.toStringTag` aren't, and a detached buffer throws a `TypeError` that says so.

‎src/render/svg.ts‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ export function render(source: string | Uint8Array, mode: 'scale' | 'width' | 'h
204204
// (a nested render, of an SVG embedded as an image, takes its points from the render it is in)
205205
if (!previousPaths) pointsLeft = MAX_POINTS + POINTS_PER_PIXEL * outWidth * outHeight;
206206
parsedPaths = new Map();
207-
const previousBitmaps = decodedBitmaps;
208-
decodedBitmaps ??= new WeakMap();
207+
const previousBitmaps = decodedBitmaps, previousBitmapPixels = decodedBitmapPixels;
208+
if (!decodedBitmaps) {
209+
decodedBitmaps = new WeakMap();
210+
decodedBitmapPixels = 0;
211+
}
209212
// (a nested render, of an SVG embedded as an image, shares the allowance: the outer paths stay kept meanwhile)
210213
if (!previousPaths) cachedCommands = 0;
211214
try {
@@ -221,6 +224,7 @@ export function render(source: string | Uint8Array, mode: 'scale' | 'width' | 'h
221224
if (!previousPaths) pointsLeft = previousPoints;
222225
parsedPaths = previousPaths;
223226
decodedBitmaps = previousBitmaps;
227+
if (!previousBitmaps) decodedBitmapPixels = previousBitmapPixels;
224228
cachedCommands = previousCached;
225229
}
226230
}
@@ -1442,16 +1446,7 @@ class Document {
14421446
const s = Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
14431447
const bitmap = tables.bitmaps(Math.round(s * font.unitsPerEm))?.(g.glyph) ?? tables.bitmaps(g.fontSize)?.(g.glyph);
14441448
if (!bitmap) return;
1445-
// (decoded once a render: an emoji drawn through <use> was decoded again for each copy)
1446-
let decoded = decodedBitmaps?.get(bitmap.png);
1447-
if (decoded === undefined) {
1448-
try {
1449-
decoded = decodePNG(bitmap.png);
1450-
} catch {
1451-
decoded = null;
1452-
}
1453-
decodedBitmaps?.set(bitmap.png, decoded);
1454-
}
1449+
const decoded = decodedBitmap(bitmap.png);
14551450
if (!decoded) return;
14561451
const k = font.unitsPerEm / bitmap.ppem;
14571452
let left = bitmap.left * k, top = bitmap.top * k;
@@ -3414,8 +3409,37 @@ let pointsLeft = Infinity;
34143409
let parsedPaths: Map<string, PathCommand[]> | null = null, cachedCommands = 0;
34153410
// the paths of texts' glyphs
34163411
const glyphPaths = new WeakSet<PathCommand[]>();
3417-
// the bitmap glyphs decoded in the render that runs, by their PNG data
3418-
let decodedBitmaps: WeakMap<Uint8Array, ReturnType<typeof decodePNG> | null> | null = null;
3412+
// the bitmap glyphs decoded in the render that runs, by the font data they are in and their offset there (a strike's
3413+
// glyphs are looked up for each size), and their pixels
3414+
let decodedBitmaps: WeakMap<ArrayBufferLike, Map<number, RasterImage | null>> | null = null, decodedBitmapPixels = 0;
3415+
// the most pixels a bitmap glyph may have (emoji fonts have strikes of 160 at most; one that said 16383x16383 took 1 GB),
3416+
// and the pixels of the bitmap glyphs kept in a render (decoded once, an emoji drawn through <use> was decoded for each
3417+
// copy; with its premultiplied floats a pixel takes 20 bytes, so some 80 MB)
3418+
const MAX_BITMAP_GLYPH_PIXELS = 2048 * 2048, MAX_KEPT_BITMAP_PIXELS = 2 ** 22;
3419+
3420+
/** a bitmap glyph's PNG decoded, or null if it can't be (damaged or too large) */
3421+
function decodedBitmap(png: Uint8Array): RasterImage | null {
3422+
let kept = decodedBitmaps?.get(png.buffer);
3423+
const key = png.byteOffset * 2 ** 21 + Math.min(png.length, 2 ** 21 - 1);
3424+
const cached = kept?.get(key);
3425+
if (cached !== undefined) return cached;
3426+
let decoded: RasterImage | null;
3427+
try {
3428+
decoded = decodePNG(png, MAX_BITMAP_GLYPH_PIXELS);
3429+
// (decoding takes about the time of painting its pixels 4 times)
3430+
charge(4 * decoded.width * decoded.height);
3431+
} catch (err) {
3432+
if (err instanceof LimitError && paintLeft < 0) throw err;
3433+
decoded = null;
3434+
}
3435+
const pixels = decoded ? decoded.width * decoded.height : 0;
3436+
if (decodedBitmaps && decodedBitmapPixels + pixels <= MAX_KEPT_BITMAP_PIXELS) {
3437+
decodedBitmapPixels += pixels;
3438+
if (!kept) decodedBitmaps.set(png.buffer, kept = new Map<number, RasterImage | null>());
3439+
kept.set(key, decoded);
3440+
}
3441+
return decoded;
3442+
}
34193443
const MAX_CACHED_COMMANDS = 2 ** 20;
34203444

34213445
/** counts the points of one path against the render's budget and the limit of a path */

‎src/render/text.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ function layOutParagraph(p: Paragraph, layout: Layout, font: (i: number) => Size
500500
async function scaledBitmap(glyph: BitmapGlyph, scale: number): Promise<ColorBitmap | null> {
501501
let decoded;
502502
try {
503-
decoded = await decodePNG(glyph.png);
503+
// (at most 2048x2048: emoji fonts have strikes of 160 at most, and one that said 16383x16383 took 1 GB)
504+
decoded = await decodePNG(glyph.png, 2048 * 2048);
504505
} catch {
505506
return null;
506507
}

‎tests/fonts/emoji-4096.ttf‎

71.9 KB
Binary file not shown.

‎tests/svg-arcs.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ const cases = {
6262
for (let i = 1; i < 8; i++) d += `<g id="t${i}">${`<use href="#t${i - 1}"/>`.repeat(4)}</g>`;
6363
return `<svg viewBox="0 0 100 100" width="10" height="10" ${NS}><defs>${d}</defs><use href="#t7"/></svg>`;
6464
})(),
65+
// a large bitmap emoji at many sizes: decoded once, not once a size
66+
'large emoji at 12 sizes': (() => {
67+
const font = require('fs').readFileSync('./tests/fonts/emoji-4096.ttf').toString('base64');
68+
let texts = '';
69+
for (let i = 0; i < 12; i++) texts += `<text x="1" y="8" font-family="f" font-size="${8 + i}">\u{1F600}</text>`;
70+
return `<svg ${NS} width="10" height="10"><style>@font-face{font-family:f;src:url(data:font/ttf;base64,${font})}</style>${texts}</svg>`;
71+
})(),
72+
// a bitmap emoji that says it is 16383x16383: not decoded (it was allocated, 1 GB)
73+
'huge emoji': (() => {
74+
const font = require('fs').readFileSync('./tests/fonts/emoji-4096.ttf');
75+
const at = font.indexOf('IHDR');
76+
font.writeUInt32BE(16383, at + 4);
77+
font.writeUInt32BE(16383, at + 8);
78+
let crc = ~0;
79+
for (const byte of font.subarray(at, at + 17)) {
80+
crc ^= byte;
81+
for (let k = 0; k < 8; k++) crc = crc & 1 ? crc >>> 1 ^ 0xedb88320 : crc >>> 1;
82+
}
83+
font.writeUInt32BE(~crc >>> 0, at + 17);
84+
let texts = '';
85+
for (let i = 0; i < 4; i++) texts += `<text x="1" y="8" font-family="f${i}" font-size="8">\u{1F600}</text>`;
86+
const faces = Array.from({length: 4}, (_, i) => `@font-face{font-family:f${i};src:url(data:font/ttf;base64,${font.toString('base64')})}`).join('');
87+
return `<svg ${NS} width="10" height="10"><style>${faces}</style>${texts}</svg>`;
88+
})(),
6589
// many tspans in one text took quadratic time
6690
'many tspans': `<svg viewBox="0 0 100 100" width="10" height="10" ${NS}><text font-size="1">${'<tspan>A</tspan>'.repeat(10000)}</text></svg>`,
6791
// a path of lines only, drawn 90000 times through <use> without painting anything: no budget counted its points

‎tests/svg-dos-markers.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const {Image} = require('..');
2+
3+
// SVGs against denial of service, part 3 (split from svg-dos-paint.js for the time a test may take): markers and the
4+
// pixels shapes and layers paint; small documents that must render (or throw) quickly
5+
const NS = 'xmlns="http://www.w3.org/2000/svg"';
6+
7+
const panic = message => {
8+
console.error(message);
9+
process.exit(1);
10+
};
11+
12+
// (Bun's JavaScriptCore runs the rasterizer several times slower than V8: 12 s where Node takes 2 s)
13+
const slack = process.versions.bun ? 4 : 1;
14+
15+
(async () => {
16+
// (not under coverage: reaching the budgets takes most of what a test may take there)
17+
if (!process.env.NODE_V8_COVERAGE) {
18+
let error = null;
19+
// markers are limited like the elements of <use> references: 120000 marker-mid instances took 135 s
20+
{
21+
const points = Array.from({length: 120000}, (_, i) => `${i % 1000} ${i / 1000 | 0}`).join(' L');
22+
const markers = `<svg ${NS} width="1000" height="200"><marker id="m" markerWidth="8" markerHeight="8"><circle cx="4" cy="4" r="3"/></marker><path d="M${points}" stroke="black" marker-mid="url(#m)"/></svg>`;
23+
const begin = Date.now();
24+
error = null;
25+
try { await Image.renderSVG(markers); } catch (err) { error = err; }
26+
if (!error || error.name !== 'LimitError' || !/markers/.test(error.message) || Date.now() - begin > 30000 * slack) panic(`120000 markers: expected the marker limit within 30 s, got ${error} after ${Date.now() - begin} ms`);
27+
}
28+
// what counts is what is painted, not the bounding boxes of the shapes: a chart of 1000 thin lines across the
29+
// image renders (each line's box is the whole image)
30+
{
31+
let lines = '';
32+
for (let i = 0; i < 1000; i++) lines += `<path d="M0 ${i} L1000 ${(i * 37) % 1000}" stroke="#08f" stroke-width="0.5"/>`;
33+
const begin = Date.now();
34+
const chart = await Image.renderSVG(`<svg ${NS} width="1000" height="1000">${lines}</svg>`).catch(err => err);
35+
if (!(chart instanceof Image) || Date.now() - begin > 60000 * slack) panic(`a chart of 1000 lines: ${chart} after ${Date.now() - begin} ms, it should render`);
36+
}
37+
// the pixels shapes and layers fill are bounded like the filters' work: 1000 <use> copies of a full-canvas group
38+
// with opacity (16 KB) took 260 s at 4000x4000, 9999 markers of a 400x400 group 36 s
39+
for (const [name, svg] of [
40+
['<use> copies', `<svg ${NS} xmlns:xlink="http://www.w3.org/1999/xlink" width="2000" height="2000"><defs><g id="g" opacity="0.5"><rect width="2000" height="2000" fill="red"/></g></defs>${'<use href="#g"/>'.repeat(1000)}</svg>`],
41+
['markers', `<svg ${NS} width="1000" height="1000"><marker id="m" markerWidth="400" markerHeight="400" markerUnits="userSpaceOnUse"><g opacity="0.5"><rect width="400" height="400"/></g></marker><path d="M0 0${' L500 500 L0 0'.repeat(4999)}" marker-mid="url(#m)"/></svg>`]
42+
]) {
43+
const begin = Date.now();
44+
error = null;
45+
try { await Image.renderSVG(svg); } catch (err) { error = err; }
46+
if (!error || error.name !== 'LimitError' || Date.now() - begin > 30000 * slack) panic(`${name}: expected a LimitError within 30 s, got ${error} after ${Date.now() - begin} ms`);
47+
}
48+
}
49+
})();

‎tests/svg-dos-paint.js‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,6 @@ const slack = process.versions.bun ? 4 : 1;
6060
error = null;
6161
try { await Image.renderSVG(`<svg ${NS} width="1e17" height="10"/>`); } catch (err) { error = err; }
6262
if (!error || error.name !== 'LimitError') panic(`a declared width of 1e17: expected a LimitError, got ${error}`);
63-
// markers are limited like the elements of <use> references: 120000 marker-mid instances took 135 s
64-
{
65-
const points = Array.from({length: 120000}, (_, i) => `${i % 1000} ${i / 1000 | 0}`).join(' L');
66-
const markers = `<svg ${NS} width="1000" height="200"><marker id="m" markerWidth="8" markerHeight="8"><circle cx="4" cy="4" r="3"/></marker><path d="M${points}" stroke="black" marker-mid="url(#m)"/></svg>`;
67-
const begin = Date.now();
68-
error = null;
69-
try { await Image.renderSVG(markers); } catch (err) { error = err; }
70-
if (!error || error.name !== 'LimitError' || !/markers/.test(error.message) || Date.now() - begin > 30000 * slack) panic(`120000 markers: expected the marker limit within 30 s, got ${error} after ${Date.now() - begin} ms`);
71-
}
72-
// what counts is what is painted, not the bounding boxes of the shapes: a chart of 1000 thin lines across the
73-
// image renders (each line's box is the whole image)
74-
{
75-
let lines = '';
76-
for (let i = 0; i < 1000; i++) lines += `<path d="M0 ${i} L1000 ${(i * 37) % 1000}" stroke="#08f" stroke-width="0.5"/>`;
77-
const begin = Date.now();
78-
const chart = await Image.renderSVG(`<svg ${NS} width="1000" height="1000">${lines}</svg>`).catch(err => err);
79-
if (!(chart instanceof Image) || Date.now() - begin > 60000 * slack) panic(`a chart of 1000 lines: ${chart} after ${Date.now() - begin} ms, it should render`);
80-
}
81-
// the pixels shapes and layers fill are bounded like the filters' work: 1000 <use> copies of a full-canvas group
82-
// with opacity (16 KB) took 260 s at 4000x4000, 9999 markers of a 400x400 group 36 s
83-
for (const [name, svg] of [
84-
['<use> copies', `<svg ${NS} xmlns:xlink="http://www.w3.org/1999/xlink" width="2000" height="2000"><defs><g id="g" opacity="0.5"><rect width="2000" height="2000" fill="red"/></g></defs>${'<use href="#g"/>'.repeat(1000)}</svg>`],
85-
['markers', `<svg ${NS} width="1000" height="1000"><marker id="m" markerWidth="400" markerHeight="400" markerUnits="userSpaceOnUse"><g opacity="0.5"><rect width="400" height="400"/></g></marker><path d="M0 0${' L500 500 L0 0'.repeat(4999)}" marker-mid="url(#m)"/></svg>`]
86-
]) {
87-
const begin = Date.now();
88-
error = null;
89-
try { await Image.renderSVG(svg); } catch (err) { error = err; }
90-
if (!error || error.name !== 'LimitError' || Date.now() - begin > 30000 * slack) panic(`${name}: expected a LimitError within 30 s, got ${error} after ${Date.now() - begin} ms`);
91-
}
9263
// a real filter on a large image still renders
9364
const shadow = await Image.renderSVG(`<svg ${NS} width="2000" height="2000"><filter id="s"><feGaussianBlur in="SourceAlpha" stdDeviation="8"/><feOffset dx="10" dy="10" result="b"/><feFlood flood-color="#0004"/><feComposite in2="b" operator="in"/><feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge></filter><rect x="100" y="100" width="1700" height="1700" fill="red" filter="url(#s)"/></svg>`);
9465
if (shadow.width !== 2000) panic('a drop shadow on a large image should render');

‎tests/text-spreads.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ const panic = message => {
1010
// call (past the stack's limit), and a failure doesn't blame the font
1111
(async () => {
1212
const font = fs.readFileSync(__dirname + '/fonts/resvg/NotoSans-Regular.ttf');
13+
// a bitmap emoji that says it is 16383x16383 isn't decoded (it was allocated, 1 GB)
14+
const emoji = fs.readFileSync(__dirname + '/fonts/emoji-4096.ttf'), at = emoji.indexOf('IHDR');
15+
emoji.writeUInt32BE(16383, at + 4);
16+
emoji.writeUInt32BE(16383, at + 8);
17+
let crc = ~0;
18+
for (const byte of emoji.subarray(at, at + 17)) {
19+
crc ^= byte;
20+
for (let k = 0; k < 8; k++) crc = crc & 1 ? crc >>> 1 ^ 0xedb88320 : crc >>> 1;
21+
}
22+
emoji.writeUInt32BE(~crc >>> 0, at + 17);
23+
const before = process.memoryUsage().rss;
24+
await Image.renderText('\u{1F600}', {font: emoji, size: 8});
25+
if (process.memoryUsage().rss - before > 512 * 2 ** 20) panic(`a huge emoji: took ${(process.memoryUsage().rss - before) >> 20} MB`);
1326
for (const [name, text, options] of [
1427
['combining marks', 'a' + '\u0301'.repeat(200000), {}],
1528
['lines', 'a '.repeat(200000), {width: 1}],

0 commit comments

Comments
 (0)