Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 24 additions & 18 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,16 @@ def preprocess(self, text: str) -> str:
(?:
{1}:(?:\n+[ \t]+.*)+ # indented lists
)|(?:
(?:{1}:\s+>(?:\n\s+.*)+?) # multiline long descriptions
(?=\n{1}:\s*.*\n|\s*\Z) # match up until the start of the next key:value definition or the end of the input text
{1}:\s+>(?:\n+[ \t]+.*)+ # multiline long descriptions
)|(?:
{1}:(?! >).*\n? # simple key:value pair, leading spaces allowed
{1}:[^\n]*\n? # simple key:value pair, leading spaces allowed
)
){0} # optional closing fence
'''.format(r'(?:---[\ \t]*\n)?', r'[\S \t]*\w[\S \t]*\s*'), re.MULTILINE | re.VERBOSE
'''.format(r'(?:---[\ \t]*\n)?', r'[ \t]*[^\s:\r\n][^:\r\n]*'), re.MULTILINE | re.VERBOSE
)

_key_val_list_pat = re.compile(
r"^-(?:[ \t]*([^\n]*)(?:[ \t]*[:-][ \t]*(\S+))?)(?:\n((?:[ \t]+[^\n]+\n?)+))?",
r"^-[ \t]*([^\n]*)(?:\n((?:[ \t]+[^\n]+\n?)+))?",
re.MULTILINE,
)
_key_val_dict_pat = re.compile(
Expand Down Expand Up @@ -681,21 +680,28 @@ def parse_structured_value(value: str) -> Union[list[Any], dict[str, Any]]:
# List
if vs.startswith("-"):
r: list[Any] = []
# the regex used has multiple capturing groups, so
# returned type from findall will be List[List[str]]
match: list[str]
# returned type from findall will be list[tuple[str, ...]]
for match in re.findall(self._key_val_list_pat, vs):
if match[0] and not match[1] and not match[2]:
r.append(match[0].strip())
elif match[0] == ">" and not match[1] and match[2]:
r.append(match[2].strip())
elif match[0] and match[1]:
r.append({match[0].strip(): match[1].strip()})
elif not match[0] and not match[1] and match[2]:
r.append(parse_structured_value(match[2]))
if len(match) == 2:
val, block = match[0], match[1]
if val and not block:
r.append(val.strip())
elif val == ">" and block:
r.append(block.strip())
elif not val and block:
r.append(parse_structured_value(block))
else:
# Broken case
pass
if match[0] and not match[1] and not match[2]:
r.append(match[0].strip())
elif match[0] == ">" and not match[1] and match[2]:
r.append(match[2].strip())
elif match[0] and match[1]:
r.append({match[0].strip(): match[1].strip()})
elif not match[0] and not match[1] and match[2]:
r.append(parse_structured_value(match[2]))
else:
# Broken case
pass

return r

Expand Down
10 changes: 10 additions & 0 deletions test/test_redos.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def issue_707_spaced_attrs():
return 'x <p' + ' a=1' * 40


def issue_metadata_redos():
return "---\nkey: -" + " " * 32000 + "\n---\n\n# hello\n"


def issue_metadata_list_redos():
return "---\nitems:\n - " + " " * 32000 + "\n---\n\n# hello\n"


# whack everything in a dict for easy lookup later on
CASES = {
fn.__name__: (fn, extras)
Expand All @@ -72,6 +80,8 @@ def issue_707_spaced_attrs():
(issue_707_unclosed_tags, None),
(issue_707_namespaced_attrs, None),
(issue_707_spaced_attrs, None),
(issue_metadata_redos, ['metadata']),
(issue_metadata_list_redos, ['metadata']),
]
}

Expand Down
Loading