Skip to content

Fix silent corruption on malformed 3MF triangle indices#102

Open
boinger wants to merge 1 commit into
CNCKitchen:mainfrom
boinger:fix/3mf-triangle-index-validation
Open

Fix silent corruption on malformed 3MF triangle indices#102
boinger wants to merge 1 commit into
CNCKitchen:mainfrom
boinger:fix/3mf-triangle-index-validation

Conversation

@boinger

@boinger boinger commented Jul 15, 2026

Copy link
Copy Markdown

parse3MF stores v1/v2/v3 into a Uint32Array before validating. The array coerces NaN→0 (and wraps negatives), so the isNaN and < 0 checks are unreachable: a missing or malformed index attribute becomes a valid-looking 0 and the triangle silently loads anchored at vertex 0 — a corrupt mesh headed for the slicer instead of the intended "Invalid triangle index in 3MF file" error.

Same spirit as #37's parser hardening. Fix: validate the raw attribute (digits-only, then range-check) before storing. This also rejects values parseInt silently truncates ("3abc" → 3) or reinterprets ("0x10" → 16). Valid files parse identically.

Repro — zip this as 3D/3dmodel.model (plus the usual [Content_Types].xml / _rels/.rels) and load it; note v2 is missing:

<?xml version="1.0"?>
<model unit="millimeter" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">
  <resources><object id="1" type="model"><mesh>
    <vertices><vertex x="0" y="0" z="0"/><vertex x="10" y="0" z="0"/><vertex x="0" y="10" z="0"/></vertices>
    <triangles><triangle v1="0" v3="2"/></triangles>
  </mesh></object></resources>
  <build><item objectid="1"/></build>
</model>
  • Before: loads without error; the triangle uses vertex 0 where v2 should be.
  • After: throws "Invalid triangle index in 3MF file".

Indices were parseInt-ed into a Uint32Array before validation. The array
coerces on write (NaN→0, negatives wrap), so the isNaN and <0 checks were
unreachable: a missing/malformed v1/v2/v3 loaded a triangle anchored at
vertex 0 instead of throwing. Validate raw attributes (digits-only,
range-checked) before storing. Valid files parse identically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant