Skip to content

fix: sanitize and escape motion type names from G-code - #31

Merged
little-did-I-know merged 1 commit into
mainfrom
fix/escape-motion-type-strings
Oct 26, 2026
Merged

little-did-I-know merged 1 commit into
mainfrom
fix/escape-motion-type-strings

Conversation

@little-did-I-know

Copy link
Copy Markdown
Owner

Summary

Feature names read from ;TYPE:, ; FEATURE: and FibreSeeker ; X start headers flowed unsanitized into innerHTML and inline onchange handlers, so a crafted G-code file could inject markup/script into the page. This was a pre-existing pattern (Cura/S3D fallbacks); #30 added a third source. Follow-up to the review of #30.

Changes

  • parser: free-form type names are folded to a safe token (A-Z 0-9 _ space . / -, max 40 chars, fallback CUSTOM). Known mapped names are unchanged.
  • parser: Object.hasOwn for type-map lookups (; constructor start / ; __proto__ start are plain text); FibreSeeker header match requires a leading ; and no longer uses a quadratic regex; inset state is a local instead of a field on the layer object.
  • ui / stats-overlay: new escapeHtml; motion legend and pressure-advance table pass types to handlers via data-type / this.dataset.type instead of building JS strings; hover tooltip escapes the type.
  • tests: 16 new cases in test/parser-type-sanitization.test.js (hostile payloads on ;TYPE: and FibreSeeker paths, prototype keys, non-comment lines ending in "start", inset outer/inner logic, slash preservation).

Testing

  • node --test test/*.test.js: 365/365 pass (349 existing + 16 new). The new hostile-payload tests failed before the parser fix.
  • node build.js re-run; bundle committed and its script parses.
  • Not checked by hand in a browser.

Known limitation

A name made up entirely of non-ASCII characters (e.g. a localized feature name) collapses to CUSTOM, so such features share one type.

🤖 Generated with Claude Code

Feature names read from ;TYPE:, ; FEATURE: and FibreSeeker "start" headers
flowed unsanitized into innerHTML and inline handlers (DOM injection via a
crafted G-code file).

- parser: fold free-form type names to a safe token (A-Z 0-9 _ space . / -)
- parser: use Object.hasOwn for type-map lookups; require ';' for FibreSeeker
  headers; replace quadratic regex; track inset state in a local
- ui/stats-overlay: add escapeHtml; pass types to handlers via data-type
- tests: 16 new cases covering hostile payloads, prototype keys, insets

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@little-did-I-know

Copy link
Copy Markdown
Owner Author

Fixing some sanitization issues.

@little-did-I-know
little-did-I-know merged commit 6f03df2 into main Oct 26, 2026
2 checks passed
@little-did-I-know
little-did-I-know deleted the fix/escape-motion-type-strings branch October 26, 2026 13:31
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