Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix image links with escaped brackets #1683

Merged
merged 2 commits into from May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Tokenizer.js
Expand Up @@ -9,22 +9,23 @@ const {
function outputLink(cap, link, raw) {
const href = link.href;
const title = link.title ? escape(link.title) : null;
const text = cap[1].replace(/\\([\[\]])/g, '$1');

if (cap[0].charAt(0) !== '!') {
return {
type: 'link',
raw,
href,
title,
text: cap[1]
text
};
} else {
return {
type: 'image',
raw,
text: escape(cap[1]),
href,
title
title,
text: escape(text)
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules.js
Expand Up @@ -198,7 +198,7 @@ inline.tag = edit(inline.tag)
.replace('attribute', inline._attribute)
.getRegex();

inline._label = /(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
inline._label = /(?:\[(?:\\.|[^\[\]])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davisjam is this regex safe from ReDOS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, it has moved from linear to exponential.

Visualization -- The problem is in the top group. The OR can be satisfied on the top or the bottom using \\a, and if the subsequent ] is missing then you get exponential backtracking.

Example misbehavior:

/\[(?:\\.|[^\[\]])*\]/.exec("[" + "\\a".repeat(300));

Copy link
Member Author

@UziTech UziTech May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about removing \\ from the second path (/\[(?:\\.|[^\\\[\]])*\]/)? that way the only path it can take is determined by whether it sees a \\

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davisjam thanks for your help on this. Your regex experience is invaluable.

inline._href = /<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/;
inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;

Expand Down
11 changes: 11 additions & 0 deletions test/specs/new/image_links.html
@@ -0,0 +1,11 @@
<p>
<a href="https://example.com/">
<img src="https://example.com/image.jpg" alt="test" title="title" />
</a>
</p>

<p>
<a href="https://example.com/">
<img src="https://example.com/image.jpg" alt="[test]" title="[title]" />
</a>
</p>
3 changes: 3 additions & 0 deletions test/specs/new/image_links.md
@@ -0,0 +1,3 @@
[![test](https://example.com/image.jpg "title")](https://example.com/)

[![\[test\]](https://example.com/image.jpg "[title]")](https://example.com/)