@@ -41,8 +41,8 @@ def __init__(self):
4141 )
4242
4343 self .kms_client = boto3 .client ('kms' , region_name = region )
44- self .models_dir = " /mnt/instance-store/models"
45- self .ollama_url = " http://localhost:11434"
44+ self .models_dir = os . getenv ( 'MODELS_DIR' , ' /mnt/instance-store/models' )
45+ self .ollama_url = os . getenv ( 'OLLAMA_URL' , ' http://localhost:11434' )
4646
4747 # Ensure models directory exists
4848 os .makedirs (self .models_dir , exist_ok = True )
@@ -287,6 +287,21 @@ def decrypt_model(self, encrypted_path: str, bucket: str, datakey_key: str, kms_
287287 def load_model_to_ollama (self , model_path : str , model_name : str , progress_callback = None ) -> Dict [str , Any ]:
288288 """Load model to Ollama and extend PCR15 with model hash"""
289289 try :
290+ # Validate and normalize the model path to prevent path traversal
291+ normalized_path = os .path .normpath (model_path )
292+ safe_model_path = os .path .join (self .models_dir , os .path .basename (normalized_path ))
293+
294+ # Verify the resulting path stays within the models directory
295+ if not os .path .abspath (safe_model_path ).startswith (os .path .abspath (self .models_dir )):
296+ return {"status" : "error" , "message" : "Invalid model path: path traversal detected" }
297+
298+ # Check if the safe path exists and use it
299+ if not os .path .exists (safe_model_path ):
300+ return {"status" : "error" , "message" : f"Model file not found in safe directory: { safe_model_path } " }
301+
302+ # Use the validated safe path
303+ model_path = safe_model_path
304+
290305 # Get file size for progress calculation
291306 file_size = os .path .getsize (model_path )
292307
0 commit comments