T09 · Insecure Skill Coding Practices
- Location
scripts/generate.py:527- Finding
Stored HTML and JavaScript Injection in Generated Reports
- Content
View full analysis
") app_html += f"""""" ``` Additional direct interpolation occurs in the document title and report header: ```python 专精特新申报材料 — {enterprise.get('name', '企业')} ({tier['name']}) ``` ```python{section['title']}
{content}{enterprise.get('name', '企业名称')} | 申报梯度:{tier['name']}``` ### Technical Analysis Enterprise data originates from attacker-controllable JSON input and is incorporated into application sections. The report generator converts newline characters to `
` elements but does not HTML-escape the content before inserting it into the generated document. Consequently, HTML elements, event handlers, and script elements supplied through fields such as `intro`, `name`, `market_position`, `core_tech`, or other application values are written directly into the report. For example, a malicious input can contain: ```json { "tier": "t2", "name": "Example Company", "intro": "" } ``` When the resulting report is opened in a browser, the embedded script is interpreted as active content rather than displayed as enterprise data. This is a stored injection because the payload is persisted in the generated HTML file. ### Attack Path 1. An attacker supplies or modifies the JSON input consumed by `generate.py`. 2. The malicious markup is accepted without schema validation or content sanitization. 3. `generate_application_text()` incorporates the attacker-controlled val ...[truncated 1274 chars]- Remediation
View remediation
") safe_title = html.escape(str(section["title"]), quote=True) ``` 2. Escape enterprise names and all other interpolated fields according to their output context: ```python safe_name = html.escape(str(enterprise.get("name", "Enterprise")), quote=True) ``` 3. Use a template engine with automatic escaping enabled rather than constructing HTML through f-strings. 4. Keep plain-text application data separate from trusted report markup. Do not allow raw HTML unless explicitly required and sanitized with a maintained allowlist-based sanitizer. 5. Add a restrictive Content Security Policy, preferably prohibiting inline scripts: ```html ``` 6. Validate input with a strict schema, including expected types, maximum lengths, and permitted formats. 7. Add regression tests covering payloads in every enterprise field, including `
