Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-004 --project .About This Rule
Understanding the vulnerability and how it is detected
Python's built-in globals() function returns a reference to the current module's global namespace as a live, mutable dictionary. Any code that receives this dictionary can read all global variables, overwrite them, inject new names, or delete existing ones.
Passing globals() to eval(), exec(), or any function that modifies the dictionary enables an attacker to redefine security-critical functions, inject callable backdoors, overwrite configuration values, or access credentials stored in module-level constants.
Common dangerous patterns include passing globals() as the namespace argument to eval() or exec(), passing it to template rendering functions, serializing it to disk or network, or using it as a lookup table to resolve user-supplied function names.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Global Namespace Corruption
An attacker who gains write access to the dictionary returned by globals() can overwrite any module-level variable or function. This includes authentication functions, access control checks, cryptographic keys, and database connection strings.
Code Execution via Namespace Injection
When globals() is passed as the namespace to eval() or exec(), an attacker controlling the evaluated code can define new entries in the global namespace that persist after the eval/exec call, creating persistent backdoors in the running process.
Credential Exposure
The global namespace typically contains imported modules, configuration constants, and application-level secrets. Serializing or logging globals() can expose API keys, database passwords, cryptographic secrets, and other sensitive values.
Authentication Bypass
Overwriting a global function such as is_authenticated or check_permission through the globals() dictionary allows an attacker to replace security checks with no-ops, effectively bypassing all authorization logic in the module.
How to Fix
Recommended remediation steps
- 1Replace globals()-based function lookup with an explicit dictionary mapping allowed names to their callables.
- 2Never pass globals() as the namespace argument to eval() or exec(); create a minimal, read-only namespace with only the names required.
- 3Do not serialize, log, or transmit the globals() dictionary as it contains credentials and sensitive configuration.
- 4Use getattr() with an allowlist on a specific module or object for dynamic dispatch rather than searching the entire global namespace.
- 5Audit all uses of globals() in code that processes external input to ensure no user-controlled key can reach the globals dict.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects all calls to the built-in globals() function in Python source code. It targets patterns where the return value of globals() is passed to another function, used as a namespace argument, stored in a variable for later use, or directly returned from a function.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Dangerous eval() Usage Detected
eval() executes arbitrary Python expressions from strings, enabling remote code execution when called with untrusted input.
Dangerous exec() Usage Detected
exec() executes arbitrary Python statements from strings or code objects, enabling remote code execution when called with untrusted input.
Non-literal Dynamic Import Detected
__import__() or importlib.import_module() with a non-literal argument can import arbitrary modules when called with untrusted input.
Frequently Asked Questions
Common questions about Dangerous globals() Usage Detected
New feature
Get these findings posted directly on your GitHub pull requests
The Dangerous globals() Usage Detected rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.