Skip to content

Commit

Permalink
fix: remove potential infinite loop, DDOS vector
Browse files Browse the repository at this point in the history
Add corresponding test
Lint
  • Loading branch information
medfreeman committed Dec 24, 2018
1 parent 1c31c41 commit 1e55cc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
19 changes: 15 additions & 4 deletions src/__tests__/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ test("markdown-it-toc-and-anchor toc", t => {
"should work with soft breaks"
);

t.is(
mdIt(
`**123**+
@[toc]`,
{ toc: true }
),
`<p><strong>123</strong>+
</p>\n`,
"should work with line breaks after text before toc"
);

t.is(
mdIt(
`@[tac]
Expand Down Expand Up @@ -86,17 +97,17 @@ and next element in the same inline token`
# Heading`,
{
toc: true,
tocClassName: null,
tocClassName: null
}
),
`<p><ul>
<li><a href="#heading">Heading</a></li>
</ul>
</p>
<h1 id="heading">Heading</h1>\n`,
/* eslint-disable max-len */
"should handle not including default class in anchors when setting tocClassName to null"
)
/* eslint-disable max-len */
"should handle not including default class in anchors when setting tocClassName to null"
);

t.is(
mdIt(
Expand Down
20 changes: 1 addition & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,6 @@ export default function(md, options) {
let token;
let match;

while (
state.src.indexOf("\n") >= 0 &&
state.src.indexOf("\n") < state.src.indexOf(TOC)
) {
if (state.tokens.slice(-1)[0].type === "softbreak") {
state.src = state.src
.split("\n")
.slice(1)
.join("\n");
state.pos = 0;
}
}

if (
// Reject if the token does not start with @[
state.src.charCodeAt(state.pos) !== 0x40 ||
Expand All @@ -290,12 +277,7 @@ export default function(md, options) {
token = state.push("toc_close", "toc", -1);

// Update pos so the parser can continue
const newline = state.src.indexOf("\n");
if (newline !== -1) {
state.pos = state.pos + newline;
} else {
state.pos = state.pos + state.posMax + 1;
}
state.pos = state.pos + 6;

return true;
});
Expand Down

0 comments on commit 1e55cc1

Please sign in to comment.