Skip to content

Commit afacf2a

Browse files
committed
fix even more vulns and run formatter
1 parent 7d81007 commit afacf2a

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Comma-separated list of allowed origins
2+
ALLOWED_ORIGINS=http://localhost:3000,http://localhost

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ python-dotenv==1.0.1
1313

1414
# Machine learning
1515
tensorflow==2.18.0
16-
keras==3.8.0
16+
keras==3.9.0
1717
scikit-learn==1.6.1
1818
numpy==1.26.4
1919
scipy==1.15.1
@@ -30,3 +30,4 @@ charset-normalizer==3.4.1
3030
urllib3==2.3.0
3131
click==8.1.8
3232
tqdm==4.67.1
33+
h5py==3.15.1

src/classifier.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# appropriate file permissions. In production, consider using safer serialization
1515
# formats like joblib or JSON + model-specific loaders.
1616

17+
1718
def safe_pickle_load(filepath: str):
1819
"""
1920
Load pickle file with basic safety checks.
@@ -30,6 +31,7 @@ def safe_pickle_load(filepath: str):
3031
with open(filepath, "rb") as f:
3132
return pickle.load(f)
3233

34+
3335
# Load models with safety checks
3436
try:
3537
bayes_model = safe_pickle_load("src/bin/bayes_model_sk.pkl")
@@ -41,7 +43,20 @@ def safe_pickle_load(filepath: str):
4143
# Fallback to old format directory (for backward compatibility)
4244
rnn_model_path = "src/bin/rnn"
4345

44-
rnn_model = keras.models.load_model(rnn_model_path)
46+
# Load with safe_mode=True to prevent arbitrary code execution
47+
# Note: This only works with .keras files, not .h5/.hdf5
48+
try:
49+
rnn_model = keras.models.load_model(rnn_model_path, safe_mode=True)
50+
print("RNN model loaded in safe mode")
51+
except Exception as safe_mode_error:
52+
print(f"Safe mode loading failed: {safe_mode_error}")
53+
# Fallback to unsafe loading only for trusted local models
54+
# In production, this should be removed or require explicit configuration
55+
if os.path.basename(rnn_model_path) == "rnn.keras" or os.path.basename(rnn_model_path) == "rnn":
56+
print("Loading model without safe_mode (trusted local model)")
57+
rnn_model = keras.models.load_model(rnn_model_path, safe_mode=False)
58+
else:
59+
raise ValueError(f"Refusing to load untrusted model: {rnn_model_path}")
4560
except Exception as e:
4661
print(f"Error loading models: {e}")
4762
raise

0 commit comments

Comments
 (0)