T09 · Insecure Skill Coding Practices
- Location
md-to-image.js:176- Finding
Untrusted Markdown Executes Active Browser Content
- Content
View full analysis
${CSS} ${content} `; } ``` ```javascript async function generateImage(html, outputPath) { const browser = await chromium.launch(); const page = await browser.newPage(); await page.setViewportSize({ width: TARGET_WIDTH, height: 8000 }); await page.setContent(html, { waitUntil: 'networkidle' }); await page.waitForTimeout(3000); const height = await page.evaluate(() => document.body.scrollHeight); await page.setViewportSize({ width: TARGET_WIDTH, height: Math.min(height, MAX_HEIGHT) }); ``` ```javascript const mdContent = fs.readFileSync(mdFile, 'utf-8'); console.log(`Converting: ${mdFile}`); const html = buildHtml(mdContent); const timestamp = Date.now(); const outputPath = `${OUTPUT_DIR}/output_${timestamp}.png`; console.log('Generating...'); await generateImage(html, outputPath); ``` ### Technical Analysis The application treats the selected Markdown file as untrusted input but passes it directly to `marked()`. Raw HTML preserved by the Markdown parser is then interpolated into a complete HTML document and loaded into a live Playwright Chromium page. No HTML sanitization, Content Security Policy, JavaScript disablement, browser-context network restriction, or request interception is applied. Consequently, active content in a crafted Markdown file—including inline scripts, event handlers, remote images, iframes, and other network-capable elements—can execute or load resources during conversion. For example, a ...[truncated 1849 chars]- Remediation
View remediation
{ const url = route.request().url(); if (url.startsWith('data:') || url === 'about:blank') { return route.continue(); } return route.abort(); }); ``` 4. Add a restrictive Content Security Policy to the generated document, for example: ```html ``` 5. If external images are a required feature, retrieve them through a controlled component that enforces: - Allowed protocols and destinations. - DNS and IP validation after resolution. - Blocking of loopback, link-local, private, and metadata address ranges. - Response size, content type, redirect, and timeout limits. 6. Add regression tests using raw scripts, event handlers, iframes, remote images, localhost URLs, private IP addresses, and cloud metadata URLs to confirm that active content and unauthorized network access are blocked. ]]>
