T09 · Insecure Skill Coding Practices
Error
- Location
- automd-viz.sh:149
- Finding
- Command Injection Through Generated PyMOL and Python Commands<![CDATA[ ## Vulnerability Details **File Location**: `automd-viz.sh:149-181` **Vulnerability Type**: Injection of untrusted CLI and environment values into executable PyMOL and Python command text **Risk Level**: High ### Vulnerable Code ```bash cat > "${OUTPUT_DIR}/pymol_script.pml" << EOF # PyMOL Publication-Quality Rendering Script load ${structure}, protein hide everything show ${style} color ${color} # High-quality rendering settings set ray_trace_mode, 1 set ray_shadows, 1 set ray_trace_fog, 0 set antialias, 2 set ambient, 0.4 set specular, 0.5 set shininess, 10 set depth_cue, 0 set ray_opaque_background, 1 # View optimization orient zoom # Output ray ${OUTPUT_DPI}, ${OUTPUT_DPI} png ${output}, dpi=${OUTPUT_DPI} quit EOF if command -v pymol &>/dev/null; then pymol -c "${OUTPUT_DIR}/pymol_script.pml" || error "PyMOL execution failed" else python3 -c "import pymol; pymol.cmd.do('run ${OUTPUT_DIR}/pymol_script.pml')" || error "PyMOL execution failed" fi ``` The affected values originate from command-line arguments or environment variables: ```bash OUTPUT_DIR="${OUTPUT_DIR:-publication-viz}" OUTPUT_DPI="${OUTPUT_DPI:-300}" STRUCTURE_STYLE="${STRUCTURE_STYLE:-cartoon}" STRUCTURE_COLOR="${STRUCTURE_COLOR:-spectrum}" ``` ```bash --structure) INPUT_STRUCTURE="$2"; shift 2 ;; --style) STRUCTURE_STYLE="$2"; shift 2 ;; --color) STRUCTURE_COLOR="$2"; shift 2 ;; -o) OUTPUT_DIR="$2"; shift 2 ;; --dpi) OUTPUT_DPI="$2"; shift 2 ;; ``` ### Technical Analysis The script directly interpolates the structure path, rendering style, color, output path, and DPI into a generated PyMOL command file. These values are not escaped and are not restricted to safe character sets or documented allowlists. A value containing a newline or other PyMOL command-language delimiters can change the generated script's command structure and append unintended PyMOL commands. The risk is particularly significant because PyMOL scripting can invoke Python functionality and interac ...[truncated 1951 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Enforce strict allowlists before generating any PyMOL command: - Style: `cartoon`, `surface`, `sticks`, or `spheres`. - Color: a finite set of explicitly supported PyMOL color schemes. - DPI: an integer within a reasonable range, such as 72–1200. - Output format: `png`, `svg`, `pdf`, or `eps`. 2. Reject values containing control characters, carriage returns, or newlines. 3. Avoid generating PyMOL source from interpolated text. Use the PyMOL Python API and pass validated strings as function arguments, for example through `cmd.load`, `cmd.show`, `cmd.color`, and `cmd.png`. 4. Do not embed `OUTPUT_DIR` in `python3 -c`. Pass it as a positional argument: ```bash python3 -c 'import pymol, sys; pymol.cmd.do("run " + sys.argv[1])' \ "${OUTPUT_DIR}/pymol_script.pml" ``` 5. Canonicalize and validate paths. Ensure generated files remain under the intended output directory when that confinement is a security requirement. 6. Add automated tests containing quotes, commas, semicolons, backslashes, newlines, and Unicode control characters to verify that inputs remain data rather than executable syntax. ]]>
