{"id":858,"cve":"CVE-2021-29471","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2021-29471","cwe":"CWE-331 Insufficient Entropy","cwe_number":331,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/331","cvss_score":5.3,"published":"2021-05-11","owasp":"A08 Software and Data Integrity Failures","odc":"Function","cdc":"Incorrect Functionality","category":"Resource Management","subcategory":"Resource Exhaustion","scope":"Remote","impact":"Denial of Service (DoS)","summary":"Synapse Matrix 1.33.2 Push rules Denial-of-Service (DoS) via event_match","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2021-29471\/","description":"Synapse is a Matrix reference homeserver written in python (pypi package matrix-synapse). Matrix is an ecosystem for open federated Instant Messaging and VoIP. In Synapse before version 1.33.2 \"Push rules\" can specify conditions under which they will match, including `event_match`, which matches event content against a pattern including wildcards. Certain patterns can cause very poor performance in the matching engine, leading to a denial-of-service when processing moderate length events. The issue is patched in version 1.33.2. A potential workaround might be to prevent users from making custom push rules, by blocking such requests at a reverse-proxy.","solution":"Update to Synapse version 1.33.2 or higher.","affected_component":"matrix-synapse","fixed_by_upgrading":true,"vulnerable_code":"def process_push_rule(push_rule):\n    if 'event_match' in push_rule:\n        pattern = push_rule['event_match']\n        match_events(pattern)\n\ndef match_events(pattern):\n    events = get_events()\n    for event in events:\n        if matches_pattern(event, pattern):\n            handle_matched_event(event)\n\ndef matches_pattern(event, pattern):\n    return pattern in event['content']\n\npush_rule = {\n    'event_match': 'some*complex?pattern'\n}\nprocess_push_rule(push_rule)","patched_code":"import re\n\ndef process_push_rule(push_rule):\n    if 'event_match' in push_rule:\n        pattern = push_rule['event_match']\n        regex = convert_wildcard_to_regex(pattern)\n        match_events(regex)\n\ndef match_events(regex):\n    events = get_events()\n    for event in events:\n        if matches_pattern(event, regex):\n            handle_matched_event(event)\n\ndef matches_pattern(event, regex):\n    content = event.get('content', '')\n    return bool(regex.search(content))\n\ndef convert_wildcard_to_regex(pattern):\n    if not isinstance(pattern, str) or len(pattern) > 256:\n        raise ValueError(\"Invalid or overly long pattern\")\n\n    pattern = re.escape(pattern)\n    pattern = pattern.replace(r'\\*', '.*').replace(r'\\?', '.')\n    return re.compile(f\"^{pattern}$\", re.IGNORECASE)\n\ndef get_events():\n    return [\n        {'content': 'some interesting content'},\n        {'content': 'something else'},\n        {'content': 'someComplexPattern match here'},\n    ]\n\ndef handle_matched_event(event):\n    print(\"Matched event:\", event)\n\npush_rule = {\n    'event_match': 'some*complex?pattern'\n}\nprocess_push_rule(push_rule)\n","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[{"repository":"matrix-org\/synapse","commit_url":"https:\/\/github.com\/matrix-org\/synapse\/commit\/03318a766cac9f8b053db2214d9c332a977d226c","commit_subject":"Merge pull request from GHSA-x345-32rc-8h85","units":[{"file":"synapse\/config\/tls.py","function":null},{"file":"synapse\/config\/tls.py","function":"TlsConfig.read_config"},{"file":"synapse\/push\/push_rule_evaluator.py","function":null},{"file":"synapse\/push\/push_rule_evaluator.py","function":"PushRuleEvaluatorForEvent._contains_display_name"},{"file":"synapse\/push\/push_rule_evaluator.py","function":"_glob_matches"},{"file":"synapse\/util\/__init__.py","function":null},{"file":"synapse\/util\/__init__.py","function":"glob_to_regex"}]}],"references":null}