{"id":"CVE-2021-43854","aliases":["CVE-2021-43854","GHSA-f8m6-h2c7-8h9x","PYSEC-2021-859"],"related":[],"kind":"vulnerability","state":"published","title":"Inefficient Regular Expression Complexity in nltk","summary":"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.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"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.","source":"cvelist"},{"lang":"en","text":"### Impact\nThe vulnerability is present in [`PunktSentenceTokenizer`](https://www.nltk.org/api/nltk.tokenize.punkt.html#nltk.tokenize.punkt.PunktSentenceTokenizer), [`sent_tokenize`](https://www.nltk.org/api/nltk.tokenize.html#nltk.tokenize.sent_tokenize)  and [`word_tokenize`](https://www.nltk.org/api/nltk.tokenize.html#nltk.tokenize.word_tokenize). Any users of this class, or these two functions, are vulnerable to a Regular Expression Denial of Service (ReDoS) attack. \nIn short, a specifically crafted long input to any of these vulnerable functions will cause them to take a significant amount of execution time. The effect of this vulnerability is noticeable with the following example:\n```python\nfrom nltk.tokenize import word_tokenize\n\nn = 8\nfor length in [10**i for i in range(2, n)]:\n    # Prepare a malicious input\n    text = \"a\" * length\n    start_t = time.time()\n    # Call `word_tokenize` and naively measure the execution time\n    word_tokenize(text)\n    print(f\"A length of {length:<{n}} takes {time.time() - start_t:.4f}s\")\n```\nWhich gave the following output during testing:\n```python\nA length of 100      takes 0.0060s\nA length of 1000     takes 0.0060s\nA length of 10000    takes 0.6320s\nA length of 100000   takes 56.3322s\n...\n```\nI canceled the execution of the program after running it for several hours.\n\nIf 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, or applying the workaround described below.\n\n### Patches\nThe problem has been patched in NLTK 3.6.6. After the fix, running the above program gives the following result:\n```python\nA length of 100      takes 0.0070s\nA length of 1000     takes 0.0010s\nA length of 10000    takes 0.0060s\nA length of 100000   takes 0.0400s\nA length of 1000000  takes 0.3520s\nA length of 10000000 takes 3.4641s\n```\nThis output shows a linear relationship in execution time versus input length, which is desirable for regular expressions.\nWe recommend updating to NLTK 3.6.6+ if possible.\n\n### Workarounds\nThe execution time of the vulnerable functions is exponential to the length of a malicious input. With other words, 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.\n\n### References\n* The issue showcasing the vulnerability: https://github.com/nltk/nltk/issues/2866\n* The pull request containing considerably more information on the vulnerability, and the fix: https://github.com/nltk/nltk/pull/2869\n* The commit containing the fix: 1405aad979c6b8080dbbc8e0858f89b2e3690341\n* Information on CWE-1333: Inefficient Regular Expression Complexity: https://cwe.mitre.org/data/definitions/1333.html\n\n### For more information\nIf you have any questions or comments about this advisory:\n* Open an issue in [github.com/nltk/nltk](https://github.com/nltk/nltk)\n* Email us at [nltk.team@gmail.com](mailto:nltk.team@gmail.com)","format":"markdown","source":"ghsa"},{"lang":"es","text":"NLTK (Natural Language Toolkit) es un conjunto de módulos de código abierto de Python, conjuntos de datos y tutoriales que apoyan la investigación y el desarrollo en el procesamiento del lenguaje natural. Las Versiones anteriores a 3.6.5, son vulnerables a ataques de denegación de servicio con expresiones regulares (ReDoS). La vulnerabilidad está presente en PunktSentenceTokenizer, sent_tokenize y word_tokenize. Cualquier usuario de esta clase, o de estas dos funciones, es vulnerable al ataque ReDoS. En resumen, una entrada larga específicamente diseñada para cualquiera de estas funciones vulnerables causará que tomen una cantidad significativa de tiempo de ejecución. Si su programa depende de cualquiera de las funciones vulnerables para tokenizar entradas de usuario imprevisibles, le recomendamos encarecidamente que actualice a una versión de NLTK sin la vulnerabilidad. Para usuarios que no puedan actualizarse, el tiempo de ejecución puede limitarse mediante la limitación de la longitud máxima de una entrada a cualquiera de las funciones vulnerables. Nuestra recomendación es implementar dicho límite","source":"nvd"}],"published":"2021-12-23T17:55:11.000Z","modified":"2026-07-08T06:28:12.494Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H","score":7.5,"rating":"HIGH"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H","score":7.5,"rating":"HIGH"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H","score":7.5,"rating":"HIGH"},{"scorer":"GitHub","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N","rating":"HIGH"},{"scorer":"NVD","scheme":"CVSS:2.0","vector":"AV:N/AC:L/Au:N/C:N/I:N/A:P","score":5,"rating":"MEDIUM"}],"cwes":["CWE-400"],"affected":[{"ranges":[{"version":"< 3.6.5","status":"affected"}],"vendor":"nltk","product":"nltk","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"3.6.6"}],"ecosystem":"PyPI","package":"nltk","purl":"pkg:pypi/nltk","source":"ghsa"},{"ranges":[{"status":"affected","fixed":"3.6.5"}],"vendor":"nltk","product":"nltk","cpe":"cpe:2.3:a:nltk:nltk:*:*:*:*:*:*:*:*","source":"nvd"},{"ranges":[{"type":"git","introduced":"0","status":"affected","fixed":"1405aad979c6b8080dbbc8e0858f89b2e3690341"},{"type":"ecosystem","introduced":"0","status":"affected","fixed":"3.6.5"}],"ecosystem":"PyPI","package":"nltk","purl":"pkg:pypi/nltk","repo":"https://github.com/nltk/nltk","source":"pysec"}],"references":[{"url":"https://github.com/nltk/nltk/security/advisories/GHSA-f8m6-h2c7-8h9x","tags":["advisory","exploit","patch","third-party-advisory","web","x-refsource-confirm","x-transferred"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/nltk/nltk/issues/2866","tags":["exploit","issue-tracking","patch","report","third-party-advisory","web","x-refsource-misc","x-transferred"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/nltk/nltk/pull/2869","tags":["exploit","patch","third-party-advisory","web","x-refsource-misc","x-transferred"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/nltk/nltk/commit/1405aad979c6b8080dbbc8e0858f89b2e3690341","tags":["fix","patch","third-party-advisory","web","x-refsource-misc","x-transferred"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2021-43854","tags":["advisory"],"sources":["ghsa"]},{"url":"https://github.com/nltk/nltk","tags":["package"],"sources":["ghsa"]},{"url":"https://github.com/pypa/advisory-database/tree/main/vulns/nltk/PYSEC-2021-859.yaml","tags":["web"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2021-43854","url":"https://www.cve.org/CVERecord?id=CVE-2021-43854","licence":"CVE Terms of Use","modified":"2024-08-04T04:10:16.281Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-f8m6-h2c7-8h9x","url":"https://github.com/advisories/GHSA-f8m6-h2c7-8h9x","licence":"CC-BY-4.0","modified":"2026-07-08T06:28:12.494Z"},{"source":"nvd","name":"NVD","id":"CVE-2021-43854","url":"https://nvd.nist.gov/vuln/detail/CVE-2021-43854","licence":"Public domain","modified":"2026-06-17T04:11:34.653Z"},{"source":"pysec","name":"PyPA Advisory Database","id":"PYSEC-2021-859","url":"https://osv.dev/vulnerability/PYSEC-2021-859","licence":"CC-BY-4.0","modified":"2023-11-08T04:07:14.650Z"}],"updatedAt":"2026-11-25T22:24:36.367Z","epss":{"score":0.0279,"percentile":0.868,"date":"2026-12-01T12:00:22.000Z"}}