Skip to content

Commit ae03c61

Browse files
authored
🐛 FIX: CVE-2023-26303 (#246)
Fix unnecessary asserts, leading to crashes
1 parent 2c93e0b commit ae03c61

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

markdown_it/renderer.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def render(
8383

8484
for i, token in enumerate(tokens):
8585
if token.type == "inline":
86-
assert token.children is not None
87-
result += self.renderInline(token.children, options, env)
86+
if token.children:
87+
result += self.renderInline(token.children, options, env)
8888
elif token.type in self.rules:
8989
result += self.rules[token.type](tokens, i, options, env)
9090
else:
@@ -206,8 +206,8 @@ def renderInlineAsText(
206206
if token.type == "text":
207207
result += token.content
208208
elif token.type == "image":
209-
assert token.children is not None
210-
result += self.renderInlineAsText(token.children, options, env)
209+
if token.children:
210+
result += self.renderInlineAsText(token.children, options, env)
211211
elif token.type == "softbreak":
212212
result += "\n"
213213

@@ -305,14 +305,10 @@ def image(
305305

306306
# "alt" attr MUST be set, even if empty. Because it's mandatory and
307307
# should be placed on proper position for tests.
308-
309-
assert (
310-
token.attrs and "alt" in token.attrs
311-
), '"image" token\'s attrs must contain `alt`'
312-
313-
# Replace content with actual value
314-
315-
token.attrSet("alt", self.renderInlineAsText(token.children, options, env))
308+
if token.children:
309+
token.attrSet("alt", self.renderInlineAsText(token.children, options, env))
310+
else:
311+
token.attrSet("alt", "")
316312

317313
return self.renderToken(tokens, idx, options, env)
318314

markdown_it/rules_core/replacements.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def replace(state: StateCore) -> None:
116116
for token in state.tokens:
117117
if token.type != "inline":
118118
continue
119-
assert token.children is not None
119+
if token.children is None:
120+
continue
120121

121122
if SCOPED_ABBR_RE.search(token.content):
122123
replace_scoped(token.children)

markdown_it/rules_core/smartquotes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,5 @@ def smartquotes(state: StateCore) -> None:
197197
for token in state.tokens:
198198
if token.type != "inline" or not QUOTE_RE.search(token.content):
199199
continue
200-
assert token.children is not None
201-
process_inlines(token.children, state)
200+
if token.children is not None:
201+
process_inlines(token.children, state)

tests/test_port/fixtures/issue-fixes.md

+9
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@
3636
.
3737
<p>💬</p>
3838
.
39+
40+
Fix CVE-2023-26303
41+
.
42+
![![]()
43+
]([)
44+
.
45+
<p><img src="%5B" alt="
46+
" /></p>
47+
.

tests/test_port/test_fixtures.py

+1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,5 @@ def test_strikethrough(line, title, input, expected):
111111
def test_issue_fixes(line, title, input, expected):
112112
md = MarkdownIt()
113113
text = md.render(input)
114+
print(text)
114115
assert text.rstrip() == expected.rstrip()

0 commit comments

Comments
 (0)