T09 · Insecure Skill Coding Practices
- Location
scripts/transcribe.py:64- Finding
Arbitrary Python Code Execution Through Unsafe Source-Code Generation
- Content
View full analysis
Vulnerability Details
File Location:
scripts/transcribe.py, lines 64–98
Vulnerability Type: Python source-code injection
Risk Level: Highpython cache_dir = WHISPER_CACHE.replace("\\", "/") video_path_escaped = video_path.replace("\\", "/") python_code = f""" import whisper, os, sys, io, time sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') cache_dir = r'{cache_dir}' os.environ['WHISPER_DOWNLOAD_ROOT'] = cache_dir print(' 加载模型...') start_load = time.time() model = whisper.load_model('{model_name}', download_root=cache_dir) print(f' 模型加载耗时: {{time.time()-start_load:.1f}}秒') print(' 转录中...') start_trans = time.time() result = model.transcribe( r'{video_path_escaped}', language='zh', initial_prompt='''{init_prompt}''', temperature=0.0, condition_on_previous_text=False, word_timestamps=False ) elapsed = time.time() - start_trans print(f' 转录耗时: {{elapsed:.1f}}秒') for seg in result['segments']: start = seg['start'] end = seg['end'] text = seg['text'].strip() if text: print(f'[{{start:.1f}}s-{{end:.1f}}s] {{text}}') print('---FULLTEXT---') print(result['text']) """ result = subprocess.run( ["python", "-c", python_code], capture_output=True, text=True, encoding='utf-8', errors='replace' )Technical Analysis
The function constructs executable Python source with an f-string and passes the resulting text to
python -c. The optional transcription prompt is assigned toinit_promptand interpolated directly inside a triple-quoted Python string:python initial_prompt='''{init_prompt}''',A prompt containing a triple-quote terminator can escape the intended string literal and introduce arbitrary Python statements into the generated program. Using an argument list for
subprocess.runprevents shell expansion, but it does not prevent injection int ...[truncated 1725 chars]- Remediation
View remediation
Remediation Suggestions
Do not generate executable Python source from user-controlled or environment-controlled values.
- Import
whisperand callwhisper.load_modelandmodel.transcribedirectly in the current process. - If process isolation is required, move the transcription logic into a fixed, reviewed Python module. Pass the prompt, model name, and paths through normal command-line arguments, JSON over standard input, or another structured data channel.
- Validate the model name against the existing strict allowlist in every execution path.
- Normalize and validate cache and download directories before use, but do not treat path validation as a substitute for separating code from data.
- Avoid
eval,exec,python -c, and dynamically generated scripts for transporting user input. - Add regression tests containing triple quotes, newlines, backslashes, quotes, and Python syntax in prompts and paths. Verify that every value remains inert data.
- Run media-processing workloads under a dedicated low-privilege account or sandbox to reduce the impact of future code-execution defects.
- Import
