Skip to content

fix(markdown): decode local image and link paths before reading disk - #821

Open
HsienW wants to merge 2 commits into
HarnessMD:mainfrom
HsienW:fix/markdown-local-path-decoding
Open

HsienW wants to merge 2 commits into
HarnessMD:mainfrom
HsienW:fix/markdown-local-path-decoding

Conversation

@HsienW

@HsienW HsienW commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

What & why

  • Markdown preview was handing URL-encoded paths straight to the filesystem. So ![QA](QA%20Shot.png) tried to read a file literally named QA%20Shot.png, which fails with ENOENT even though QA Shot.png is sitting right there. Worse, if both files exist, it quietly shows the wrong one.

  • The root cause is that we mix two kinds of strings: Markdown hrefs (URLs) and disk file names. This PR keeps them apart. Each URL path segment now gets decoded exactly once, right at the Markdown boundary. Relative .md links had the same bug, so they share the same decoder. On the disk side, extension checks now look at the real file name, so a # or ? in a name like QA#1.png no longer breaks image detection. MIME lookup also only checks the table's own keys now, so something like x.constructor no longer counts as an image.

  • If a path can't be decoded safely (a malformed escape like %zz, or an encoded /), we refuse it instead of falling back to the raw string. The image chip then shows the reason in every shipped locale instead of failing silently. A sync throw from the read call used to take down the whole preview pane. Now it settles into the same visible error state.

Closes: #820

Updated for 0.6.0

  • Rebased onto 0.6.0. The decoder, the disk-path helpers, the preview changes and all 15 regression tests carry over line for line. The only code that had to be redone is the IDE wiring: 0.6.0 rewrote IdePanel.tsx and the imports in ImagePreview.tsx, so the switch to isImageDiskPath / isSvgDiskPath now sits on the new image tab, the "view source" control and the "view image" button.
  • 0.6.0 ships 11 locales, up from the 3 this PR originally covered (English, Arabic, Simplified Chinese). The six markdownImage strings are now translated for all 11, so German, Spanish, French, Italian, Japanese, Korean, Brazilian Portuguese and Turkish are new in this update.
  • Traditional Chinese is not included yet, because zh-TW.json only arrives with feat(i18n): add Traditional Chinese (Taiwan) locale #914. Until both are in, a zh-TW user sees these six strings in English through the en fallback. I'll add the zh-TW strings in a follow-up PR.
  • Heads-up on Guardrail 6/6: Ask me puts the call on an Approvals card in ASK ME #897: a local trial merge shows both PRs append a new top-level block at the end of the same 11 locale files (rulesPanel there, markdownImage here). Git reports it as a conflict in each file, but there is nothing to choose between. Whichever lands second should keep both blocks.
  • Relative .md links now check the extension on the decoded file name, the one that actually opens, so a link like secrets.env?.md no longer opens secrets.env. One test was added for this.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Docs
  • Build / CI

Evidence

Before

The new tests running against the code currently on main: 14 failing (tests 34 / pass 20 / fail 14). Check out a space-named image and its literal-%20 twin each read their OWN bytes. That one shows the bug best. I re-ran this against 0.6.0 main and got the same result. The screenshots predate the link test added in this update; with it, the count on main is tests 35 / pass 20 / fail 15.

2026-11-04-053351

The one new test that passes here is what the parser actually hands the resolver. It only pins down what react-markdown hands us, so it's green on both sides by design.

After

Same command with the fix applied: tests 34 / pass 34 / fail 0. On the current branch, with the added link test, it is tests 35 / pass 35 / fail 0.

2026-11-04-053428

How I tested it

  • OS: Windows 11, Node v22.22.3
  • Steps (re-run on the 0.6.0 rebase):
    1. Ran the 16 new regression tests in test/ide-image.test.cjs against 0.6.0 main and confirmed that 15 of them fail. The 16th is the parser test described above, which passes on both sides by design. The failures are the missing functions and a byte comparison showing the wrong file was read.
    2. Ran node --test test/ide-image.test.cjs on this branch: 35/35 pass.
    3. Ran npm run test:focused with and without the change, then diffed the failure lists by file and test name. Before: 922 tests, 812 pass, 99 fail. After: 938 tests, 828 pass, 99 fail. The 99 failures are the exact same tests on both sides, and none of them is in the image or Markdown code. They're pre-existing on my Windows box.
    4. Ran npm run typecheck. Both typecheck:node and typecheck:web exit 0.
    5. Ran npm run build and git diff --check. Both pass.
    6. Checked all 11 markdownImage blocks against the English key set. Each has the same six keys and a real translation for every value.
    7. The tests use real react-markdown output, so they cover what the resolver actually receives, including %25 and %5C normalization, and not just hand-written inputs.

