T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/wechat_formatter.py:76
- Finding
- Unescaped HTML and Unsafe URL Scheme Injection in WeChat Output<![CDATA[ ## Vulnerability Details **File Location**: `scripts/wechat_formatter.py`, lines 76-102 **Vulnerability Type**: HTML injection and unsafe link generation **Risk Level**: Medium ### Vulnerable Code ```python for line in lines: line = line.strip() if not line: continue if line.startswith("# "): html_parts.append(self.HEADER_STYLE.format(text=line[2:])) elif line.startswith("## "): html_parts.append(f'<section style="margin: 24px 0 16px; font-size: 18px; font-weight: bold; color: #333;">{line[3:]}</section>') elif line.startswith("### "): html_parts.append(f'<section style="margin: 20px 0 12px; font-size: 16px; font-weight: bold; color: #555; padding-left: 12px; border-left: 3px solid #667eea;">{line[4:]}</section>') elif line.startswith("> "): html_parts.append(self.QUOTE_STYLE.format(text=line[2:])) elif line.startswith("- ") or line.startswith("* "): html_parts.append(f'<section style="margin: 8px 0 8px 20px;">🔹 {line[2:]}</section>') elif re.match(r"^\d+\.\s", line): html_parts.append(f'<section style="margin: 8px 0 8px 20px;">📌 {re.sub(r"^\d+\.\s", "", line)}</section>') else: text = self._inline_format(line) if text.strip(): html_parts.append(self.PARAGRAPH_STYLE.format(text=text)) html_body = "\n".join(html_parts) return f""" <section style="font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif;"> {html_body} {self.FOOTER} </section> """.strip() ``` The link conversion performed by `_inline_format` also inserts an unvalidated destination into an HTML attribute: ```python text = re.sub(r"\[(.*?)\]\((.*?)\)", r'<a href="\2" style="color: #667eea;">\1</a>', text) return text ``` ### Technical Analysis The formatter directly interpolates user-controlled article content into HTML elements without first applying HTML escaping. This affects headings, quotations, list items ...[truncated 2257 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Escape every user-controlled text fragment with `html.escape(value, quote=True)` before inserting it into HTML. 2. Parse Markdown using a maintained library configured to disable raw HTML rather than implementing Markdown conversion through regular expressions. 3. Validate link destinations with `urllib.parse.urlsplit()` and allow only explicitly approved schemes, such as `https` and optionally `http`. 4. Reject dangerous or ambiguous schemes, including `javascript:`, `data:`, `vbscript:`, and scheme-relative URLs unless specifically required. 5. Construct anchor elements through a safe HTML builder, or escape both link labels and attribute values before interpolation. 6. Sanitize the completed output with a strict allowlist that permits only required elements and attributes. Remove event handlers, scripts, embedded objects, forms, and unsafe CSS or URL values. 7. Add security tests covering raw tags, quoted attribute breakouts, mixed-case and whitespace-obfuscated URL schemes, encoded payloads, malformed Markdown links, and nested formatting. 8. Treat downstream platform sanitization as defense in depth rather than the primary security control. ]]>
