fix(common): replace .toString() with String() on DOMPurify.sanitize() calls#7439
fix(common): replace .toString() with String() on DOMPurify.sanitize() calls#7439ashishjain0512 wants to merge 1 commit intodevelopfrom
Conversation
…) calls Resolves #6113 DOMPurify.sanitize() can return DocumentFragment when config includes RETURN_DOM_FRAGMENT, which lacks a meaningful .toString(). Using String() wrapper avoids the @typescript-eslint/no-base-to-string lint rule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
✅ Deploy Preview for mermaid-js ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
🦋 Changeset detectedLatest commit: c62c074 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@mermaid-js/examples
mermaid
@mermaid-js/layout-elk
@mermaid-js/layout-tidy-tree
@mermaid-js/mermaid-zenuml
@mermaid-js/parser
@mermaid-js/tiny
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7439 +/- ##
=======================================
Coverage 3.53% 3.54%
=======================================
Files 491 490 -1
Lines 48972 48965 -7
Branches 766 766
=======================================
+ Hits 1733 1734 +1
+ Misses 47239 47231 -8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
knsv
left a comment
There was a problem hiding this comment.
[sisyphus-bot]
Thanks for picking up issue #6113, @ashishjain0512 — this lint error has been tripping up contributors for a while, so it's great to see it tackled. The approach is sound and the changeset is properly scoped.
What's working well
🎉 Correct fix for the lint rule. String() is exactly the right idiom here. @typescript-eslint/no-base-to-string fires because TypeScript knows DOMPurify.sanitize() can return string | DocumentFragment | Node depending on the config shape, and .toString() on those non-string types would silently produce "[object DocumentFragment]". Using String() satisfies the rule because it's an explicit global conversion rather than a method lookup on the object.
🎉 Minimal and focused. Two call sites changed, changeset included, PR description clearly explains the why. This is a well-scoped fix.
Things to consider before marking ready
💡 [suggestion] — Consistency with removeScript() (common.ts:62)
export const removeScript = (txt: string): string => {
setupDompurifyHooksIfNotSetup();
const sanitizedText = DOMPurify.sanitize(txt);
return sanitizedText;
};removeScript also calls DOMPurify.sanitize() and returns the result directly. The no-arg overload of DOMPurify does narrow the return type to string, so the lint rule probably does not fire here — but worth a quick check: run pnpm lint with the fix in place and confirm no residual warnings on this function. If it's clean, no action needed.
🟢 [nit] — Guard against misuse of RETURN_DOM_FRAGMENT in public config
If a user ever passes { dompurifyConfig: { RETURN_DOM_FRAGMENT: true } } in their Mermaid config, String(fragment) will silently produce "[object DocumentFragment]" rather than sanitized HTML — same behavior as the old .toString(), so no regression introduced here. But it's a footgun in the public API surface. A small JSDoc note on sanitizeText (or on the dompurifyConfig config schema key) warning that RETURN_DOM and RETURN_DOM_FRAGMENT must not be set would help future contributors. This is out of scope for this PR — just flagging it as a follow-up idea.
Security
No XSS or injection issues introduced. Sanitization happens in DOMPurify.sanitize() before the string conversion, and String() vs .toString() makes no difference to the sanitization result. Reviewed sanitizeText, sanitizeMore, and the DOMPurify hook setup — all intact.
Good work — this is on the right track. Once you've confirmed no residual lint warnings, this should be straightforward to land. Let's get it across the finish line!
Summary
Resolves #6113
.toString()withString()wrapper onDOMPurify.sanitize()calls incommon.tsto avoid@typescript-eslint/no-base-to-stringlint errorsDOMPurify.sanitize()can returnDocumentFragment(when config includesRETURN_DOM_FRAGMENT), which lacks a meaningful.toString()—String()is the safe alternativeTest plan
common.spec.ts)common.ts🤖 Generated with Claude Code