T09 · Insecure Skill Coding Practices
- Location
scripts/export_generator.py:73- Finding
Stored HTML and Script Injection in Exported Travelogues
- Content
View full analysis
Vulnerability Details
File Location:
scripts/export_generator.py:73-100,scripts/export_generator.py:150-169, andassets/templates/travelogue.html:268-271
Vulnerability Type: Stored HTML injection / stored cross-site scripting
Risk Level: HighVulnerable Code
scripts/export_generator.py:73-100:python def generate_markdown(trip: Dict, moments: List[Dict]) -> str: """生成 Markdown 格式的游记""" # 游记标题 title = trip.get('title', '我的旅行') md_lines = [f"# 【{title}】", ""] # 旅行开始信息 start_time = format_date(trip['startTime']) start_loc = trip.get('startLocation', '') # 按日期分组 date_groups = group_moments_by_date(moments) dates = sorted(date_groups.keys()) # 为每一天生成内容 for i, date in enumerate(dates, 1): day_moments = date_groups[date] # 提取这一天的主要地点 locations = [m.get('location') for m in day_moments if m.get('location')] main_location = locations[0] if locations else "未知地点" md_lines.append(f"## Day {i} · {main_location}") md_lines.append(f"> {date}") md_lines.append("") # 按时间顺序记录每个素材 for m in day_moments: time_str = format_date(m['timestamp']) if m['type'] == 'text': md_lines.append(m['content']) md_lines.append("") elif m['type'] == 'image': md_lines.append(f"})")scripts/export_generator.py:150-169:python def generate_html_from_markdown(md_content: str, trip: Dict) -> str: """从 Markdown 生成 HTML""" # 使用 markdown 库转换 html_content = markdown.markdown( md_content, extensions=['extra', 'codehilite', 'tables'] ) # 使用模板 template_path = os.path.join(TEMPLATES_DIR, 'trav ...[truncated 3122 chars]- Remediation
View remediation
Remediation Suggestions
-
Sanitize converted HTML with a strict allowlist before passing it to the template. Permit only required formatting elements and safe attributes.
-
Remove executable elements and attributes, including script-capable tags, inline event handlers, dangerous URL schemes, embedded frames, and active SVG content.
-
Enable Jinja autoescaping:
python from jinja2 import Environment, FileSystemLoader, select_autoescape env = Environment( loader=FileSystemLoader(TEMPLATES_DIR), autoescape=select_autoescape(['html', 'xml']) ) -
Use
|safeonly after the HTML has passed through a trusted sanitizer. Do not treat arbitrary Markdown conversion output as inherently safe. -
Escape or normalize user-controlled title, location, description, and caption values before composing Markdown.
-
Validate image and media destinations against an approved local upload directory and reject unexpected URL schemes.
-
Add regression tests covering raw HTML, event-handler attributes, malformed Markdown links, active SVG, and script-capable URL schemes.
-
Where possible, apply a restrictive Content Security Policy to hosted exports, disabling inline scripts and limiting resource origins.
-
