fix: support formatter functions in ECharts code blocks#6767
fix: support formatter functions in ECharts code blocks#6767octo-patch wants to merge 1 commit intolabring:mainfrom
Conversation
…g#6536) When an ECharts config contains formatter or other function values, json5.parse() fails silently and the chart renders as a blank box. Add a fallback that evaluates the config as a JavaScript expression using the Function constructor, enabling formatter callbacks to work. json5.parse() is still attempted first as it is safer for standard JSON5 configs.
|
octo-patch seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
1 similar comment
|
octo-patch seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
✅ Build Successful - Preview code-sandbox Image for this PR: |
|
✅ Build Successful - Preview fastgpt Image for this PR: |
|
✅ Build Successful - Preview mcp_server Image for this PR: |
|
Please confirm whether there is a risk of injection attacks in this approach. |
|
@c121914yu Yes — The practical risk depends on the trust model for ECharts code-block content:
If you'd prefer a safer fix, I can switch to one of:
Happy to update the PR to whichever direction you prefer, or close it if you'd rather not widen the surface at all. My preference would be option 1 (AST guard) — it covers most formatter callbacks people write while keeping the dangerous eval features off-limits. Let me know which way you'd like to go. |
Fixes #6536
Problem
When an ECharts configuration contains
formatter(or other function-valued properties),json5.parse()throws aSyntaxErrorbecause function literals are not valid JSON5. The error is silently caught,optionbecomesundefined, andsetOption({})is called — the chart renders as a blank white box with no error shown.Solution
Add a fallback parser that evaluates the ECharts config as a JavaScript expression using the
Functionconstructor. This allows formatter callbacks and other JS function values to work correctly.json5.parse()is still tried first (safer, handles comments/trailing commas)Functionconstructor fallback is usedbuildOptionhelper is extracted to avoid duplication between the two pathsTesting
Using an ECharts code block with a
formatterfunction (as in the issue report) now renders the chart correctly instead of showing a blank box.