T09 · Insecure Skill Coding Practices
- Location
scripts/md2html.js:101- Finding
Unsanitized Markdown Conversion Enables HTML Injection and Cross-Site Scripting
- Content
View full analysis
'); // Bold **text** text = text.replace(/\*\*(.+?)\*\*/g, '$1'); // Italic *text* (Node v0.12+ compatible, no lookbehind needed) text = text.replace(/\*([^*\n]+?)\*/g, '$1'); // Inline code `text` text = text.replace(/`(.+?)`/g, '$1'); // Links [text](url) text = text.replace(/\[(.+?)\]\((.+?)\)/g, '$1'); // Line breaks (double space at end of line) text = text.replace(/ $/gm, '
'); return text; } ``` Representative output sinks include: ```javascript result.push('' + inlineFormat(line.replace(/^#\s*/, '')) + '
'); result.push('
'); result.push('' + inlineFormat(line.replace(/^>\s*/, '')) + '
- ' + inlineFormat(line.replace(/^[\-\*]\s*/, '')) + '
'); result.push('' + inlineFormat(line) + '
'); ``` ### Technical Analysis The converter inserts untrusted Markdown content into generated HTML without first applying HTML escaping. The `inlineFormat()` function also places attacker-controlled image URLs, link URLs, alternative text, and labels directly into HTML attribute or element contexts. The only escaping routine in the project is applied to fenced code-block contents. Ordinary paragraphs, headings, list items, blockquotes, inline code, links, and images remain unsafe. This creates multiple exploitation mechanisms: 1. **Direct HTML injection**: Raw HTML in an ordinary Markdown line is preserved inside the generated paragraph. 2. **Attribute inje ...[truncated 2704 chars]- Remediation
View remediation
`, `"`, and `'` where appropriate. - Ensure inline-code content is escaped rather than merely wrapped in ``. 2. **Apply context-aware attribute encoding** - Never interpolate captured regular-expression groups directly into quoted attributes. - Encode image alternative text, image sources, link destinations, and any future attribute values using an attribute-safe encoder. 3. **Validate link and image URL schemes** - Parse URLs and allow only explicitly required schemes, such as `https:`, `http:`, and optionally `mailto:`. - Reject `javascript:`, unsafe `data:`, `vbscript:`, file URLs, control characters, encoded scheme bypasses, and protocol-relative URLs unless specifically required. - Validation must occur after normalization and decoding. 4. **Sanitize the final HTML** - Use a maintained Markdown implementation with secure configuration. - Pass generated HTML through an allowlist-based HTML sanitizer before publishing it. - Allow only necessary elements and attributes, and remove event-handler attributes and dangerous URL protocols. - If raw HTML is unnecessary, disable raw HTML support entirely. 5. **Harden browser integration** - Apply an appropriately restrictive Content Security Policy as defense in depth. - Do not treat converted output as trusted solely because it was produced by this converter. - Preserve `rel="noopener"` and consider adding `noreferrer` where suitable, but do not treat these attributes as an XSS mitigation. 6. **Add security regression tests** - Test raw elements such as `
