Skip to content

Commit

Permalink
Link uri encoding, URL-escaping should be left alone inside the desti…
Browse files Browse the repository at this point in the history
…nation (#598)

By the Rule "URL-escaping should be left alone inside the destination", Reimplement
`normalizeLinkDestination` util function with splitting by URL escapings and concat them
  • Loading branch information
mym0404 committed Mar 19, 2024
1 parent 9c6b1af commit 8d07abc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 18 additions & 11 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,28 @@ String normalizeLinkLabel(String label) {
}

/// Normalizes a link destination, including the process of HTML characters
/// decoding and percent encoding.
/// decoding and percent encoding.
// See the description of these examples:
// https://spec.commonmark.org/0.30/#example-501
// https://spec.commonmark.org/0.30/#example-502
String normalizeLinkDestination(String destination) {
// Decode first, because the destination might have been partly encoded.
// For example https://spec.commonmark.org/0.30/#example-502.
// With this function, `foo%20bä` will be parsed in the following steps:
// 1. foo bä
// 2. foo bä
// 3. foo%20b%C3%A4
try {
destination = Uri.decodeFull(destination);
} catch (_) {}
return Uri.encodeFull(decodeHtmlCharacters(destination));
// Split by url escaping characters
// Concatenate them with unmodified URL-escaping.
// URL-escaping should be left alone inside the destination
// Refer: https://spec.commonmark.org/0.30/#example-502.

final regex = RegExp('%[0-9A-Fa-f]{2}');

return destination.splitMapJoin(
regex,
onMatch: (m) => m.match,
onNonMatch: (e) {
try {
e = Uri.decodeFull(e);
} catch (_) {}
return Uri.encodeFull(decodeHtmlCharacters(e));
},
);
}

/// Normalizes a link title, including the process of HTML characters decoding
Expand Down
6 changes: 5 additions & 1 deletion test/original/inline_images.unit
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
![Uh oh...]("onerror="alert('XSS'))

<<<
<p><img src="%22onerror=%22alert('XSS')" alt="Uh oh..." /></p>
<p><img src="%22onerror=%22alert('XSS')" alt="Uh oh..." /></p>
>>> URL-escaping should be left alone inside the destination
![](https://example/foo%2Fvar)
<<<
<p><img src="https://example/foo%2Fvar" alt="" /></p>

0 comments on commit 8d07abc

Please sign in to comment.