Skip to content

Commit 1e55cc1

Browse files
committed
fix: remove potential infinite loop, DDOS vector
Add corresponding test Lint
1 parent 1c31c41 commit 1e55cc1

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/__tests__/toc.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ test("markdown-it-toc-and-anchor toc", t => {
3131
"should work with soft breaks"
3232
);
3333

34+
t.is(
35+
mdIt(
36+
`**123**+
37+
@[toc]`,
38+
{ toc: true }
39+
),
40+
`<p><strong>123</strong>+
41+
</p>\n`,
42+
"should work with line breaks after text before toc"
43+
);
44+
3445
t.is(
3546
mdIt(
3647
`@[tac]
@@ -86,17 +97,17 @@ and next element in the same inline token`
8697
# Heading`,
8798
{
8899
toc: true,
89-
tocClassName: null,
100+
tocClassName: null
90101
}
91102
),
92103
`<p><ul>
93104
<li><a href="#heading">Heading</a></li>
94105
</ul>
95106
</p>
96107
<h1 id="heading">Heading</h1>\n`,
97-
/* eslint-disable max-len */
98-
"should handle not including default class in anchors when setting tocClassName to null"
99-
)
108+
/* eslint-disable max-len */
109+
"should handle not including default class in anchors when setting tocClassName to null"
110+
);
100111

101112
t.is(
102113
mdIt(

src/index.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,6 @@ export default function(md, options) {
253253
let token;
254254
let match;
255255

256-
while (
257-
state.src.indexOf("\n") >= 0 &&
258-
state.src.indexOf("\n") < state.src.indexOf(TOC)
259-
) {
260-
if (state.tokens.slice(-1)[0].type === "softbreak") {
261-
state.src = state.src
262-
.split("\n")
263-
.slice(1)
264-
.join("\n");
265-
state.pos = 0;
266-
}
267-
}
268-
269256
if (
270257
// Reject if the token does not start with @[
271258
state.src.charCodeAt(state.pos) !== 0x40 ||
@@ -290,12 +277,7 @@ export default function(md, options) {
290277
token = state.push("toc_close", "toc", -1);
291278

292279
// Update pos so the parser can continue
293-
const newline = state.src.indexOf("\n");
294-
if (newline !== -1) {
295-
state.pos = state.pos + newline;
296-
} else {
297-
state.pos = state.pos + state.posMax + 1;
298-
}
280+
state.pos = state.pos + 6;
299281

300282
return true;
301283
});

0 commit comments

Comments
 (0)