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

