T09 · Insecure Skill Coding Practices
- Location
scripts/parse.py:569- Finding
HTML Attribute Injection in Contrast Card Background Rendering
- Content
View full analysis
{_e(label)}{_e(title)}{_e(body)}""" ``` ### Technical Analysis The `bg` property is obtained from the supplied analysis JSON and inserted directly into a quoted HTML `style` attribute. Unlike the other fields in this block, it is neither escaped with `html.escape()` nor validated as a permitted CSS color. An attacker who controls the analysis JSON can terminate the `style` attribute and add a new event-handler attribute. For example: ```json { "bg": "red;\" onmouseover=\"alert(document.domain)" } ``` This produces markup equivalent to: ```html``` The injected JavaScript runs when the relevant user interaction occurs. Encoding the value alone would prevent attribute breakout, but arbitrary CSS values should also be rejected to reduce CSS-based content manipulation. ### Attack Path 1. An attacker creates or modifies an analysis JSON file. 2. The attacker places the injection payload in a `bg` property under a capsule whose `type` is `contrast`. 3. The user or agent runs: ```bash python scripts/parse.py --generate-html malicious-analysis.json ``` 4. `_render_contrast()` inserts the payload into the generated HTML without escaping or validation. 5. The user opens the generated report and triggers the injected event. 6. Attacker-controlled JavaScript executes in the report's browser conte ...[truncated 502 chars]- Remediation
View remediation
' ``` If custom colors are required: 1. Accept only a narrowly defined color syntax, such as validated hexadecimal colors. 2. Reject quotes, parentheses, semicolons, URL expressions, and unexpected whitespace. 3. Escape the final attribute value with `html.escape(value, quote=True)`. 4. Add a restrictive Content Security Policy that blocks inline scripts and event handlers. 5. Add regression tests using quote-breaking and event-handler payloads. Example restrictive validation: ```python if bg and not re.fullmatch(r"#[0-9A-Fa-f]{6}", bg): raise ValueError("Invalid background color") style = f' style="background:{html.escape(bg, quote=True)};"' if bg else "" ``` ]]>
