Skip to content

fix(desktop): confirm before opening executable link targets (code execution) - #4843

Merged
Jocs merged 5 commits into
developfrom
fix/3575-dangerous-link-exec
Jul 4, 2026
Merged

Jocs merged 5 commits into
developfrom
fix/3575-dangerous-link-exec

Conversation

@Jocs

@Jocs Jocs commented Jul 4, 2026

Copy link
Copy Markdown
Member

Problem

Fixes #3575

Clicking a normal markdown link that points at a co-located local file executed the file with no confirmation. On Windows, a malicious note could ship a payload beside it:

untrusted.md   ->  [open me](./update.js)
update.js      ->  WSH JScript payload (ActiveXObject ...)

Clicking the link ran mt::format-link-click → shell.openPath('update.js') → wscript.exe executed the script with no prompt — silent code execution just from opening a document and clicking a link.

Root cause

packages/desktop/src/main/menu/actions/file.ts (mt::format-link-click): for a local, non-markdown target the handler called shell.openPath(pathname) unconditionally. shell.openPath hands the file to the OS default handler, which for .js/.vbs/.wsf/.bat/.exe/… means run it.

Fix

  1. New pure helper isDangerousExecutableFile (+ DANGEROUS_EXECUTABLE_EXTENSIONS) in common/filesystem/paths.ts — flags OS-executable extensions (WSH scripts, native executables/installers, batch, PowerShell, shortcut/registry/JVM launchers). Case-insensitive, no filesystem access, so it can gate the decision before opening.
  2. The non-markdown branch of the link handler now shows a warning dialog (default Cancel) when the target is dangerous, and only calls shell.openPath if the user explicitly confirms. Markdown files, images, PDFs and other normal documents are unaffected.

Tests

  • New test/unit/specs/dangerous-executable-file.spec.ts covers the guard (WSH/native/PowerShell/shortcut extensions, case-insensitivity, absolute paths, and negative cases for docs/images/markdown/no-extension).
  • Full desktop unit suite (711 tests) passes; lint + typecheck clean.

The confirmation dialog wiring is verified by the unit-tested guard; the dialog itself is a thin Electron showMessageBox call.

🤖 Generated with Claude Code

Jocs and others added 2 commits July 4, 2026 16:10
Add a pure extension-based check (and the backing extension list) for files
the OS shell would execute as code — Windows Script Host scripts (js, vbs,
wsf, hta…), native executables/installers, batch, PowerShell, and shortcut/
registry/JVM launchers. Case-insensitive; no filesystem access so it can gate
a decision before the file is opened.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Clicking a markdown link to a co-located local file called shell.openPath
with no safety check. On Windows a link like `[open](./update.js)` next to an
untrusted document ran the script through wscript.exe with no prompt, giving
silent code execution.

Guard the non-markdown local-file branch of mt::format-link-click with
isDangerousExecutableFile and show a warning dialog (default Cancel) before
opening; the file is only handed to the OS shell if the user confirms.

Fixes #3575

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4843:

Run: https://github.com/marktext/marktext/actions/runs/28705517813

Artifact Size Link
marktext-windows-arm64 275.7 MB Download
marktext-linux 627.5 MB Download
marktext-macos-x64 293.0 MB Download
marktext-windows-x64 283.9 MB Download
marktext-macos-arm64 282.7 MB Download

@Jocs

Jocs commented Jul 4, 2026

Copy link
Copy Markdown
Member Author

Thanks for tackling this — the fix is clean and well-tested. Three points from review:

1. Trailing-dot / trailing-space filenames bypass the guard but still execute on Windows

isDangerousExecutableFile keys off path.extname(filepath).slice(1):

  • update.js. → path.extname returns "." → empty ext → not flagged
  • update.js (trailing space) → path.extname returns ".js " → "js " isn't in the list → not flagged

Windows strips trailing dots/spaces from the final path component during ShellExecute canonicalization, so shell.openPath still runs update.js. Both are reachable from a link: [x](./update.js.) needs no space, and the trailing-space variant is reachable via an angle-bracket–wrapped link <./update.js > (the space rejection in file.ts only applies to un-wrapped URLs). This defeats the guard on the exact platform the PR targets.

