pickle
module BAN-B301 84 """
85 model_path = os.path.join(model_dir, "drug_discovery_model.pkl")
86 with open(model_path, "rb") as f:
87 model = pickle.load(f) 88 return model
89
90
11 - model (LogisticRegression): The pre-trained diagnosis model.
12 """
13 with open("models/diagnosis_model.pkl", "rb") as f:
14 model = pickle.load(f)15 return model
16
17
9 - treatment_models (dict): A dictionary containing the pre-trained treatment models.
10 """
11 with open("models/treatment_models.pkl", "rb") as f:
12 treatment_models = pickle.load(f) 13 return treatment_models
14
15def generate_medication_schedule(health_data):
The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.
Python's pickle
module is used for serializing and de-serializing a Python object structure. Data serialization is the process of converting structured data to a format that allows sharing or storage of the data in a form that allows recovery of its original structure.
Insecure deserialization is when an application deserializes the data that it gets without any kind of validation, or even the authenticity of the data. It is easy to execute arbitrary code when unpickling data. Unpickling can be exploited to execute arbitrary commands on your machine.
If pickle
is not absolutely necessary for the use-case, consider using a safer serialization, like PyYaml.PyYAML is a YAML parser and emitter for Python. YAML is language-agnostic and human-readable serialization format. But pickle
has its advantages too. Pickle format is specific to Python and can represent a wide variety of data structures and objects where as YAML represents simple data types & structures in a language-portable manner.
Recommended practices when using pickle
module:
Refer to this blog post to know more about dangers of using pickle
module.
from flask import request
import picke
@app.route('/pickle')
def load():
data = request.GET.get("data")
conf = pickle.load(data) # Insecure. Avoid using pickle
from flask import request
import yaml
@app.route('/yaml')
def load():
data = request.GET.get("data")
conf = yaml.load(data) # Secure