/

CVE-2025-71408

Severity:

HIGH RISK

(7.8)

Eval Injection (CWE-95) in NLTK Collocations Module

CVE-2025-71408

Analyzed

Vulnerability type

Category:

Eval Injection

CWE:

CWE-95

Impact:

Remote Code Execution

Severity:

HIGH RISK

(7.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

NLTK's collocations module contains an eval injection flaw that allows arbitrary Python code execution when command-line arguments are passed directly into eval() without validation.

If an attacker can influence the command-line options used to run collocations.py, they can escape the intended attribute lookup on BigramAssocMeasures and execute Python and OS-level commands. This becomes risky in scripted or shared environments where NLTK utilities are invoked with user-controlled parameters.

Affected context

This vulnerability occurs when the nltk.collocations module is executed directly via its __main__ block and command-line arguments are used to select scoring functions or measures.

It is relevant in environments where NLTK utilities are wrapped in scripts, CLIs, or workflows that expose these arguments to users or other systems.

The risk is elevated when argument values are not restricted to a known-safe set and users can pass arbitrary strings that reach eval() unmodified.

Exploit preconditions

  • Attack surface: Command-line interface of the nltk.collocations module (or wrappers that delegate to it)
  • Required access: Ability to run or trigger collocations.py with attacker-controlled arguments, such as CLI access, job submission, or parameterized scripts
  • Trigger point: Execution of the __main__ block, which passes argument suffixes directly into eval() on BigramAssocMeasures
  • Why exploitation is possible: The code trusts user-supplied strings and evaluates them as Python expressions without allowlisting or sanitization, allowing expressions that break out of attribute access and invoke arbitrary code (e.g., via the os module)

Why this happens

The underlying bug is the use of eval() on untrusted input within a command-line tool.

Instead of mapping user-provided measure names to a fixed set of attributes on BigramAssocMeasures, the implementation evaluates arbitrary Python expressions derived from command-line arguments.

This aligns with CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code, where dynamic code evaluation treats user input as executable logic, violating the principle of separating data from code and enabling code injection.

Potential impact

  • Execution of arbitrary Python code within the NLTK process
  • Running OS commands via modules such as os, subprocess, or shutil
  • Reading or modifying local files accessible to the process
  • Stealing credentials, API keys, or other secrets from environment variables or configuration files
  • Using the compromised process as a pivot to attack other services or data in the same environment

General mitigation guidance

Effective mitigation focuses on eliminating unsafe dynamic evaluation and constraining how user input influences computation.

Common patterns include:

  • Replacing eval() with explicit mappings or lookup tables that map allowed strings to known functions or attributes
  • Implementing strict allowlists for measure names or options accepted via command-line arguments
  • Treating command-line parameters as data only, avoiding any execution or interpretation as code
  • Running helper utilities with least privilege so that, if misused, they have minimal access to sensitive files and system resources
  • Designing CLIs so that advanced behavior is implemented via internal logic rather than user-provided expressions

How teams detect issues like this

Organizations can identify similar issues through:

  • Static analysis rules targeting dangerous functions such as eval(), exec(), and equivalent dynamic code evaluation APIs
  • Code review checklists that flag use of dynamic evaluation on any user-controlled input, including command-line arguments
  • Policy enforcement that bans or tightly restricts eval-style constructs in production code, especially in publicly exposed tools
  • Automated security scanning tools configured to search for CWE-95 patterns and highlight any dynamic evaluation paths in utility modules and CLIs

Sources

SourceLink
aydinnyunus.github.ioaydinnyunus.github.io/2026/06/07/command-injection-nltk-collocations-eval/
github.comgithub.com/nltk/nltk/commit/66f14096d952ec8f04934f515e027534bd4eb0ac
github.comgithub.com/nltk/nltk/pull/3465
github.comgithub.com/nltk/nltk/releases/tag/3.9.3
vulncheck.comvulncheck.com/advisories/nltk-eval-injection-via-collocations-py-command-line-arguments

Frequently asked

questions

It can be, depending on how the collocations module is exposed. If the command-line invocation is wrapped by a web service, job scheduler, or other remote interface that passes user input directly to arguments, remote exploitation is possible.

An attacker must be able to influence the command-line arguments used when collocations.py is executed. This may involve shell access, the ability to submit jobs, or control over higher-level services that construct those arguments.

Remove use of eval() on user-supplied data and replace it with a deterministic mapping from allowed strings to specific functions or attributes. Enforce an allowlist of valid options and reject anything outside that set.

Avoid dynamic code evaluation in application and utility code, treat all external inputs (including CLI parameters) as untrusted, enforce secure coding guidelines that prohibit eval/exec on user input, and use static analysis and code review to catch any reintroduction of such patterns.

Find out what's already
exploitable in your codebase.