T09 · Insecure Skill Coding Practices
- Location
scripts/report_generator.py:72- Finding
Stored HTML and JavaScript Injection in Generated Legal Reports
- Content
View full analysis
未检索到相关法规", cases_section=cases_html if cases_html else "未检索到相关案例
", similar_section=similar_html if similar_html else "未检索到类案
", analysis_section=analysis_html if analysis_html else "暂无分析建议
", ) ``` Legal-data fields are also interpolated without HTML escaping: ```python items.append(f"""""") ``` Case information is handled in the same unsafe manner: ```python items.append(f"""{title} {num}{sxx} {effect}{content}""") ``` Similar-case fields a ...[truncated 3023 chars]{title or case_num} {case_num}🏛️ {court} 📅 {date} 📋 {procedure}{summary}{f'判决结果:{result}' if result else ''}- Remediation
View remediation
str: if value is None: return "" return escape(str(value), quote=True) ``` Apply this function to the title, query, source, legal fields, case fields, similar-case fields, and every analysis item. 2. Prefer an auto-escaping template engine such as Jinja2: ```python from jinja2 import Environment, select_autoescape env = Environment(autoescape=select_autoescape(["html", "xml"])) ``` Keep trusted structural HTML separate from untrusted text values. 3. Validate the input schema before rendering. Require expected container and scalar types, reject unexpected objects, and enforce reasonable field-length limits. 4. If selected rich-text fields must support formatting, sanitize them with a maintained HTML sanitizer and a minimal allowlist. Do not permit script elements, event-handler attributes, embedded frames, active URLs, or arbitrary styles. 5. Add a restrictive Content Security Policy to the generated report, for example: ```html ``` CSP should be defense in depth and must not replace output escaping. 6. Add automated tests using payloads in every report field, including closing tags, script elements, event handlers, encoded markup, and malicious URL schemes. Verify that payloads appear only as text in the resulting report. ]]>
