From 5f8df5e77a81432d16a9d72295efdb6ca71cc9cf Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Wed, 11 Nov 2026 19:04:59 +0200 Subject: [PATCH] fix(markdown): prevent double encoding of GFM autolink URLs Otzaria/tools#40 --- pkgs/markdown/CHANGELOG.md | 1 + .../inline_syntaxes/autolink_extension_syntax.dart | 14 +++++++++++++- .../test/extensions/autolink_extension.unit | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index a8ae6b82e8..ea3564288d 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -18,6 +18,7 @@ discarded are never allocated. * Reuse HTML escape converters and skip HTML entity matching for strings without ampersands, reducing allocations during inline and code-block parsing. * Sort footnote definitions by their original case-insensitive labels rather than URI-encoded HTML IDs, preserving first-reference order for Unicode and escaped labels. +* Preserve existing percent-encoded sequences in extended autolink destinations instead of double-encoding their percent signs; still escape invalid percent sequences. * Skip the regular expression in `EmojiSyntax`, `AutolinkSyntax`, `CodeSyntax`, and `ColorSwatchSyntax` when the character at the current position cannot start a match. This makes inline parsing faster, mostly on diff --git a/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart b/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart index ddc04b3116..6afa9e20e4 100644 --- a/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart +++ b/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart @@ -49,6 +49,7 @@ class AutolinkExtensionSyntax extends InlineSyntax { : super('($_linkPattern)|($_emailPattern)', caseSensitive: false); static final _trailingEntityPattern = RegExp(r'&[0-9a-z]+;$'); + static final _percentEscapePattern = RegExp('%[0-9A-Fa-f]{2}'); static final _emailOnly = RegExp( _emailPattern, @@ -122,7 +123,7 @@ class AutolinkExtensionSyntax extends InlineSyntax { } final anchor = Element.text('a', text) - ..attributes['href'] = Uri.encodeFull(destination); + ..attributes['href'] = _encodeHref(destination); parser ..addNode(anchor) @@ -131,6 +132,17 @@ class AutolinkExtensionSyntax extends InlineSyntax { return true; } + /// Preserve existing percent escapes. Encoding the whole URI would turn + /// an already escaped percent sign into a double-encoded URL. + static String _encodeHref(String destination) { + if (!destination.contains('%')) return Uri.encodeFull(destination); + return destination.splitMapJoin( + _percentEscapePattern, + onMatch: (match) => match.match, + onNonMatch: Uri.encodeFull, + ); + } + int _getConsumeLength(String text) { var excludedLength = 0; diff --git a/pkgs/markdown/test/extensions/autolink_extension.unit b/pkgs/markdown/test/extensions/autolink_extension.unit index 6599b0224e..9bf5c58cf4 100644 --- a/pkgs/markdown/test/extensions/autolink_extension.unit +++ b/pkgs/markdown/test/extensions/autolink_extension.unit @@ -24,3 +24,11 @@ https://example.com and ftp://example.com and awww.example.com and foo+bar-baz.q /www.a.b@c.d and /foo@bar.com and xhttp://a.b <<<

/www.a.b@c.d and /foo@bar.com and xhttp://a.b

+>>> percent-encoded characters are not double-encoded +https://example.com/%40foo?q=%26 and www.example.com/%e2%82%AC +<<< +

https://example.com/%40foo?q=%26 and www.example.com/%e2%82%AC

+>>> invalid percent-encoding and other characters are still encoded +https://example.com/%zz%4/%%41/fü +<<< +

https://example.com/%zz%4/%%41/fü