T09 · Insecure Skill Coding Practices
- Location
scripts/edu_tool.py:842- Finding
Unvalidated Zero Denominators Can Crash Data Analysis
- Content
View full analysis
Vulnerability Details
File Location:
scripts/edu_tool.py, lines 842-860
Vulnerability Type: Improper input validation resulting in division by zero
Risk Level: LowVulnerable Code:
python # 转化建议 trials = data.get("trials", 0) leads = data.get("leads", 1) if trials / leads < 0.3: recommendations.append("📱 来访-体验转化率低,建议:1)优化首电话术 2)增加体验课频次 3)提供更多诱饵(测评报告等)") # 社群建议 active = data.get("active", 0) total = data.get("total", 1) if active / total < 0.1: recommendations.append("💬 社群活跃度低,建议:1)固定每日内容时间 2)增加互动活动 3)培养核心KOL家长") # 续费建议 renewals = data.get("renewals", 0) students = data.get("total_students", 1) if renewals / students < 0.8: recommendations.append("📈 续费率有提升空间,建议:1)提前1个月启动续费 2)增加增值服务 3)完善学情反馈")Technical Analysis
_generate_recommendations()performs three divisions using values obtained directly from the caller-provideddatadictionary. The default value of1only applies when a key is absent; it does not protect the operation when a caller explicitly supplies zero.Consequently, any of the following values can trigger a
ZeroDivisionError:leads: 0total: 0total_students: 0
Other analysis methods in the same class guard some divisions with conditions such as
if leads > 0, but this method applies no equivalent validation. Becauseanalyze_data()invokes_generate_recommendations()as part of normal report generation, one invalid denominator aborts the entire operation.Attack Path
- An attacker or ordinary user submits an analysis dataset containing a zero denominator, such as:
json { "leads": 0, "trials": 0, "active": 0, "total": 0, "renewals": 0, "total_students": 0 } - The application passes the dataset to
EduPrivateOperations.analyze_data(). analyze_data()invokes_generate_recommendations(data).- The expression `t ...[truncated 634 chars]
- Remediation
View remediation
Remediation Suggestions
- Validate all numeric fields before analysis:
- Require finite numeric values.
- Reject negative counts and costs.
- Define explicit behavior for empty datasets.
- Guard every denominator consistently:
python trial_rate = trials / leads if leads > 0 else 0 activity_rate = active / total if total > 0 else 0 renewal_rate = renewals / students if students > 0 else 0 if trial_rate < 0.3: ... if activity_rate < 0.1: ... if renewal_rate < 0.8: ... - Prefer centralized validation at the beginning of
analyze_data()so all downstream methods receive normalized values. - Return a structured validation error when a denominator is invalid rather than allowing a runtime exception to propagate.
- Add unit tests covering absent, zero, negative, nonnumeric, and extremely large input values.
- Validate all numeric fields before analysis:
