T09 · Insecure Skill Coding Practices
- Location
scripts/generate_report.py:62- Finding
Stored HTML and JavaScript Injection Through Unvalidated Module Identifiers
- Content
View full analysis
{info["icon"]} {info["name"]}' return tabs_html def make_module_content(data, modules): """生成所有模块内容""" all_html = "" for i, m in enumerate(modules): module_data = data.get(m, {}) active = "active" if i == 0 else "" content = render_module(m, module_data) all_html += f'{content}' return all_html ``` The module list is also inserted into the report metadata without output encoding: ```python def build_full_html(title, subject, modules, tab_nav, tab_contents, timestamp): """构建完整HTML页面""" mod_list = ", ".join([MODULE_NAMES.get(m, {}).get("name", m) for m in modules]) return f''' 科研助理报告 - {escape(title)} ``` ```html分析模块: {mod_list} | 生成时间: {timestamp}``` ### Technical Analysis The `modules` array comes from caller-supplied JSON: ```python modules = data.get("modules", []) ``` Each module identifier is interpolated directly into three distinct browser contexts: 1. An inline JavaScript event-handler argument in `onclick`. 2. An HTML `id` attribute. 3. HTML body content through the fallback module name and `mod_list`. No allowlist validation or context-appropriate encoding is applied. Although known module identifi ...[truncated 2030 chars]- Remediation
View remediation
' f'{escape(info["icon"])} {escape(info["name"])}' ) ``` 3. Escape every value according to its output context. For HTML attributes, use: ```python escape(value, quote=True) ``` 4. Generate safe DOM identifiers independently rather than deriving them from input, or permit only a narrow identifier pattern such as: ```python re.fullmatch(r"[a-z0-9_]+", m) ``` 5. Bind tab behavior using `addEventListener()` and retrieve the validated module from `event.currentTarget.dataset.module`. 6. Add regression tests with quote characters, event-handler fragments, closing tags, and unknown module names. Confirm that malformed or unsupported identifiers are rejected rather than rendered. 7. Add a restrictive Content Security Policy that disallows inline scripts. This is defense in depth and should not replace input validation and safe output construction. ]]>
