T09 · Insecure Skill Coding Practices
- Location
scripts/缠论扫描_条件单闭环.py:390- Finding
Stored HTML and Script Injection in Generated Trading Report
- Content
View full analysis
{r['code']} {r['name']} ``` Other dynamic values are also rendered without HTML escaping: ```python rows.append( f"{it['tag']}" f"{coef}" f"{trig}" f"{pct}" f"{it['qty']}" f"{it['reach']}" f"{it['memo']}" ) ``` ```python形态推导{r['detail']}``` ```python{r['tmw_act']}``` The overview table repeats the unescaped stock information: ```python _tmw_rows.append( f"{_fcell}{_r['code']}{_r['name']}" f"{_r['board']}{_r['close']:.2f}" f"{_r['today_act']}" f"{_r['tmw_act']}" ) ``` The resulting document is written as an executable HTML file: ```python with open(out, "w", encoding="utf-8") as f: f.write(html) ``` ### Technical Analysis The report generator builds HTML through direct string interpolation without applying context-appropriate HTML encoding. The documented workflow populates `POOL` using external market-data connectors and manual updates. Consequently, fields such as stock names and codes cross a trust boundary before being embedded in the report. If an attacker controls or compromises a connector response, influences manually supplied pool data, or otherwise places markup in an interpolated field, browser-int ...[truncated 1978 chars]- Remediation
View remediation
{safe_code}" f"{safe_name}" ) ``` 2. Validate structured fields before rendering. For example, restrict stock codes to the expected syntax: ```python import re STOCK_CODE_RE = re.compile(r"^(?:sh|sz)\d{6}$") if not STOCK_CODE_RE.fullmatch(code): raise ValueError(f"Invalid stock code: {code!r}") ``` Use allowlists for board, category, form, and group values. 3. Keep trusted formatting separate from untrusted text. Some internally generated fields intentionally contain HTML formatting such as ``. Do not indiscriminately trust connector-provided strings merely because internal strategy text uses markup. Prefer structured templates where formatting is added by the application and all data values are escaped. 4. Add a restrictive Content Security Policy to reduce the impact of any missed encoding sink: ```html ``` The current report uses inline button handlers. Replace them with non-script controls or move the required behavior to an approved local script with a nonce or hash before enforcing `script-src 'none'`. 5. Add regression tests using adversarial values such as: ```python name = '' ``` Verify that the generated report contains escaped text such as `<img` and no executable element. 6. Treat all connector output as untrusted, ev ...[truncated 179 chars]
