{"id":905,"cve":"CVE-2021-43854","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2021-43854","cwe":"CWE-400 Uncontrolled Resource Consumption","cwe_number":400,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/400","cvss_score":7.5,"published":"2021-12-23","owasp":"A06 Vulnerable and Outdated Components","odc":"Timing\/Serialization","cdc":"Timing Issues","category":"Input Validation and Sanitization","subcategory":"Insecure Parsing or Deserialization","scope":"Local","impact":"Denial of Service (DoS)","summary":"NLTK < 3.6.5: ReDoS in PunktSentenceTokenizer, sent_tokenize, word_tokenize","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2021-43854\/","description":"NLTK (Natural Language Toolkit) is a suite of open source Python modules, data sets, and tutorials supporting research and development in Natural Language Processing. Versions prior to 3.6.5 are vulnerable to regular expression denial of service (ReDoS) attacks. The vulnerability is present in PunktSentenceTokenizer, sent_tokenize and word_tokenize. Any users of this class, or these two functions, are vulnerable to the ReDoS attack. In short, a specifically crafted long input to any of these vulnerable functions will cause them to take a significant amount of execution time. If your program relies on any of the vulnerable functions for tokenizing unpredictable user input, then we would strongly recommend upgrading to a version of NLTK without the vulnerability. For users unable to upgrade the execution time can be bounded by limiting the maximum length of an input to any of the vulnerable functions. Our recommendation is to implement such a limit.","solution":"Update NLTK to version 3.6.5 or higher.","affected_component":"NLTK","fixed_by_upgrading":true,"vulnerable_code":"import nltk\n\ntext_input = \"A\" * 1000000\n\nsentences = nltk.tokenize.sent_tokenize(text_input)\nwords = nltk.tokenize.word_tokenize(text_input)\n\nprint(sentences)\nprint(words)","patched_code":"import nltk\n\ndef safe_tokenize(text, max_length=1000):\n    if len(text) > max_length:\n        raise ValueError(f\"Input text exceeds the maximum allowed length of {max_length}.\")\n    return nltk.tokenize.sent_tokenize(text), nltk.tokenize.word_tokenize(text)\n\ntry:\n    text_input = \"A\" * 1000000\n    sentences, words = safe_tokenize(text_input)\n    print(sentences)\n    print(words)\nexcept ValueError as e:\n    print(e)","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[{"repository":"nltk\/nltk","commit_url":"https:\/\/github.com\/nltk\/nltk\/commit\/1405aad979c6b8080dbbc8e0858f89b2e3690341","commit_subject":"Resolved serious ReDoS in PunktSentenceTokenizer (#2869)","units":[{"file":"nltk\/tokenize\/punkt.py","function":null},{"file":"nltk\/tokenize\/punkt.py","function":"PunktSentenceTokenizer.debug_decisions"},{"file":"nltk\/tokenize\/punkt.py","function":"PunktSentenceTokenizer._slices_from_text"}]}],"references":null}