') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Catch hanging figure payloads in parse step by etpinard · Pull Request #55 · plotly/orca · GitHub
Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
/orcaPublic archive
Merged
106 changes: 106 additions & 0 deletions src/component/plotly-graph/parse.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -93,6 +93,10 @@ function parse (body, _opts, sendToRenderer) {
result.width = parseDim(result, opts, 'width')
result.height = parseDim(result, opts, 'height')

if (willFigureHang(result)) {
return errorOut(400, 'figure data is likely to make exporter hang, rejecting request')
}

sendToRenderer(null, result)
}

Expand All@@ -108,4 +112,106 @@ function parseDim (result, opts, dim) {
}
}

function willFigureHang (result) {
const data = result.figure.data

// cap the number of traces
if (data.length > 200) return true

let maxPtBudget = 0

for (let i = 0; i < data.length; i++) {
const trace = data[i] || {}

// cap the number of points using a budget
maxPtBudget += estimateDataLength(trace) / maxPtsPerTrace(trace)
if (maxPtBudget > 1) return true
}
}

// Consider the array of maximum length as a proxy to determine
// the number of points to be drawn. In general, this estimate
// can be (much) smaller than the true number of points plotted
// when it does not match the length of the other coordinate arrays.
function findMaxArrayLength (cont) {
const arrays = Object.keys(cont)
.filter(k => Array.isArray(cont[k]))
.map(k => cont[k])

const lengths = arrays.map(arr => {
if (Array.isArray(arr[0])) {
// 2D array case
return arr.reduce((a, r) => a + r.length, 0)
} else {
return arr.length
}
})

return Math.max(0, ...lengths)
}

function estimateDataLength (trace) {
// special case for e.g. parcoords and splom traces
if (Array.isArray(trace.dimensions)) {
return trace.dimensions
.map(findMaxArrayLength)
.reduce((a, v) => a + v)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done

}

return findMaxArrayLength(trace)
}

function maxPtsPerTrace (trace) {
const type = trace.type || 'scatter'

switch (type) {
case 'scattergl':
case 'splom':
case 'pointcloud':
return 1e7

case 'scatterpolargl':
case 'heatmap':
case 'heatmapgl':
return 1e6

case 'scatter3d':
case 'surface':
return 5e5

case 'mesh3d':
if ('alphahull' in trace && Number(trace.alphahull) >= 0) {
return 1000
} else {
return 5e5
}

case 'parcoords':
return 5e5
case 'scattermapbox':
return 5e5

case 'histogram':
case 'histogram2d':
case 'histogram2dcontour':
return 1e6

case 'box':
if (trace.boxpoints === 'all') {
return 5e4
} else {
return 1e6
}
case 'violin':
if (trace.points === 'all') {
return 5e4
} else {
return 1e6
}

default:
return 5e4
}
}

module.exports = parse
190 changes: 190 additions & 0 deletions test/unit/plotly-graph_test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,6 +326,196 @@ tap.test('parse:', t => {
})
})

t.test('should not access figures that a likely to make renderer hang', t => {
t.test('failing svg scatter case', t => {
var x = new Array(1e6)

fn({
data: [{
type: 'scatter',
x: x
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('passing scattergl case', t => {
var x = new Array(1e6)

fn({
data: [{
type: 'scattergl',
x: x
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, null, 'code')
t.type(result.figure, 'object', 'figure type')
t.end()
})
})

t.test('failing parcoords case', t => {
var x = new Array(3e5)

fn({
data: [{
type: 'parcoords',
dimensions: [{
values: x
}, {
values: x
}]
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing heatmap case', t => {
var z = [
new Array(5e5),
new Array(5e5),
new Array(5e5)
]

fn({
data: [{
type: 'heatmap',
z: z
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing case from too many traces', t => {
var data = new Array(3e3)

fn({
data: data
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing edge case (box with boxpoints all)', t => {
fn({
data: [{
type: 'box',
x: new Array(1e5),
boxpoints: 'all'
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing edge case (violin with points all)', t => {
fn({
data: [{
type: 'violin',
x: new Array(1e5),
points: 'all'
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing edge case (mesh3d and alphahull)', t => {
var data = new Array(2e3)

fn({
data: [{
type: 'mesh3d',
x: data,
alphahull: 1
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing case from too many traces', t => {
var data = new Array(3e3)

fn({
data: data
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing edge case (to test budget)', t => {
fn({
data: [{
type: 'scatter',
x: new Array(4e4) // below 5e4 threshold
}, {
type: 'heatmap',
z: [
new Array(5e5), // below 5e4 threshold
new Array(4e5)
]
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing edge case (to test budget of edge cases)', t => {
fn({
data: [{
type: 'violin',
points: 'all',
x: new Array(4e4) // below 5e4 threshold
}, {
type: 'box',
boxpoints: 'all',
x: new Array(4e4) // below 5e4 threshold
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.test('failing case (with no arrays in starting trace)', t => {
fn({
data: [{}, {}, {
type: 'scatter',
x: new Array(1e6)
}]
}, {}, (errorCode, result) => {
t.equal(errorCode, 400, 'code')
t.type(result.msg, 'string', 'msg type')
t.end()
})
})

t.end()
})

t.end()
})

Expand Down