T09 · Insecure Skill Coding Practices
- Location
handler.py:475- Finding
Malformed Input Types Cause Unhandled Exceptions and Denial of Service
- Content
View full analysis
Vulnerability Details
File Location:
handler.py:475-516, with vulnerable type assumptions athandler.py:226-230andhandler.py:273-290
Vulnerability Type: Improper input validation and uncaught exceptions
Risk Level: MediumVulnerable Code
handler.py:475-516:python def analyze(input_data: Dict[str, Any]) -> Dict[str, Any]: """ Main analysis entry point, dispatching according to intent. """ # Input length validation raw_str = json.dumps(input_data, ensure_ascii=False) if len(raw_str) > 10000: return { "status": "error", "module": "input_validation", "result": {}, "errors": [f"Input data is too long ({len(raw_str)} characters), exceeding the 10000-character limit"], } intent = input_data.get("intent", "") if intent == "health_score": module = "health_score" result = {"health_score": health_score(input_data)} elif intent == "keyword_analysis": module = "keyword_analysis" result = {"keyword_analysis": keyword_analysis(input_data)} elif intent == "competitor_benchmark": module = "competitor_benchmark" result = {"competitor_benchmark": competitor_benchmark(input_data)} elif intent == "full_optimization": module = "full_optimization" result = { "health_score": health_score(input_data), "keyword_analysis": keyword_analysis(input_data), "competitor_benchmark": competitor_benchmark(input_data), "optimization_package": full_optimization(input_data), } else: # Default to health-score analysis module = "health_score" result = {"health_score": health_score(input_data)} return { "status": "success", "module": module, "result": result, }Representative downstr ...[truncated 3402 chars]
- Remediation
View remediation
Remediation Suggestions
-
Validate the top-level value before calling
json.dumps()or.get():python if not isinstance(input_data, dict): return { "status": "error", "module": "input_validation", "result": {}, "errors": ["Input must be a JSON object"], } -
Define and enforce a strict schema:
intent,product_title,product_description,search_terms,backend_keywords,competitor_asin, andproduct_categorymust be strings.bullet_pointsandproduct_featuresmust be lists of strings.- Reject
null, objects, numbers, booleans, and nested lists where strings are expected. - Restrict list lengths and individual string lengths in addition to the total serialized-size limit.
- Reject unsupported intents instead of silently routing them to health scoring.
-
Return validation failures through the documented error structure. For example:
python def validation_error(message: str) -> Dict[str, Any]: return { "status": "error", "module": "input_validation", "result": {}, "errors": [message], } -
Add a narrow exception boundary at the external handler interface as defense in depth. Log internal diagnostic details securely, but return a generic structured error to callers. Schema validation should remain the primary control rather than relying solely on broad exception handling.
-
Add negative tests covering:
- Non-dictionary top-level inputs
nullfields- Numeric, boolean, object, and list substitutions
- Mixed-type
bullet_pointsandproduct_features - Excessive list sizes and oversized individual fields
- Unsupported intent values
-
If the handler is exposed through a service, also apply request-size limits, rate limiting, worker isolation, and exception monitoring at the service bo ...[truncated 7 chars]
-
