Skip to content

Commit

Permalink
Fix incorrect extraction path for ZIP archives in REST API
Browse files Browse the repository at this point in the history
Correct the file extraction logic to ensure archives are extracted to the correct database path instead of the scan-specific subdirectory.
  • Loading branch information
jlegrand62 committed Feb 6, 2025
1 parent fff51ba commit f5f3751
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/plantdb/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ def post(self, scan_id):
self.logger.debug(f"REST API path to fsdb is '{self.db.path()}'...")
scan_path = Path(self.db.get_scan(scan_id, create=True).path())
self.logger.debug(f"Exporting archive contents to '{scan_path}'...")
db_path = scan_path.parent # move up to db path as the archive contain the top level

# Open the zip file and extract non-existing files:
extracted_files = []
Expand All @@ -1199,16 +1200,16 @@ def post(self, scan_id):
Path(temp_path).unlink(missing_ok=True) # Cleanup temporary file
return {'error': 'Filename encoding error in zip archive'}, 400

file_path = scan_path / file
file_path = db_path / file
# Ensure the extracted files remain within the target directory
if not is_within_directory(scan_path, file_path):
if not is_within_directory(db_path, file_path):
self.logger.error(f"Invalid file path detected in ZIP: '{file}'")
Path(temp_path).unlink(missing_ok=True) # Cleanup temporary file
return {'error': 'Invalid file paths in zip archive'}, 400

# Extract only if the file does not already exist
if not file_path.exists():
zip_obj.extract(file, path=scan_path)
zip_obj.extract(file, path=db_path)
extracted_files.append(file)
except Exception as e:
self.logger.error(f"Failed to extract ZIP archive: {e}")
Expand Down

0 comments on commit f5f3751

Please sign in to comment.