luulinh90s commited on
Commit
38fa440
·
1 Parent(s): 5768e8b
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -259,16 +259,25 @@ def completed(username):
259
  logger.exception(f"An error occurred in the completed route: {e}")
260
  return "An error occurred", 500
261
 
 
262
  @app.route('/visualizations/<path:filename>')
263
  def send_visualization(filename):
264
  logger.info(f"Attempting to serve file: {filename}")
265
  # Ensure the path is safe and doesn't allow access to files outside the intended directory
266
- safe_path = os.path.join(os.getcwd(), filename)
267
- directory = os.path.dirname(safe_path)
268
- file_name = os.path.basename(safe_path)
 
 
 
 
 
 
 
269
  logger.info(f"Serving file from directory: {directory}, filename: {file_name}")
270
  return send_from_directory(directory, file_name)
271
 
 
272
  if __name__ == "__main__":
273
  os.makedirs('session_data', exist_ok=True) # Ensure the directory for session files exists
274
  app.run(host="0.0.0.0", port=7860, debug=True)
 
259
  logger.exception(f"An error occurred in the completed route: {e}")
260
  return "An error occurred", 500
261
 
262
+
263
  @app.route('/visualizations/<path:filename>')
264
  def send_visualization(filename):
265
  logger.info(f"Attempting to serve file: {filename}")
266
  # Ensure the path is safe and doesn't allow access to files outside the intended directory
267
+ base_dir = os.getcwd()
268
+ file_path = os.path.normpath(os.path.join(base_dir, filename))
269
+ if not file_path.startswith(base_dir):
270
+ return "Access denied", 403
271
+
272
+ if not os.path.exists(file_path):
273
+ return "File not found", 404
274
+
275
+ directory = os.path.dirname(file_path)
276
+ file_name = os.path.basename(file_path)
277
  logger.info(f"Serving file from directory: {directory}, filename: {file_name}")
278
  return send_from_directory(directory, file_name)
279
 
280
+
281
  if __name__ == "__main__":
282
  os.makedirs('session_data', exist_ok=True) # Ensure the directory for session files exists
283
  app.run(host="0.0.0.0", port=7860, debug=True)