T09 · Insecure Skill Coding Practices
- Location
scripts/parse.py:382- Finding
Persistent HTML, JavaScript, and CSS Injection in Generated Reports
- Content
View full analysis
str: """渲染 Hero 区域""" meta = analysis.get("meta", {}) platform = meta.get("platform", "") platform = platform if platform != "未知" else "" title = meta.get("title", "") duration = meta.get("duration", "") or "" category = meta.get("category", "") date = meta.get("date", dt.datetime.now().strftime("%Y.%m.%d")) badge_text = f"{platform}视频 · 深度解析" if platform else "视频 · 深度解析" meta_items = [] if meta.get("author"): meta_items.append(f"{meta['author']}") if duration: meta_items.append(f"{duration}") if category: meta_items.append(f"{category}") meta_items.append(f"{date}") hero = f'\n' hero += f'{badge_text}\n' hero += f'{title}
\n' if meta_items: hero += f'{"".join(meta_items)}\n' hero += f'\n' return hero ``` The summary is also inserted as unrestricted HTML: ```python def _render_summary(analysis: dict) -> str: """渲染一句话总结卡片""" s = analysis.get("summary", "") if not s: return "" return f"""""" ``` Capsule identifiers are interpolated into an inline JavaScript event handler and an HTML `id` attribute: ```python cards = "" for c in capsules: cid = c.get("id", "") icon = c.get("icon", "📌") title = c.get("title", "") subtitle = c.get("subtitle", "") cards += f""" ...[truncated 4483 chars]一句话总结{s}
- Remediation
View remediation
str: return html.escape(str(value or ""), quote=True) ``` 2. Apply escaping separately to all metadata, quotes, capsule titles, subtitles, labels, bodies, tags, and document titles. 3. Handle the summary's intended `` support with a strict sanitizer that permits only the `em` element and no attributes. Do not accept arbitrary markup. 4. Remove inline event handlers. Emit validated identifiers through `data-*` attributes and register event listeners from trusted static JavaScript: ```html``` 5. Restrict capsule IDs to a safe pattern such as: ```python if not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", cid): raise ValueError("Invalid capsule ID") ``` 6. Do not interpolate arbitrary CSS. Validate `bg` against an allowlist of named theme tokens or a strict color grammar before use. 7. Add a restrictive Content Security Policy. In particular, avoid `unsafe-inline` for scripts and restrict network destinations. 8. Add regression tests using payloads containing closing tags, quotes, event handlers, script elements, malformed IDs, and CSS-breaking characters. Verify that generated reports display these values as text and never execute them. ]]>
