T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/format.py:1649
- Finding
- Unsanitized Markdown permits active HTML injection in generated documents<![CDATA[ ## Vulnerability Details **File Location**: `scripts/format.py:1346-1367`, `scripts/format.py:1649-1685` **Vulnerability Type**: T09: Insecure Skill Coding Practices **Risk Level**: High ### Vulnerable Code ```python img_match = re.match(r'^!\[(.*?)\]\(\s*(\S+?)(?:\s+["\u201c\u2018\'](.*?)["\u201d\u2019\'])?\s*\)$', stripped) if img_match: flush_paragraph() alt = img_match.group(1) src = img_match.group(2) caption_text = (img_match.group(3) or "").strip() if ("封面" in alt) or alt.startswith("cover"): continue alt_escaped = html_mod.escape(alt) img_style = styles.get("img", "") or "max-width:100%; border-radius:4px;" img_html = ( f'<p style="text-align:center; margin:1.5em 0;">' f'<img src="{src}"{_image_dims(src, base_dir)} alt="{alt_escaped}" style="{img_style}" />' f'</p>' ) ``` ```python text = re.sub( r'(?<!!)\[(.+?)\]\((.+?)\)', rf'<a style="{a_style}" href="\2">\1</a>', text, ) text = re.sub(r'<(https?://[^\s<>]+)>|<(https?://[^\s<>]+)>', lambda m: f'<a style="{a_style}" href="{m.group(1) or m.group(2)}">' f'{m.group(1) or m.group(2)}</a>', text) links: list[str] = [] def _stash_link(m: re.Match) -> str: links.append(m.group(0)) return f"\x00L{len(links) - 1}\x00" text = re.sub(r'<a\s[^>]*>.*?</a>', _stash_link, text, flags=re.S) text = re.sub(r'(?<![\w"\'=/])(https?://[^\s<>"\',。、)】」]+)', lambda m: f'<a style="{a_style}" href="{m.group(1)}">{m.group(1)}</a>', text) text = re.sub(r"\x00L(\d+)\x00", lambda m: links[int(m.group(1))], text) text = re.sub(r"\x00C(\d+)\x00", lambda m: code_spans[int(m.group(1))], text) text = re.sub(r"\x00E(\d+)\x00", lambda m: escapes[int(m.group(1))], text) return text ``` The preformatter also explicitly preserves raw HTML tags: ```python _PREFORMAT_PROTECT_PATTERNS = ( re.compile(r"^[ \t]*```[^\n]*\n.*?^[ \t]*```[ \t]*$", re.M | re.S), re.compile(r"`[ ...[truncated 2960 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Escape ordinary Markdown text before applying controlled formatting: ```python text = html_mod.escape(text, quote=False) ``` Preserve only renderer-generated tags through internal placeholders. 2. Do not preserve arbitrary raw HTML by default. Either reject it or sanitize it using a strict allowlist of permitted tags and attributes. 3. Escape every value inserted into an HTML attribute: ```python safe_src = html_mod.escape(src, quote=True) safe_href = html_mod.escape(href, quote=True) ``` 4. Parse URLs with `urllib.parse.urlsplit()` and permit only explicitly approved schemes: - `https` - `http`, if required - Approved relative paths for local images Reject `javascript:`, `vbscript:`, unexpected `data:` URLs, protocol-relative URLs when inappropriate, and control characters. 5. Validate image paths separately from remote URLs. Local paths should be normalized and restricted to the article directory or another approved asset directory. 6. Sanitize configurable style strings or replace free-form styles with structured, validated style properties. 7. Add security regression tests covering: - Raw `script` elements. - SVG event handlers. - `img` elements with `onerror`. - Quotes in image and link destinations. - `javascript:` and encoded dangerous schemes. - Closing files and component content containing raw HTML. ]]>
