NLTK's pathsec module implements hostname validation to prevent SSRF — it checks that requested URLs point to public hosts before fetching. That validation works correctly in isolation. The problem emerges in proxy environments: when HTTP_PROXY or HTTPS_PROXY is set (common in corporate networks), the validation still runs and passes on the input URL, but the actual request is forwarded to the proxy, which resolves and connects to an entirely different destination. The proxy inherits no knowledge of the pathsec checks, so a URL like http://localhost:8080/path passes validation but reaches the proxy, which dutifully forwards the request to the internal service you were trying to protect.
This transforms the vulnerability from a standard SSRF into a supply chain compromise. NLTK's downloader fetches package indexes and tarballs — if an attacker can redirect that fetch through the proxy to a forged index, they control what packages NLTK installs and loads into downstream systems. The attack surface isn't internal service enumeration; it's poisoning the trust chain between NLTK and every system that consumes its data.
The fix is not simply patching the URL validation — it's architectural. Security-sensitive fetch operations must either explicitly disable proxy inheritance by constructing their own URL openers with proxy=False, or re-validate the resolved destination after the proxy would connect. The first approach is cleaner but breaks legitimate enterprise deployments where proxies are mandatory. The second is architecturally sound but technically difficult because proxies don't always expose resolved destinations.
For defenders: check whether HTTP_PROXY or HTTPS_PROXY is set in any environment where NLTK runs. If it is, understand that pathsec.urlopen provides no protection against internal service access — the proxy bypasses it entirely. Treat NLTK's data fetching as a trust boundary: the library implicitly assumes it's fetching from a trusted index but performs no cryptographic verification of fetched content. That assumption is now exposed as hollow.