T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/render-cp2k-input.py:115
- Finding
- Unsanitized Request Fields Permit CP2K Input Directive Injection<![CDATA[ ## Vulnerability Details **File Location**: `scripts/normalize-request.py:199-203`, `scripts/normalize-request.py:268-272`, `scripts/render-cp2k-input.py:115-126`, and `scripts/render-cp2k-input.py:318-323` **Vulnerability Type**: Generated-input injection through unvalidated template fields **Risk Level**: Medium ### Complete Code Snippet ```python def infer_xc(raw: Dict[str, Any], defaults: List[str]) -> str: explicit = raw.get("xc_functional") if isinstance(explicit, str) and explicit.strip(): return explicit.strip().upper() defaults.append("Defaulted xc_functional=PBE") return "PBE" ``` ```python def infer_basis_and_potential(job: Dict[str, Any], raw: Dict[str, Any], defaults: List[str], review: List[str]) -> None: if isinstance(raw.get("basis_family"), str) and raw["basis_family"].strip(): job["basis_family"] = raw["basis_family"].strip() if isinstance(raw.get("potential_family"), str) and raw["potential_family"].strip(): job["potential_family"] = raw["potential_family"].strip() ``` ```python def format_kind_blocks(elements: List[str], basis_family: str, xc: str) -> str: chunks = [] for e in elements: chunks.append( "\n".join([ f" &KIND {e}", f" BASIS_SET {basis_for(e, basis_family)}", f" POTENTIAL {potential_for(e, xc)}", " &END KIND", ]) ) return "\n".join(chunks) ``` ```python sections += [ periodic_poisson(job['periodicity']), " &QS", " METHOD GPW", " &END QS", scf_block(job), " &MGRID", f" CUTOFF {job['cutoff']}", f" REL_CUTOFF {job['rel_cutoff']}", " &END MGRID", " &XC", " &XC_FUNCTIONAL", f" &{job['xc_functional'].upper()}\n &END {job['xc_functional'].upper()}", " &END XC_FUNCTIONAL", ``` ### Technical Analysis The normalizer accepts arbitrary strings ...[truncated 2287 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Define explicit allowlists for supported exchange-correlation functionals, basis families, and potential families. 2. Reject values containing carriage returns, line feeds, tabs, control characters, `&`, `@`, quotes, or other CP2K grammar metacharacters. 3. Apply strict full-string validation, for example with a conservative identifier pattern appropriate to each field rather than a generic free-form string. 4. Validate normalized data again in the renderer. Do not assume that every job specification was produced by the bundled normalizer. 5. Represent supported methods as internal enum values and map those values to fixed CP2K fragments rather than interpolating user text. 6. Apply equivalent validation to every field inserted into generated inputs, including project names, periodicity, run types, SCF modes, optimizers, MD values, and element labels. 7. Add negative tests using multiline values, section terminators, preprocessor syntax, and unexpected Unicode control characters. Tests should verify that rendering fails safely instead of emitting modified CP2K structure. ]]>
