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

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

Merged
merged 3 commits into from
Jun 3, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Lexer.prototype.token = function(src, top) {
this.tokens.push({
type: 'code',
text: !this.options.pedantic
? cap.replace(/\n+$/, '')
? rtrim(cap, '\n')
: cap
});
continue;
Expand Down Expand Up @@ -1303,7 +1303,7 @@ function resolveUrl(base, href) {
if (/^[^:]+:\/*[^/]*$/.test(base)) {
baseUrls[' ' + base] = base + '/';
} else {
baseUrls[' ' + base] = base.replace(/[^/]*$/, '');
baseUrls[' ' + base] = rtrim(base, '/', true);
}
}
base = baseUrls[' ' + base];
Expand Down Expand Up @@ -1355,6 +1355,38 @@ function splitCells(tableRow, count) {
return cells;
}

// Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
// /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) {
Copy link
Member

@styfle styfle Jun 2, 2018

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
Contributor Author

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.

invert = false;
} else {
invert = true;
}

if (str.length === 0) {
return '';
}

// Length of suffix matching the invert condition.
var suffLen = 0;

// Step left until we fail to match the invert condition.
while (suffLen < str.length) {
var currChar = str.charAt(str.length - suffLen - 1);
if (currChar === c && !invert) {
suffLen++;
} else if (currChar !== c && invert) {
suffLen++;
} else {
break;
}
}

return str.substr(0, str.length - suffLen);
}

/**
* Marked
*/
Expand Down