Skip to content

Commit

Permalink
Merge pull request #1260 from davisjam/Rtrim
Browse files Browse the repository at this point in the history
security: use rtrim, not unsafe /X+$/
  • Loading branch information
styfle committed Jun 3, 2018
2 parents 1548175 + 0610f9f commit 37a9f1f
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 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,32 @@ 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 falsey.
function rtrim(str, c, invert) {
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

0 comments on commit 37a9f1f

Please sign in to comment.