Skip to content

Harden Two.load SVG imports against untrusted markup - #892

Draft
jonobr1 wants to merge 2 commits into
devfrom
codex/issue-797-svg-import-hardening
Draft

jonobr1 wants to merge 2 commits into
devfrom
codex/issue-797-svg-import-hardening

Conversation

@jonobr1

@jonobr1 jonobr1 commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Two.load() currently parses supplied markup inside a temporary element attached to document.head, allowing source content to affect the page before SVG interpretation. This change imports a restricted SVG drawing format through DOMPurify and a detached document, without inserting source markup into the page.

Addresses the loading path in #797. The report's separate renderer attribute-injection and SSRF severity claims remain unconfirmed; interpret() and programmatic renderer utilities retain their trusted-input contract.

Changes

  • Sanitize supported SVG elements and attributes, convert supported inline presentation styles into validated attributes, and restrict local paint, clipping, and <use> references to the imported document.
  • Retain embedded raster images by default; require allowExternalImages: true for HTTP(S) images. Resolve relative image URLs against the final SVG response URL.
  • Return sanitized detached callback nodes, with stable arrays for multiple roots. Isolate definitions between imports, detect circular references, and avoid adding partial scene content on failure.
  • Add options.onError and an error event for asynchronous failures; handle HTTP and network failures and support source URLs with query strings or no .svg suffix.
  • Preserve supported transforms, gradients, visibility inheritance, text, and clipping. Resolve currentColor and inherit within the imported style chain, forward styles through nested SVGs and rounded rectangles, and avoid applying paint opacity twice through groups. Fix manual matrix conversion, connect AutoCalculateImportedMatrices to the interpreter flag, and preserve referenced image URLs when expanding <use>.
  • Add local Playwright fixtures, regression coverage, and GitHub Actions for Chromium, Firefox, and WebKit. Update declarations, documentation, migration guidance, and generated bundles.
  • Make bundle metadata replacement literal so dependency template delimiters and trailing license comments do not break builds or the exported constructor.

Compatibility

  • Stylesheets and host-page computed styles no longer determine load() output. Move supported styling into inline presentation styles or attributes; use interpret() only for trusted DOM when page styles are required.
  • External images now require an explicit option. SVG data URLs, blob images, active content, and external SVG references are removed.
  • Markup must be well-formed XML. The standard Illustrator SVG 1.1 doctype is removed before parsing; custom DTDs and entities are rejected.
  • Callback nodes are sanitized and detached; multiple roots return an array instead of a live HTMLCollection.
  • The production bundle grows from approximately 50 KB to 65 KB gzipped with the sanitizer.

See the SVG import contract and migration guide.

Validation

Local validation, including the Copilot review fixes:

  • npm ci
  • npm run test:browser: 87 passed across Chromium, Firefox, and WebKit, including existing QUnit SVG interpreter regressions, an executable control for the previous injection behavior, hostile inline/fetched inputs across all renderers, pixel checks, image policy, reference isolation, callback lifecycle, failure recovery, blank fragments, italic text, nested and rounded-shape inheritance, imported color keywords and opacity, and normalized manual-transform bounds.
  • npx eslint .
  • npm run types:check
  • npm run docs:build
  • npm audit --omit=dev: zero findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Active SVG-import regressions lose text and inherited styles, produce incorrect opacity and geometry offsets, and turn empty fragments into unintended page requests.

7 open issues
What changed in this PR

This PR hardens Two.load() against untrusted SVG markup, addressing the loading path reported in #797 while retaining the trusted-input contract for interpret().

Changes:

  • Adds detached SVG sanitization, restricted references, and opt-in external images.
  • Updates import handling, error reporting, TypeScript declarations, and migration documentation.
  • Adds cross-browser regression coverage and CI, updates build processing, and regenerates the bundle.
File Description
wiki/​svg-import/​README.md Documents the import contract and migration steps.
wiki/​docs/​two/​README.md Updates loader options and trust boundaries.
utils/​file-sizes.json Records increased bundle sizes.
utils/​build.js Uses literal metadata replacement and reports build failures.
tests/​typescript/​index.ts Exercises loader options and callback types.
tests/​browser/​svg-import.spec.js Adds security and drawing regression coverage.
tests/​browser/​playwright.config.js Configures three browsers and local serving.
tests/​browser/​interpreter.html Hosts existing interpreter regressions.
tests/​browser/​import.html Provides the SVG import test page.
tests/​browser/​fixtures/​hostile.svg Supplies hostile markup test cases.
tests/​browser/​fixtures/​drawing.svg Supplies supported drawing feature cases.
src/​utils/​xhr.js Reports request failures and exposes response metadata.
src/​utils/​xhr.d.ts Updates request callback declarations.
src/​utils/​svg-import.js Implements sanitization and resource restrictions.
src/​utils/​svg-import.d.ts Declares SVG import utilities.
src/​utils/​interpret-svg.js Adapts interpretation for detached, isolated imports.
src/​utils/​dom.js Removes the live-DOM loading container.
src/​utils/​dom.d.ts Aligns declarations with remaining DOM helpers.
src/​two.js Integrates sanitized loading and error handling.
src/​two.d.ts Types loader options and sanitized callback nodes.
src/​events.js Adds the error event name.
src/​events.d.ts Declares the error event name.
README.md Introduces SVG loading security guidance.
package.json Adds sanitizer and browser-test dependencies.
package-lock.json Locks added dependencies.
build/​two.js Regenerates the distributable with import hardening.
.gitignore Excludes browser-test reports and artifacts.
.github/​workflows/​browser-tests.yml Runs browser regressions in GitHub Actions.

Review effort: Balanced

Comment thread src/two.js Resolved
Comment thread src/utils/interpret-svg.js Resolved
Comment thread src/utils/interpret-svg.js Resolved
Comment thread src/utils/interpret-svg.js Resolved
Comment thread src/utils/interpret-svg.js Resolved
Comment thread src/utils/svg-import.js Resolved
Comment thread src/utils/svg-import.js Resolved

This branch has not been deployed

No deployments
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.

2 participants