T09 · Insecure Skill Coding Practices
- Location
scripts/guoxue.py:794- Finding
Unescaped Query Injection in Generated HTML Reports
- Content
View full analysis
未找到与「{query}」相关的经典内容
" items_html = "" for i, item in enumerate(data, 1): items_html += f"""""" return f""" 经典解读:{query} ...📖 {i}. {item['classic']} · {item['chapter']}
{item['school']} {item['era']} {item['author']}
📜 原文
「{item['text']}」
📝 白话译文
{item['translation']}
💡 深度解读
{item['insight']}
""" def generate_poetry_report(data, query): """生成诗词赏析报告""" if not data: return f"{items_html}📖 经典解读
查询:{query}
未找到与「{query}」相关的诗词
" ``` The same unsafe interpolation also occurs in the poetry report at lines 871 and 888 and in the idiom report at line 898. ### Technical Analysis The command-line `--query` value is controlled by the caller and is inserted directly into generated HTML through Python formatted strings. No contextual HTML encoding, sanitization, or restrictive Content Security Policy is applied. A value containing markup such as: ```html``` can therefore become active HTML when a generated report is ...[truncated 2165 chars]
- Remediation
View remediation
" f"No classic content found for “{safe_query}”" "
" ) ``` 3. Prefer an auto-escaping template engine such as Jinja2 rather than constructing HTML with formatted strings. Keep auto-escaping enabled for HTML templates. 4. Treat all dynamic values as untrusted, including future LLM responses, network-derived content, imported databases, and metadata fields. Encode according to context: - HTML text encoding for element content. - HTML attribute encoding for attribute values. - URL validation for links. - JavaScript serialization for values placed in scripts. 5. Add a restrictive Content Security Policy to generated reports as defense in depth: ```html ``` 6. Add regression tests using payloads such as: ```text``` Tests should verify that these values appear only as encoded text and never produce executable elements. ]]>
