T09 · Insecure Skill Coding Practices
- Location
pregna_risk.py:116- Finding
Unvalidated JSON Factor Values Can Corrupt Clinical Risk Results
- Content
View full analysis
float: """Compute deterministic PREGNA-RISK score.""" score = 0.0 for factor_key, present in patient.items(): if present and factor_key in RISK_FACTORS: score += RISK_FACTORS[factor_key]["weight"] return max(score, 0) # floor at 0 ``` The same implicit truth-value handling is used when selecting factors for Monte Carlo analysis: ```python active_factors = [k for k, v in patient.items() if v and k in RISK_FACTORS] ``` The JSON interface passes externally supplied values to these functions without schema validation: ```python data = json.loads(sys.stdin.read()) patient = data.get("factors", {}) pid = data.get("patient_id", "ANON") score = compute_score(patient) cat, rec = classify_risk(score) mc = monte_carlo_ci(patient) ``` ### Technical Analysis The type annotation `Dict[str, bool]` is not enforced at runtime. The JSON interface does not verify that `data` and `factors` are objects or that every factor value has the exact Boolean type. Python interprets many non-Boolean values as truthy, including non-empty strings, non-empty arrays, non-empty objects, and nonzero numbers. Consequently, values such as `"false"` or `"no"` activate a risk factor even though they appear to express a negative response. The deterministic score and Monte Carlo analysis both use this unsafe interpretation. Malformed top-level structures can also trigger unhandled exceptions. For example, a JSON array has no `get` method, while a non-object `factors` value may have no `items` method. This creates an availability issue for integrations that repeatedly invoke the command. ### Attack Path 1. An application exposes or wraps the ` ...[truncated 1425 chars]- Remediation
View remediation
