T09 · Insecure Skill Coding Practices
- Location
scripts/mlx_asr.py:76- Finding
Temporary audio and transcript artifacts are retained after processing
- Content
View full analysis
Vulnerability Details
File Location:
scripts/mlx_asr.py:76-82,scripts/mlx_asr.py:99, andscripts/mlx_tts_voice.py:52-53
Vulnerability Type: Insecure temporary-file lifecycle and sensitive-data retention
Risk Level: MediumVulnerable Code
scripts/mlx_asr.py:76-82:python def _ffmpeg_to_wav(src: Path) -> Path: """Convert audio to 16k mono wav using ffmpeg (best-effort).""" import subprocess import tempfile out = Path(tempfile.mkstemp(prefix="mlx_asr_", suffix=".wav")[1]) cmd = ["ffmpeg", "-y", "-i", str(src), "-ac", "1", "-ar", "16000", str(out)] p = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)scripts/mlx_asr.py:99:python out_prefix = Path(tempfile.mkdtemp(prefix="mlx_asr_out_")) / "out"scripts/mlx_tts_voice.py:52-53:python tmpdir = Path(tempfile.mkdtemp(prefix="mlx_tts_")) mp3_path = tmpdir / "reply.mp3"Technical Analysis
The ASR script creates a temporary WAV containing converted user speech and a temporary output directory in which the external
mlx_audiocommand can store transcripts or JSON results. The TTS script similarly creates a temporary directory containing an MP3 representation of the generated reply.None of these temporary files or directories are deleted on successful completion or on an error path. Consequently, private source speech, generated speech, and transcript data may remain on local storage indefinitely.
In addition,
tempfile.mkstemp()returns an open file descriptor and a path. The code retains only the path and does not close the returned descriptor. Repeated processing can therefore leak file descriptors until the Python process terminates. The temporary names themselves are securely generated, so this is not a predictable-name or symlink vulnerability; the primary risks are sensitive-data retention and resource leakage.Attack Path
...[truncated 1647 chars]
- Remediation
View remediation
Remediation Suggestions
- Use
tempfile.TemporaryDirectory()as a context manager for all intermediate directories so they are recursively removed after processing. - Use
tempfile.NamedTemporaryFile()as a context manager for converted audio where platform behavior permits. - If
mkstemp()remains necessary, preserve and immediately close its descriptor withos.close(fd). - Place cleanup in a
finallyblock so artifacts are removed after both successful processing and exceptions. - Delete partial output files when
ffmpegormlx_audiofails. - Avoid writing transcripts to disk when standard output or an in-memory API is available.
- Apply restrictive permissions to temporary directories and files as defense in depth.
- Consider an explicit retention policy if artifacts must be preserved for diagnostics, with user consent, short expiration periods, and secure deletion where supported.
Example hardening pattern:
python import os import shutil import tempfile from pathlib import Path tmpdir = Path(tempfile.mkdtemp(prefix="mlx_asr_out_")) try: out_prefix = tmpdir / "out" # Run processing and consume the result. finally: shutil.rmtree(tmpdir, ignore_errors=True) fd, temporary_name = tempfile.mkstemp(prefix="mlx_asr_", suffix=".wav") os.close(fd) temporary_path = Path(temporary_name) try: # Convert and process the audio. pass finally: temporary_path.unlink(missing_ok=True)- Use