Checklist

  • Before and after evidence is attached above, under both headings.
  • npm run typecheck passes.
  • npm run test:focused passes. It does not pass on main either. No new failures: the same 99 pre-existing failures show up with and without this change (details above).
  • npm run build succeeds.
  • This PR is one change. Unrelated fixes belong in their own PR.
  • I read the diff myself before opening this, and there is no debug output,
    commented-out code, or unrelated formatting churn in it.
  • Any new UI derives from DESIGN.md / tokens.ts. No ad-hoc colors,
    spacing, or fonts.
  • If I added art, it's my own or compatibly licensed, and listed in
    ATTRIBUTION.md. Not applicable: no art was added.

- Decode each Markdown URL path segment exactly once before resolving workspace paths
- Share the decoder between local images and relative Markdown links
- Refuse malformed escapes and encoded separators without falling back to the raw path
- Treat a leading slash in Markdown links as the workspace root, matching images
- Judge disk paths by their real file name so `#` and `?` stay part of the name
- Stamp image MIME from the disk file name instead of the full absolute path
- Restrict image MIME lookup to the table's own extension keys
- Settle synchronous image read failures into the visible error state
- Show localized chip notes when a local image source is refused
- Cover parser output, same-name collisions, and disk MIME cases in tests
@HsienW
HsienW force-pushed the fix/markdown-local-path-decoding branch from 5896d15 to a1c08b7 Compare December 4, 2026 06:51
@HsienW

HsienW commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

/copilot-review claude-opus-5.5 medium

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Copilot review

Asked by @HsienW. Model: claude-opus-5.5. Effort: medium. Head: a1c08b712.

1. A ? in a relative .md link can open a file that isn't Markdown (src/renderer/src/markdown/mdLinks.ts, resolveMdLinkRel, around new lines 135–140)

resolveMdLinkRel decides a link is Markdown by checking the raw href with isRelativeMd. That regex only allows a #… suffix after .md/.markdown. The path it then opens comes from decodeHrefPath, which cuts at the first ? or #. So the string that was checked and the path that gets opened can differ.

react-markdown passes a literal ? through unchanged. Two examples with baseRel = 'docs/report.md':

  • [notes](what?-notes.md)
    • Before this PR it opened docs/what?-notes.md.
    • Now isRelativeMd passes, decodeHrefPath cuts at ?, and it opens docs/what.
  • [x](secrets.env?.md)
    • The link is clickable.
    • Clicking it calls onOpenMarkdownLink('docs/secrets.env'), so a non-Markdown file opens in the Markdown viewer.
    • Markdown written by an agent is treated as untrusted elsewhere in this PR. Here it can make a link look like a .md link while opening a different file.

Images don't have this problem, because resolveLocalImageSource checks the extension on the decoded last segment. Links should do the same:

  • Check the .md/.markdown extension on the decoded last segment from decodeHrefPath, the way images are handled.
  • Keep hasScheme as the guard against URLs with a scheme.
  • Add a test such as resolveMdLinkRel(base, 'secrets.env?.md') === null.

I found no other real problems:

  • The decoder rejects malformed % escapes and encoded ., .., /, \ and NUL.
  • The checks that use disk paths (IdePanel, fs.ts) are wired correctly.
  • settleImageRead keeps the existing .catch for errors after the read call.
  • All 11 locale files present on the base branch get the six new keys.

- check .md/.markdown on the decoded last segment, the name that is opened
- `secrets.env?.md` and `secrets.env#.md` no longer open secrets.env
- a query after a real .md (`notes.md?v=2`) now opens notes.md, as for images
- drop isRelativeMd, which no longer has a caller
- add a regression test for links whose href and opened path disagree
@HsienW

HsienW commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

/copilot-review claude-opus-5.5 medium

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Copilot review

Asked by @HsienW. Model: claude-opus-5.5. Effort: medium. Head: 77a40c000.

No blocking problems found. I checked the decoding and link/image resolution against the base code and the call sites (IdePanel, MarkdownReview, fs.ts → safeResolve, and useWorkspaceImage), and found no bugs, regressions or security holes.

One case looked risky at first. An encoded drive letter, such as ![x](/C%3A/Windows/x.png), now gets past hasScheme because the scheme check runs before decoding. It still can't escape the workspace: the main process runs every read through lexicalJoin and safeResolve, which reject any path outside it.

This branch has not been deployed

No deployments
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.

Markdown preview can't load local images (or .md links) whose path has a %-encoded character like %20

1 participant