Skip to content

fix: escape HTML attributes in image tag construction to prevent XSS/RCE - #4206

Closed
andesyteoss wants to merge 1 commit into
marktext:developfrom
andesyteoss:fix/cwe79-imagectrl-html-acbb
Closed

andesyteoss wants to merge 1 commit into
marktext:developfrom
andesyteoss:fix/cwe79-imagectrl-html-acbb

Conversation

@andesyteoss

@andesyteoss andesyteoss commented May 14, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
Breaking changes? no
Deprecations? no
New tests added? not needed
Fixed tickets none
License MIT

Description

MarkText constructs <img> tags by interpolating image attributes (src, alt, title, etc.) directly into an HTML string without escaping. Because MarkText runs with nodeIntegration: true and contextIsolation: 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, the replaceImage and updateImage code paths build image HTML like this:

imageText += `${attr}="${value}" `

Neither attr nor value is 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.md with the following content:

![test" onerror="require('child_process').exec('calc')](invalid-url)

Open this file in MarkText. Because the alt attribute value test" onerror="require('child_process').exec('calc') is interpolated without escaping, the resulting HTML becomes:

<img alt="test" onerror="require('child_process').exec('calc')" src="invalid-url" >

The image fails to load (invalid-url), onerror fires, and calc.exe (or any arbitrary command) executes. On macOS/Linux, substitute with open /System/Applications/Calculator.app or xcalc.

This works because MarkText's Electron renderer has nodeIntegration: true, so require('child_process') is available in the DOM context.

Fix

This PR adds an escapeHtmlAttr helper 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

  • Verified that the PoC above no longer triggers code execution after the fix — the onerror payload is rendered as literal text inside the alt attribute.
  • Verified that normal images (local paths, URLs, data URIs, images with titles and alt text containing safe special characters like &) render correctly after escaping.
  • Verified that the escaping function handles non-string inputs gracefully by returning them unchanged.

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 .md file, 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.

@Jocs

Jocs commented May 14, 2026

Copy link
Copy Markdown
Member

@sebastiondev resolve conflicts first

@andesyteoss

Copy link
Copy Markdown
Contributor Author

Hi @Jocs, thanks for the feedback. I've resolved the merge conflicts locally by rebasing the fix onto the latest develop branch. The conflict was in src/muya/lib/contentState/imageCtrl.js — the escapeHtmlAttr helper and the arrow function signature have been reconciled with the upstream changes.

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!

UCHIHAHA103 pushed a commit to UCHIHAHA103/marktext that referenced this pull request May 14, 2026
…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
@Jocs

Jocs commented May 15, 2026

Copy link
Copy Markdown
Member

@sebastiondev Pls resolve the conflicts?

@Jocs

Jocs commented May 15, 2026

Copy link
Copy Markdown
Member

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.

Note: This PR currently has merge conflicts with the develop branch. If you decide to continue with this PR, please rebase or merge the latest develop branch to resolve the conflicts before it can be reviewed.

@andesyteoss

Copy link
Copy Markdown
Contributor Author

Hi @Jocs, thanks for your patience.

I've resolved the merge conflicts locally — the escapeHtmlAttr helper and imageCtrl function signature are reconciled with the latest develop branch after the electron-vite refactor (PR #4001).

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 develop, image attributes are still interpolated directly into the <img> tag without escaping (imageText += \${attr}="${value}" `), which allows specially crafted alt text or titles to break out of the attribute context. This PR adds the escapeHtmlAttr` helper to sanitize those values.

However, I'm hitting a GitHub OAuth scope limitation when force-pushing the rebased branch — the workflow scope is required because upstream's refactor changed .github/workflows/release.yml, and GitHub rejects the push without that scope. I'm working on resolving this and will update the branch shortly.

Apologies for the delay.


const imageCtrl = (ContentState) => {
// Escape special HTML characters in attribute values to prevent injection.
const escapeHtmlAttr = (str) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe you can use escapeHTML in codebase?

@andesyteoss

Copy link
Copy Markdown
Contributor Author

Hi @Jocs, thank you for the review feedback and the suggestion to use the existing escapeHTML from the codebase — that's a great call, and I've adopted it. The custom escapeHtmlAttr helper has been replaced with import { escapeHTML } from '../utils', which escapes the same characters (&, <, >, ', ") and is already well-tested in the project.

Regarding the merge conflicts: I've resolved them locally and rebased onto the latest develop branch. The fix is minimal — just 3 lines changed in imageCtrl.js. However, GitHub is rejecting the force-push because the rebase includes upstream workflow file changes (.github/workflows/build.yml from PR #4001), and the OAuth token doesn't have the workflow scope required to push commits that touch workflow files.

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 develop, image attributes in updateImage and replaceImage are still interpolated directly into <img> tags without escaping, which allows crafted alt text or titles to break out of the attribute context (CWE-79).

Apologies for the delay on the push — will update as soon as the scope issue is resolved.

@Jocs

Jocs commented May 16, 2026

Copy link
Copy Markdown
Member

@sebastiondev pls resole the conflicts

@Jocs

Jocs commented May 16, 2026

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

1 similar comment
@Jocs

Jocs commented May 18, 2026

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

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