Skip to content

[Bug] Malformed percent-encoded local link throws an unhandled URIError in the main process #4749

Closed

Description

@Renakoni

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:

  1. Start MarkText desktop from the repository with pnpm run dev.
  2. Open the Markdown file in the desktop window.
  3. On Windows/Linux, hold Ctrl and click broken local link. On macOS, hold Cmd and click it.
  4. 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:

  1. show a controlled "cannot open location" notification/dialog,
  2. offer to create the missing local file,
  3. safely ignore the click, or
  4. 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:

  1. Muya emits format-click for modifier-clicked links. This is gated by isModifierClick() in packages/muya/src/editor/linkMouseEvents.ts:60-61 and emitted around packages/muya/src/editor/linkMouseEvents.ts:149-182.
  2. The desktop renderer receives format-click in packages/desktop/src/renderer/src/components/editorWithTabs/editor.vue:1867-1875 and forwards the link to editorStore.FORMAT_LINK_CLICK().
  3. The link popover's jump/open action can reach the same path through jumpClick in packages/desktop/src/renderer/src/components/editorWithTabs/editor.vue:826-829, called from packages/muya/src/ui/linkTools/index.ts:173-174.
  4. FORMAT_LINK_CLICK() handles local #anchor links in the renderer, but sends all other links to the main process through mt::format-link-click in packages/desktop/src/renderer/src/store/editor.ts:406-431.
  5. The main-process listener in packages/desktop/src/main/menu/actions/file.ts:578-625 handles the link target:
    • bad%zz.md is not an HTTP(S) URL, so it does not go to shell.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 dirname if relative.
  6. 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:

Image

Operating System

Windows

MarkText Version

0.20.0-beta.2

OS Version

Windows 11 Pro

Reproducibility

  • I can reliably reproduce this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions