T09 · Insecure Skill Coding Practices
- Location
scripts/molecular_docking.py:45- Finding
OS Command Injection Through User-Controlled File and Directory Paths
- Content
View full analysis
Vulnerability Details
File Location:
scripts/molecular_docking.py, lines 45-46, 90-91, 146-147, and 171-172
Vulnerability Type: Shell command injection
Risk Level: HighVulnerable Code
python temp_pdb = os.path.join(self.output_dir, "protein_no_water.pdb") io.save(temp_pdb, NonWaterAndStandardSelect()) cmd = f"obabel {temp_pdb} -O {self.prepared_pdbqt} -xr -h --partialcharge gasteiger" subprocess.run(cmd, shell=True, check=True, capture_output=True)python p2rank_output = os.path.join(self.output_dir, "p2rank_output") os.makedirs(p2rank_output, exist_ok=True) cmd = f"prank predict -f {self.protein_pdb} -o {p2rank_output}" subprocess.run(cmd, shell=True, check=True, capture_output=True)python cmd = f"vina --config {config_file}" result = subprocess.run(cmd, shell=True, capture_output=True, text=True)python ligand_pdb = os.path.join(self.output_dir, f"ligand_mode_{i+1}.pdb") cmd = f"obabel {self.docking_pdbqt} -O {ligand_pdb} -m -f {i+1} -l {i+1}" subprocess.run(cmd, shell=True, check=True, capture_output=True)Technical Analysis
The script constructs shell command strings by directly interpolating paths derived from command-line arguments. In particular:
self.output_diroriginates from the user-controlled--output_dirargument.self.protein_pdboriginates from the user-controlled--proteinargument.config_file,prepared_pdbqt,p2rank_output, anddocking_pdbqtare derived from those paths.
These strings are passed to
subprocess.run(..., shell=True). Consequently, shell metacharacters such as semicolons, command substitutions, redirections, pipes, and comment characters are interpreted by the operating-system shell rather than treated as literal filename characters.No shell is required for any of these operations.
obabel,prank, andvinacan all be invoked directly with an argument list. Using a shell ther ...[truncated 2268 chars]- Remediation
View remediation
Remediation Suggestions
-
Replace every shell command string with a fixed argument list and leave
shelldisabled:python subprocess.run( [ "obabel", temp_pdb, "-O", self.prepared_pdbqt, "-xr", "-h", "--partialcharge", "gasteiger", ], check=True, capture_output=True, text=True, )python subprocess.run( ["prank", "predict", "-f", self.protein_pdb, "-o", p2rank_output], check=True, capture_output=True, text=True, )python result = subprocess.run( ["vina", "--config", config_file], check=True, capture_output=True, text=True, )python subprocess.run( [ "obabel", self.docking_pdbqt, "-O", ligand_pdb, "-m", "-f", str(i + 1), "-l", str(i + 1), ], check=True, capture_output=True, text=True, ) -
Resolve paths with
pathlib.Path.resolve()and enforce an approved workspace root if the deployment model does not require arbitrary filesystem locations. -
Reject output directories that are files, symlinks to unauthorized locations, or outside the permitted workspace.
-
Validate that the input protein is a regular file with an expected extension and impose reasonable file-size limits before parsing it.
-
Run docking tools under a dedicated, unprivileged account or sandbox with:
- No access to unrelated user files or credentials.
- Restricted outbound network access.
- CPU, memory, process, and storage limits.
- A dedicated temporary and output directory.
-
Add regression tests using paths containing spaces and shell metacharacters to verify that th ...[truncated 74 chars]
-
