T09 · Insecure Skill Coding Practices
- Location
references/html-template.md:216- Finding
Unescaped Report Data Allows Stored HTML and JavaScript Injection
- Content
View full analysis
Vulnerability Details
File Location:
references/html-template.md, lines 216–276 and 350–367
Vulnerability Type: Stored HTML/JavaScript injection caused by missing contextual output escaping
Risk Level: HighVulnerable Code
The template inserts report data into HTML text, element identifiers, and inline JavaScript without explicit contextual escaping:
html <span class="category-name">{{ category.name }}</span> <div class="review-item {{ status_class }}" id="nav_{{ item.id }}" onclick="showItem('{{ item.id }}')"> <span class="item-icon">{{ icon }}</span> <span class="item-name">{{ item.name }}</span> </div>Additional potentially untrusted report fields are rendered directly into HTML:
html <span class="detail-value">{{ item.error_location }}</span> <span class="detail-value">{{ item.error_reason }}</span> <span class="detail-value">{{ item.suggestion }}</span> <span class="detail-value">{{ item.consequence }}</span> <span class="detail-value">{{ item.standard }}</span>The documented renderer constructs a bare Jinja2
Template, for which HTML autoescaping is not enabled by default:python from jinja2 import Template import json with open('html-template.md', 'r', encoding='utf-8') as f: template_content = f.read() import re match = re.search(r'```html\n(.*?)\n```', template_content, re.DOTALL) if match: jinja_template = Template(match.group(1)) with open('review_data.json', 'r', encoding='utf-8') as f: review_data = json.load(f) html_output = jinja_template.render(**review_data) with open('report.html', 'w', encoding='utf-8') as f: f.write(html_output)Technical Analysis
Jinja2 does not automatically enable HTML escaping when a template is instantiated directly through `Template(. ...[truncated 2492 chars]
- Remediation
View remediation
Remediation Suggestions
-
Enable automatic escaping through a configured Jinja2 environment:
python from jinja2 import Environment, select_autoescape environment = Environment( autoescape=select_autoescape( enabled_extensions=("html", "htm", "xml"), default_for_string=True ) ) jinja_template = environment.from_string(match.group(1)) html_output = jinja_template.render(**review_data) -
Validate
item.idusing a strict allowlist before rendering. For example, accept only identifiers matching^[A-Za-z0-9_-]+$and reject all other values. -
Remove inline
onclickhandlers. Store validated identifiers indata-*attributes and register event handlers usingaddEventListener. -
Do not place untrusted data directly into JavaScript string literals. If data must be transferred to JavaScript, serialize it with a context-aware JSON mechanism such as Jinja2's
tojsonfilter. -
Treat all OCR output, uploaded-label content, model-generated text, and imported JSON fields as untrusted. Do not apply Jinja2's
safefilter to these values. -
Consider deploying a restrictive Content Security Policy that disallows inline scripts and limits outbound connections. This provides defense in depth but does not replace correct output encoding.
-
Add automated security tests that place HTML tags, event handlers, quotation marks, and JavaScript-string termination sequences in every rendered field. Verify that text fields remain escaped and identifiers cannot alter the document structure or script behavior.
-
