Critical security considerations when using BinaryDB
BinaryDB uses Python’s pickle module for serialization. Never load database files from untrusted sources. Pickle can execute arbitrary code during deserialization.
BinaryDB uses Python’s pickle module to serialize data to disk. While pickle is convenient and supports any Python object, it has a critical security flaw: arbitrary code execution.When unpickling data, Python can execute code embedded in the serialized data. A malicious actor can craft a pickle file that executes arbitrary commands on your system.
from binarydb.database import Database# DANGEROUS: Loading a database from an untrusted sourcedb = Database("/tmp/untrusted_database")db.load() # This could execute malicious code!
The Database class includes this warning in its docstring:
"""Persistent key-value database.This database stores all records in memory and serializes them to diskusing pickle. It is designed for small to medium datasets wheresimplicity, reliability and embeddability are preferred over performance.⚠️ Security notice: Never load database files from untrusted sources. Pickle is unsafe."""
This is not a theoretical risk - it’s a well-known and easily exploitable vulnerability.
# NEVER DO THISfrom binarydb.database import Database# User uploads a .pkl fileuploaded_file = request.files['database']uploaded_file.save('/tmp/user_db.pkl')db = Database('/tmp/user_db')db.load() # DANGEROUS: User can execute arbitrary code
# NEVER DO THISfrom binarydb.database import Database# Load database from shared directorydb = Database('/shared/untrusted/user_database')db.load() # DANGEROUS: Any user with write access can exploit this
BinaryDB is safe when used in controlled environments where you control all data sources:
from binarydb.database import Database# SAFE: Application-controlled databasedb = Database('./data/app_database')db.load() # Safe - you control this filedb.set('config:version', '1.0.0')db.commit()db.close()
import sqlite3# SAFE: SQLite with untrusted dataconn = sqlite3.connect('/tmp/untrusted.db')cursor = conn.cursor()cursor.execute(''' CREATE TABLE IF NOT EXISTS data ( key TEXT PRIMARY KEY, value TEXT )''')cursor.execute('INSERT OR REPLACE INTO data VALUES (?, ?)', ('key', 'value'))conn.commit()conn.close()