From 00b8a93c8fe60d66e862445082b61d1ce1abae1c Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Fri, 1 Mar 2024 23:49:38 +0400 Subject: [PATCH] limit the number of autocompleted cells in a table fix https://github.com/markdown-it/markdown-it/issues/1000 --- lib/rules_block/table.mjs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/rules_block/table.mjs b/lib/rules_block/table.mjs index 6ec66b1..be0ba0a 100644 --- a/lib/rules_block/table.mjs +++ b/lib/rules_block/table.mjs @@ -2,6 +2,14 @@ import { isSpace } from '../common/utils.mjs' +// Limit the amount of empty autocompleted cells in a table, +// see https://github.com/markdown-it/markdown-it/issues/1000, +// +// Both pulldown-cmark and commonmark-hs limit the number of cells this way to ~200k. +// We set it to 65k, which can expand user input by a factor of x370 +// (256x256 square is 1.8kB expanded into 650kB). +const MAX_AUTOCOMPLETED_CELLS = 0x10000 + function getLine (state, line) { const pos = state.bMarks[line] + state.tShift[line] const max = state.eMarks[line] @@ -157,6 +165,7 @@ export default function table (state, startLine, endLine, silent) { state.push('thead_close', 'thead', -1) let tbodyLines + let autocompletedCells = 0 for (nextLine = startLine + 2; nextLine < endLine; nextLine++) { if (state.sCount[nextLine] < state.blkIndent) { break } @@ -177,6 +186,11 @@ export default function table (state, startLine, endLine, silent) { if (columns.length && columns[0] === '') columns.shift() if (columns.length && columns[columns.length - 1] === '') columns.pop() + // note: autocomplete count can be negative if user specifies more columns than header, + // but that does not affect intended use (which is limiting expansion) + autocompletedCells += columnCount - columns.length + if (autocompletedCells > MAX_AUTOCOMPLETED_CELLS) { break } + if (nextLine === startLine + 2) { const token_tbo = state.push('tbody_open', 'tbody', 1) token_tbo.map = tbodyLines = [startLine + 2, 0]