Repository navigation
fix: escape HTML attributes in image tag construction to prevent XSS/RCE - #4206
andesyteoss wants to merge 1 commit into
Conversation
|
@sebastiondev resolve conflicts first |
|
Hi @Jocs, thanks for the feedback. I've resolved the merge conflicts locally by rebasing the fix onto the latest However, I'm running into a tooling constraint that's preventing me from force-pushing the rebased branch right now. I'll get this sorted and update the PR shortly. Appreciate your patience! |
…ll position / XSS / contextIsolation - fix: 编辑菜单截图分割线仅 macOS 可见(Windows 不再出现双分割线) - fix(marktext#4093): 深色主题打开窗口预设背景色,消除白闪 - feat(marktext#4134): capitalizeAccelerator - 自定义快捷键修饰符大写修复 - fix(marktext#4146): 搜索打开文件后首次 muya 序列化不触发未保存状态 - fix(marktext#4075): 外部文件变更:已保存时静默加载,未保存时才提示 - fix(marktext#4152): 切换 Tab 保留滚动位置;打开文件光标定位到开头 - fix(marktext#4206): 图片属性 HTML 转义防 XSS/RCE - fix(marktext#4192): contextIsolation=true 防渲染进程调用 Node.js API
|
@sebastiondev Pls resolve the conflicts? |
|
This project recently merged a large refactor (electron-vite migration, PR #4001) that upgraded dependencies and fixed several issues. Please review this PR and let us know if it is still needed, or if the changes have already been addressed upstream.
|
|
Hi @Jocs, thanks for your patience. I've resolved the merge conflicts locally — the Regarding whether this PR is still needed: Yes, the XSS fix is still relevant. The upstream refactor did not address the HTML attribute escaping issue. On However, I'm hitting a GitHub OAuth scope limitation when force-pushing the rebased branch — the Apologies for the delay. |
|
|
||
| const imageCtrl = (ContentState) => { | ||
| // Escape special HTML characters in attribute values to prevent injection. | ||
| const escapeHtmlAttr = (str) => { |
There was a problem hiding this comment.
Maybe you can use escapeHTML in codebase?
|
Hi @Jocs, thank you for the review feedback and the suggestion to use the existing Regarding the merge conflicts: I've resolved them locally and rebased onto the latest I'm working on resolving the token scope issue. In the meantime, the intended change is straightforward: +import { escapeHTML } from '../utils'
// In updateImage:
- imageText += `${attr}="${value}" `
+ imageText += `${escapeHTML(attr)}="${escapeHTML(value)}" `
// In replaceImage:
- imageText += `${attr}="${value}" `
+ imageText += `${escapeHTML(attr)}="${escapeHTML(value)}" `Is this PR still needed? Yes — the upstream electron-vite refactor (PR #4001) did not address the HTML attribute escaping. On Apologies for the delay on the push — will update as soon as the scope issue is resolved. |
|
@sebastiondev pls resole the conflicts |
|
@copilot resolve the merge conflicts in this pull request |
1 similar comment
|
@copilot resolve the merge conflicts in this pull request |
Description
MarkText constructs
<img>tags by interpolating image attributes (src, alt, title, etc.) directly into an HTML string without escaping. Because MarkText runs withnodeIntegration: trueandcontextIsolation: false, any JavaScript that executes in the renderer process has full access to Node.js APIs. This means a crafted Markdown file can achieve remote code execution simply by being opened.Vulnerability details
CWE-79 (Cross-Site Scripting) → Remote Code Execution
In
src/muya/lib/contentState/imageCtrl.js, thereplaceImageandupdateImagecode paths build image HTML like this:Neither
attrnorvalueis escaped. A Markdown image with a crafted alt text or title can break out of the attribute context and inject arbitrary HTML attributes, including event handlers.Proof of Concept
Create a file called
exploit.mdwith the following content:Open this file in MarkText. Because the
altattribute valuetest" onerror="require('child_process').exec('calc')is interpolated without escaping, the resulting HTML becomes:The image fails to load (
invalid-url),onerrorfires, andcalc.exe(or any arbitrary command) executes. On macOS/Linux, substitute withopen /System/Applications/Calculator.apporxcalc.This works because MarkText's Electron renderer has
nodeIntegration: true, sorequire('child_process')is available in the DOM context.Fix
This PR adds an
escapeHtmlAttrhelper that escapes&,",<, and>in both attribute names and values before they are interpolated into the HTML string. This prevents breaking out of the quoted attribute context.The fix is applied at both call sites in
imageCtrl.js(lines 127 and 169) where image HTML is constructed.Testing
onerrorpayload is rendered as literal text inside thealtattribute.&) render correctly after escaping.Adversarial review
Before submitting, we considered whether existing protections would prevent exploitation. MarkText does not use a Content Security Policy that blocks inline event handlers, and the Electron configuration (
nodeIntegration: true,contextIsolation: false) means any injected JS runs with full Node.js privileges. The only precondition is that a user opens a malicious.mdfile, which is the core use case of the application — users routinely open Markdown files from external sources (downloads, git repos, shared documents). There is no sanitization layer between the Markdown parser output and the DOM insertion in this code path.