Skip to content

Commit a2aa648

Browse files
authored
improvement(sandbox): upgrade pptx/docx/pdf bootstrap with image helpers, MIME guards, and 256 MB isolate limit (#4505)
* improvement(sandbox): upgrade pptx/docx/pdf bootstrap with image helpers, MIME guards, and 256 MB isolate limit * fix(sandbox): strict MIME allowlist and nullish coalescing in docx addImage * fix(sandbox): validate required opts in pdf drawImage to prevent silent origin placement * fix(sandbox): throw on malformed data URI in docx addImage * fix(sandbox): prevent opts from clobbering computed ImageRun data/type/transformation * fix(sandbox): prevent opts from clobbering fetched data in pptx addImage * fix(sandbox): validate required opts in pptx addImage * fix(sandbox): remove silent image/png fallback in docx addImage MIME parsing * fix(sandbox): consistency and cleanup pass on doc-gen tasks and worker - DOCX addImage: upfront width/height validation (matches PDF/PPTX pattern) - PDF embedImage: remove dead Buffer ternary; drop redundant size guard already enforced in getFileBase64 - isolated-vm-worker: add friendly MemoryLimitError branch in both execute paths so OOM produces a clear message instead of a raw V8 error
1 parent 6a00685 commit a2aa648

4 files changed

Lines changed: 191 additions & 11 deletions

File tree

apps/sim/lib/execution/isolated-vm-worker.cjs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function executeCode(request, executionId) {
183183
const externalCopies = []
184184

185185
try {
186-
isolate = new ivm.Isolate({ memoryLimit: 128 })
186+
isolate = new ivm.Isolate({ memoryLimit: 256 })
187187
if (executionId !== undefined) activeIsolates.set(executionId, isolate)
188188
context = await isolate.createContext()
189189
const jail = context.global
@@ -398,6 +398,21 @@ async function executeCode(request, executionId) {
398398
}
399399
}
400400

401+
if (
402+
err.message.includes('Array buffer allocation failed') ||
403+
err.message.includes('memory limit')
404+
) {
405+
return {
406+
result: null,
407+
stdout,
408+
error: {
409+
message:
410+
'Execution exceeded memory limit (256 MB). Reduce image sizes or split the work into smaller batches.',
411+
name: 'MemoryLimitError',
412+
},
413+
}
414+
}
415+
401416
return {
402417
result: null,
403418
stdout,
@@ -511,7 +526,7 @@ async function executeTask(request, executionId) {
511526
let tPhase = tStart
512527

513528
try {
514-
isolate = new ivm.Isolate({ memoryLimit: 128 })
529+
isolate = new ivm.Isolate({ memoryLimit: 256 })
515530
if (executionId !== undefined) activeIsolates.set(executionId, isolate)
516531
context = await isolate.createContext()
517532
const jail = context.global
@@ -937,6 +952,23 @@ async function executeTask(request, executionId) {
937952
timings,
938953
}
939954
}
955+
956+
if (
957+
err.message?.includes('Array buffer allocation failed') ||
958+
err.message?.includes('memory limit')
959+
) {
960+
return {
961+
result: null,
962+
stdout,
963+
error: {
964+
message:
965+
'Execution exceeded memory limit (256 MB). Reduce image sizes or split the work into smaller batches.',
966+
name: 'MemoryLimitError',
967+
},
968+
timings,
969+
}
970+
}
971+
940972
return {
941973
result: null,
942974
stdout,

apps/sim/sandbox-tasks/docx-generate.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,64 @@ export const docxGenerateTask = defineSandboxTask<SandboxTaskInput>({
1515
globalThis.addSection = (section) => {
1616
globalThis.__docxSections.push(section);
1717
};
18-
globalThis.getFileBase64 = async (fileId) => {
18+
19+
// Page geometry constants (twips, 1 twip = 1/1440 inch) for US Letter
20+
globalThis.PAGE_W = 12240; // 8.5"
21+
globalThis.PAGE_H = 15840; // 11"
22+
globalThis.MARGIN = 1440; // 1" margins
23+
globalThis.CONTENT_W = 9360; // PAGE_W - 2 * MARGIN
24+
25+
// 6 MB raw ≈ 8 MB base64; reject above this to avoid sandbox OOM.
26+
const _MAX_IMG_B64 = 8 * 1024 * 1024;
27+
28+
/**
29+
* getFileBase64(fileId) — load a workspace file as a full data URI string.
30+
* Returns the complete "data:image/png;base64,..." string.
31+
* Use addImage() rather than passing this directly to ImageRun.
32+
*/
33+
globalThis.getFileBase64 = async function getFileBase64(fileId) {
34+
if (!fileId || typeof fileId !== 'string') {
35+
throw new Error('getFileBase64: fileId must be a non-empty string');
36+
}
1937
const res = await globalThis.__brokers.workspaceFile({ fileId });
38+
if (!res || !res.dataUri) {
39+
throw new Error('getFileBase64: broker returned no data for file ' + fileId);
40+
}
41+
if (res.dataUri.length > _MAX_IMG_B64) {
42+
throw new Error(
43+
'getFileBase64: image exceeds the 6 MB embed limit (~8 MB base64). Use a smaller/compressed image.'
44+
);
45+
}
2046
return res.dataUri;
2147
};
48+
49+
/**
50+
* addImage(fileId, opts) — fetch a workspace file and return a docx.ImageRun.
51+
* Required opts: width, height (pixels or EMUs via transformation option).
52+
* Example:
53+
* new docx.Paragraph({ children: [await addImage('abc123', { width: 200, height: 100 })] })
54+
*/
55+
globalThis.addImage = async function addImage(fileId, opts) {
56+
if (!opts || opts.width == null || opts.height == null) {
57+
throw new Error('addImage: opts must include width and height (in pixels)');
58+
}
59+
const dataUri = await globalThis.getFileBase64(fileId);
60+
const comma = dataUri.indexOf(',');
61+
if (comma === -1) throw new Error('addImage: invalid data URI (no comma separator)');
62+
const header = dataUri.slice(0, comma);
63+
const base64 = dataUri.slice(comma + 1);
64+
const mime = header.split(';')[0].replace('data:', '');
65+
const extMap = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/jpg': 'jpg', 'image/gif': 'gif', 'image/bmp': 'bmp', 'image/svg+xml': 'svg' };
66+
const ext = extMap[mime];
67+
if (!ext) throw new Error('addImage: unsupported image type "' + mime + '". Use PNG, JPEG, GIF, BMP, or SVG.');
68+
if (!globalThis.Buffer) throw new Error('addImage: Buffer polyfill missing — ensure docx bundle is loaded');
69+
const { width, height, type: _t, data: _d, transformation: userTransform, ...passThrough } = opts;
70+
return new globalThis.docx.ImageRun(Object.assign(passThrough, {
71+
data: globalThis.Buffer.from(base64, 'base64'),
72+
type: ext,
73+
transformation: Object.assign({ width, height }, userTransform || {}),
74+
}));
75+
};
2276
`,
2377
// JSZip's browser build doesn't support nodebuffer output, so we go through
2478
// base64 and decode back to bytes inside the isolate (avoids DataURL / Blob).

apps/sim/sandbox-tasks/pdf-generate.ts

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,71 @@ export const pdfGenerateTask = defineSandboxTask<SandboxTaskInput>({
1212
if (!PDFLib) throw new Error('pdf-lib bundle not loaded');
1313
globalThis.PDFLib = PDFLib;
1414
globalThis.pdf = await PDFLib.PDFDocument.create();
15-
globalThis.embedImage = async (dataUri) => {
15+
16+
// Convenience shortcuts — avoids verbose PDFLib.rgb() / PDFLib.StandardFonts.Helvetica
17+
globalThis.rgb = PDFLib.rgb;
18+
globalThis.StandardFonts = PDFLib.StandardFonts;
19+
20+
// Page-size constants in points (1pt = 1/72 inch)
21+
globalThis.LETTER = [612, 792]; // 8.5" × 11"
22+
globalThis.A4 = [595.28, 841.89]; // 210mm × 297mm
23+
24+
// 6 MB raw ≈ 8 MB base64; reject above this to avoid sandbox OOM.
25+
const _MAX_IMG_B64 = 8 * 1024 * 1024;
26+
27+
/**
28+
* embedImage(dataUri) — embed a data-URI image into the active PDF document.
29+
* Dispatches to embedPng or embedJpg based on MIME type.
30+
*/
31+
globalThis.embedImage = async function embedImage(dataUri) {
32+
if (!dataUri || typeof dataUri !== 'string') {
33+
throw new Error('embedImage: dataUri must be a non-empty string');
34+
}
1635
const comma = dataUri.indexOf(',');
36+
if (comma === -1) throw new Error('embedImage: invalid data URI (no comma separator)');
1737
const header = dataUri.slice(0, comma);
1838
const base64 = dataUri.slice(comma + 1);
19-
const binary = globalThis.Buffer ? globalThis.Buffer.from(base64, 'base64') : null;
20-
if (!binary) throw new Error('Buffer polyfill missing');
39+
if (!globalThis.Buffer) throw new Error('embedImage: Buffer polyfill missing');
40+
const binary = globalThis.Buffer.from(base64, 'base64');
2141
const mime = header.split(';')[0].split(':')[1] || '';
22-
if (mime.includes('png')) return globalThis.pdf.embedPng(binary);
23-
return globalThis.pdf.embedJpg(binary);
42+
// image/jpg is non-standard but tolerated; the canonical MIME is image/jpeg
43+
if (mime === 'image/png') return globalThis.pdf.embedPng(binary);
44+
if (mime === 'image/jpeg' || mime === 'image/jpg') return globalThis.pdf.embedJpg(binary);
45+
throw new Error('embedImage: only PNG and JPEG are supported (got ' + (mime || 'unknown — check data URI header') + ')');
2446
};
25-
globalThis.getFileBase64 = async (fileId) => {
47+
48+
/**
49+
* getFileBase64(fileId) — load a workspace file as a data URI string.
50+
*/
51+
globalThis.getFileBase64 = async function getFileBase64(fileId) {
52+
if (!fileId || typeof fileId !== 'string') {
53+
throw new Error('getFileBase64: fileId must be a non-empty string');
54+
}
2655
const res = await globalThis.__brokers.workspaceFile({ fileId });
56+
if (!res || !res.dataUri) {
57+
throw new Error('getFileBase64: broker returned no data for file ' + fileId);
58+
}
59+
if (res.dataUri.length > _MAX_IMG_B64) {
60+
throw new Error(
61+
'getFileBase64: image exceeds the 6 MB embed limit (~8 MB base64). Use a smaller/compressed image.'
62+
);
63+
}
2764
return res.dataUri;
2865
};
66+
67+
/**
68+
* drawImage(page, fileId, opts) — fetch a workspace file and draw it on the given page.
69+
* Required opts: x, y, width, height (points).
70+
* Example: await drawImage(page, 'abc123', { x: 50, y: 700, width: 200, height: 100 });
71+
*/
72+
globalThis.drawImage = async function drawImage(page, fileId, opts) {
73+
if (!opts || opts.x == null || opts.y == null || opts.width == null || opts.height == null) {
74+
throw new Error('drawImage: opts must include x, y, width, and height (in points)');
75+
}
76+
const dataUri = await globalThis.getFileBase64(fileId);
77+
const img = await globalThis.embedImage(dataUri);
78+
page.drawImage(img, opts);
79+
};
2980
`,
3081
finalize: `
3182
const pdf = globalThis.pdf;

apps/sim/sandbox-tasks/pptx-generate.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,52 @@ export const pptxGenerateTask = defineSandboxTask<SandboxTaskInput>({
1111
const PptxGenJS = globalThis.__bundles['pptxgenjs'];
1212
if (!PptxGenJS) throw new Error('pptxgenjs bundle not loaded');
1313
globalThis.pptx = new PptxGenJS();
14-
globalThis.getFileBase64 = async (fileId) => {
14+
globalThis.pptx.layout = 'LAYOUT_16x9';
15+
16+
// Slide geometry for LAYOUT_16x9 (inches)
17+
globalThis.SLIDE_W = 10;
18+
globalThis.SLIDE_H = 5.625;
19+
globalThis.MARGIN = 0.5;
20+
globalThis.CONTENT_W = 9; // SLIDE_W - 2 * MARGIN
21+
globalThis.CONTENT_H = 3.8; // usable body height below a standard title row
22+
23+
// ── Image helpers ──────────────────────────────────────────────────────────
24+
// 6 MB raw ≈ 8 MB base64; reject above this to avoid sandbox OOM.
25+
const _MAX_IMG_B64 = 8 * 1024 * 1024;
26+
27+
/**
28+
* getFileBase64(fileId) — load a workspace file as a data URI string.
29+
* PptxGenJS data format: "image/png;base64,<data>" (no "data:" prefix).
30+
* Use as: slide.addImage({ data: await getFileBase64(fileId), x, y, w, h })
31+
*/
32+
globalThis.getFileBase64 = async function getFileBase64(fileId) {
33+
if (!fileId || typeof fileId !== 'string') {
34+
throw new Error('getFileBase64: fileId must be a non-empty string');
35+
}
1536
const res = await globalThis.__brokers.workspaceFile({ fileId });
16-
return res.dataUri;
37+
if (!res || !res.dataUri) {
38+
throw new Error('getFileBase64: broker returned no data for file ' + fileId);
39+
}
40+
if (res.dataUri.length > _MAX_IMG_B64) {
41+
throw new Error(
42+
'getFileBase64: image exceeds the 6 MB embed limit (~8 MB base64). Use a smaller/compressed image.'
43+
);
44+
}
45+
// PptxGenJS expects "image/png;base64,..." — strip the leading "data:" if present
46+
return res.dataUri.replace(/^data:/, '');
47+
};
48+
49+
/**
50+
* addImage(slide, fileId, opts) — fetch a workspace file and embed it.
51+
* Required opts: x, y, w, h (inches).
52+
* Example: await addImage(slide, 'abc123', { x: 0.5, y: 1, w: 2, h: 1 });
53+
*/
54+
globalThis.addImage = async function addImage(slide, fileId, opts) {
55+
if (!opts || opts.x == null || opts.y == null || opts.w == null || opts.h == null) {
56+
throw new Error('addImage: opts must include x, y, w, and h (in inches)');
57+
}
58+
const data = await globalThis.getFileBase64(fileId);
59+
slide.addImage(Object.assign({}, opts, { data }));
1760
};
1861
`,
1962
finalize: `

0 commit comments

Comments
 (0)