T09 · Insecure Skill Coding Practices
- Location
scripts/store_inspection.py:642- Finding
Stored Cross-Site Scripting in Generated HTML Reports
- Content
View full analysis
{name} {grade}级{stats['latest_score']}/100巡检{stats['count']}次 最高{stats['max_score']} {trend_icon}趋势""" ``` Issue fields are also inserted into table markup without contextual encoding: ```python issue_rows = "" for issue in all_issues[-15:]: sev_color = "#ef4444" if issue.get("severity") == "P0" else "#eab308" issue_rows += f""" {issue.get('severity','P1')} {issue.get('dimension','')} {issue.get('item','')} {issue.get('action','')} """ ``` The latest store name is likewise rendered directly: ```python {latest['store_name'] if latest else ''} - {latest['inspection_date'][:10] if latest else ''} | 总分 {latest['total_score'] if latest else 0}/100 | {latest['grade'] if latest else ''} ``` Store names are additionally serialized into an executable inline script: ```python labels: {json.dumps(list(store_stats.keys()), ensure_ascii=False)}, ``` ### Technical Analysis Inspection values originate from interactive user input or locally stored JSON records. The report generator treats those values as trusted and concatenates them directly into an HTML document. At ...[truncated 2638 chars]- Remediation
View remediation
... ``` Before embedding serialized JSON in HTML, encode HTML-significant characters, especially `<`, for example by replacing it with `\u003c`. Parse the data using `JSON.parse(document.getElementById("report-data").textContent)`. 3. Prefer constructing report elements through DOM APIs and assigning untrusted values through `textContent` rather than `innerHTML`. 4. Validate every loaded inspection file against a strict schema. Enforce expected types, maximum lengths, and allowed values for fields such as severity and status. Validation is defense in depth and must not replace output encoding. 5. Add a restrictive Content Security Policy. Remove inline scripts where practical and disallow inline event handlers. A suitable baseline should restrict scripts to explicitly approved sources. 6. Add regression tests covering payloads containing: - `` - Event-handler attributes - Quotes and apostrophes - Ampersands and angle brackets - Unicode and malformed markup 7. Consider bundling Chart.js locally. If the CDN remains in use, add Subresource Integrity and `crossorigin="anonymous"` to reduce dependency-tampering risk. ]]>
