Repository navigation
Description
Description
Clicking a local Markdown link whose target contains a malformed percent escape can throw an unhandled URIError from the Electron main-process link handler.
Minimal input:
[broken local link](bad%zz.md)%20 is valid percent encoding, but %zz is not. MarkText currently treats the link as a local path and calls decodeURIComponent() on it without catching URIError. As a result, clicking the link produces an unhandled main-process exception instead of a controlled user-facing "cannot open this location" behavior.
This is not specific to %zz; any malformed percent escape in a local link target can trigger the same path, for example %, %E0%A4%A, or other incomplete/invalid percent-encoded sequences.
Steps to Reproduce
Create a Markdown file with this content:
Ctrl-click the broken local link in MarkText on Windows:
[broken local link](bad%zz.md)Then:
- Start MarkText desktop from the repository with
pnpm run dev. - Open the Markdown file in the desktop window.
- On Windows/Linux, hold
Ctrland clickbroken local link. On macOS, holdCmdand click it. - Check the terminal/main-process output.
Expected Behavior
MarkText should handle the malformed local link target without throwing an unhandled exception.
Reasonable outcomes would include:
- show a controlled "cannot open location" notification/dialog,
- offer to create the missing local file,
- safely ignore the click, or
- treat the target as a literal filesystem path only if that is intentional and safe.
For comparison, Typora handles the same [broken local link](bad%zz.md) input as a failed local-file navigation and shows a user-facing "Cannot open location ... would you want to create file ..." prompt instead of throwing.
Actual Behavior
MarkText logs an unhandled main-process exception:
22:56:48.238 > Unhandled URIError: URI malformed
at decodeURIComponent (<anonymous>)
at IpcMainImpl.<anonymous> (<repo>\packages\desktop\out\main\index.js:19896:31)
at IpcMainImpl.emit (node:events:509:28)
at Session.<anonymous> (node:electron/js2c/browser_init:2:115331)
at Session.emit (node:events:509:28)
The generated out/main/index.js bundle location maps back to the source call to decodeURIComponent(pathname) in packages/desktop/src/main/menu/actions/file.ts.
Screenshots / Logs
Main-process log:
Unhandled URIError: URI malformed
at decodeURIComponent (<anonymous>)
at IpcMainImpl.<anonymous> (<repo>\packages\desktop\out\main\index.js:19896:31)
The issue appears to be in the desktop main-process handler for document link clicks:
packages/desktop/src/main/menu/actions/file.ts:578
The relevant flow is:
- Muya emits
format-clickfor modifier-clicked links. This is gated byisModifierClick()inpackages/muya/src/editor/linkMouseEvents.ts:60-61and emitted aroundpackages/muya/src/editor/linkMouseEvents.ts:149-182. - The desktop renderer receives
format-clickinpackages/desktop/src/renderer/src/components/editorWithTabs/editor.vue:1867-1875and forwards the link toeditorStore.FORMAT_LINK_CLICK(). - The link popover's jump/open action can reach the same path through
jumpClickinpackages/desktop/src/renderer/src/components/editorWithTabs/editor.vue:826-829, called frompackages/muya/src/ui/linkTools/index.ts:173-174. FORMAT_LINK_CLICK()handles local#anchorlinks in the renderer, but sends all other links to the main process throughmt::format-link-clickinpackages/desktop/src/renderer/src/store/editor.ts:406-431.- The main-process listener in
packages/desktop/src/main/menu/actions/file.ts:578-625handles the link target:bad%zz.mdis not an HTTP(S) URL, so it does not go toshell.openExternal().- it is not another explicit
scheme://URL, so it is not rejected by the scheme guard. - it has no whitespace, so it does not trigger the existing "Links cannot contain spaces" notification.
- it is treated as a local path and joined with
dirnameif relative.
- The handler then calls:
pathname = path.normalize(decodeURIComponent(pathname))at packages/desktop/src/main/menu/actions/file.ts:617.
For a target such as bad%zz.md, decodeURIComponent() throws URIError: URI malformed. There is no try/catch around this call, so the IPC event handler throws synchronously in the main process.
The original reason for this decode appears to be valid CommonMark-style percent-encoded local file names, for example bad%20name.md. That valid use case should still work, but malformed percent escapes need a guarded path. Since % is also a valid character in local filenames on Windows, treating the original target as a literal local path after decode failure may be more user-friendly than rejecting it outright, as long as the behavior is deliberate and does not throw.
A targeted fix would be to wrap the decode step and handle URIError by showing a controlled notification/dialog or falling back to a deliberate literal-path behavior.
Potential regression coverage:
[valid encoded space](bad%20name.md)
[malformed percent](bad%zz.md)
[bare percent](%.md)
[incomplete UTF-8 escape](%E0%A4%A.md)The malformed cases should not throw from mt::format-link-click, and the valid encoded-space case should keep working.
Reference comparison screenshot:
Operating System
Windows
MarkText Version
0.20.0-beta.2
OS Version
Windows 11 Pro
Reproducibility
- I can reliably reproduce this issue
Activity
fix: cherry-pick seven upstream fixes; cure state-clone aliasing