fix: recognize percent-encoded octets in bare file-path links - #945
Open
csmcneill wants to merge 1 commit into
Open
fix: recognize percent-encoded octets in bare file-path links#945csmcneill wants to merge 1 commit into
csmcneill wants to merge 1 commit into
Conversation
Bare (non-file://) markdown links to local files fail whenever the path contains a space, because percent-encoding (%20) is the only CommonMark-valid way to represent a space in an unbracketed link destination, and FILE_PATH_TARGET_REGEX's character class didn't include `%`. The link falls through to being treated as a generic URL, where `new URL()` throws on a schemeless absolute path, producing a generic "malformed URL" error instead of opening the file. - Add a shared PATH_CHAR fragment (in linkify.ts) that accepts either a normal path character or a percent-encoded octet (%XX), and use it in both FILE_PATH_TARGET_REGEX and the sibling FILE_PATH_REGEX used for auto-linkifying bare paths in plain text. - Decode the matched path in resolveMarkdownLinkTarget (link-target.ts) before handing it to the file-open handler, mirroring the decode step resolveFileUrlPath already performs for file:// targets. - Add regression tests covering percent-encoded spaces, multiple octets in one path, and a negative case (bare unescaped % is still rejected). Fixes craft-ai-agents#944
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #944. Bare (non-
file://) markdown links to local files fail whenever the path contains a space, because percent-encoding (%20) is the only CommonMark-valid way to represent a space in an unbracketed link destination, andFILE_PATH_TARGET_REGEX's character class didn't include%. The link falls through to being classified as a generic URL, wherenew URL()throws on a schemeless absolute path — producingFailed to open URL: URL blocked. URL is malformed and cannot be parsed.instead of opening the file.PATH_CHARfragment inlinkify.tsthat accepts either a normal path character or a percent-encoded octet (%XX), used in bothFILE_PATH_TARGET_REGEXand the siblingFILE_PATH_REGEX(the auto-linkify pass for bare paths in plain text — same gap existed there).resolveMarkdownLinkTarget(link-target.ts) before handing it to the file-open handler, mirroring the decode stepresolveFileUrlPathalready performs forfile://targets.%alone (not followed by two hex digits) is still rejected — the fix matches valid percent-encoding grammar rather than loosening the character class to accept any%.Explicit
file:///...links are unaffected by this bug (already correct before this change) and continue to work the same way.Test plan
resolveMarkdownLinkTarget/classifyMarkdownLinkTarget, and a negative case confirming a bare unescaped%is still rejected.bun test src/components/markdown/__tests__/inpackages/ui— 158 pass, 0 fail (full markdown suite, no regressions).bun run tsc --noEmitinpackages/ui— clean.