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-045 --project .About This Rule
Understanding the vulnerability and how it is detected
Python's shelve module provides a persistent dictionary interface backed by a dbm file. Values stored in a shelve database are serialized using pickle when written and deserialized using pickle when read. This means that reading values from a shelve database is equivalent to calling pickle.loads() on those values.
If an attacker can write data to the shelve database file (via file upload, directory traversal, shared filesystem access, or any other means), they can cause arbitrary code execution the next time any value is read from the database using shelve.open().
Shelve databases are also not portable between Python versions or platforms due to their reliance on pickle and dbm. For persistent data storage, use SQLite, JSON files, or a proper database engine.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Pickle-based Deserialization Risk
Every shelve[key] read operation calls pickle.loads() on the stored value. An attacker who can write to the shelve database file can plant malicious pickle payloads that execute arbitrary code when read. The innocent-looking shelf access hides the underlying pickle deserialization.
Shared Filesystem Attack
Applications using shelve on shared filesystems (NFS, container-shared volumes, cloud storage) are vulnerable if other tenants or processes with filesystem access can modify the database files. The attacker replaces a legitimate entry with a malicious pickle payload.
Backup and Restore Injection
Restoring a shelve database from an attacker-controlled backup source triggers pickle deserialization of all stored values, enabling code execution through the restore process.
Unpredictable File Format
shelve uses dbm which has multiple backends (ndbm, gdbm, dumbdbm) with different file extensions and compatibility. Switching platforms or Python versions may silently fail or corrupt data, making it unsuitable for reliable production use.
How to Fix
Recommended remediation steps
- 1Replace shelve with SQLite + JSON for persistent key-value storage, which is portable, safe, and version-independent.
- 2If shelve must be used, ensure the database files are stored on a filesystem accessible only to the application process with no external write access.
- 3Never restore shelve database files from untrusted or external backup sources without treating the restore as equivalent to executing arbitrary code.
- 4Consider using a proper database (SQLite, PostgreSQL) for production data storage instead of file-based shelve.
- 5For simple configuration persistence, use JSON files with appropriate filesystem permissions.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to shelve.open() from the Python shelve module. All call sites are flagged since shelve uses pickle for all value serialization, and any shelve database that can be written to by untrusted parties enables code execution through subsequent reads.
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
Pickle Deserialization of Untrusted Data
pickle.loads() and pickle.load() execute arbitrary Python code during deserialization. Never unpickle data from untrusted sources.
marshal Deserialization Detected
marshal.loads() and marshal.load() are not secure against erroneous or malicious data and should not be used to deserialize untrusted input.
dill Deserialization Detected
dill.loads() and dill.load() extend pickle with broader serialization capabilities and can execute arbitrary code when deserializing untrusted data.
Frequently Asked Questions
Common questions about shelve Module Usage Detected
New feature
Get these findings posted directly on your GitHub pull requests
The shelve Module Usage Detected rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.