Suggest trimming trailing dots/spaces before extracting the extension, e.g.:

const ext = path.extname(filepath.replace(/[ .]+$/, '')).slice(1).toLowerCase()

2. The guard is Windows-only — the same silent-exec path is open on macOS/Linux

The vulnerable code path (shell.openPath on a link target) is cross-platform, but the extension list is entirely Windows. The identical attack survives elsewhere:

  • macOS: .command opens in Terminal and executes; .app bundles launch.
  • Linux: a .desktop file handed to xdg-open / gio can run arbitrary Exec= commands.

So [open me](./run.command) or ./launch.desktop reproduces #3575 off Windows. If limiting to Windows is intentional, could you note it explicitly? Otherwise it'd be good to cover at least .command / .app / .desktop.

3. Dialog strings should go through i18n

The new showMessageBox uses hardcoded English (title, message, detail, and the 'Cancel' / 'Open Anyway' buttons). This same file already localizes its other dialogs via t('dialog.…') (e.g. the save-changes dialog), and there's a dialog namespace across all 10 locale files in static/locales/. New keys (e.g. dialog.unsafeFileTitle / …Message / …Detail / openAnyway, reusing dialog.cancel) wired through t() would keep it consistent.

Jocs and others added 3 commits July 4, 2026 19:59
Two gaps from review:

- Trailing dot/space bypass: `update.js.` and `<./update.js >` (an angle-
  bracket link skips the space rejection) reach the guard with an extension of
  `.` / `js ` that isn't listed, yet Windows strips trailing dots/spaces during
  ShellExecute and still runs `update.js`. Trim trailing `[ .]+` before reading
  the extension.
- Windows-only coverage: the vulnerable shell.openPath path is cross-platform,
  so add macOS (`command`, `app`) and Linux (`desktop`, `appimage`, `run`)
  launchers — `[x](./run.command)` / `./launch.desktop` reproduced #3575 off
  Windows.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Add dialog.unsafeFileTitle / unsafeFileMessage / unsafeFileDetail ({name})
and dialog.openAnyway to the ten locale files so the #3575 confirmation
dialog is localized (getTranslation returns the raw key when one is missing).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
)

Route the dialog title/message/detail and buttons through t('dialog.…')
instead of hardcoded English, matching the other dialogs in this file.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Jocs

Jocs commented Jul 4, 2026

Copy link
Copy Markdown
Member Author

Thanks for the thorough review — all three addressed:

1. Trailing dot/space bypass. isDangerousExecutableFile now trims trailing [ .]+ before reading the extension:

const ext = path.extname(filepath.replace(/[ .]+$/, '')).slice(1).toLowerCase()

Added tests for update.js., update.js , payload.exe..., payload.bat (all flagged), plus note.md. / photo.png (still safe — trailing chars trimmed, real ext is safe).

2. Cross-platform coverage. Extended the list beyond Windows to macOS (command, app) and Linux (desktop, appimage, run), with a test asserting run.command / Foo.app / launch.desktop / App.AppImage / installer.run are flagged. Updated the comment/naming to reflect it's no longer Windows-only.

3. i18n. The dialog now goes through t('dialog.…') — new keys dialog.unsafeFileTitle / unsafeFileMessage / unsafeFileDetail ({name} interpolation) / openAnyway, reusing dialog.cancel. Added to all 10 locale files (getTranslation returns the raw key when one is missing, so every locale needed them).

Split into three atomic commits (guard hardening / i18n keys / dialog localization). Full desktop unit suite (714) + typecheck + lint green locally.

@Jocs
Jocs merged commit abd81c1 into develop Jul 4, 2026
9 checks passed
@Jocs
Jocs deleted the fix/3575-dangerous-link-exec branch July 4, 2026 12:07
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.

MarkText on Windows doesn't filter WSH JScript, which may cause code execution

1 participant