Skip to content

security: use rtrim, not unsafe /X+$/ - #1260

Merged
styfle merged 3 commits into
markedjs:masterfrom
davisjam:Rtrim
Jun 3, 2018
Merged

security: use rtrim, not unsafe /X+$/#1260
styfle merged 3 commits into
markedjs:masterfrom
davisjam:Rtrim

Conversation

@davisjam

Copy link
Copy Markdown
Contributor

Problem:
replace(/X+$/, '') is vulnerable to REDOS

Solution:
Replace these instances with a custom rtrim.

Problem:
replace(/X+$/, '') is vulnerable to REDOS
Solution:
Replace all instances I could find with a custom rtrim
@davisjam

davisjam commented May 9, 2018

Copy link
Copy Markdown
ContributorAuthor

This is an uncontentious excerpt from #1226. It affects "clean-up" regexes, not "parsing" regexes.

@davisjam
davisjam requested a review from UziTechMay 9, 2018 15:10
Comment threadlib/marked.js Outdated
} else {
allButC = true;
}
var mustMatchC = !allButC;

@UziTechUziTechMay 9, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If someone calls rtrim(cap, '\n', false) allButC will be true? seems like a potential source of many future errors.

I would just replace the mustMatchC's below with !allButC

It should be fine for allButC to be truthy or falsey

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I'll tweak the naming.

@davisjam

Copy link
Copy Markdown
ContributorAuthor

(I am on vacation until end of May so won't be acting on this until then @joshbruce@styfle@UziTech).

@davisjam

Copy link
Copy Markdown
ContributorAuthor

@UziTech How's the new version look? I think it's more readable now, especially the clearer variable names in the final "step left" section.

Comment threadlib/marked.js Outdated
// /c*$/ is vulnerable to REDOS.
// invert: Remove suffix of non-c chars instead. Default false.
function rtrim(str, c, invert) {
if (typeof invert === 'undefined' || !invert) {

@styflestyfleJun 2, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this if statement necessary?
It seems like this would be the same (falsly by default) without explicitly setting it to false.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It appears not. Too many years of C have addled my brain.

@davisjam

Copy link
Copy Markdown
ContributorAuthor

@styfle0610f9f removes the if check you flagged.

@styflestyfle changed the title security: rtrim, not unsafe /X+$/ idiomsecurity: use rtrim, not unsafe /X+$/ idiomJun 3, 2018
@styflestyfle changed the title security: use rtrim, not unsafe /X+$/ idiomsecurity: use rtrim, not unsafe /X+$/Jun 3, 2018
@styfle
styfle merged commit 37a9f1f into markedjs:masterJun 3, 2018
zhenalexfan pushed a commit to zhenalexfan/MarkdownHan that referenced this pull request Nov 8, 2021
security: use rtrim, not unsafe /X+$/
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@davisjam@UziTech@styfle