T09 · Insecure Skill Coding Practices
- Location
scripts/migrate_ollama.py:188- Finding
Source Models Are Deleted Even When Migration Verification Fails
- Content
View full analysis
Vulnerability Details
File Location:
scripts/migrate_ollama.py:97-110,scripts/migrate_ollama.py:188-201
Vulnerability Type: Improper migration validation before destructive cleanup
Risk Level: HighVulnerable Code
python # Verify size source_size = sum(f.stat().st_size for f in self.source.rglob("*") if f.is_file()) target_size = sum(f.stat().st_size for f in self.target.rglob("*") if f.is_file()) source_gb = round(source_size / (1024**3), 2) target_gb = round(target_size / (1024**3), 2) print(f" Source size: {source_gb} GB") print(f" Target size: {target_gb} GB") if abs(source_size - target_size) > 1024: print("Warning: sizes do not match!") return Falsepython if not self.verify_migration(): print("Warning: verification failed, but migration may have succeeded") print("Run 'ollama list' manually to verify") # Cleanup if cleanup: print("\nCleaning source files...") try: shutil.rmtree(self.source) print(f"Source directory deleted: {self.source}") except Exception as e: print(f"Cleanup failed: {e}")Technical Analysis
Migration verification is not enforced as a prerequisite for source deletion. When
verify_migration()returnsFalse, execution only prints a warning and then continues into the cleanup block. If the user supplied--cleanup,shutil.rmtree(self.source)recursively deletes the original model directory despite the failed verification.The preceding copy verification is also insufficient for destructive cleanup. It compares only the aggregate byte size of the source and target directories. It does not verify:
- Cryptographic hashes of individual files.
- The complete set of relative file paths.
- Whether files at the target predated the current migration.
- Whether each copied model can be loaded successfully.
- Whether files changed while the copy was in progress.
...[truncated 1484 chars]
- Remediation
View remediation
Remediation Suggestions
- Immediately abort the migration when
verify_migration()returnsFalse; never enter cleanup after a failed verification. - Build a fixed source manifest before copying. Record each relative path, file size, and a cryptographic digest such as SHA-256.
- Compare the destination against that manifest after copying rather than comparing aggregate directory sizes.
- Distinguish files copied during the current operation from files that already existed at the destination.
- Stop cleanup if any file is missing, has a mismatched size or digest, or cannot be read.
- Require a separate explicit confirmation immediately before destructive deletion.
- Prefer renaming the source to a dated rollback directory and retaining it until a later, separate cleanup operation.
- Record verification results and cleanup decisions in a durable migration log.
- Where practical, test loading at least one migrated model in addition to listing model metadata.
- Immediately abort the migration when
