T09 · Insecure Skill Coding Practices
- Location
scripts/generate_image.py:275- Finding
Unescaped User Input Allows Script Execution in Generated HTML
- Content
View full analysis
{line}' for line in content_lines if line.strip()) # 作者首字母 author_initial = config.get('author', 'P')[0] # 当前日期 date = datetime.now().strftime('%Y-%m-%d') html = TEMPLATE.format( title=config.get('title', '今日分享'), content_html=content_html or '每天进步一点点
', tag=config.get('tag', '📌 今日分享'), author=config.get('author', '袁佳鹏'), author_initial=author_initial, date=date, emoji=config.get('emoji', '💜'), bg_start=color['start'], bg_end=color['end'], border_radius=style['border_radius'], shadow=style['shadow'], title_size=style['title_size'], content_size=style['content_size'], ) with open(output_path, 'w', encoding='utf-8') as f: f.write(html) ``` ### Technical Analysis The generator accepts user-controlled values through command-line arguments or the `--prompt` option. The title, content, tag, author, author initial, and emoji fields are inserted directly into the HTML template without context-appropriate escaping. In particular, content is enclosed in a `` element through string concatenation, but the content itself is not escaped: ```python content_html = ''.join(f'
{line}
' for line in content_lines if line.strip()) ``` An attacker can supply markup that closes the generated element and inserts an executable element or event handler. Other fields can similarly break out of their corresponding HTML contexts. For example, a malicious title could contain: ```html ``` The resulting file is subsequently hosted by `SimpleHTTPRequestHandler` and automatically opened through `webbrowser.open()`. The injected ...[truncated 1709 chars]- Remediation
View remediation
{escape(line, quote=True)}' for line in content_lines if line.strip() ) author_initial = escape(author[:1], quote=True) ``` 2. Keep user content in text contexts. If rich HTML is required, process it through a well-maintained allowlist sanitizer that removes scripts, event-handler attributes, dangerous URLs, embedded frames, and active SVG content. 3. Add a restrictive Content Security Policy to generated documents, for example: ```html ``` 4. Serve generated files from a dedicated, otherwise empty output directory instead of changing into an arbitrary user-selected directory. This prevents unrelated files from becoming accessible through `SimpleHTTPRequestHandler`. 5. Validate the output path and avoid overwriting arbitrary existing files. Where possible, create a unique temporary output directory with restrictive permissions. 6. Add regression tests using payloads containing `
