T09 · Insecure Skill Coding Practices
- Location
skills/linkfox-report-generator/scripts/inject_report.py:167- Finding
Arbitrary JavaScript Injection in Generated HTML Reports
- Content
View full analysis
([\s\S]*?)", re.IGNORECASE, ) _CANVAS_BLOCK_RE = re.compile( r"([\s\S]*?)", re.IGNORECASE, ) _SCRIPT_OPEN_RE = re.compile(r"]*>", re.IGNORECASE) ``` ```python def _inject(template: str, content: str, title: str | None, language: str) -> str: # 1. Extract ECharts / Canvas blocks echarts_code = "" m = _ECHARTS_BLOCK_RE.search(content) if m: echarts_code = m.group(1).strip() echarts_code = _SCRIPT_OPEN_RE.sub("", echarts_code) content = _ECHARTS_BLOCK_RE.sub("", content).strip() canvas_code = "" m = _CANVAS_BLOCK_RE.search(content) if m: canvas_code = m.group(1).strip() canvas_code = _SCRIPT_OPEN_RE.sub("", canvas_code) content = _CANVAS_BLOCK_RE.sub("", content).strip() # 2. Replace document-level placeholders html = template.replace("{{TITLE}}", title or "LinkFox Analysis Report") html = html.replace("{{LANG}}", language) # 3. Inject the primary content html = re.sub( r".*?", "\n" + content + "\n", html, flags=re.DOTALL, ) # 4. Insert ECharts / Canvas initialization code into template markers if echarts_code: html = html.replace( "// ECHARTS_INIT_START\n // ECHARTS_INIT_END", "// ECHARTS_INIT_START\n " + echarts_code + "\n // ECHARTS_INIT_END", ) if canvas_code: html = html.replace( "// CANVAS_INIT_START\n // CANVAS_INI ...[truncated 2503 chars]- Remediation
View remediation
`, `&`, U+2028, and U+2029 when embedding JSON into HTML. 3. **Sanitize the HTML fragment.** - Use a maintained HTML sanitizer with a restrictive allowlist of required report elements, attributes, and CSS properties. - Remove event-handler attributes such as `onclick` and `onerror`. - Reject active elements such as `script`, `iframe`, `object`, `embed`, and `meta`. - Restrict URL-bearing attributes to approved HTTPS schemes and, where practical, approved hosts. 4. **Deploy a restrictive Content Security Policy.** - Move trusted scripts into separate static files. - Set a policy such as `default-src 'none'`, with narrowly scoped allowances for scripts, images, and styles. - Avoid both `'unsafe-inline'` and `'unsafe-eval'`. - Restrict outbound connections with `connect-src`. 5. **Add adversarial tests.** - Verify rejection of raw JavaScript, event attributes, script-closing sequences, `javascript:` URLs, SVG-based payloads, and encoded variants. - Confirm that valid ECharts and Canvas data continues to render without accepting executable code. ]]>
