Skip to content

Editorial diagram design for Claude Code, Codex, GitHub Copilot, Cursor, Factory Droid, Qoder, and Pi. 45 diagram types. Self-contained HTML + SVG. No shadows. No Mermaid slop.

fix(drawio): validate and resolve geometry safely (#283)

Thomas Chauchefoin1d29cb2511c7
diff --git a/scripts/verify-drawio-import.py b/scripts/verify-drawio-import.py
index 087f098..0b08bc1 100644
--- a/scripts/verify-drawio-import.py
+++ b/scripts/verify-drawio-import.py
@@ -51,27 +51,25 @@ def normalize_newlines(text: str) -> str:
return text.replace("\r\n", "\n").replace("\r", "\n")
-def run_extract(args: list[str]) -> str:
- proc = subprocess.run(
+def invoke(args: list[str]) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
[sys.executable, str(EXTRACT), *args],
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
+
+
+def run_extract(args: list[str]) -> str:
+ proc = invoke(args)
if proc.returncode != 0:
fail(f"extractor exited {proc.returncode} for {args}: {proc.stderr.strip()}")
return proc.stdout
def expect_extract_error(args: list[str], message: str) -> None:
- proc = subprocess.run(
- [sys.executable, str(EXTRACT), *args],
- capture_output=True,
- text=True,
- encoding="utf-8",
- errors="replace",
- )
+ proc = invoke(args)
if proc.returncode != 2 or message not in proc.stderr:
fail(
f"expected exit 2 containing {message!r} for {args}; got "
@@ -520,6 +518,78 @@ def check_security_and_limits(tmp: Path) -> None:
f"cell limit exceeded (max {extractor.MAX_CELLS_PER_PAGE})",
)
+ for name, coordinate, diagnostic in (
+ ("nan", "NaN", "must be finite"),
+ ("infinity", "Infinity", "must be finite"),
+ ("derived-overflow", "1e308", "bounding box overflow"),
+ ):
+ malformed_geometry = tmp / f"{name}.drawio"
+ malformed_geometry.write_text(
+ '<mxGraphModel><root><mxCell id="node" value="Node" vertex="1">'
+ f'<mxGeometry x="{coordinate}" y="0" width="{coordinate}" height="10"/>'
+ '</mxCell></root></mxGraphModel>',
+ encoding="utf-8",
+ )
+ for output_args in ([], ["--json"]):
+ expect_extract_error(
+ [str(malformed_geometry), *output_args], diagnostic
+ )
+
+ canvas_span_overflow = tmp / "canvas-span-overflow.drawio"
+ canvas_span_overflow.write_text(
+ '<mxGraphModel><root>'
+ '<mxCell id="left" value="Left" vertex="1">'
+ '<mxGeometry x="-1e308" y="0" width="10" height="10"/></mxCell>'
+ '<mxCell id="right" value="Right" vertex="1">'
+ '<mxGeometry x="1e308" y="0" width="10" height="10"/></mxCell>'
+ '</root></mxGraphModel>',
+ encoding="utf-8",
+ )
+ for output_args in ([], ["--json"]):
+ expect_extract_error(
+ [str(canvas_span_overflow), *output_args], "canvas span overflow"
+ )
+
+ parent_position_overflow = tmp / "parent-position-overflow.drawio"
+ parent_position_overflow.write_text(
+ '<mxGraphModel><root>'
+ '<mxCell id="parent" value="Parent" vertex="1">'
+ '<mxGeometry x="1e308" y="0" width="10" height="10"/></mxCell>'
+ '<mxCell id="child" value="Child" vertex="1" parent="parent">'
+ '<mxGeometry x="1e308" y="0" width="10" height="10"/></mxCell>'
+ '</root></mxGraphModel>',
+ encoding="utf-8",
+ )
+ for output_args in ([], ["--json"]):
+ expect_extract_error(
+ [str(parent_position_overflow), *output_args], "page 0: geometry overflow"
+ )
+
+ deep_parents = tmp / "deep-parents.drawio"
+ deep_parents.write_text(
+ '<mxGraphModel><root>'
+ + ''.join(
+ f'<mxCell id="n{index}" value="Node" vertex="1" parent="n{index - 1}">'
+ '<mxGeometry x="1" y="1" width="1" height="1"/></mxCell>'
+ for index in range(1, 1101)
+ )
+ + '</root></mxGraphModel>',
+ encoding="utf-8",
+ )
+ deepest = json.loads(run_extract([str(deep_parents), "--json"]))["pages"][0]["nodes"][-1]
+ if (deepest["x"], deepest["y"], deepest["depth"]) != (1100, 1100, 1099):
+ fail(f"deep parent chain resolved incorrectly: {deepest}")
+
+ parent_cycle = tmp / "parent-cycle.drawio"
+ parent_cycle.write_text(
+ '<mxGraphModel><root>'
+ '<mxCell id="a" value="A" vertex="1" parent="b"/>'
+ '<mxCell id="b" value="B" vertex="1" parent="a"/>'
+ '</root></mxGraphModel>',
+ encoding="utf-8",
+ )
+ expect_extract_error([str(parent_cycle)], "parent cycle")
+
proc = subprocess.run(
[sys.executable, str(EXTRACT), str(FIXTURE), "--max-rows", "0"],
capture_output=True,
diff --git a/skills/diagram-design/scripts/drawio_extract.py b/skills/diagram-design/scripts/drawio_extract.py
index d517fd6..40984db 100644
--- a/skills/diagram-design/scripts/drawio_extract.py
+++ b/skills/diagram-design/scripts/drawio_extract.py
@@ -24,6 +24,7 @@ import argparse
import base64
import html
import json
+import math
import re
import struct
import sys
@@ -352,9 +353,12 @@ def _num(geom: ET.Element | None, key: str) -> float:
if geom is None:
return 0.0
try:
- return float(geom.get(key, "0") or 0)
- except ValueError:
+ value = float(geom.get(key, "0") or 0)
+ except (OverflowError, ValueError):
return 0.0
+ if not math.isfinite(value):
+ _fail(f"invalid geometry: {key} must be finite")
+ return value
def parse_page(diagram: ET.Element, index: int) -> Page:
@@ -462,18 +466,34 @@ def parse_page(diagram: ET.Element, index: int) -> Page:
node_map = page.node_map
- # Resolve absolute geometry + depth by walking the parent chain.
- def resolve(node: Node, seen: set[str]) -> tuple[float, float, int]:
- if node.id in seen:
- return node.x, node.y, 0
- seen.add(node.id)
- parent = node_map.get(node.parent or "")
- if parent is None:
- return node.x, node.y, 0
- px, py, pdepth = resolve(parent, seen)
- return node.x + px, node.y + py, pdepth + 1
+ # Resolve absolute geometry + depth iteratively and reuse resolved prefixes.
+ resolved_by_id: dict[str, tuple[float, float, int]] = {}
+ for node in page.nodes:
+ chain: list[Node] = []
+ active: set[str] = set()
+ current = node
+ while current.id not in resolved_by_id:
+ if current.id in active:
+ _fail(f"page {index}: parent cycle")
+ active.add(current.id)
+ chain.append(current)
+ parent = node_map.get(current.parent or "")
+ if parent is None:
+ position = (0.0, 0.0, -1)
+ break
+ current = parent
+ else:
+ position = resolved_by_id[current.id]
- resolved = [resolve(node, set()) for node in page.nodes]
+ px, py, pdepth = position
+ while chain:
+ current = chain.pop()
+ px, py, pdepth = current.x + px, current.y + py, pdepth + 1
+ if not math.isfinite(px) or not math.isfinite(py):
+ _fail(f"page {index}: geometry overflow")
+ resolved_by_id[current.id] = (px, py, pdepth)
+
+ resolved = [resolved_by_id[node.id] for node in page.nodes]
for node, (ax, ay, depth) in zip(page.nodes, resolved):
node.x, node.y, node.depth = ax, ay, depth
parent = node_map.get(node.parent or "")
@@ -710,14 +730,21 @@ def _escape_table(text: str) -> str:
def page_bounds(page: Page) -> tuple[float, float, float, float]:
boxes = [(n.x, n.y, n.x + n.w, n.y + n.h) for n in page.nodes if n.w and n.h]
+ if any(not math.isfinite(value) for box in boxes for value in box):
+ _fail("invalid geometry: bounding box overflow")
if not boxes:
return (0.0, 0.0, 0.0, 0.0)
- return (
+ bounds = (
min(b[0] for b in boxes),
min(b[1] for b in boxes),
max(b[2] for b in boxes),
max(b[3] for b in boxes),
)
+ if not math.isfinite(bounds[2] - bounds[0]) or not math.isfinite(
+ bounds[3] - bounds[1]
+ ):
+ _fail("invalid geometry: canvas span overflow")
+ return bounds
def digest(path: Path, pages: list[Page], selected: list[Page], max_rows: int) -> str: