镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

fix image links with escaped brackets - #1683

Merged
UziTech merged 2 commits into
markedjs:masterfrom
UziTech:image-links
May 20, 2020
Merged

UziTech merged 2 commits into
markedjs:masterfrom
UziTech:image-links

Conversation

@UziTech

@UziTech UziTech commented May 19, 2020 •

Copy link
Copy Markdown
Member

Marked version: 1.1.0

Description

Fix image link with \[ or \] in alt text

demo

Contributor

  • Test(s) exist to ensure functionality and minimize regression

Committer

In most cases, this should be a different person than the contributor.

  • Draft GitHub release notes have been updated.
  • CI is green (no forced merge required).
  • Merge PR

@vercel

vercel Bot commented May 19, 2020 •

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/markedjs/markedjs/qpzfg7jxn
✅ Preview: https://markedjs-git-fork-uzitech-image-links.markedjs.now.sh

Comment thread src/rules.js Outdated
.getRegex();

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

Copy link
Copy Markdown
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
Copy Markdown
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));

@UziTech UziTech May 20, 2020 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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.

@davisjam davisjam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Introduces an exponential regex

@davisjam davisjam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

New regex is back to linear time.

@UziTech
UziTech merged commit 04ea199 into markedjs:master May 20, 2020
@UziTech
UziTech deleted the image-links branch May 20, 2020 15:49
@UziTech UziTech mentioned this pull request Jul 13, 2020
12 tasks done

This branch was successfully deployed

1 active deployment
Preview — d57c89d0 Deployed May 20, 2020 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alt text with escaped characters breaks image links

3 participants