T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/batch_report.py:318
- Finding
- Stored HTML and JavaScript Injection in Generated Reports<![CDATA[ ## Vulnerability Details **File Location**: `scripts/batch_report.py:318,432,475-509`; related unsafe report interpolation in `scripts/report.py:547-579,725` **Vulnerability Type**: Stored cross-site scripting through unescaped report data **Risk Level**: High ### Vulnerable Code ```python # scripts/batch_report.py import json rows_json = json.dumps(rows_data, ensure_ascii=False) ``` ```html <script> // Data is inserted directly into an executable script context. var ROWS = {rows_json}; ``` ```javascript var sigText = r.signal || ''; var sigDisplay = sigText ? ('<td class="' + sigCss + '">' + sigText + '</td>') : '<td style="color:var(--text-dim)">-</td>'; html += '<tr style="display:table-row" data-sig="' + (r.sig_cat || '') + '">' + '<td><strong>' + r.code + '</strong></td>' + '<td>' + r.name + '</td>' + sigDisplay + '<td style="color:' + vbtC + ';font-weight:600;">' + vbtV + '</td>' + '<td>' + r.best_name + '</td>' + '<td>' + statusCell + '</td>' + '</tr>'; document.getElementById('table-body').innerHTML = html; ``` The individual report generator contains a second unsafe HTML interpolation path: ```python # scripts/report.py label = f"{ticker} {stock_name}" if stock_name else ticker html = f"""<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{label} - VectorBT 向量化回测分析报告</title> ... <div class="header"> <h1>{label} - VectorBT 向量化回测分析报告</h1> ... </body> </html>""" with open(out_path, 'w', encoding='utf-8') as f: f.write(html) ``` ### Technical Analysis Stock names, signal text, report filenames, strategy names, and other values originating from CSV/XLS input or external market-data services are embedded into generated HTML without context-sensitive escaping. `json.dumps()` produces valid JSON, but it does not make arbitrary strings safe inside an HTML `<script>` element. In particular, an attacker-contro ...[truncated 2010 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Avoid building HTML with string concatenation and `innerHTML`. Create DOM elements and assign untrusted values through `textContent`. 2. Assign link destinations through validated DOM properties and reject values that are not expected local report filenames. 3. Escape all server-generated HTML values with `html.escape(value, quote=True)` according to their HTML text or attribute context. 4. When placing JSON in an inline script, escape at least `<`, `>`, `&`, U+2028, and U+2029. Prefer placing JSON in a non-executable element and parsing its `textContent`. 5. Validate tickers against the documented six-digit A-share format. 6. Treat names and signals from CSV files and external APIs as untrusted data. 7. Add a restrictive Content Security Policy that disallows inline scripts and event handlers. Use a nonce or external local script where scripting is required. 8. Add regression tests using payloads containing HTML tags, quotes, event handlers, `</script>`, and encoded variants. ]]>
