Render inline SVG diagrams to hosted images when cross-posting - #7
Merged
Merged
Conversation
Dev.to (Forem) and Hashnode sanitise raw HTML and reject inline <svg>: a post whose body contains SVG diagrams fails to syndicate (Dev.to answers 403) or loses the figure. Instead of dropping diagrams, the body sent to a target now has each inline <svg> rendered to a PNG, hosted on the site, and replaced with a normal markdown image, so the diagram actually shows on the cross-post. The stored post body is never changed; only the outgoing copy is transformed, in the shared Syndicator path so both targets benefit. - SvgRasterizer (behind a Rasterizer interface) shells out to rsvg-convert via an argv array (no shell), rendering onto a solid light background with a dark ink for currentColor so currentColor-based diagrams stay legible on any platform theme; declared colours are untouched. Author SVG is untrusted: scripts, event handlers, foreignObject, DOCTYPE/entities, and external refs are refused, size- and time-capped, and the SVG is isolated in a private temp dir. librsvg runs no scripts and fetches no network. - Content-addressed hosting (UploadsDiagramStore): a diagram is keyed by the hash of its SVG and stored under uploads/<pluginId>/diagrams/, so it renders once and re-syndication reuses it. The file is the cache, so there is no table and nothing to migrate. - DiagramInliner leaves fenced code blocks and prose untouched. Any render or host failure degrades to a short pointer to the canonical original, so a hiccup is a working post, not a 403. - A target 403 is now translated to a clear "rejected the body, likely raw HTML or SVG" message, so an agent is not misled into blaming the API key. Rendering needs rsvg-convert (librsvg2-bin) plus a base font in the image; without it, diagrams fall back to the pointer. README documents this. Tests: DiagramInliner (svg -> hosted image, fenced code + prose preserved, render-once, render/host failure -> canonical pointer), SvgRasterizer safety guard (a stub proves safe SVG renders while script/handler/foreignObject/ doctype/external-href/oversize/non-svg are refused, and a missing binary degrades to null), the Syndicator applies the transform to the outgoing body, and the Dev.to/Hashnode 403 translations. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Flagged by the danmat.dev portfolio agent: syndicating a post whose body has a raw inline
<svg>diagram fails with HTTP 403 from Dev.to (Forem sanitises body HTML and rejects inline SVG; Hashnode has the same limitation). Confirmed by probing the Dev.to API directly: the body with<svg>blocks stripped posts 201; with them, 403.What this does
Instead of dropping the diagrams, the body sent to a target now has each inline
<svg>rendered to a PNG, hosted on the site, and replaced with a normal markdown image, so the diagram actually shows on Dev.to and Hashnode. Done in the sharedSyndicatorpath, so both targets benefit. The stored post body on the site keeps its inline SVG unchanged.How
SvgRasterizer(behind aRasterizerinterface) shells out torsvg-convertvia an argv array (no shell). Renders at ~1200px onto a solid light background with a dark ink forcurrentColor(declared colours like the accent hex untouched), so acurrentColordiagram stays legible on any platform's light/dark theme.UploadsDiagramStore): keyed by the SVG's hash, stored underuploads/nimbuscms.blog/diagrams/<ab>/<hash>.png. The file is the cache, so a diagram renders once and re-syndication reuses it. No DB table, nothing to migrate.DiagramInlinerleaves fenced code blocks and prose untouched; alt text from the SVG'saria-label, else "Diagram".Security
Author SVG is treated as untrusted:
<script>, event handlers,foreignObject,DOCTYPE/entities (XXE), and any externalhref(afile:/remote ref is a local-file-read / SSRF vector, since the rendered pixels get hosted publicly) are refused; size- and time-capped; SVG isolated in a private temp dir; librsvg runs no scripts and fetches no network. A refused SVG falls back to the pointer.Requires (image)
Rendering needs
rsvg-convert(librsvg2-bin) plus a base font (fonts-dejavu-core) in the image. Without it, diagrams fall back to the pointer, so the plugin still works everywhere. Documented in the README.Verified
Rasterized the real diagram from
how-i-stopped-an-llm-from-fabricating-citations(via the app's PDO path, valid UTF-8): renders cleanly, text crisp,currentColor→ dark ink, accent amber preserved, solid paper background. The SVG passes the safety guard (no scripts/foreignObject/external refs).Tests
DiagramInlinerTest(svg → hosted image; fenced code + prose preserved; render-once; render/host failure → canonical pointer),SvgRasterizerTest(a stub binary proves safe SVG renders while script/handler/foreignObject/doctype/external-href/oversize/non-svg are all refused, and a missing binary degrades to null), aSyndicatortest that the transform reaches the outgoing body, and the Dev.to/Hashnode 403 translations. phpstan L6 + cs-fixer clean.🤖 Generated with Claude Code