T09 · Insecure Skill Coding Practices
Error
- Location
- module4_report_generator.py:421
- Finding
- Stored HTML Injection Through Unsanitized News and Model Content<![CDATA[ ## Vulnerability Details **File Location**: `module4_report_generator.py:421-499` **Supporting Locations**: `module4_report_generator.py:1454-1457`, `main.py:555-572`, `main.py:699-721` **Vulnerability Type**: Stored HTML injection **Risk Level**: High ### Vulnerable Code ```python hot_news_html = "" for news in hot_news: keywords_html = ', '.join(news.get('keywords', ['财经', '热点'])) hot_news_html += f""" <div class="hot-news-item"> <div class="hot-news-rank">{news['rank']}</div> <div class="hot-news-content"> <div class="hot-news-title">{news['title']}</div> <div class="hot-news-meta"> <span class="source">{news['source']}</span> <span class="heat-score">热度: {news['score']:.1f}分</span> <span class="publish-time">{news['publish_time']}</span> </div> <div class="hot-news-summary">{news['summary']}</div> <div class="hot-news-keywords"> <strong>关键词:</strong> {keywords_html} </div> """ if news.get('url'): hot_news_html += f""" <div class="hot-news-link"> <a href="{news['url']}" target="_blank">查看原文</a> </div> """ deep_reports_html = "" for report_data in deep_reports: analysis_text = report_data.get('analysis', '无分析内容') analysis_html = "" for line in analysis_text.split('\n'): if line.strip(): analysis_html += f"<p>{line}</p>" deep_reports_html += f""" <div class="deep-analysis-item"> <div class="deep-analysis-rank">TOP{report_data['rank']}</div> <div class="deep-analysis-content"> <div class="deep-analysis-title">{report_data['title']}</div> <div class="deep-analysis-meta"> <span class="source">{report_data['source']}</span> <span class="score">评分: {report_data.get('score', 0):.1f}</span> </div> ...[truncated 3169 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Escape every untrusted text value with `html.escape(value, quote=True)` before inserting it into HTML. 2. Use a template engine with automatic escaping enabled rather than constructing HTML with f-strings. 3. Validate links with `urllib.parse.urlparse` and permit only approved `https` hostnames. 4. Reject dangerous schemes such as `javascript:`, `data:`, `file:`, and `vbscript:`. 5. If limited rich text is required, sanitize it with a strict allowlist-based HTML sanitizer. 6. Add a restrictive Content Security Policy, such as disabling inline scripts and limiting network destinations. 7. Disable automatic report opening by default and require explicit user action. 8. Treat model-generated content as untrusted input and apply the same escaping and validation rules used for scraped data. ]]>
