T09 · Insecure Skill Coding Practices
- Location
scripts/generate_report.py:196- Finding
Stored Cross-Site Scripting in Generated HTML Reports
- Content
View full analysis
{cat} ``` ```python # Findings are serialized directly into an executable script context. findings_json = json.dumps(findings, ensure_ascii=False) ``` ```python # The project name is inserted directly into multiple HTML contexts. {title_text} - {project_name} ``` ```python{project_name} · {generated_text} {now}``` ```javascript // JSON containing untrusted finding fields is embedded in a normal script. const findings = {findings_json}; ``` ```javascript // File, category, and type values are not escaped before assignment to innerHTML. div.innerHTML = sevTag + '' + ''; ``` ### Technical Analysis The report generator processes findings loaded from JSON files, standard input, or inline JSON. These findings may contain data derived from an untrusted repository, including source-code snippets and file paths. Crafted JSON input can additionally control fields such as `category`, `type`, and `message`. Calling `json.dumps()` does not make a value safe for direct placement inside an HTML ` ``` can terminate the original script element b ...[truncated 2903 chars]' + escapeHtml(f.message || noDesc) + '' + '' + metaParts.join(' | ') + '' + snippet + '- Remediation
View remediation
... ``` - Before embedding serialized JSON in HTML, escape at least `<` as `\u003c` so `` cannot terminate the element. - Parse the element's `textContent` with `JSON.parse()`. - Alternatively, store the data in a separate JSON file when a self-contained report is not required. 3. **Apply context-appropriate server-side escaping** - Escape `project_name` before inserting it into the title and visible header. - Escape category names before inserting them into table HTML. - Do not rely on one generic escaping routine for HTML text, HTML attributes, URLs, and JavaScript contexts. 4. **Validate input structure** - Require every finding to match a strict schema. - Restrict severity to known values. - Normalize scalar fields to strings and reject nested objects where strings are expected. - Apply reasonable length limits to findings, snippets, paths, categories, and project names. 5. **Add defense in depth** - Add a restrictive Content Security Policy that blocks inline and remote scripts. If inline JavaScript remains necessary, use a nonce or hash. - Avoid loading remote resources in generated reports. - Document that findings files and repositories may be attacker-controlled. 6. **Add regression tests** Test every rendered field with payloads containing: ```text" ' < > & ``` Tests should generate the report, open or parse it with a browser-capable test fram ...[truncated 103 chars]
