T09 · Insecure Skill Coding Practices
Warning
- Location
- engine/rules.py:123
- Finding
- Incomplete Safety Validation and Unbounded Numeric Input Handling<![CDATA[ ## Vulnerability Details **File Location**: `engine/rules.py:123-130`, `engine/rules.py:195-198`, `engine/rules.py:395-396`, `engine/rules.py:410-411` **Vulnerability Type**: Incomplete input validation and fail-open content-safety enforcement **Risk Level**: Medium ### Complete Vulnerable Code ```python @staticmethod def validate_content_safety(params: StoryParams) -> ValidationResult: """内容安全验证""" errors = [] # 禁止内容关键词 forbidden = ["暴力", "恐怖", "成人", "政治", "violence", "horror", "adult"] theme = params.theme or "" if any(word in theme.lower() for word in forbidden): errors.append(f"主题包含禁止内容: {params.theme}") return ValidationResult(valid=len(errors) == 0, errors=errors) ``` Related unchecked page override: ```python @classmethod def calculate_pages(cls, age: int, override: Optional[int] = None) -> int: """计算页数""" if override: return override config = cls.get_age_config(age) return config["default_pages"] ``` Related unchecked integer conversion and page parsing: ```python params = StoryParams( style=parts[0] if len(parts) > 0 else "storybook", scene=parts[1] if len(parts) > 1 else "meadow", age=int(parts[2]) if len(parts) > 2 else 5, ) ``` ```python for i, part in enumerate(parts[3:], start=3): if part.isdigit(): params.pages = int(part) ``` ### Technical Analysis The documented safety policy states that safety checks are mandatory and must reject numerous categories, including weapons, dangerous activities, political and religious material, commercial or branded material, substances, bullying, and inappropriate relationships. The executable validator does not implement that policy comprehensively. The validator has the following weaknesses: 1. It checks only seven literal substrings. 2. It examines only `params.theme`. 3. It does not inspect action or expression values passed to `assemble_prompt()`. 4. It does not validate supporting-character entries in ...[truncated 2790 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Implement the complete documented safety policy as structured, testable rules rather than a seven-term substring list. 2. Validate every user-controlled content field, including: - Theme - Emotion - Action - Expression - Main character - Supporting characters - Any free-form prompt additions 3. Normalize input before validation using Unicode normalization, case folding, whitespace normalization, and punctuation handling. 4. Use category-aware detection for violence, weapons, dangerous activities, substances, political content, religious proselytizing, brands, copyrighted characters, and other prohibited categories. 5. Treat ambiguous or unvalidated free-form content as requiring additional review rather than automatically passing. 6. Validate `with_characters` against the supported-character and animal registries. 7. Catch numeric conversion failures and return a controlled validation error: ```python try: age = int(parts[2]) except (ValueError, IndexError): return ValidationResult( valid=False, errors=["Age must be an integer between 3 and 12."] ) ``` 8. Enforce page limits according to `AGE_SYSTEM`, with an additional global maximum: ```python min_pages, max_pages = config["pages"] if override is not None and not min_pages <= override <= max_pages: raise ValueError( f"Page count must be between {min_pages} and {max_pages} for age {age}." ) ``` 9. Add regression tests covering every forbidden category, mixed-case variants, Unicode variants, malformed ages, zero and negative values, and excessively large page counts. 10. Re-run safety validation against the final assembled prompt immediately before returning or writing generated content. ]]>
